From 4620953ea4c126afbb3486ef27e4145abfb407b6 Mon Sep 17 00:00:00 2001 From: Lorenzo Corallo Date: Mon, 23 Nov 2026 21:03:40 +0100 Subject: [PATCH 1/3] feat(regulations): import from the repository in the background The import takes minutes, so "Importa" now closes the dialog at once and a loading toast follows the import until it ends, then turns into the summary (or the error, kept until dismissed). The running state lives in a module store, so the toast and the disabled button survive leaving the page, and the skipped documents stay until the next import. appToast gains `loading` and an `id`/`duration` option to update a toast. --- docs/design.md | 3 +- docs/regulations/README.md | 5 +-- src/components/shell/toast.ts | 20 ++++++----- src/components/ui/sonner.tsx | 3 +- src/features/regulations/documents-page.tsx | 32 +++++++++++++----- src/features/regulations/documents-sync.ts | 33 +++++++++++++++++++ src/features/regulations/regulations-hooks.ts | 10 +++--- src/styles.css | 4 +++ 8 files changed, 84 insertions(+), 26 deletions(-) create mode 100644 src/features/regulations/documents-sync.ts diff --git a/docs/design.md b/docs/design.md index c55b542d..6a726063 100644 --- a/docs/design.md +++ b/docs/design.md @@ -368,7 +368,8 @@ Grant date/time fields use the calendar and time wheels on touch devices too, wi **Alerts and toasts.** `InlineAlert` for what persists or needs action: load errors, dialog submit errors, partial saves. It defaults to `tone="danger"` and `role="alert"`, so pass another tone for notices that aren't errors. Toasts (`appToast.success/info/warning/error`, never raw `toast`) confirm actions; untinted, tone on the icon only, at most -three, never infinite, no "Undo". +three, no "Undo", never infinite but for a task that runs in the background for minutes: `appToast.loading` until it +ends, then its outcome in the same toast (`id`), an error kept until dismissed. **Accessibility.** Every input has a label; focus is never removed; dialogs trap focus and return it to the trigger; when a row disappears (optimistic removal, confirmed delete) `useFocusAfterRemoval` keeps focus in the list. Color is diff --git a/docs/regulations/README.md b/docs/regulations/README.md index 2d607d39..d98c7fcf 100644 --- a/docs/regulations/README.md +++ b/docs/regulations/README.md @@ -45,8 +45,9 @@ Every section but Bozze also needs `admin:access`, like the rest of the dashboar (repository: a pull request, the document listed "In creazione" with its link until it merges, then active; the list reloads every 15 s meanwhile; a closed one is "Non creato", with "Crea di nuovo") or private (opened at once); "Carica un PDF" makes a private read-only document. The key is a slug of the title until edited, checked like the backend. -- **Import.** "Importa dal repository" runs `documents.sync` and lists what it skipped, with the backend's reason, above - the sections until the page is left. +- **Import.** "Importa dal repository" runs `documents.sync` in the background (`documents-sync.ts`, it takes minutes): + the dialog closes at once, a loading toast becomes the outcome when it ends, even on another page, and the button + stays disabled meanwhile. What it skipped, with the backend's reason, shows above the sections until the next import. - **PDFs.** Every PDF opens in `PdfDialog` (versions, the latest of a sola consultazione document, uploads, the editor's preview "Apri a tutta altezza", the differences), with Download in its toolbar. - Pages act on the document's `editable`, the draft's `can` and the IdP permissions, never on a kind alone. diff --git a/src/components/shell/toast.ts b/src/components/shell/toast.ts index 47c25f64..465e36ab 100644 --- a/src/components/shell/toast.ts +++ b/src/components/shell/toast.ts @@ -1,13 +1,17 @@ -import { toast } from "sonner" +import { type ExternalToast, toast } from "sonner" + +/** `id` turns an earlier toast (a `loading` one) into this one; `duration: Infinity` keeps it until dismissed. */ +type AppToastOptions = Pick /** - * Toasts with these durations: success/info 4s, warning 6s, error 8s. They go to the one - * `Toaster` mounted in the root document. Copy follows the toast formula in docs/design.md: "Group deleted.", - * "Couldn't delete the group." + * Toasts with these durations: success/info 4s, warning 6s, error 8s; loading stays until its `id` gets another + * toast. They go to the one `Toaster` mounted in the root document. Copy follows the toast formula in docs/design.md: + * "Group deleted.", "Couldn't delete the group." */ export const appToast = { - success: (message: string) => toast.success(message, { duration: 4000 }), - info: (message: string) => toast.info(message, { duration: 4000 }), - warning: (message: string) => toast.warning(message, { duration: 6000 }), - error: (message: string) => toast.error(message, { duration: 8000 }), + success: (message: string, options?: AppToastOptions) => toast.success(message, { duration: 4000, ...options }), + info: (message: string, options?: AppToastOptions) => toast.info(message, { duration: 4000, ...options }), + warning: (message: string, options?: AppToastOptions) => toast.warning(message, { duration: 6000, ...options }), + error: (message: string, options?: AppToastOptions) => toast.error(message, { duration: 8000, ...options }), + loading: (message: string) => toast.loading(message), } diff --git a/src/components/ui/sonner.tsx b/src/components/ui/sonner.tsx index 12feaca7..b60e2087 100644 --- a/src/components/ui/sonner.tsx +++ b/src/components/ui/sonner.tsx @@ -1,4 +1,4 @@ -import { CircleCheck, CircleX, Info, TriangleAlert } from "lucide-react" +import { CircleCheck, CircleX, Info, LoaderCircle, TriangleAlert } from "lucide-react" import { useEffect, useState } from "react" import { Toaster as Sonner, type ToasterProps } from "sonner" @@ -7,6 +7,7 @@ const icons = { info: , warning: , error: , + loading: , } /** The app's one Toaster: bottom-right, three visible, untinted `app-toast` surfaces. */ diff --git a/src/features/regulations/documents-page.tsx b/src/features/regulations/documents-page.tsx index 81ad2af2..ed9410e8 100644 --- a/src/features/regulations/documents-page.tsx +++ b/src/features/regulations/documents-page.tsx @@ -21,13 +21,15 @@ import { IconButton, InlineAlert, SectionHeading, + Spinner, StatusBadge, useOpenGeneration, } from "@/components/primitives" -import { PageBar, PageContent, useCan } from "@/components/shell" +import { appToast, PageBar, PageContent, useCan } from "@/components/shell" import { Button } from "@/components/ui/button" import { cn } from "@/lib/utils" +import { setDocumentsSync, useDocumentsSync } from "./documents-sync" import { NewDocumentDialog } from "./new-document-dialog" import { DOCUMENT_KIND_LABELS, formatDay } from "./regulations-format" import { failureMessage, useRefreshAfter } from "./regulations-hooks" @@ -64,7 +66,7 @@ export function DocumentsPage({ documents }: { documents: DocumentInfo[] }) { const create = useServerFn(createRegulationDocument) const refresh = useRefreshAfter() const [syncOpen, setSyncOpen] = useState(false) - const [syncResult, setSyncResult] = useState(null) + const { running: syncing, result: syncResult } = useDocumentsSync() const [newOpen, setNewOpen] = useState(false) const generation = useOpenGeneration(newOpen) const [retrying, setRetrying] = useState(null) @@ -91,15 +93,23 @@ export function DocumentsPage({ documents }: { documents: DocumentInfo[] }) { ), })).filter((group) => group.documents.length > 0) + /** Runs in the background: the dialog closes at once and one toast follows the import until it ends. */ async function runSync() { + setDocumentsSync({ running: true, result: null }) + const toastId = appToast.loading("Importazione dei documenti in corso…") let result: SyncResult try { result = await sync() } catch (caught) { console.error(caught) - throw new Error(failureMessage(caught, "Non è stato possibile importare i documenti."), { cause: caught }) + setDocumentsSync({ running: false }) + appToast.error(failureMessage(caught, "Non è stato possibile importare i documenti."), { + id: toastId, + duration: Infinity, + }) + return } - setSyncResult(result) + setDocumentsSync({ result }) const changes = [ result.imported.length ? plural(result.imported.length, "documento nuovo", "documenti nuovi") : null, result.updated.length ? plural(result.updated.length, "aggiornato", "aggiornati") : null, @@ -107,8 +117,10 @@ export function DocumentsPage({ documents }: { documents: DocumentInfo[] }) { await refresh( changes.length ? `Importazione completata: ${changes.join(", ")}.` - : "Importazione completata: nessun documento nuovo o cambiato." + : "Importazione completata: nessun documento nuovo o cambiato.", + toastId ) + setDocumentsSync({ running: false }) } async function runRetry() { @@ -131,8 +143,8 @@ export function DocumentsPage({ documents }: { documents: DocumentInfo[] }) { right={ canManage ? ( <> -