"use client"; import { Button } from "@suphark/ui/components/shared/button/button"; import { useUiText } from "@suphark/ui/components/shared/ui-text"; import { FormControl, FormDescription, FormField, FormItem, FormLabel, FormMessage, } from "@suphark/ui/components/ui/form"; import { formatBytes } from "@suphark/ui/lib/formatters"; import { cn } from "@suphark/ui/lib/utils"; import { ImageIcon } from "lucide-react"; import { type ChangeEvent, type ReactNode, useEffect, useRef, useState } from "react"; import { type Control, type FieldValues, type Path, type PathValue, type UseFormSetValue, useFormContext, } from "react-hook-form"; import { ActionDelete, ActionUpload } from "../semantic-icons"; export interface FormFieldImageUploadProps { control: Control; /** ช่องที่เก็บไฟล์ที่เลือก (`File | null`) */ name: Path; /** ช่องที่เก็บ URL รูปเดิม — เลือกรูปใหม่/ลบรูปแล้วตั้งเป็น `null` (server รู้ว่าต้องลบ/แทนรูปเดิม) */ urlName?: Path; label: ReactNode; description?: ReactNode; /** รูปเดิมตอนเปิดฟอร์ม (แก้ไขข้อมูล) */ initialImageUrl?: string | null; /** ชนิดไฟล์ที่รับ — ค่าเริ่มต้น `image/*` */ accept?: string; /** ขนาดไฟล์สูงสุด (ไบต์) — เกินแล้วไม่รับ แสดงข้อความผิดพลาดใต้ช่อง */ maxSize?: number; /** กรอบตัวอย่าง: `square` (ค่าเริ่มต้น — โลโก้/สินค้า) · `circle` (รูปโปรไฟล์) */ shape?: "square" | "circle"; disabled?: boolean; className?: string; /** ไม่ต้องส่งแล้ว (ใช้ของ `Form`) — คงไว้ให้ย้ายจาก supplychain ได้โดยไม่แก้จุดเรียก */ setValue?: UseFormSetValue; } /** * ช่องเลือกรูปเดียวในฟอร์ม (โลโก้ / รูปโปรไฟล์ / รูปสินค้า) — ตัวอย่างรูป + ปุ่ม "เลือกรูป"/"เปลี่ยนรูป" + "ลบรูป" * ปุ่มแสดงตลอด (แตะได้บนจอสัมผัส ไม่ซ่อนไว้รอ hover) · ป้ายของช่องผูกกับปุ่มเลือกรูป · ข้อความจาก UiText `imageUpload` * ยกจาก supplychain `FormFieldImageUpload` (props เดิม; `setValue` ไม่บังคับแล้ว) · หลายไฟล์/ลากวาง → `Dropzone` */ export function FormFieldImageUpload({ control, name, urlName, label, description, initialImageUrl, accept = "image/*", maxSize, shape = "square", disabled, className, setValue: setValueProp, }: FormFieldImageUploadProps) { const form = useFormContext(); const setValue = setValueProp ?? form.setValue; const text = useUiText().imageUpload; const inputRef = useRef(null); const buttonsRef = useRef(null); const [preview, setPreview] = useState(initialImageUrl ?? null); // URL ของไฟล์ที่เลือก (blob:) — คืนหน่วยความจำเมื่อเปลี่ยนรูป/ออกจากหน้า const objectUrl = useRef(null); const releaseObjectUrl = () => { if (objectUrl.current) URL.revokeObjectURL(objectUrl.current); objectUrl.current = null; }; // biome-ignore lint/correctness/useExhaustiveDependencies: คืน URL ตอน unmount เท่านั้น useEffect(() => releaseObjectUrl, []); const clearUrl = () => { if (urlName) setValue(urlName, null as PathValue>, { shouldDirty: true }); }; // เลือกไฟล์เดิมซ้ำได้ (onChange ไม่ยิงถ้าค่าเท่าเดิม) const resetInput = () => { if (inputRef.current) inputRef.current.value = ""; }; const handleChange = (event: ChangeEvent) => { const file = event.target.files?.[0]; resetInput(); if (!file) return; if (maxSize !== undefined && file.size > maxSize) { form.setError(name, { type: "maxSize", message: (text?.tooLarge ?? "ไฟล์ใหญ่เกิน {size}").replace("{size}", formatBytes(maxSize)), }); return; } form.clearErrors(name); setValue(name, file as PathValue>, { shouldValidate: true, shouldDirty: true }); clearUrl(); releaseObjectUrl(); objectUrl.current = URL.createObjectURL(file); setPreview(objectUrl.current); }; const handleRemove = () => { setValue(name, null as PathValue>, { shouldValidate: true, shouldDirty: true }); clearUrl(); releaseObjectUrl(); setPreview(null); resetInput(); // ปุ่ม "ลบรูป" ที่ถือโฟกัสอยู่หายไป → โฟกัสหลุดไป body · ย้ายไปปุ่ม "เลือกรูป" (ปุ่มแรก) หลัง render requestAnimationFrame(() => buttonsRef.current?.querySelector("button")?.focus(), ); }; const chooseText = preview ? (text?.change ?? "เปลี่ยนรูป") : (text?.choose ?? "เลือกรูป"); const removeText = text?.remove ?? "ลบรูป"; // ชื่อปุ่ม = คำสั่ง + ชื่อช่อง ("เปลี่ยนรูป: โลโก้") — ป้ายของช่องผูกกับปุ่มนี้ ถ้าไม่ตั้ง ชื่อปุ่มจะเหลือแค่ "โลโก้" const withField = (action: string) => typeof label === "string" ? `${action}: ${label}` : undefined; return ( ( {label}
{preview ? ( // biome-ignore lint/performance/noImgElement: รูปตัวอย่างจาก blob:/URL ภายนอก — ไม่พึ่ง next/image (barrel ห้ามพึ่ง next) {text?.preview ) : (
{/* FormControl ที่ปุ่ม — ป้าย/คำอธิบาย/ข้อความผิดพลาดผูกกับสิ่งที่ผู้ใช้กดจริง (input ซ่อนอยู่) */} {preview && ( )}
{description && {description}}
)} /> ); }