From ddc6e5f9b8baf93411f2d0829a5c9b1cd5fba211 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Boduro=C4=9Flu?= <80620802+burakboduroglu@users.noreply.github.com> Date: Thu, 16 Jul 2026 09:35:14 +0300 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20note=20file=20preview=20-?= =?UTF-8?q?=20add=20blob=20fetch=20helper=20for=20inline=20note=20file=20p?= =?UTF-8?q?reviews=20-=20preview=20image,=20video,=20audio,=20PDF,=20and?= =?UTF-8?q?=20text=20attachments=20in=20the=20note=20panel=20-=20separate?= =?UTF-8?q?=20preview=20and=20download=20actions=20so=20one=20click=20does?= =?UTF-8?q?=20one=20thing=20-=20add=20preview=20close=20control=20and=20un?= =?UTF-8?q?supported-file=20copy?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/api/client.ts | 13 ++++ src/api/getNoteFileBlob.ts | 5 ++ src/components/notes/note-file-preview.tsx | 88 ++++++++++++++++++++++ src/components/notes/note-files-panel.tsx | 19 ++++- src/i18n/messages.ts | 9 +++ 5 files changed, 130 insertions(+), 4 deletions(-) create mode 100644 src/api/getNoteFileBlob.ts create mode 100644 src/components/notes/note-file-preview.tsx diff --git a/src/api/client.ts b/src/api/client.ts index b5770eb..144d37a 100644 --- a/src/api/client.ts +++ b/src/api/client.ts @@ -202,6 +202,19 @@ export async function formClient(path: string, body: FormData, signal?: Abort return data as T; } +export async function blobClient(path: string, signal?: AbortSignal): Promise { + const res = await fetch(`${API_PREFIX}${path}`, { + credentials: "same-origin", + signal, + }); + + if (!res.ok) { + throw new ApiError(res.status, res.statusText || "Request failed"); + } + + return res.blob(); +} + export function formatApiErrorMessage(message: string, locale: Locale = currentLocale()): string { const normalized = normalizeApiMessage(message); const known = API_ERROR_MESSAGES[normalized]?.[locale]; diff --git a/src/api/getNoteFileBlob.ts b/src/api/getNoteFileBlob.ts new file mode 100644 index 0000000..2b1cb7c --- /dev/null +++ b/src/api/getNoteFileBlob.ts @@ -0,0 +1,5 @@ +import { blobClient } from "./client"; + +export async function getNoteFileBlob(noteId: string, fileId: string, signal?: AbortSignal): Promise { + return blobClient(`/notes/${noteId}/files/${fileId}`, signal); +} diff --git a/src/components/notes/note-file-preview.tsx b/src/components/notes/note-file-preview.tsx new file mode 100644 index 0000000..356d305 --- /dev/null +++ b/src/components/notes/note-file-preview.tsx @@ -0,0 +1,88 @@ +import { Show, createEffect, createSignal, onCleanup } from "solid-js"; +import { getNoteFileBlob } from "@/api/getNoteFileBlob"; +import type { NoteFile } from "@/api/types"; +import { getNoteFileUrl } from "@/api/getNoteFileUrl"; +import { Button } from "@/components/ui/button"; +import { useT } from "@/stores/preferences-context"; + +function canFrame(type: string): boolean { + return type === "application/pdf" || type.startsWith("text/"); +} + +function canPreview(file: NoteFile): boolean { + const type = file.content_type; + return type.startsWith("image/") || type.startsWith("video/") || type.startsWith("audio/") || canFrame(type); +} + +export function NoteFilePreview(props: { noteId: string; file: NoteFile | null; onClose: () => void }) { + const t = useT(); + let currentUrl = ""; + const [previewUrl, setPreviewUrl] = createSignal(""); + const downloadUrl = () => (props.file ? getNoteFileUrl(props.noteId, props.file.id) : ""); + const type = () => props.file?.content_type ?? ""; + + createEffect(() => { + if (currentUrl) URL.revokeObjectURL(currentUrl); + currentUrl = ""; + setPreviewUrl(""); + + const file = props.file; + if (!file || !canPreview(file)) return; + + const controller = new AbortController(); + void getNoteFileBlob(props.noteId, file.id, controller.signal).then((blob) => { + if (controller.signal.aborted) return; + currentUrl = URL.createObjectURL(blob); + setPreviewUrl(currentUrl); + }); + + onCleanup(() => { + controller.abort(); + if (currentUrl) URL.revokeObjectURL(currentUrl); + currentUrl = ""; + }); + }); + + onCleanup(() => { + if (currentUrl) URL.revokeObjectURL(currentUrl); + }); + + return ( + + {(file) => ( +
+
+
+

{file().name}

+

{type() || t("notes.unknownFileType")}

+
+ +
+ + {t("notes.previewUnsupported")}

} + > + + {file().name} + + + + + + +