reboot alterflixhub

This commit is contained in:
Bkk1234 committed 2026-11-01 17:24:44 +07:00
commit 1e84023a44
188 files changed
+31629

No files matched your search

+120
View File
@@ -0,0 +1,120 @@
import { useEffect, useRef, useState } from "react";
/**
* ช่องแก้ slug (ใช้ใน URL) ของโรงหนัง
*
* ทำไมของเดิม "แก้ไม่ได้": อินพุตผูกกับ `cinema.slug` ตรง ๆ และแปลงค่าทุกคีย์
* (`replace(/[^a-z0-9-]/g, "-")`) — พอพิมพ์ตัวอักษรไทย/เว้นวรรค มันกลายเป็น "-"
* ทันที และถ้าลบจนว่าง ค่าก็ว่างทันที ทำให้ดูเหมือนพิมพ์อะไรไม่ได้เลย
*
* ที่นี่เก็บ "ข้อความดิบ" ระหว่างพิมพ์ (draft) แล้วค่อยจัดรูปตอนออกจากช่อง (blur)
* slug ว่างหรือซ้ำกับโรงอื่นจะไม่ถูกบันทึก — คืนค่าเดิมให้
*/
const SLUG_PATTERN = /^[a-z0-9]+(?:-[a-z0-9]+)*$/;
/** จัดรูป slug: ตัวเล็ก, ช่องว่าง/ขีดซ้ำ → ขีดเดียว, ตัดอักขระที่ไม่ใช่ a-z0-9- */
export function normalizeSlug(value: string): string {
return value
.trim()
.toLowerCase()
.replace(/[\s_]+/g, "-")
.replace(/[^a-z0-9-]/g, "")
.replace(/-{2,}/g, "-")
.replace(/^-+|-+$/g, "");
}
/**
* แนะนำ slug จากชื่อโรงหนังภาษาอังกฤษ
* "HOUSE CINEMA SAMYAN" → "house-cinema-samyan" · "." และอักขระอื่นถูกตัดให้เอง
*/
export function suggestSlug(name: string): string {
return normalizeSlug(
name
.normalize("NFKD")
.replace(/[\u0300-\u036f]/g, "")
.replace(/&/g, " and ")
.replace(/['’]/g, ""),
);
}
/** เหตุผลที่ slug ใช้ไม่ได้ (null = ใช้ได้) */
export function slugProblem(
slug: string,
options: { others: string[]; self?: string },
): string | null {
if (slug === "") return "slug ว่างไม่ได้";
if (!SLUG_PATTERN.test(slug)) return "ใช้ได้เฉพาะ a-z, 0-9 และ - (ห้ามขีดติดกัน/หัวท้าย)";
if (options.others.some((other) => other === slug && other !== options.self)) {
return "slug นี้มีโรงอื่นใช้อยู่";
}
return null;
}
type SlugInputProps = {
value: string;
/** slug ของโรงอื่นทั้งหมด ใช้ตรวจซ้ำ */
others: string[];
onChange: (slug: string) => void;
className?: string;
};
export function SlugInput({ value, others, onChange, className }: SlugInputProps) {
const [draft, setDraft] = useState<string | null>(null);
const [notice, setNotice] = useState<string | null>(null);
const lastValue = useRef(value);
// ค่าถูกเปลี่ยนจากภายนอก (สลับโรง / ใช้ JSON / คืนค่า) → ทิ้ง draft
// เทียบกับค่าก่อนหน้า ไม่ใช่เทียบกับ draft เพราะระหว่างพิมพ์ value ยังไม่เปลี่ยน
useEffect(() => {
if (lastValue.current === value) return;
lastValue.current = value;
setDraft(null);
}, [value]);
const shown = draft ?? value;
const preview = normalizeSlug(shown);
const problem = slugProblem(preview, { others, self: value });
const dirty = preview !== value;
return (
<div className="flex flex-col gap-1">
<input
className={className}
value={shown}
spellCheck={false}
placeholder="house-cinema-samyan"
onChange={(event) => {
setDraft(event.target.value);
setNotice(null);
}}
onBlur={() => {
const next = normalizeSlug(draft ?? value);
if (next !== "" && slugProblem(next, { others, self: value }) === null) {
if (next !== value) {
onChange(next);
setNotice("เปลี่ยน slug แล้ว — ลิงก์เดิมของโรงนี้จะใช้ไม่ได้ (ต้องกดบันทึก)");
}
} else if (next !== value) {
setNotice("slug ไม่ถูกบันทึก (ว่าง/ซ้ำ/รูปแบบไม่ถูก) — คงค่าเดิมไว้");
}
setDraft(null);
}}
/>
<div className="flex flex-wrap items-center gap-2 text-[11px]">
<code className="text-[#777]">/cinema/{preview || "…"}</code>
{problem && dirty ? (
<span className="text-[#D47272]">{problem}</span>
) : null}
{!problem && dirty ? (
<span className="text-[#B3B3B3]">กด Tab/คลิกออกเพื่อยืนยัน slug ใหม่</span>
) : null}
</div>
{notice ? (
<span className="text-[11px] text-[#D47272]">{notice}</span>
) : null}
</div>
);
}