forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBdcf123aBurak Boduroğlu2 months ago674 commits

file-type.tsx29 lines, 2.1 KiBHistoryRawDownload
1import type { JSX } from "solid-js";
2import {
3 IconFileAudio,
4 IconFileImage,
5 IconFileSpreadsheet,
6 IconFileText,
7 IconFileVideo,
8 IconNote,
9} from "@/components/ui/icons";
10
11export function fileExtension(name: string): string {
12 const ext = name.split(".").pop()?.toLowerCase();
13 return ext && ext !== name.toLowerCase() ? ext : "file";
14}
15
16export type FileTypeMeta = { label: string; class: string; icon: JSX.Element };
17
18/** Icon + badge color for a file, by content type / extension — shared across notes and homework file lists. */
19export function fileTypeMeta(file: { name: string; content_type: string }, iconClass = "h-5 w-5"): FileTypeMeta {
20 const type = file.content_type.toLowerCase();
21 const ext = fileExtension(file.name);
22 if (type.startsWith("image/")) return { label: ext, class: "border-sky-500/25 bg-sky-500/10 text-sky-700 dark:text-sky-300", icon: <IconFileImage class={iconClass} /> };
23 if (type.startsWith("video/")) return { label: ext, class: "border-violet-500/25 bg-violet-500/10 text-violet-700 dark:text-violet-300", icon: <IconFileVideo class={iconClass} /> };
24 if (type.startsWith("audio/")) return { label: ext, class: "border-fuchsia-500/25 bg-fuchsia-500/10 text-fuchsia-700 dark:text-fuchsia-300", icon: <IconFileAudio class={iconClass} /> };
25 if (type === "application/pdf" || ext === "pdf") return { label: "pdf", class: "border-rose-500/25 bg-rose-500/10 text-rose-700 dark:text-rose-300", icon: <IconFileText class={iconClass} /> };
26 if (["xls", "xlsx", "csv"].includes(ext) || type.includes("spreadsheet")) return { label: ext, class: "border-emerald-500/25 bg-emerald-500/10 text-emerald-700 dark:text-emerald-300", icon: <IconFileSpreadsheet class={iconClass} /> };
27 if (["doc", "docx", "txt", "md", "rtf"].includes(ext) || type.startsWith("text/") || type.includes("word")) return { label: ext, class: "border-blue-500/25 bg-blue-500/10 text-blue-700 dark:text-blue-300", icon: <IconFileText class={iconClass} /> };
28 return { label: ext, class: "border-border bg-background text-muted-foreground", icon: <IconNote class={iconClass} /> };
29}