From 944d3bc00afaa62192ff40e89e1d07f034971bb1 Mon Sep 17 00:00:00 2001 From: AmirmahdiNourkazemi Date: Sun, 7 Jun 2026 21:53:01 +0330 Subject: [PATCH] feat: media & music file name --- app/dashboard/media/page.tsx | 9 +++++++-- app/dashboard/music/tracks/page.tsx | 9 +++++++-- lib/utils.ts | 10 ++++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) diff --git a/app/dashboard/media/page.tsx b/app/dashboard/media/page.tsx index f08c2ce..2d2e3a2 100644 --- a/app/dashboard/media/page.tsx +++ b/app/dashboard/media/page.tsx @@ -18,7 +18,7 @@ import { Badge, } from "@/components/ui"; import { EditIcon, PlusIcon, TrashIcon } from "@/components/icons"; -import { formatMinutes } from "@/lib/utils"; +import { formatMinutes, fileNameToTitle } from "@/lib/utils"; import { MediaPreview } from "@/components/MediaPreview"; import { ImagePicker } from "@/components/ImagePicker"; import { pickUrl, SOUND_KEYS, VIDEO_KEYS, IMAGE_KEYS } from "@/lib/media"; @@ -397,7 +397,12 @@ export default function MediaPage() { setFile(e.target.files?.[0] ?? null)} + onChange={(e) => { + const f = e.target.files?.[0] ?? null; + setFile(f); + // Fill the title from the file name if still empty. + if (f && !title.trim()) setTitle(fileNameToTitle(f.name)); + }} /> diff --git a/app/dashboard/music/tracks/page.tsx b/app/dashboard/music/tracks/page.tsx index 3390cfe..c297704 100644 --- a/app/dashboard/music/tracks/page.tsx +++ b/app/dashboard/music/tracks/page.tsx @@ -15,7 +15,7 @@ import { PageHeader, } from "@/components/ui"; import { EditIcon, PlusIcon, TrashIcon } from "@/components/icons"; -import { formatMinutes } from "@/lib/utils"; +import { formatMinutes, fileNameToTitle } from "@/lib/utils"; import { MediaPreview } from "@/components/MediaPreview"; import { ImagePicker } from "@/components/ImagePicker"; import { pickUrl, SOUND_KEYS, IMAGE_KEYS } from "@/lib/media"; @@ -301,7 +301,12 @@ export default function MusicTracksPage() { setFile(e.target.files?.[0] ?? null)} + onChange={(e) => { + const f = e.target.files?.[0] ?? null; + setFile(f); + // Fill the title from the file name if still empty. + if (f && !title.trim()) setTitle(fileNameToTitle(f.name)); + }} required={!editing} /> diff --git a/lib/utils.ts b/lib/utils.ts index c6d90a6..ffb0342 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -19,6 +19,16 @@ export function formatDuration(seconds?: number | null): string { return toFa(`${m}:${String(s).padStart(2, "0")}`); } +// Derive a clean title from an uploaded file name: drop the extension and turn +// underscores/dashes into spaces (e.g. "morning_calm-01.mp3" -> "morning calm 01"). +export function fileNameToTitle(name: string): string { + return name + .replace(/\.[^./\\]+$/, "") + .replace(/[_-]+/g, " ") + .replace(/\s+/g, " ") + .trim(); +} + // Minutes -> Persian label. Media and music store `duration` in minutes. export function formatMinutes(minutes?: number | null): string { if (minutes === null || minutes === undefined) return "—";