✨ note file preview
Browse files at this commitddc6e5f9b8baf93411f2d0829a5c9b1cd5fba211Parent: 7992c8f- feat
- ✨ note file preview
5 changed files +130 -4
- src/api/client.ts +13 -0
- src/api/getNoteFileBlob.ts +5 -0
- src/components/notes/note-file-preview.tsx +88 -0
- src/components/notes/note-files-panel.tsx +15 -4
- src/i18n/messages.ts +9 -0
src/api/client.tsmodified+13 -0
| @@ -202,6 +202,19 @@ export async function formClient<T>(path: string, body: FormData, signal?: Abort | |||
| 202 | return data as T; | 202 | return data as T; |
| 203 | } | 203 | } |
| 204 | 204 | ||
| 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 | + | ||
| 205 | export function formatApiErrorMessage(message: string, locale: Locale = currentLocale()): string { | 218 | export function formatApiErrorMessage(message: string, locale: Locale = currentLocale()): string { |
| 206 | const normalized = normalizeApiMessage(message); | 219 | const normalized = normalizeApiMessage(message); |
| 207 | const known = API_ERROR_MESSAGES[normalized]?.[locale]; | 220 | 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"; | |||
| 6 | import { postNoteFile } from "@/api/postNoteFile"; | 6 | import { postNoteFile } from "@/api/postNoteFile"; |
| 7 | import { ApiError, formatApiError } from "@/api/client"; | 7 | import { ApiError, formatApiError } from "@/api/client"; |
| 8 | import type { NoteFile } from "@/api/types"; | 8 | import type { NoteFile } from "@/api/types"; |
| 9 | +import { NoteFilePreview } from "@/components/notes/note-file-preview"; | ||
| 9 | import { Button } from "@/components/ui/button"; | 10 | import { Button } from "@/components/ui/button"; |
| 10 | import { ConfirmDialog } from "@/components/ui/confirm-dialog"; | 11 | import { ConfirmDialog } from "@/components/ui/confirm-dialog"; |
| 11 | -import { IconPlus, IconTrash } from "@/components/ui/icons"; | 12 | +import { IconEye, IconPlus, IconTrash } from "@/components/ui/icons"; |
| 12 | import { PageSpinner } from "@/components/ui/page-spinner"; | 13 | import { PageSpinner } from "@/components/ui/page-spinner"; |
| 13 | import { useT } from "@/stores/preferences-context"; | 14 | import { useT } from "@/stores/preferences-context"; |
| 14 | 15 | ||
| @@ -25,6 +26,7 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) { | |||
| 25 | const t = useT(); | 26 | const t = useT(); |
| 26 | const [error, setError] = createSignal(""); | 27 | const [error, setError] = createSignal(""); |
| 27 | const [pending, setPending] = createSignal(false); | 28 | const [pending, setPending] = createSignal(false); |
| 29 | + const [previewFile, setPreviewFile] = createSignal<NoteFile | null>(null); | ||
| 28 | const [deleteTarget, setDeleteTarget] = createSignal<NoteFile | null>(null); | 30 | const [deleteTarget, setDeleteTarget] = createSignal<NoteFile | null>(null); |
| 29 | const [files, { refetch }] = createResource( | 31 | const [files, { refetch }] = createResource( |
| 30 | () => (props.active ? props.noteId : null), | 32 | () => (props.active ? props.noteId : null), |
| @@ -107,11 +109,18 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) { | |||
| 107 | <ul class="divide-y divide-border/70 rounded-md border border-border/70 bg-background/70"> | 109 | <ul class="divide-y divide-border/70 rounded-md border border-border/70 bg-background/70"> |
| 108 | <For each={files() ?? []}> | 110 | <For each={files() ?? []}> |
| 109 | {(file) => ( | 111 | {(file) => ( |
| 110 | - <li class="flex items-center justify-between gap-3 px-3 py-2 text-sm"> | 112 | + <li class="flex items-center justify-between gap-2 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}> | 113 | + <span class="min-w-0 flex-1 truncate font-medium"> |
| 112 | {file.name} | 114 | {file.name} |
| 113 | - </a> | 115 | + </span> |
| 114 | <span class="shrink-0 text-xs text-muted-foreground">{formatBytes(file.size)}</span> | 116 | <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> | ||
| 115 | <Button type="button" variant="ghost" size="icon" class="h-8 w-8 rounded-md text-destructive hover:text-destructive" onClick={() => setDeleteTarget(file)}> | 124 | <Button type="button" variant="ghost" size="icon" class="h-8 w-8 rounded-md text-destructive hover:text-destructive" onClick={() => setDeleteTarget(file)}> |
| 116 | <IconTrash class="h-4 w-4" /> | 125 | <IconTrash class="h-4 w-4" /> |
| 117 | </Button> | 126 | </Button> |
| @@ -119,6 +128,7 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) { | |||
| 119 | )} | 128 | )} |
| 120 | </For> | 129 | </For> |
| 121 | </ul> | 130 | </ul> |
| 131 | + <NoteFilePreview noteId={props.noteId} file={previewFile()} onClose={() => setPreviewFile(null)} /> | ||
| 122 | </Show> | 132 | </Show> |
| 123 | </Suspense> | 133 | </Suspense> |
| 124 | 134 | ||
| @@ -132,6 +142,7 @@ export function NoteFilesPanel(props: { noteId: string; active: boolean }) { | |||
| 132 | const target = deleteTarget(); | 142 | const target = deleteTarget(); |
| 133 | if (!target) return; | 143 | if (!target) return; |
| 134 | await deleteNoteFileById(props.noteId, target.id); | 144 | await deleteNoteFileById(props.noteId, target.id); |
| 145 | + if (previewFile()?.id === target.id) setPreviewFile(null); | ||
| 135 | setDeleteTarget(null); | 146 | setDeleteTarget(null); |
| 136 | await refetch(); | 147 | await refetch(); |
| 137 | }} | 148 | }} |
src/i18n/messages.tsmodified+9 -0
| @@ -180,9 +180,12 @@ export type MessageKey = | |||
| 180 | | "notes.files" | 180 | | "notes.files" |
| 181 | | "notes.filesHelp" | 181 | | "notes.filesHelp" |
| 182 | | "notes.addFile" | 182 | | "notes.addFile" |
| 183 | + | "notes.downloadFile" | ||
| 183 | | "notes.noFiles" | 184 | | "notes.noFiles" |
| 184 | | "notes.fileLimit" | 185 | | "notes.fileLimit" |
| 185 | | "notes.fileTooLarge" | 186 | | "notes.fileTooLarge" |
| 187 | + | "notes.previewUnsupported" | ||
| 188 | + | "notes.unknownFileType" | ||
| 186 | | "notes.helpTitle" | 189 | | "notes.helpTitle" |
| 187 | | "notes.helpBody" | 190 | | "notes.helpBody" |
| 188 | | "events.title" | 191 | | "events.title" |
| @@ -693,9 +696,12 @@ const en: Dict = { | |||
| 693 | "notes.files": "Attachments", | 696 | "notes.files": "Attachments", |
| 694 | "notes.filesHelp": "Up to 10 files. Max {size} each.", | 697 | "notes.filesHelp": "Up to 10 files. Max {size} each.", |
| 695 | "notes.addFile": "Add file", | 698 | "notes.addFile": "Add file", |
| 699 | + "notes.downloadFile": "Download", | ||
| 696 | "notes.noFiles": "No files attached.", | 700 | "notes.noFiles": "No files attached.", |
| 697 | "notes.fileLimit": "This note already has 10 files.", | 701 | "notes.fileLimit": "This note already has 10 files.", |
| 698 | "notes.fileTooLarge": "File is too large. Max {size}.", | 702 | "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", | ||
| 699 | "notes.helpTitle": "About notes", | 705 | "notes.helpTitle": "About notes", |
| 700 | "notes.helpBody": | 706 | "notes.helpBody": |
| 701 | "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.", | 707 | "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 = { | |||
| 1218 | "notes.files": "Ekler", | 1224 | "notes.files": "Ekler", |
| 1219 | "notes.filesHelp": "En fazla 10 dosya. Dosya başına {size} sınırı.", | 1225 | "notes.filesHelp": "En fazla 10 dosya. Dosya başına {size} sınırı.", |
| 1220 | "notes.addFile": "Dosya ekle", | 1226 | "notes.addFile": "Dosya ekle", |
| 1227 | + "notes.downloadFile": "İndir", | ||
| 1221 | "notes.noFiles": "Henüz dosya eklenmemiş.", | 1228 | "notes.noFiles": "Henüz dosya eklenmemiş.", |
| 1222 | "notes.fileLimit": "Bu notta zaten 10 dosya var.", | 1229 | "notes.fileLimit": "Bu notta zaten 10 dosya var.", |
| 1223 | "notes.fileTooLarge": "Dosya çok büyük. En fazla {size}.", | 1230 | "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ü", | ||
| 1224 | "notes.helpTitle": "Notlar hakkında", | 1233 | "notes.helpTitle": "Notlar hakkında", |
| 1225 | "notes.helpBody": | 1234 | "notes.helpBody": |
| 1226 | "Notlar yalnızca sana aittir. Başlık en fazla 200, içerik 10.000 karakter. Silme ve güncelleme özetli onay penceresi ister.", | 1235 | "Notlar yalnızca sana aittir. Başlık en fazla 200, içerik 10.000 karakter. Silme ve güncelleme özetli onay penceresi ister.", |