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.
This commit is contained in:
@@ -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"
|
||||
>
|
||||
<Row className="w-full gap-3! flex-nowrap items-center pl-5 pr-14 py-3 border-0 border-b border-solid border-hairline">
|
||||
<Stack className="min-w-0 grow gap-0! items-start">
|
||||
<Stack className="w-full min-w-0 gap-1! items-stretch pl-5 pr-14 py-3 border-0 border-b border-solid border-hairline">
|
||||
<Row className="w-full gap-3! flex-wrap sm:flex-nowrap items-center">
|
||||
<H2
|
||||
title={name}
|
||||
className="text-base! leading-6! tracking-normal! font-bold text-ink truncate w-full"
|
||||
className="text-base! leading-6! tracking-normal! font-bold text-ink truncate min-w-0 w-full sm:w-auto sm:grow"
|
||||
>
|
||||
{base_name}
|
||||
</H2>
|
||||
<FilePreviewDetails size={size} full_path={full_path} />
|
||||
</Stack>
|
||||
{stepping ? (
|
||||
<FilePreviewStepper
|
||||
index={index}
|
||||
count={count}
|
||||
onPrevious={() => step({ by: -1 })}
|
||||
onNext={() => step({ by: 1 })}
|
||||
/>
|
||||
) : null}
|
||||
<Link
|
||||
href={url}
|
||||
download={base_name}
|
||||
title={`Download ${base_name}`}
|
||||
className={ActionClassName}
|
||||
>
|
||||
<Download size={16} aria-hidden />
|
||||
<Span className="hidden sm:inline">Download</Span>
|
||||
</Link>
|
||||
<Link
|
||||
href={url}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
title="Open in a new tab"
|
||||
aria-label="Open in a new tab"
|
||||
className={ActionClassName}
|
||||
>
|
||||
<SquareArrowOutUpRight size={16} aria-hidden />
|
||||
</Link>
|
||||
</Row>
|
||||
{stepping ? (
|
||||
<FilePreviewStepper
|
||||
index={index}
|
||||
count={count}
|
||||
onPrevious={() => step({ by: -1 })}
|
||||
onNext={() => step({ by: 1 })}
|
||||
/>
|
||||
) : null}
|
||||
<Link
|
||||
href={url}
|
||||
download={base_name}
|
||||
title={`Download ${base_name}`}
|
||||
className={ActionClassName}
|
||||
>
|
||||
<Download size={16} aria-hidden />
|
||||
<Span className="hidden sm:inline">Download</Span>
|
||||
</Link>
|
||||
<Link
|
||||
href={url}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
title="Open in a new tab"
|
||||
aria-label="Open in a new tab"
|
||||
className={ActionClassName}
|
||||
>
|
||||
<SquareArrowOutUpRight size={16} aria-hidden />
|
||||
</Link>
|
||||
</Row>
|
||||
<FilePreviewDetails size={size} full_path={full_path} />
|
||||
</Stack>
|
||||
<Stack className="relative grow min-h-0 gap-0! items-stretch w-full">
|
||||
<FilePreviewBody
|
||||
key={url}
|
||||
|
||||
@@ -11,7 +11,12 @@ import useStorageFolder from "@/src/hooks/use-storage-folder";
|
||||
import useTopModalEscape from "@/src/hooks/use-top-modal-escape";
|
||||
import { emptyFolderText, folderSummary } from "@/src/utils/folder-text";
|
||||
import imagePreviewGallery from "@/src/utils/image-preview-gallery";
|
||||
import { storagePathName, storageSpecHref, type StorageSpec } from "@/src/utils/storage-volumes";
|
||||
import {
|
||||
storageBreadcrumbs,
|
||||
storagePathName,
|
||||
storageSpecHref,
|
||||
type StorageSpec,
|
||||
} from "@/src/utils/storage-volumes";
|
||||
import FilesBreadcrumbRow from "./files-breadcrumb-row";
|
||||
import FilesSearchInput from "./files-search-input";
|
||||
import StorageEntryRow from "./storage-entry-row";
|
||||
@@ -38,8 +43,14 @@ export default function FolderPreviewModal({ spec, path: start_path, onClose }:
|
||||
const [search, set_search] = useState("");
|
||||
const query = useDebouncedValue({ value: search.trim() });
|
||||
const { folder, loading, error } = useStorageFolder({ spec, path, search: query });
|
||||
const name = folder?.breadcrumbs.at(-1)?.name || folder?.label || storagePathName({ volume: spec.volume, path });
|
||||
|
||||
const crumbs = useMemo(
|
||||
() => 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}
|
||||
</H2>
|
||||
<Span className="text-[13px] leading-[18px] text-ink-secondary">
|
||||
{folderSummary({ folder })}
|
||||
{folderSummary({ folder, loading })}
|
||||
</Span>
|
||||
</Stack>
|
||||
<Link
|
||||
@@ -82,10 +93,13 @@ export default function FolderPreviewModal({ spec, path: start_path, onClose }:
|
||||
</Link>
|
||||
</Row>
|
||||
<Stack className="w-full gap-0! items-stretch px-5">
|
||||
<FilesBreadcrumbRow crumbs={folder?.breadcrumbs || []} onOpenFolder={openFolder} />
|
||||
<FilesBreadcrumbRow crumbs={crumbs} onOpenFolder={openFolder} />
|
||||
<FilesSearchInput value={search} onChange={set_search} />
|
||||
</Stack>
|
||||
<Stack className="grow min-h-0 w-full mt-2 gap-0! items-stretch overflow-y-auto px-5 pb-4">
|
||||
<Stack
|
||||
key={`${path}|${query}`}
|
||||
className="grow min-h-0 w-full mt-2 gap-0! items-stretch overflow-y-auto px-5 pb-4"
|
||||
>
|
||||
{folder?.entries.length ? (
|
||||
folder.entries.map((entry) => (
|
||||
<StorageEntryRow
|
||||
@@ -98,10 +112,10 @@ export default function FolderPreviewModal({ spec, path: start_path, onClose }:
|
||||
))
|
||||
) : (
|
||||
<Span className="py-8 text-[13px] leading-[18px] text-ink-secondary">
|
||||
{emptyFolderText({ folder, search, loading, error })}
|
||||
{emptyFolderText({ folder, search: query, loading, error })}
|
||||
</Span>
|
||||
)}
|
||||
</Stack>
|
||||
</Modal>
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
@@ -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<StorageFolderPayload | null>(
|
||||
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<string>();
|
||||
|
||||
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}`;
|
||||
}
|
||||
@@ -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 (
|
||||
<Row className="w-full mt-6 gap-6! flex-wrap items-start lg:flex-nowrap">
|
||||
@@ -82,16 +84,14 @@ export default function FilesBrowser({
|
||||
<Stack className="grow min-w-0 w-full items-stretch">
|
||||
<Stack className="gap-1! items-stretch">
|
||||
<H2 className="text-[18px]! leading-6 font-bold tracking-normal text-ink">
|
||||
{folder?.breadcrumbs.at(-1)?.name ||
|
||||
folder?.label ||
|
||||
"Files"}
|
||||
{crumbs.at(-1)?.name || folder?.label || "Files"}
|
||||
</H2>
|
||||
<Span className="text-[13px] leading-[18px] text-ink-secondary">
|
||||
{folderSummary({ folder })}
|
||||
{folderSummary({ folder, loading })}
|
||||
</Span>
|
||||
</Stack>
|
||||
<FilesBreadcrumbRow
|
||||
crumbs={folder?.breadcrumbs || []}
|
||||
crumbs={crumbs}
|
||||
onOpenFolder={onOpenFolder}
|
||||
/>
|
||||
<FilesSearchInput value={search} onChange={onSearch} />
|
||||
|
||||
@@ -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";
|
||||
|
||||
/**
|
||||
|
||||
@@ -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";
|
||||
|
||||
/**
|
||||
|
||||
@@ -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", () => {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user