From f843155394aebca6170a395e85841bbadafb9bb7 Mon Sep 17 00:00:00 2001 From: grngxd <36968271+grngxd@users.noreply.github.com> Date: Sat, 21 Jun 2025 22:37:32 +0100 Subject: [PATCH] Add file preview component and enhance file size formatting in dashboard --- src/routes/dashboard/index.tsx | 93 ++++++++++++++++++++++++++++++++-- 1 file changed, 90 insertions(+), 3 deletions(-) diff --git a/src/routes/dashboard/index.tsx b/src/routes/dashboard/index.tsx index b6f48e8..d2a0f3d 100644 --- a/src/routes/dashboard/index.tsx +++ b/src/routes/dashboard/index.tsx @@ -1,4 +1,4 @@ -import { component$, Signal, useVisibleTask$ } from "@builder.io/qwik"; +import { component$, Signal, useSignal, useTask$, useVisibleTask$ } from "@builder.io/qwik"; import { routeLoader$, type DocumentHead } from "@builder.io/qwik-city"; import OSBar from "~/components/dashboard/OSBar"; import TitleBar from "~/components/dashboard/TitleBar"; @@ -34,6 +34,13 @@ export default component$(() => { ); }); +const formatSize = (bytes: number) => { + if (bytes < 1024) return `${bytes} B`; + if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`; + if (bytes < 1024 * 1024 * 1024) return `${(bytes / (1024 * 1024)).toFixed(1)} MB`; + return `${(bytes / (1024 * 1024 * 1024)).toFixed(1)} GB`; +} + const Files = component$<{ files: Signal; loaded: Signal; @@ -41,9 +48,89 @@ const Files = component$<{ }>(({ files, loaded }) => { const File = component$(({ file }: { file: StereoFile }) => { + const Preview = component$(() => { + type FileType = + | "image" + | "video" + | "audio" + | "other"; + + const fileType: Signal = useSignal("other"); + const type = file.Mime.split("/")[1]; + + useTask$(async () => { + if ( + ["jpeg", "jpg", "png", "gif", "webp"] + .includes(type) + ) fileType.value = "image"; + + else if ( + ["mp4", "webm", "ogg", "avi", "mov"] + .includes(type) + ) fileType.value = "video"; + + else if ( + ["mp3", "wav", "flac", "aac"] + .includes(type) + ) fileType.value = "audio"; + + else fileType.value = "other"; + }); + + return ( +
+ {fileType.value === "image" && ( + {file.Name} + )} + + {fileType.value === "video" && ( +
+ ) + }) + return ( -
- {file.Name} +
+ + +
+

{file.Name}

+

+ {formatSize(file.Size)} + + Uploaded on {new Date(file.CreatedAt).toLocaleDateString()} +

+
) })