forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiB5fcd955Burak Boduroğlu2 months ago730 commits

note-file-preview.tsx105 lines, 4.9 KiBHistoryRawDownload
1import { Match, Show, Switch, createEffect, createSignal, onCleanup } from "solid-js";
2import { formatApiError } from "@/api/client";
3import type { NoteFile } from "@/api/client";
4import type { NoteFileSource } from "@/lib/note-source";
5import { Dialog, DialogContent, DialogTitle } from "@/components/ui/dialog";
6import { useT } from "@/stores/preferences-context";
7
8function canFrame(type: string): boolean {
9 return type === "application/pdf" || type.startsWith("text/");
10}
11
12function canPreview(file: NoteFile): boolean {
13 const type = file.content_type;
14 return type.startsWith("image/") || type.startsWith("video/") || type.startsWith("audio/") || canFrame(type);
15}
16
17export function NoteFilePreview(props: { noteId: string; source: NoteFileSource; file: NoteFile | null; onClose: () => void }) {
18 const t = useT();
19 let currentUrl = "";
20 const [previewUrl, setPreviewUrl] = createSignal("");
21 const [previewError, setPreviewError] = createSignal("");
22 const downloadUrl = () => (props.file ? props.source.fileUrl(props.noteId, props.file.id) : "");
23 const type = () => props.file?.content_type ?? "";
24
25 createEffect(() => {
26 if (currentUrl) URL.revokeObjectURL(currentUrl);
27 currentUrl = "";
28 setPreviewUrl("");
29 setPreviewError("");
30
31 const file = props.file;
32 if (!file || !canPreview(file)) return;
33
34 const controller = new AbortController();
35 void props.source.fileBlob(props.noteId, file.id, controller.signal)
36 .then((blob) => {
37 if (controller.signal.aborted) return;
38 currentUrl = URL.createObjectURL(blob);
39 setPreviewUrl(currentUrl);
40 })
41 .catch((err) => {
42 if (!controller.signal.aborted) setPreviewError(formatApiError(err));
43 });
44
45 onCleanup(() => {
46 controller.abort();
47 if (currentUrl) URL.revokeObjectURL(currentUrl);
48 currentUrl = "";
49 });
50 });
51
52 onCleanup(() => {
53 if (currentUrl) URL.revokeObjectURL(currentUrl);
54 });
55
56 return (
57 <Dialog open={props.file != null} onOpenChange={(open) => !open && props.onClose()}>
58 <DialogContent class="h-[min(86vh,54rem)] max-w-[min(72rem,calc(100vw-1.5rem))] rounded-xl bg-background p-0">
59 <Show when={props.file}>
60 {(file) => (
61 <>
62 <div class="shrink-0 border-b border-border/80 px-4 py-3 pr-36">
63 <div class="min-w-0">
64 <DialogTitle class="truncate text-base">{file().name}</DialogTitle>
65 <p class="mt-0.5 truncate text-xs text-muted-foreground">{type() || t("notes.unknownFileType")}</p>
66 </div>
67 <a class="absolute right-14 top-3 z-10 inline-flex h-8 items-center justify-center rounded-md border border-input bg-background/80 px-3 text-xs font-medium shadow-xs transition-all hover:bg-accent hover:text-accent-foreground" href={downloadUrl()} download={file().name}>
68 {t("notes.downloadFile")}
69 </a>
70 </div>
71
72 <div class="min-h-0 flex-1 bg-neutral-950 p-3 sm:p-5">
73 <div class="flex h-full min-h-0 items-center justify-center overflow-hidden rounded-lg bg-neutral-900/70">
74 <Show
75 when={canPreview(file())}
76 fallback={<p class="mx-auto max-w-md rounded-md border border-dashed border-white/20 bg-white/5 px-4 py-8 text-center text-sm text-white/70">{t("notes.previewUnsupported")}</p>}
77 >
78 <Show when={!previewError()} fallback={<p class="mx-auto max-w-md rounded-md border border-dashed border-white/20 bg-white/5 px-4 py-8 text-center text-sm text-white/70">{previewError()}</p>}>
79 <Switch fallback={<p class="text-sm text-white/60">{t("common.loading")}</p>}>
80 <Match when={type().startsWith("image/") && previewUrl()}>
81 <img src={previewUrl()} alt={file().name} class="max-h-full max-w-full object-contain" />
82 </Match>
83 <Match when={type().startsWith("video/") && previewUrl()}>
84 <video src={previewUrl()} controls class="max-h-full max-w-full" />
85 </Match>
86 <Match when={type().startsWith("audio/") && previewUrl()}>
87 <div class="w-full max-w-2xl rounded-lg bg-background p-4 shadow-xs">
88 <audio src={previewUrl()} controls class="w-full" />
89 </div>
90 </Match>
91 <Match when={canFrame(type()) && previewUrl()}>
92 <iframe title={file().name} src={previewUrl()} class="h-full w-full rounded-md border border-white/10 bg-background" />
93 </Match>
94 </Switch>
95 </Show>
96 </Show>
97 </div>
98 </div>
99 </>
100 )}
101 </Show>
102 </DialogContent>
103 </Dialog>
104 );
105}