forge
Explore
Log in

Don't you dare go Hollow

hezarfen_frontend

Clone
★ 0

0 watching0 forksTypeScript5.5 MiBc35a83aBurak Boduroğlu4 months ago440 commits

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