forge
Explore
Log in

Don't you dare go Hollow

✨ note file preview

Browse files at this commit

ddc6e5f9b8baf93411f2d0829a5c9b1cd5fba211Burak BoduroğluParent: 7992c8f
  • feat
    • ✨ note file preview
Show full message
feat: ✨ note file preview
- add blob fetch helper for inline note file previews
- preview image, video, audio, PDF, and text attachments in the note panel
- separate preview and download actions so one click does one thing
- add preview close control and unsupported-file copy
src/api/client.tsmodified+13 -0
@@ -202,6 +202,19 @@ export async function formClient<T>(path: string, body: FormData, signal?: Abort
202202 return data as T;
203203 }
204204
205+export async function blobClient(path: string, signal?: AbortSignal): Promise<Blob> {
206+ const res = await fetch(`${API_PREFIX}${path}`, {
207+ credentials: "same-origin",
208+ signal,
209+ });
210+
211+ if (!res.ok) {
212+ throw new ApiError(res.status, res.statusText || "Request failed");
213+ }
214+
215+ return res.blob();
216+}
217+
205218 export function formatApiErrorMessage(message: string, locale: Locale = currentLocale()): string {
206219 const normalized = normalizeApiMessage(message);
207220 const known = API_ERROR_MESSAGES[normalized]?.[locale];
src/api/getNoteFileBlob.tsadded+5 -0
@@ -0,0 +1,5 @@
1+import { blobClient } from "./client";
2+
3+export async function getNoteFileBlob(noteId: string, fileId: string, signal?: AbortSignal): Promise<Blob> {
4+ return blobClient(`/notes/${noteId}/files/${fileId}`, signal);
5+}
src/components/notes/note-file-preview.tsxadded+88 -0
@@ -0,0 +1,88 @@
1+import { Show, createEffect, createSignal, onCleanup } from "solid-js";
2+import { getNoteFileBlob } from "@/api/getNoteFileBlob";
3+import type { NoteFile } from "@/api/types";
4+import { getNoteFileUrl } from "@/api/getNoteFileUrl";
5+import { Button } from "@/components/ui/button";
6+import { useT } from "@/stores/preferences-context";
7+
8+function canFrame(type: string): boolean {
9+ return type === "application/pdf" || type.startsWith("text/");
10+}
11+
12+function 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+
17+export function NoteFilePreview(props: { noteId: string; file: NoteFile | null; onClose: () => void }) {
18+ const t = useT();
19+ let currentUrl = "";
20+ const [previewUrl, setPreviewUrl] = createSignal("");
21+ const downloadUrl = () => (props.file ? getNoteFileUrl(props.noteId, props.file.id) : "");
22+ const type = () => props.file?.content_type ?? "";
23+
24+ createEffect(() => {
25+ if (currentUrl) URL.revokeObjectURL(currentUrl);
26+ currentUrl = "";
27+ setPreviewUrl("");
28+
29+ const file = props.file;
30+ if (!file || !canPreview(file)) return;
31+
32+ const controller = new AbortController();
33+ void getNoteFileBlob(props.noteId, file.id, controller.signal).then((blob) => {
34+ if (controller.signal.aborted) return;
35+ currentUrl = URL.createObjectURL(blob);
36+ setPreviewUrl(currentUrl);
37+ });
38+
39+ onCleanup(() => {
40+ controller.abort();
41+ if (currentUrl) URL.revokeObjectURL(currentUrl);
42+ currentUrl = "";
43+ });
44+ });
45+
46+ onCleanup(() => {
47+ if (currentUrl) URL.revokeObjectURL(currentUrl);
48+ });
49+
50+ return (
51+ <Show when={props.file}>
52+ {(file) => (
53+ <div class="space-y-3 rounded-md border border-border/70 bg-background/70 p-3">
54+ <div class="flex flex-wrap items-center justify-between gap-2">
55+ <div class="min-w-0">
56+ <h4 class="truncate text-sm font-semibold">{file().name}</h4>
57+ <p class="mt-0.5 text-xs text-muted-foreground">{type() || t("notes.unknownFileType")}</p>
58+ </div>
59+ <Button type="button" size="sm" variant="ghost" class="rounded-md" onClick={props.onClose}>
60+ {t("nav.close")}
61+ </Button>
62+ </div>
63+
64+ <Show
65+ when={canPreview(file())}
66+ fallback={<p class="rounded-md border border-dashed border-border/80 bg-muted/20 px-3 py-6 text-center text-sm text-muted-foreground">{t("notes.previewUnsupported")}</p>}
67+ >
68+ <Show when={type().startsWith("image/") && previewUrl()}>
69+ <img src={previewUrl()} alt={file().name} class="max-h-[28rem] w-full rounded-md border border-border/60 bg-muted/20 object-contain" />
70+ </Show>
71+ <Show when={type().startsWith("video/") && previewUrl()}>
72+ <video src={previewUrl()} controls class="max-h-[28rem] w-full rounded-md border border-border/60 bg-black" />
73+ </Show>
74+ <Show when={type().startsWith("audio/") && previewUrl()}>
75+ <audio src={previewUrl()} controls class="w-full" />
76+ </Show>
77+ <Show when={canFrame(type()) && previewUrl()}>
78+ <iframe title={file().name} src={previewUrl()} class="h-[28rem] w-full rounded-md border border-border/60 bg-background" />
79+ </Show>
80+ </Show>
81+ <a class="inline-flex h-8 items-center justify-center rounded-md border border-input bg-background/80 px-3 text-xs font-medium shadow-sm transition-all hover:bg-accent hover:text-accent-foreground" href={downloadUrl()} download={file().name}>
82+ {t("notes.downloadFile")}
83+ </a>
84+ </div>
85+ )}
86+ </Show>
87+ );
88+}
src/components/notes/note-files-panel.tsxmodified+15 -4
@@ -6,9 +6,10 @@ import { getSettings } from "@/api/getSettings";
66 import { postNoteFile } from "@/api/postNoteFile";
77 import { ApiError, formatApiError } from "@/api/client";
88 import type { NoteFile } from "@/api/types";
9+import { NoteFilePreview } from "@/components/notes/note-file-preview";
910 import { Button } from "@/components/ui/button";
1011 import { ConfirmDialog } from "@/components/ui/confirm-dialog";
11-import { IconPlus, IconTrash } from "@/components/ui/icons";
12+import { IconEye, IconPlus, IconTrash } from "@/components/ui/icons";
1213 import { PageSpinner } from "@/components/ui/page-spinner";
1314 import { useT } from "@/stores/preferences-context";
1415
@@ -25,6 +26,7 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) {
2526 const t = useT();
2627 const [error, setError] = createSignal("");
2728 const [pending, setPending] = createSignal(false);
29+ const [previewFile, setPreviewFile] = createSignal<NoteFile | null>(null);
2830 const [deleteTarget, setDeleteTarget] = createSignal<NoteFile | null>(null);
2931 const [files, { refetch }] = createResource(
3032 () => (props.active ? props.noteId : null),
@@ -107,11 +109,18 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) {
107109 <ul class="divide-y divide-border/70 rounded-md border border-border/70 bg-background/70">
108110 <For each={files() ?? []}>
109111 {(file) => (
110- <li class="flex items-center justify-between gap-3 px-3 py-2 text-sm">
111- <a class="min-w-0 flex-1 truncate font-medium hover:text-primary" href={getNoteFileUrl(props.noteId, file.id)} download={file.name}>
112+ <li class="flex items-center justify-between gap-2 px-3 py-2 text-sm">
113+ <span class="min-w-0 flex-1 truncate font-medium">
112114 {file.name}
113- </a>
115+ </span>
114116 <span class="shrink-0 text-xs text-muted-foreground">{formatBytes(file.size)}</span>
117+ <Button type="button" variant="ghost" size="sm" class="h-8 rounded-md" onClick={() => setPreviewFile(file)}>
118+ <IconEye class="h-4 w-4" />
119+ {t("common.view")}
120+ </Button>
121+ <a class="inline-flex h-8 items-center justify-center rounded-md px-3 text-xs font-medium transition-all hover:bg-accent hover:text-accent-foreground" href={getNoteFileUrl(props.noteId, file.id)} download={file.name}>
122+ {t("notes.downloadFile")}
123+ </a>
115124 <Button type="button" variant="ghost" size="icon" class="h-8 w-8 rounded-md text-destructive hover:text-destructive" onClick={() => setDeleteTarget(file)}>
116125 <IconTrash class="h-4 w-4" />
117126 </Button>
@@ -119,6 +128,7 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) {
119128 )}
120129 </For>
121130 </ul>
131+ <NoteFilePreview noteId={props.noteId} file={previewFile()} onClose={() => setPreviewFile(null)} />
122132 </Show>
123133 </Suspense>
124134
@@ -132,6 +142,7 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) {
132142 const target = deleteTarget();
133143 if (!target) return;
134144 await deleteNoteFileById(props.noteId, target.id);
145+ if (previewFile()?.id === target.id) setPreviewFile(null);
135146 setDeleteTarget(null);
136147 await refetch();
137148 }}
src/i18n/messages.tsmodified+9 -0
@@ -180,9 +180,12 @@ export type MessageKey =
180180 | "notes.files"
181181 | "notes.filesHelp"
182182 | "notes.addFile"
183+ | "notes.downloadFile"
183184 | "notes.noFiles"
184185 | "notes.fileLimit"
185186 | "notes.fileTooLarge"
187+ | "notes.previewUnsupported"
188+ | "notes.unknownFileType"
186189 | "notes.helpTitle"
187190 | "notes.helpBody"
188191 | "events.title"
@@ -693,9 +696,12 @@ const en: Dict = {
693696 "notes.files": "Attachments",
694697 "notes.filesHelp": "Up to 10 files. Max {size} each.",
695698 "notes.addFile": "Add file",
699+ "notes.downloadFile": "Download",
696700 "notes.noFiles": "No files attached.",
697701 "notes.fileLimit": "This note already has 10 files.",
698702 "notes.fileTooLarge": "File is too large. Max {size}.",
703+ "notes.previewUnsupported": "Preview is not available for this file type. Download the file to open it.",
704+ "notes.unknownFileType": "Unknown file type",
699705 "notes.helpTitle": "About notes",
700706 "notes.helpBody":
701707 "Notes are private to you only. Title max 200 characters, content max 10,000. Delete and update open a confirmation dialog with a short summary before they run.",
@@ -1218,9 +1224,12 @@ const tr: Dict = {
12181224 "notes.files": "Ekler",
12191225 "notes.filesHelp": "En fazla 10 dosya. Dosya başına {size} sınırı.",
12201226 "notes.addFile": "Dosya ekle",
1227+ "notes.downloadFile": "İndir",
12211228 "notes.noFiles": "Henüz dosya eklenmemiş.",
12221229 "notes.fileLimit": "Bu notta zaten 10 dosya var.",
12231230 "notes.fileTooLarge": "Dosya çok büyük. En fazla {size}.",
1231+ "notes.previewUnsupported": "Bu dosya türü için önizleme yok. Açmak için dosyayı indirin.",
1232+ "notes.unknownFileType": "Bilinmeyen dosya türü",
12241233 "notes.helpTitle": "Notlar hakkında",
12251234 "notes.helpBody":
12261235 "Notlar yalnızca sana aittir. Başlık en fazla 200, içerik 10.000 karakter. Silme ve güncelleme özetli onay penceresi ister.",