import { useRef } from "react"; import { useTranslation } from "react-i18next"; import { FeatherDatabase, FeatherTrash2 } from "@subframe/core"; import type { DemoDataSettingsVM } from "@hivegrid/view-models"; import ConfirmDialog, { type RefConfirmDialog } from "~/components/ui/modals/ConfirmDialog"; import { useLocaleFormatters } from "~/locale/format"; import { Alert } from "~/ui/components/Alert"; import { BadgeFeedback } from "~/ui/components/BadgeFeedback"; import { Button } from "~/ui/components/Button"; import { Card } from "~/ui/components/Card"; import { EmptyState } from "~/ui/components/EmptyState"; import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageIntro, PageShell } from "../shared/PageShell"; export type DemoDataSettingsActions = { readonly onCreate: () => void | Promise; readonly onDelete: (provisioningId: string) => void | Promise; readonly onRetry?: () => void | Promise; }; export type DemoDataSettingsPageProps = { readonly vm: DemoDataSettingsVM; readonly actions: DemoDataSettingsActions; readonly busy?: boolean; /** Inside the Workspace hub (AK-SET-01) the hub owns the header — see PageIntro. */ readonly embedded?: boolean; }; const TITLE_KEY = "onboarding.demoData.title"; const DESCRIPTION_KEY = "onboarding.demoData.description"; /** Page chrome shared by every VM state, so the headline bar does not flicker between loading and ready. */ function DemoDataShell({ titleKey = TITLE_KEY, descriptionKey = DESCRIPTION_KEY, headerActions, embedded = false, children, }: { titleKey?: string; descriptionKey?: string; headerActions?: React.ReactNode; /** Inside the Workspace hub (AK-SET-01) the hub owns the header. */ embedded?: boolean; children: React.ReactNode; }) { const { t } = useTranslation(); return ( {embedded ? null : ( } title={t(titleKey)} subtitle={t(descriptionKey)} actions={headerActions} /> )} {embedded ? ( // The header carried the destructive actions, so they have to be // re-hosted rather than disappear with it.
{t(descriptionKey)} {headerActions ?
{headerActions}
: null}
) : null} {children}
); } export function DemoDataSettingsPage({ vm, actions, busy = false, embedded = false }: DemoDataSettingsPageProps) { const { t } = useTranslation(); const formatters = useLocaleFormatters(); const confirmDialog = useRef(null); if (vm.status === "loading" || vm.status === "idle") { return ( ); } if (vm.status === "error") { return ( void actions.onRetry?.()}> {t("shared.retry")} ) : undefined } /> ); } if (vm.status === "empty" || !vm.data) { return ( ); } const data = vm.data; const confirmDelete = () => { if (!data.provisioningId) return; confirmDialog.current?.show({ title: t("onboarding.demoData.deleteDialog.title"), description: t("onboarding.demoData.deleteDialog.description", { count: data.totalCount }), confirmText: t("onboarding.demoData.delete"), cancelText: t("shared.cancel"), destructive: true, onConfirm: () => void actions.onDelete(data.provisioningId as string), }); }; return ( {t(data.statusLabelKey)}} > {!data.hasDemoData ? ( } onClick={() => void actions.onCreate()} data-ui-id="ui.onboarding.demoData.create" > {t("onboarding.demoData.create")} ) : undefined } /> ) : (
{t("onboarding.demoData.contents.title")} {t("onboarding.demoData.contents.summary", { count: data.totalCount })}
{data.entities.map((entity) => (
{t(entity.labelKey)} {formatters.number(entity.count)}
))}
{data.createdAt || data.removableUntil ? (
{data.createdAt ? ( {t("onboarding.demoData.createdAt", { date: formatters.dateTime(data.createdAt) })} ) : null} {data.removableUntil ? ( {t("onboarding.demoData.removableUntil", { date: formatters.date(data.removableUntil) })} ) : null}
) : null}
)}
); }