372 lines
14 KiB
TypeScript
372 lines
14 KiB
TypeScript
// R-P11b (P11 dashboard, 3 fixed role layouts) Interim-UI (AD-28) — composed
|
|
// from ~/ui (D-DASH archetype "KPI + Widget-Compose"). Pure: renders the
|
|
// permission-gated widget grid of the one layout the server derived from the
|
|
// member's seat. The route owns the SSE stream and feeds the latest snapshot in
|
|
// as the VM, so the page has no live-state controls of its own. No
|
|
// useLoaderData. Final Subframe optics = tracked design-nachzug (D-DASH).
|
|
|
|
import { lazy, Suspense, type ReactNode } from "react";
|
|
import { useTranslation } from "react-i18next";
|
|
import {
|
|
FeatherActivity,
|
|
FeatherCalendar,
|
|
FeatherCheckSquare,
|
|
FeatherCreditCard,
|
|
FeatherFolder,
|
|
FeatherHome,
|
|
FeatherLayoutDashboard,
|
|
FeatherPhoneCall,
|
|
FeatherTrendingUp,
|
|
FeatherUsers,
|
|
} from "@subframe/core";
|
|
import type { DashboardPageVM, DashboardWidgetDomain, DashboardWidgetItemVM, DashboardWidgetVM } from "@hivegrid/view-models";
|
|
import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageShell } from "../shared/PageShell";
|
|
import { Alert } from "~/ui/components/Alert";
|
|
import { Card } from "~/ui/components/Card";
|
|
import { EmptyState } from "~/ui/components/EmptyState";
|
|
import { Loader } from "~/ui/components/Loader";
|
|
import { StatsCard } from "~/ui/components/StatsCard";
|
|
import { APP_TIME_ZONE, createLocaleFormatters } from "~/locale/format";
|
|
import { formatMonthLabel } from "./dashboardFormatters";
|
|
import { AppLink } from "~/components/app/AppLink";
|
|
|
|
const DashboardStageDistributionChart = lazy(() => import("./DashboardStageDistributionChart"));
|
|
const DashboardMonthlySeriesChart = lazy(() => import("./DashboardMonthlySeriesChart"));
|
|
|
|
export type DashboardPageProps = {
|
|
vm: DashboardPageVM;
|
|
};
|
|
|
|
const sectionHeading = "text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary";
|
|
const mutedText = "text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-secondary";
|
|
const metricValue = "text-subtitle-0 font-subtitle-0 text-text-neutral-primary";
|
|
|
|
const domainIcons: Record<DashboardWidgetDomain, ReactNode> = {
|
|
general: <FeatherHome />,
|
|
sales: <FeatherTrendingUp />,
|
|
finance: <FeatherCreditCard />,
|
|
projects: <FeatherFolder />,
|
|
tasks: <FeatherCheckSquare />,
|
|
hr: <FeatherUsers />,
|
|
activity: <FeatherActivity />,
|
|
comms: <FeatherPhoneCall />,
|
|
schedule: <FeatherCalendar />,
|
|
};
|
|
|
|
/** Same currency/count rendering as the chart, for the screen-reader table. */
|
|
function formatSeriesValue(value: number, format: "currency" | "count", language: string): string {
|
|
const formatters = createLocaleFormatters(language);
|
|
return format === "currency"
|
|
? formatters.currency(value, "EUR", { maximumFractionDigits: 0 })
|
|
: formatters.number(value, { maximumFractionDigits: 0 });
|
|
}
|
|
|
|
function formatDateTime(value: string | null, language: string): string {
|
|
if (!value) return "—";
|
|
return new Intl.DateTimeFormat(language, { timeZone: APP_TIME_ZONE, dateStyle: "medium", timeStyle: "short" }).format(new Date(value));
|
|
}
|
|
|
|
export function DashboardPage(props: DashboardPageProps) {
|
|
const { t, i18n } = useTranslation();
|
|
|
|
const header = (
|
|
<PageHeader icon={<FeatherLayoutDashboard className={PAGE_HEADER_ICON_CLASS} />} title={t("app.dashboard.title")} />
|
|
);
|
|
|
|
if (props.vm.status === "loading" || props.vm.status === "idle") {
|
|
return (
|
|
<PageShell uiId="ui.dashboard.page.loading">
|
|
{header}
|
|
<PageBody className="items-center gap-4">
|
|
<Loader size="medium" />
|
|
<span className={mutedText}>{t("app.dashboard.loading")}</span>
|
|
</PageBody>
|
|
</PageShell>
|
|
);
|
|
}
|
|
if (props.vm.status === "error") {
|
|
return (
|
|
<PageShell uiId="ui.dashboard.page.error">
|
|
{header}
|
|
<PageBody>
|
|
<Alert variant="error" title={t(props.vm.error.messageKey)} />
|
|
</PageBody>
|
|
</PageShell>
|
|
);
|
|
}
|
|
if (props.vm.status === "empty") {
|
|
return (
|
|
<PageShell uiId="ui.dashboard.page.empty">
|
|
{header}
|
|
<PageBody>
|
|
<EmptyState outlined title={t("app.dashboard.title")} body={t("app.dashboard.widgets.empty")} />
|
|
</PageBody>
|
|
</PageShell>
|
|
);
|
|
}
|
|
|
|
const data = props.vm.data;
|
|
const activeRole = data.layout;
|
|
|
|
return (
|
|
<PageShell uiId="ui.dashboard.roleLayouts">
|
|
<PageHeader
|
|
icon={<FeatherLayoutDashboard className={PAGE_HEADER_ICON_CLASS} />}
|
|
title={t("app.dashboard.title")}
|
|
subtitle={t(activeRole.descriptionKey)}
|
|
/>
|
|
<PageBody>
|
|
<div className="grid grid-cols-1 gap-4 md:grid-cols-2 lg:grid-flow-row-dense lg:grid-cols-12">
|
|
{activeRole.widgets.map((widget) => (
|
|
<div key={widget.key} className={dashboardWidgetGridClass(widget)}>
|
|
<DashboardWidgetCard widget={widget} language={i18n.language} />
|
|
</div>
|
|
))}
|
|
</div>
|
|
</PageBody>
|
|
</PageShell>
|
|
);
|
|
}
|
|
|
|
function DashboardWidgetCard({ widget, language }: { widget: DashboardWidgetVM; language: string }) {
|
|
const { t } = useTranslation();
|
|
|
|
if (!widget.visible) {
|
|
return (
|
|
<Card className="h-full" data-ui-id={`ui.dashboard.widget.${widget.key}`}>
|
|
<span className={sectionHeading}>{t(widget.titleKey)}</span>
|
|
<span className={mutedText}>{t("app.dashboard.widgets.permissionHidden")}</span>
|
|
</Card>
|
|
);
|
|
}
|
|
|
|
if (widget.series) {
|
|
const series = widget.series;
|
|
const descriptionId = `dashboard-series-${widget.key.replace(/[^a-z0-9]+/gi, "-")}-description`;
|
|
return (
|
|
<Card className="h-full" data-ui-id={`ui.dashboard.widget.${widget.key}`}>
|
|
<div className="flex w-full items-start justify-between gap-2">
|
|
<div className="flex flex-col gap-1">
|
|
<span className={sectionHeading}>{t(widget.titleKey)}</span>
|
|
<span id={descriptionId} className={mutedText}>
|
|
{t(widget.descriptionKey)}
|
|
</span>
|
|
</div>
|
|
<SubframeIcon domain={widget.domain} />
|
|
</div>
|
|
{widget.metrics.length > 0 ? (
|
|
<div className="grid w-full grid-cols-2 gap-4">
|
|
{widget.metrics.map((metric) => (
|
|
<div key={metric.labelKey} className="flex flex-col gap-1">
|
|
<span className={mutedText}>{t(metric.labelKey)}</span>
|
|
<span className={metricValue}>{metric.value}</span>
|
|
</div>
|
|
))}
|
|
</div>
|
|
) : null}
|
|
<Suspense
|
|
fallback={
|
|
<div className="flex h-64 items-center justify-center">
|
|
<Loader size="medium" />
|
|
</div>
|
|
}
|
|
>
|
|
<DashboardMonthlySeriesChart points={series.points} valueFormat={series.valueFormat} hasComparison={series.hasComparison} />
|
|
</Suspense>
|
|
<div className="sr-only">
|
|
<table aria-describedby={descriptionId}>
|
|
<caption>{t(widget.titleKey)}</caption>
|
|
<thead>
|
|
<tr>
|
|
<th scope="col">{t("app.dashboard.series.monthColumn")}</th>
|
|
<th scope="col">{t("app.dashboard.series.currentPeriod")}</th>
|
|
{series.hasComparison ? <th scope="col">{t("app.dashboard.series.previousYear")}</th> : null}
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{series.points.map((point) => (
|
|
<tr key={point.id}>
|
|
<th scope="row">{formatMonthLabel(point.id, language)}</th>
|
|
<td>{formatSeriesValue(point.value, series.valueFormat, language)}</td>
|
|
{series.hasComparison ? (
|
|
<td>{point.comparisonValue === undefined ? "—" : formatSeriesValue(point.comparisonValue, series.valueFormat, language)}</td>
|
|
) : null}
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</Card>
|
|
);
|
|
}
|
|
|
|
if (widget.kind === "chart") {
|
|
const points = widget.chart?.points ?? [];
|
|
const resolvedPoints = points.map((point) => ({
|
|
...point,
|
|
label: point.labelKey ? t(point.labelKey) : point.label ?? "—",
|
|
}));
|
|
const descriptionId = `dashboard-chart-${widget.key.replace(/[^a-z0-9]+/gi, "-")}-description`;
|
|
return (
|
|
<Card className="h-full min-h-[22rem]" data-ui-id={`ui.dashboard.widget.${widget.key}`}>
|
|
<div className="flex w-full items-start justify-between gap-2">
|
|
<div className="flex flex-col gap-1">
|
|
<span className={sectionHeading}>{t(widget.titleKey)}</span>
|
|
<span id={descriptionId} className={mutedText}>{t(widget.descriptionKey)}</span>
|
|
</div>
|
|
<SubframeIcon domain={widget.domain} />
|
|
</div>
|
|
{points.length > 0 ? (
|
|
<>
|
|
<Suspense fallback={<div className="flex min-h-60 items-center justify-center"><Loader size="medium" /></div>}>
|
|
<DashboardStageDistributionChart points={resolvedPoints} valueFormat={widget.chart?.valueFormat ?? "count"} />
|
|
</Suspense>
|
|
<div className="sr-only">
|
|
<p>{t("app.dashboard.widgets.stageDistribution.a11ySummary", { count: points.reduce((sum, point) => sum + point.value, 0) })}</p>
|
|
<table aria-describedby={descriptionId}>
|
|
<caption>{t(widget.titleKey)}</caption>
|
|
<thead>
|
|
<tr>
|
|
<th scope="col">{t("app.dashboard.widgets.stageDistribution.stageColumn")}</th>
|
|
<th scope="col">{t("app.dashboard.widgets.stageDistribution.projectCountColumn")}</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{resolvedPoints.map((point) => (
|
|
<tr key={point.id}>
|
|
<th scope="row">{point.label}</th>
|
|
<td>{formatSeriesValue(point.value, widget.chart?.valueFormat ?? "count", language)}</td>
|
|
</tr>
|
|
))}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</>
|
|
) : (
|
|
<span className={mutedText}>{t("app.dashboard.widgets.stageDistribution.empty")}</span>
|
|
)}
|
|
</Card>
|
|
);
|
|
}
|
|
|
|
if (widget.kind === "hero") {
|
|
return (
|
|
<Card className="h-full" data-ui-id={`ui.dashboard.widget.${widget.key}`}>
|
|
<span className={sectionHeading}>{t(widget.titleKey)}</span>
|
|
<span className={mutedText}>{t(widget.descriptionKey)}</span>
|
|
{widget.metrics[0] ? (
|
|
<span className={mutedText}>
|
|
{t(widget.metrics[0].labelKey)}: {formatDateTime(widget.metrics[0].value, language)}
|
|
</span>
|
|
) : null}
|
|
</Card>
|
|
);
|
|
}
|
|
|
|
if (widget.metrics.length === 1 && widget.items.length === 0) {
|
|
const card = (
|
|
<StatsCard
|
|
trend="neutral"
|
|
label={t(widget.titleKey)}
|
|
value={widget.metrics[0].value}
|
|
comparisonText={t(widget.metrics[0].labelKey)}
|
|
icon={domainIcons[widget.domain]}
|
|
className="h-full"
|
|
data-ui-id={`ui.dashboard.widget.${widget.key}`}
|
|
/>
|
|
);
|
|
return widget.href ? (
|
|
<AppLink href={widget.href} className="block h-full w-full" data-ui-id={`ui.dashboard.widget.${widget.key}.link`}>
|
|
{card}
|
|
</AppLink>
|
|
) : (
|
|
card
|
|
);
|
|
}
|
|
|
|
return (
|
|
<Card className="h-full" data-ui-id={`ui.dashboard.widget.${widget.key}`}>
|
|
<div className="flex w-full items-center justify-between gap-2">
|
|
<span className={sectionHeading}>{t(widget.titleKey)}</span>
|
|
<SubframeIcon domain={widget.domain} />
|
|
</div>
|
|
<span className={mutedText}>{t(widget.descriptionKey)}</span>
|
|
{widget.metrics.length > 0 ? (
|
|
<div className="grid w-full grid-cols-2 gap-4">
|
|
{widget.metrics.map((metric) => (
|
|
<div key={metric.labelKey} className="flex flex-col gap-1">
|
|
<span className={mutedText}>{t(metric.labelKey)}</span>
|
|
<span className={metricValue}>{metric.value}</span>
|
|
</div>
|
|
))}
|
|
</div>
|
|
) : null}
|
|
{widget.kind === "list" ? (
|
|
widget.items.length > 0 ? (
|
|
<div className="flex w-full flex-col gap-1">
|
|
{widget.items.map((item) => (
|
|
<DashboardWidgetItemRow key={item.id} item={item} />
|
|
))}
|
|
</div>
|
|
) : (
|
|
<span className={mutedText}>{t("app.dashboard.widgets.empty")}</span>
|
|
)
|
|
) : null}
|
|
</Card>
|
|
);
|
|
}
|
|
|
|
function dashboardWidgetGridClass(widget: DashboardWidgetVM): string {
|
|
const widthClass =
|
|
widget.size.w <= 3
|
|
? "lg:col-span-3"
|
|
: widget.size.w <= 4
|
|
? "lg:col-span-4"
|
|
: widget.size.w <= 6
|
|
? "lg:col-span-6"
|
|
: widget.size.w <= 8
|
|
? "lg:col-span-8"
|
|
: "lg:col-span-12";
|
|
const startClass =
|
|
widget.position.x === 0
|
|
? "lg:col-start-1"
|
|
: widget.position.x === 3
|
|
? "lg:col-start-4"
|
|
: widget.position.x === 4
|
|
? "lg:col-start-5"
|
|
: widget.position.x === 6
|
|
? "lg:col-start-7"
|
|
: widget.position.x === 8
|
|
? "lg:col-start-9"
|
|
: "";
|
|
return `min-w-0 ${widthClass} ${startClass}`;
|
|
}
|
|
|
|
function SubframeIcon({ domain }: { domain: DashboardWidgetDomain }) {
|
|
return <span className="text-icon-neutral-tertiary">{domainIcons[domain]}</span>;
|
|
}
|
|
|
|
function DashboardWidgetItemRow({ item }: { item: DashboardWidgetItemVM }) {
|
|
const { t, i18n } = useTranslation();
|
|
const label = item.labelKey ? t(item.labelKey) : item.label;
|
|
const tooltip = item.tooltip ?? label;
|
|
const meta = item.metaDateIso ? formatDateTime(item.metaDateIso, i18n.language) : item.meta;
|
|
|
|
return (
|
|
<div className="flex w-full min-w-0 items-center justify-between gap-3 overflow-hidden">
|
|
{item.href ? (
|
|
<AppLink href={item.href} title={tooltip} className="text-body-default-medium-0 font-body-default-medium-0 text-brand-brand-text-default min-w-0 truncate hover:underline">
|
|
{label}
|
|
</AppLink>
|
|
) : (
|
|
<span className={`${mutedText} min-w-0 truncate`} title={tooltip}>
|
|
{label}
|
|
</span>
|
|
)}
|
|
{meta ? <span className={`${mutedText} max-w-[50%] shrink-0 truncate whitespace-nowrap`}>{meta}</span> : null}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export default DashboardPage;
|