Files
HealthLog/src/lib/queries/use-documents-layout.ts
T
Marc-André Bombeck 4273af231e feat(documents): choose how the vault is laid out, and confirm every delete
The wrench beside Upload opens Settings, Appearance, Documents, the way
the illness journal offers its display options: preview cards or a
compact list, and months stacked as blocks or flowing across the full
width with the month name inline above the document that opens it. The
choice is stored per account in `users.documents_layout_json` behind
GET/PUT /api/documents/inbound/layout (preserve-when-absent, module
gated, the read delegable like the rest of the vault), carried in the
account-settings backup and cleared by the data wipe.

Deleting from the vault now always asks first. The bulk bar's Delete and
a focused card's Delete key both go through one confirmation on the page,
and the detail sheet's Delete opens its own; before, all three deleted on
the spot with only an undo toast behind them.

The selection bar is two tight lines: the count with an X at its right
edge, then one action row with Delete alone at the right in the
destructive style. Below 1024 px the verbs are icon-only at the 44 px tap
floor, so the row stays one line at 360 px. The bar carries the
selection-bar slot the Coach launcher already steps aside for.
2026-10-08 16:45:06 +02:00

86 lines
2.5 KiB
TypeScript

"use client";
import {
useQuery,
useQueryClient,
type QueryClient,
} from "@tanstack/react-query";
import { toast } from "sonner";
import { apiGet, apiPut } from "@/lib/api/api-fetch";
import {
DEFAULT_DOCUMENTS_LAYOUT,
type DocumentsLayout,
type DocumentsLayoutArrangement,
type DocumentsLayoutView,
} from "@/lib/documents/documents-layout";
import { useTranslations } from "@/lib/i18n/context";
import { queryKeys } from "@/lib/query-keys";
type Translator = (
key: string,
params?: Record<string, string | number>,
) => string;
type LayoutPatch = {
view?: DocumentsLayoutView;
arrangement?: DocumentsLayoutArrangement;
};
/**
* Apply one field optimistically (the switch has to feel instant), then
* persist it. The PUT is field-scoped, so a view change never resets the
* arrangement and vice versa. On failure the cache rolls back and a calm
* toast says so.
*/
export async function runSetDocumentsLayout(deps: {
patch: LayoutPatch;
queryClient: QueryClient;
t: Translator;
}): Promise<void> {
const { patch, queryClient, t } = deps;
const key = queryKeys.documentsLayout();
const previous = queryClient.getQueryData<DocumentsLayout>(key);
queryClient.setQueryData<DocumentsLayout>(key, {
...(previous ?? DEFAULT_DOCUMENTS_LAYOUT),
...patch,
});
try {
const saved = await apiPut<DocumentsLayout>(
"/api/documents/inbound/layout",
{ version: 1, ...patch },
);
if (saved) queryClient.setQueryData(key, saved);
} catch {
queryClient.setQueryData(key, previous);
toast.error(t("documents.layout.saveFailed"));
}
}
/** The vault presentation, persisted per user. Defaults until the read lands. */
export function useDocumentsLayout(enabled: boolean = true): {
layout: DocumentsLayout;
isLayoutLoading: boolean;
setView: (view: DocumentsLayoutView) => Promise<void>;
setArrangement: (arrangement: DocumentsLayoutArrangement) => Promise<void>;
} {
const queryClient = useQueryClient();
const { t } = useTranslations();
const { data, isLoading } = useQuery({
queryKey: queryKeys.documentsLayout(),
queryFn: () => apiGet<DocumentsLayout>("/api/documents/inbound/layout"),
staleTime: 5 * 60 * 1000,
enabled,
});
return {
layout: data ?? DEFAULT_DOCUMENTS_LAYOUT,
isLayoutLoading: enabled && isLoading,
setView: (view) =>
runSetDocumentsLayout({ patch: { view }, queryClient, t }),
setArrangement: (arrangement) =>
runSetDocumentsLayout({ patch: { arrangement }, queryClient, t }),
};
}