From 7e1135beb1a0fef4d55ec71d95eee26000827801 Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Sun, 22 Nov 2026 15:56:19 +0100 Subject: [PATCH] Start a folder at the top and never show the last one's entries A folder opened from the popup kept the previous folder's rows, trail and name until its own listing arrived, and it kept that listing's scroll offset, so a folder opened part-way down or at its bottom. The listing is now handed back only for the volume and path it belongs to, shows a loading line meanwhile, and is keyed by folder and search so each one starts at the top. The trail is built from the path at once instead of waiting for the listing, and the file preview header puts the size and full path on their own line so a phone no longer squeezes them into a character column. --- src/components/general/file-preview-modal.tsx | 66 +++++++++---------- .../general/folder-preview-modal.tsx | 30 ++++++--- .../backend/storage/list-storage-entries.ts | 24 ------- src/hooks/use-storage-folder.ts | 31 +++++++-- .../admin/files/(sections)/files-browser.tsx | 10 +-- src/pages/admin/files/index.server.ts | 4 +- src/pages/api/admin/storage/list.ts | 4 +- src/utils/folder-text.test.ts | 5 ++ src/utils/folder-text.ts | 9 ++- src/utils/storage-volumes.test.ts | 23 +++++++ src/utils/storage-volumes.ts | 25 +++++++ 11 files changed, 149 insertions(+), 82 deletions(-) diff --git a/src/components/general/file-preview-modal.tsx b/src/components/general/file-preview-modal.tsx index ae82487e..53e6f8b5 100644 --- a/src/components/general/file-preview-modal.tsx +++ b/src/components/general/file-preview-modal.tsx @@ -84,44 +84,44 @@ export default function FilePreviewModal({ }} className="max-w-[95vw]! w-[95vw] h-[90vh] p-0 bg-paper flex flex-col overflow-hidden" > - - + +

{base_name}

- -
- {stepping ? ( - step({ by: -1 })} - onNext={() => step({ by: 1 })} - /> - ) : null} - - - Download - - - - -
+ {stepping ? ( + step({ by: -1 })} + onNext={() => step({ by: 1 })} + /> + ) : null} + + + Download + + + + + + + storageBreadcrumbs({ spec, path }), + [spec.volume, spec.owner_id, path], + ); + const name = + crumbs.at(-1)?.name || + folder?.label || + storagePathName({ volume: spec.volume, path }); const gallery = useMemo( () => imagePreviewGallery({ items: (folder?.entries || []).map((entry) => ({ ...entry, url: entry.url || "" })) }), [folder], @@ -69,7 +80,7 @@ export default function FolderPreviewModal({ spec, path: start_path, onClose }: {name} - {folderSummary({ folder })} + {folderSummary({ folder, loading })} - + - + {folder?.entries.length ? ( folder.entries.map((entry) => ( - {emptyFolderText({ folder, search, loading, error })} + {emptyFolderText({ folder, search: query, loading, error })} )} ); -} +} \ No newline at end of file diff --git a/src/functions/backend/storage/list-storage-entries.ts b/src/functions/backend/storage/list-storage-entries.ts index 08d224f6..a78b6ded 100644 --- a/src/functions/backend/storage/list-storage-entries.ts +++ b/src/functions/backend/storage/list-storage-entries.ts @@ -202,27 +202,3 @@ export function canSeePath({ resolved, path }: { resolved: ResolvedStorageSpec; function joinPath({ relative, name }: { relative: string; name: string }) { return relative ? `${relative}/${name}` : name; } - -/** - * The breadcrumb trail of a folder, from the volume's top: one crumb per - * segment, each with the address that opens it. - */ -export function storageBreadcrumbs({ - spec, - path: relative, -}: { - spec: StorageSpec; - path?: string | null; -}) { - const segments = (relative || "").split("/").filter(Boolean); - const crumbs = [{ name: "", href: storageSpecHref({ spec }) }]; - - for (let i = 1; i <= segments.length; i++) { - crumbs.push({ - name: segments[i - 1] || "", - href: storageSpecHref({ spec, path: segments.slice(0, i).join("/") }), - }); - } - - return crumbs; -} diff --git a/src/hooks/use-storage-folder.ts b/src/hooks/use-storage-folder.ts index a5cd3fa6..bdbcd592 100644 --- a/src/hooks/use-storage-folder.ts +++ b/src/hooks/use-storage-folder.ts @@ -23,6 +23,11 @@ type Params = { * One folder of the files browser. The page's own copy is used when it is * the folder asked for and nothing is searched; anything else loads from * the API, so a page never carries the whole volume. + * + * A loaded folder is only ever handed back for the volume and path it + * belongs to: opening another folder shows its own name and a loading + * line, never the previous folder's entries (which would still be + * clickable, and open the folder the user just left). */ export default function useStorageFolder({ spec, path, search, page_folder }: Params) { const from_page = Boolean( @@ -33,21 +38,22 @@ export default function useStorageFolder({ spec, path, search, page_folder }: Pa page_folder.path == path && !search, ); - const [folder, set_folder] = useState( - from_page ? page_folder || null : null, + const request_key = folderKey({ spec, path }); + const [loaded, set_loaded] = useState<{ key: string; folder: StorageFolderPayload | null } | null>( + from_page && page_folder ? { key: request_key, folder: page_folder } : null, ); const [loading, set_loading] = useState(!from_page && Boolean(spec)); const [error, set_error] = useState(); useEffect(() => { if (!spec) { - set_folder(null); + set_loaded(null); set_loading(false); return; } if (from_page) { - set_folder(page_folder || null); + set_loaded({ key: request_key, folder: page_folder || null }); set_loading(false); set_error(undefined); return; @@ -77,12 +83,12 @@ export default function useStorageFolder({ spec, path, search, page_folder }: Pa set_loading(false); if (!res?.success || !res.singleRes) { - set_folder(null); + set_loaded({ key: request_key, folder: null }); set_error(res?.msg || `Couldn't open that folder.`); return; } - set_folder(res.singleRes); + set_loaded({ key: request_key, folder: res.singleRes }); }; load().catch(() => {}); @@ -90,5 +96,16 @@ export default function useStorageFolder({ spec, path, search, page_folder }: Pa return () => controller.abort(); }, [spec?.volume, spec?.owner_id, path, search, from_page, page_folder]); - return { folder, loading, error }; + const folder = loaded?.key == request_key ? loaded.folder : null; + const pending = Boolean(spec) && loaded?.key != request_key; + + return { folder, loading: loading || pending, error }; } + +/** + * What a loaded folder belongs to: its volume, its owner and its path. A + * search is left out, since filtered entries are still that folder's. + */ +function folderKey({ spec, path }: { spec: Params["spec"]; path: string }) { + return `${spec?.volume || ""}:${spec?.owner_id ?? ""}:${path}`; +} \ No newline at end of file diff --git a/src/pages/admin/files/(sections)/files-browser.tsx b/src/pages/admin/files/(sections)/files-browser.tsx index 55d5267a..26796b43 100644 --- a/src/pages/admin/files/(sections)/files-browser.tsx +++ b/src/pages/admin/files/(sections)/files-browser.tsx @@ -5,6 +5,7 @@ import Span from "@moduletrace/twui/layout/Span"; import Stack from "@moduletrace/twui/layout/Stack"; import type { StorageFolderPayload, StorageSpecOption } from "@/src/types"; import type { StorageSpec } from "@/src/utils/storage-volumes"; +import { storageBreadcrumbs } from "@/src/utils/storage-volumes"; import { emptyFolderText, folderSummary } from "@/src/utils/folder-text"; import imagePreviewGallery from "@/src/utils/image-preview-gallery"; import FilesBreadcrumbRow from "@/src/components/general/files-breadcrumb-row"; @@ -48,6 +49,7 @@ export default function FilesBrowser({ () => imagePreviewGallery({ items: (folder?.entries || []).map((entry) => ({ ...entry, url: entry.url || "" })) }), [folder], ); + const crumbs = spec ? storageBreadcrumbs({ spec, path }) : folder?.breadcrumbs || []; return ( @@ -82,16 +84,14 @@ export default function FilesBrowser({

- {folder?.breadcrumbs.at(-1)?.name || - folder?.label || - "Files"} + {crumbs.at(-1)?.name || folder?.label || "Files"}

- {folderSummary({ folder })} + {folderSummary({ folder, loading })}
diff --git a/src/pages/admin/files/index.server.ts b/src/pages/admin/files/index.server.ts index 91a7898c..8aa4c63d 100644 --- a/src/pages/admin/files/index.server.ts +++ b/src/pages/admin/files/index.server.ts @@ -1,9 +1,9 @@ import userAuth from "@/src/functions/backend/auth/user-auth"; -import listStorageEntries, { storageBreadcrumbs } from "@/src/functions/backend/storage/list-storage-entries"; +import listStorageEntries from "@/src/functions/backend/storage/list-storage-entries"; import resolveStoragePath from "@/src/functions/backend/storage/resolve-storage-path"; import resolveStorageSpec, { grabStorageSpecs } from "@/src/functions/backend/storage/resolve-storage-spec"; import type { PagePropsType, StorageFolderPayload } from "@/src/types"; -import { parseStorageSpec, type StorageSpec } from "@/src/utils/storage-volumes"; +import { parseStorageSpec, storageBreadcrumbs, type StorageSpec } from "@/src/utils/storage-volumes"; import type { BunextPageServerFn } from "@moduletrace/bunext/types"; /** diff --git a/src/pages/api/admin/storage/list.ts b/src/pages/api/admin/storage/list.ts index ccdd5bbe..75019301 100644 --- a/src/pages/api/admin/storage/list.ts +++ b/src/pages/api/admin/storage/list.ts @@ -1,12 +1,12 @@ import userAuth from "@/src/functions/backend/auth/user-auth"; -import listStorageEntries, { storageBreadcrumbs } from "@/src/functions/backend/storage/list-storage-entries"; +import listStorageEntries from "@/src/functions/backend/storage/list-storage-entries"; import resolveStoragePath from "@/src/functions/backend/storage/resolve-storage-path"; import resolveStorageSpec from "@/src/functions/backend/storage/resolve-storage-spec"; import type { StorageFolderPayload } from "@/src/types"; import parseWithSchema from "@/src/utils/parse-with-schema"; import safeErrorMessage from "@/src/utils/safe-error-message"; import { StorageListRequestSchema } from "@/src/schemas/storage-schema"; -import { parseStorageSpec } from "@/src/utils/storage-volumes"; +import { parseStorageSpec, storageBreadcrumbs } from "@/src/utils/storage-volumes"; import type { APIResponseObject, BunextAPIRouteHandler } from "@moduletrace/bunext/types"; /** diff --git a/src/utils/folder-text.test.ts b/src/utils/folder-text.test.ts index c5a3011b..c5f1728a 100644 --- a/src/utils/folder-text.test.ts +++ b/src/utils/folder-text.test.ts @@ -30,6 +30,11 @@ describe("folderSummary", () => { test("asks for a folder when there is none", () => { expect(folderSummary({ folder: null })).toBe("Pick a folder to look inside."); }); + + test("a folder still loading is never counted as none", () => { + expect(folderSummary({ folder: null, loading: true })).toBe("Loading..."); + expect(folderSummary({ folder: folder({ total: 4 }), loading: true })).toBe("Loading..."); + }); }); describe("emptyFolderText", () => { diff --git a/src/utils/folder-text.ts b/src/utils/folder-text.ts index 75e1c0a5..1bbeb73d 100644 --- a/src/utils/folder-text.ts +++ b/src/utils/folder-text.ts @@ -5,7 +5,14 @@ import type { StorageFolderPayload } from "@/src/types"; * that isn't on disk, that it isn't there. A stale folder link must never * read as an empty folder. */ -export function folderSummary({ folder }: { folder: StorageFolderPayload | null }) { +export function folderSummary({ + folder, + loading, +}: { + folder: StorageFolderPayload | null; + loading?: boolean; +}) { + if (loading) return "Loading..."; if (!folder) return "Pick a folder to look inside."; if (folder.missing) { diff --git a/src/utils/storage-volumes.test.ts b/src/utils/storage-volumes.test.ts index b8865e5e..106be391 100644 --- a/src/utils/storage-volumes.test.ts +++ b/src/utils/storage-volumes.test.ts @@ -4,6 +4,7 @@ import { isSameStorageSpec, isStorageVolume, parseStorageSpec, + storageBreadcrumbs, storagePathName, storageSpecHref, storageVolumeFromRef, @@ -77,6 +78,28 @@ describe("volume names", () => { }); }); +describe("storageBreadcrumbs", () => { + const spec = { volume: "computers", owner_id: 1 } as const; + + test("at the top of a volume there is only the way back to it", () => { + expect(storageBreadcrumbs({ spec })).toEqual([ + { name: "", href: `${StorageBrowserPath}?volume=computers&owner=1` }, + ]); + expect(storageBreadcrumbs({ spec, path: "/" })).toHaveLength(1); + }); + + test("walks down one crumb per segment", () => { + expect(storageBreadcrumbs({ spec, path: "seo/raw mes" })).toEqual([ + { name: "", href: `${StorageBrowserPath}?volume=computers&owner=1` }, + { name: "seo", href: `${StorageBrowserPath}?volume=computers&owner=1&path=seo` }, + { + name: "raw mes", + href: `${StorageBrowserPath}?volume=computers&owner=1&path=seo%2Fraw+mes`, + }, + ]); + }); +}); + describe("isSameStorageSpec", () => { test("compares volume and owner", () => { const a = { volume: "bots", owner_id: 1 } as const; diff --git a/src/utils/storage-volumes.ts b/src/utils/storage-volumes.ts index 552777a1..ca4bd3cd 100644 --- a/src/utils/storage-volumes.ts +++ b/src/utils/storage-volumes.ts @@ -154,6 +154,31 @@ export function storageSpecHref({ spec, path }: { spec: StorageSpec; path?: stri return `${StorageBrowserPath}?${query.toString()}`; } +/** + * The breadcrumb trail of a folder, from the volume's top: one crumb per + * segment, each with the address that opens it. Built from the path asked + * for, so a trail can be shown while its listing still loads. + */ +export function storageBreadcrumbs({ + spec, + path: relative, +}: { + spec: StorageSpec; + path?: string | null; +}) { + const segments = (relative || "").split("/").filter(Boolean); + const crumbs = [{ name: "", href: storageSpecHref({ spec }) }]; + + for (let i = 1; i <= segments.length; i++) { + crumbs.push({ + name: segments[i - 1] || "", + href: storageSpecHref({ spec, path: segments.slice(0, i).join("/") }), + }); + } + + return crumbs; +} + /** * A folder's label for a chip: the last path segment, or the volume's * title at the top.