import { cn } from "@suphark/ui/lib/utils"; import { File, FileArchive, FileAudio, FileCode, FileImage, FileSpreadsheet, FileText, FileVideo, Folder, type LucideIcon, Presentation, } from "lucide-react"; import type { ComponentProps } from "react"; export type FileKind = | "folder" | "image" | "video" | "audio" | "pdf" | "doc" | "sheet" | "slides" | "zip" | "code" | "text" | "default"; const EXT: Record = { png: "image", jpg: "image", jpeg: "image", gif: "image", webp: "image", svg: "image", bmp: "image", heic: "image", avif: "image", mp4: "video", mov: "video", webm: "video", mkv: "video", avi: "video", mp3: "audio", m4a: "audio", wav: "audio", flac: "audio", ogg: "audio", aac: "audio", pdf: "pdf", doc: "doc", docx: "doc", odt: "doc", rtf: "doc", xls: "sheet", xlsx: "sheet", csv: "sheet", ods: "sheet", ppt: "slides", pptx: "slides", odp: "slides", key: "slides", zip: "zip", rar: "zip", "7z": "zip", gz: "zip", tar: "zip", tgz: "zip", txt: "text", md: "text", log: "text", js: "code", ts: "code", tsx: "code", jsx: "code", json: "code", html: "code", css: "code", py: "code", go: "code", rs: "code", java: "code", sql: "code", sh: "code", yml: "code", yaml: "code", xml: "code", }; /** ประเภทไฟล์จาก mime (หลัก) หรือนามสกุลของ `filename` (สำรอง) — ใช้เลือกไอคอน/สี */ export function getFileKind(mime?: string | null, filename?: string | null): FileKind { const m = (mime ?? "").toLowerCase(); if (m === "inode/directory" || m === "folder") return "folder"; if (m.startsWith("image/")) return "image"; if (m.startsWith("video/")) return "video"; if (m.startsWith("audio/")) return "audio"; if (m === "application/pdf") return "pdf"; if (m.includes("spreadsheet") || m.includes("excel") || m === "text/csv") return "sheet"; if (m.includes("presentation") || m.includes("powerpoint")) return "slides"; if (m.includes("wordprocessing") || m.includes("msword") || m.includes("opendocument.text")) return "doc"; if (m.includes("zip") || m.includes("compressed") || m.includes("x-tar") || m.includes("gzip")) return "zip"; if (m.includes("json") || m.includes("javascript") || m.includes("xml") || m.includes("html")) return "code"; if (m.startsWith("text/")) return "text"; const ext = (filename ?? "").split(".").pop()?.toLowerCase() ?? ""; return (filename?.includes(".") && EXT[ext]) || "default"; } const ICON: Record = { folder: Folder, image: FileImage, video: FileVideo, audio: FileAudio, pdf: FileText, doc: FileText, sheet: FileSpreadsheet, slides: Presentation, zip: FileArchive, code: FileCode, text: FileText, default: File, }; /** สีตาม token (ไม่ใช้ palette ดิบ — ธีม dark ตามไปเอง) */ const COLOR: Record = { folder: "text-warning", image: "text-primary", video: "text-primary", audio: "text-primary", pdf: "text-destructive", doc: "text-primary", sheet: "text-success", slides: "text-warning", zip: "text-muted-foreground", code: "text-muted-foreground", text: "text-muted-foreground", default: "text-muted-foreground", }; /** * ไอคอน lucide ตามชนิดไฟล์ (image/video/audio/pdf/doc/sheet/slides/zip/code/text/folder) สีตาม token * · */ export function FileTypeIcon({ mime, filename, size = 16, className, ...props }: Omit, "size"> & { mime?: string | null; filename?: string | null; /** ขนาดเป็น px (ค่าเริ่มต้น 16) */ size?: number; }) { const kind = getFileKind(mime, filename); const Icon = ICON[kind]; return (