215 lines
8.1 KiB
TypeScript
215 lines
8.1 KiB
TypeScript
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<void>;
|
|
readonly onDelete: (provisioningId: string) => void | Promise<void>;
|
|
readonly onRetry?: () => void | Promise<void>;
|
|
};
|
|
|
|
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 (
|
|
<PageShell uiId="ui.onboarding.demoData.page">
|
|
{embedded ? null : (
|
|
<PageHeader
|
|
icon={<FeatherDatabase className={PAGE_HEADER_ICON_CLASS} />}
|
|
title={t(titleKey)}
|
|
subtitle={t(descriptionKey)}
|
|
actions={headerActions}
|
|
/>
|
|
)}
|
|
<PageBody>
|
|
{embedded ? (
|
|
// The header carried the destructive actions, so they have to be
|
|
// re-hosted rather than disappear with it.
|
|
<div className="mobile:flex-col mobile:items-start flex w-full items-start justify-between gap-4">
|
|
<PageIntro uiId="ui.onboarding.demoData.intro">{t(descriptionKey)}</PageIntro>
|
|
{headerActions ? <div className="flex items-center gap-2">{headerActions}</div> : null}
|
|
</div>
|
|
) : null}
|
|
{children}
|
|
</PageBody>
|
|
</PageShell>
|
|
);
|
|
}
|
|
|
|
export function DemoDataSettingsPage({ vm, actions, busy = false, embedded = false }: DemoDataSettingsPageProps) {
|
|
const { t } = useTranslation();
|
|
const formatters = useLocaleFormatters();
|
|
const confirmDialog = useRef<RefConfirmDialog>(null);
|
|
|
|
if (vm.status === "loading" || vm.status === "idle") {
|
|
return (
|
|
<DemoDataShell embedded={embedded}>
|
|
<Alert variant="neutral" title={t("onboarding.demoData.loading.title")} description={t("onboarding.demoData.loading.body")} />
|
|
</DemoDataShell>
|
|
);
|
|
}
|
|
|
|
if (vm.status === "error") {
|
|
return (
|
|
<DemoDataShell embedded={embedded}>
|
|
<EmptyState
|
|
outlined
|
|
variant="error"
|
|
title={t("onboarding.demoData.error.title")}
|
|
body={t(vm.error.messageKey)}
|
|
action={
|
|
actions.onRetry ? (
|
|
<Button variant="tertiary" onClick={() => void actions.onRetry?.()}>
|
|
{t("shared.retry")}
|
|
</Button>
|
|
) : undefined
|
|
}
|
|
/>
|
|
</DemoDataShell>
|
|
);
|
|
}
|
|
|
|
if (vm.status === "empty" || !vm.data) {
|
|
return (
|
|
<DemoDataShell embedded={embedded}>
|
|
<EmptyState outlined title={t("onboarding.demoData.unavailable.title")} body={t("onboarding.demoData.unavailable.body")} />
|
|
</DemoDataShell>
|
|
);
|
|
}
|
|
|
|
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 (
|
|
<DemoDataShell
|
|
embedded={embedded}
|
|
titleKey={data.titleKey}
|
|
descriptionKey={data.descriptionKey}
|
|
headerActions={<BadgeFeedback farbe={data.hasDemoData ? "subtle-positive" : "subtle-neutral"}>{t(data.statusLabelKey)}</BadgeFeedback>}
|
|
>
|
|
<Alert
|
|
variant={data.canCreate ? "information" : "neutral"}
|
|
notDismissible
|
|
title={t(data.environmentNoticeKey)}
|
|
description={t(data.hasDemoData ? "onboarding.demoData.environment.cleanupHint" : "onboarding.demoData.environment.createHint")}
|
|
/>
|
|
|
|
{!data.hasDemoData ? (
|
|
<EmptyState
|
|
outlined
|
|
title={t("onboarding.demoData.empty.title")}
|
|
body={t("onboarding.demoData.empty.body")}
|
|
action={
|
|
data.canCreate ? (
|
|
<Button
|
|
loading={busy}
|
|
disabled={busy}
|
|
icon={<FeatherDatabase />}
|
|
onClick={() => void actions.onCreate()}
|
|
data-ui-id="ui.onboarding.demoData.create"
|
|
>
|
|
{t("onboarding.demoData.create")}
|
|
</Button>
|
|
) : undefined
|
|
}
|
|
/>
|
|
) : (
|
|
<Card data-ui-id="ui.onboarding.demoData.summary">
|
|
<div className="flex w-full flex-wrap items-start justify-between gap-4">
|
|
<div className="flex flex-col gap-1">
|
|
<span className="text-body-large-0 font-body-large-0 text-text-neutral-primary">{t("onboarding.demoData.contents.title")}</span>
|
|
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">
|
|
{t("onboarding.demoData.contents.summary", { count: data.totalCount })}
|
|
</span>
|
|
</div>
|
|
<Button
|
|
variant="destructive"
|
|
loading={busy}
|
|
disabled={busy || !data.canDelete}
|
|
icon={<FeatherTrash2 />}
|
|
onClick={confirmDelete}
|
|
data-ui-id="ui.onboarding.demoData.delete"
|
|
data-testid="demo-delete-trigger"
|
|
>
|
|
{t("onboarding.demoData.delete")}
|
|
</Button>
|
|
</div>
|
|
|
|
<div className="mobile:grid-cols-1 grid w-full grid-cols-3 gap-3">
|
|
{data.entities.map((entity) => (
|
|
<div
|
|
key={entity.kind}
|
|
className="border-border-default bg-surface-neutral-primary flex items-center justify-between gap-3 rounded-md border border-solid px-4 py-3"
|
|
>
|
|
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">{t(entity.labelKey)}</span>
|
|
<span className="text-body-large-0 font-body-large-0 text-text-neutral-primary">{formatters.number(entity.count)}</span>
|
|
</div>
|
|
))}
|
|
</div>
|
|
|
|
{data.createdAt || data.removableUntil ? (
|
|
<div className="border-border-default flex w-full flex-wrap gap-x-6 gap-y-1 border-t border-solid pt-4">
|
|
{data.createdAt ? (
|
|
<span className="text-body-extra-small-regular-0 font-body-extra-small-regular-0 text-text-neutral-tertiary">
|
|
{t("onboarding.demoData.createdAt", { date: formatters.dateTime(data.createdAt) })}
|
|
</span>
|
|
) : null}
|
|
{data.removableUntil ? (
|
|
<span className="text-body-extra-small-regular-0 font-body-extra-small-regular-0 text-text-neutral-tertiary">
|
|
{t("onboarding.demoData.removableUntil", { date: formatters.date(data.removableUntil) })}
|
|
</span>
|
|
) : null}
|
|
</div>
|
|
) : null}
|
|
</Card>
|
|
)}
|
|
|
|
<ConfirmDialog ref={confirmDialog} destructive />
|
|
</DemoDataShell>
|
|
);
|
|
}
|