Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/routes/admin/system/deploys.tsx
T

217 lines
8.9 KiB
TypeScript

import { buildDeployAdminVM, type DeployAdminData, type DeploySignal, type MigrationState } from "@hivegrid/view-models";
import { FeatherRocket, FeatherTableProperties } from "@subframe/core";
import { LoaderFunctionArgs, MetaFunction, useLoaderData } from "react-router";
import { useTranslation } from "react-i18next";
import { AdminPageHeader } from "~/components/screens/admin/AdminPageHeader";
import { getTranslations } from "~/locale/i18next.server";
import { getDeployAdminSnapshot } from "~/services/admin/deploy-admin.server";
import { ADMIN_AREA_ACCESS } from "~/services/admin/admin-access-contract";
import { verifyUserHasPermission } from "~/utils/helpers/.server/PermissionsService";
import { BadgeFeedback } from "~/ui/components/BadgeFeedback";
import { Card } from "~/ui/components/Card";
import { EmptyState } from "~/ui/components/EmptyState";
import { StatsCard } from "~/ui/components/StatsCard";
import { Table } from "~/ui/components/Table";
type LoaderData = {
title: string;
vm: ReturnType<typeof buildDeployAdminVM>;
};
export const meta: MetaFunction<typeof loader> = ({ data }) => [{ title: data?.title }];
export const loader = async ({ request }: LoaderFunctionArgs) => {
await verifyUserHasPermission(request, ADMIN_AREA_ACCESS.deploys);
const { t } = await getTranslations(request);
const snapshot = await getDeployAdminSnapshot();
return {
title: `${t("admin.deploys.title")} | ${process.env.APP_NAME}`,
vm: buildDeployAdminVM(snapshot),
} satisfies LoaderData;
};
export default function AdminDeploysRoute() {
const { t } = useTranslation();
const data = useLoaderData<LoaderData>();
return (
<div className="flex w-full flex-col">
<AdminPageHeader title={t("admin.deploys.title")} description={t("admin.deploys.description")} />
<div className="mobile:px-4 flex w-full flex-col items-start gap-4 px-6 py-6">
{data.vm.status === "ready" ? <DeploysPage data={data.vm.data} /> : <DeploysEmpty />}
</div>
</div>
);
}
function DeploysPage({ data }: { data: DeployAdminData }) {
const { t } = useTranslation();
return (
<>
<Card variant="neutral">
<div className="flex w-full flex-wrap items-center justify-between gap-3">
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">
{t("admin.deploys.deployLine", {
environment: data.metadata.environment,
release: data.metadata.release,
branch: data.metadata.branch,
})}
</span>
<BadgeFeedback farbe={statusTone(data.overallStatus)}>{t(data.overallLabelKey)}</BadgeFeedback>
</div>
</Card>
<div className="mobile:grid-cols-1 grid w-full gap-4 lg:grid-cols-3">
{data.signals.map((signal) => (
<SignalCard key={signal.id} signal={signal} />
))}
</div>
<div className="mobile:grid-cols-1 grid w-full gap-4 xl:grid-cols-[minmax(0,0.9fr)_minmax(0,1.1fr)]">
<VersionCard data={data} />
<MigrationCard data={data} />
</div>
</>
);
}
function SignalCard({ signal }: { signal: DeploySignal }) {
const { t } = useTranslation();
return (
<StatsCard
label={t(signal.labelKey)}
value={signal.value}
trend={signal.status === "ok" ? "positive" : signal.status === "unknown" ? "neutral" : "negative"}
percentage=""
comparisonText={t(signal.detailKey)}
icon={<FeatherRocket />}
/>
);
}
function VersionCard({ data }: { data: DeployAdminData }) {
const { t } = useTranslation();
const rows = [
["admin.deploys.fields.environment", data.metadata.environment],
["admin.deploys.fields.nodeEnv", data.metadata.nodeEnv],
["admin.deploys.fields.release", data.metadata.release],
["admin.deploys.fields.revision", data.metadata.revision],
["admin.deploys.fields.branch", data.metadata.branch],
["admin.deploys.fields.image", data.metadata.image],
["admin.deploys.fields.buildTime", data.metadata.buildTime],
] as const;
return (
<Card>
<div className="flex w-full items-start gap-3">
<span className="text-heading- font-heading- text-icon-neutral-tertiary">
<FeatherRocket />
</span>
<div className="flex min-w-0 flex-col items-start">
<span className="text-body-large-0 font-body-large-0 text-text-neutral-primary">{t("admin.deploys.version.title")}</span>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">{t("admin.deploys.version.description")}</span>
</div>
</div>
{/*
`min-w-0` keeps the grid from growing past the card: without it a 40-char
commit hash pushed the value column wider than its container and ran over
the neighbouring cards (admin audit 2026-07-26, P0-07). Long hashes are
additionally shortened for reading, with the full value on hover.
*/}
<dl className="grid w-full min-w-0 grid-cols-[9rem_minmax(0,1fr)] gap-x-3 gap-y-2">
{rows.map(([labelKey, value]) => (
<div key={labelKey} className="contents">
<dt className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">{t(labelKey)}</dt>
<dd className="text-text-neutral-primary min-w-0 truncate font-mono text-xs" title={value}>
{shortenRevision(value)}
</dd>
</div>
))}
</dl>
</Card>
);
}
function MigrationCard({ data }: { data: DeployAdminData }) {
const { t } = useTranslation();
return (
<Card>
<div className="flex w-full items-start justify-between gap-4">
<div className="flex min-w-0 items-start gap-3">
<span className="text-heading- font-heading- text-icon-neutral-tertiary">
<FeatherTableProperties />
</span>
<div className="flex min-w-0 flex-col items-start">
<span className="text-body-large-0 font-body-large-0 text-text-neutral-primary">{t("admin.deploys.migrations.title")}</span>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">
{t("admin.deploys.migrations.description")}
</span>
</div>
</div>
<BadgeFeedback farbe={data.migrationSummary.pending > 0 || data.migrationSummary.failed > 0 ? "subtle-warning" : "subtle-positive"} size="small">
{t("admin.deploys.migrations.summary", data.migrationSummary)}
</BadgeFeedback>
</div>
<Table
header={
<Table.HeaderRow>
<Table.HeaderCell>{t("admin.deploys.migrations.name")}</Table.HeaderCell>
<Table.HeaderCell>{t("admin.deploys.migrations.state")}</Table.HeaderCell>
<Table.HeaderCell>{t("admin.deploys.migrations.appliedAt")}</Table.HeaderCell>
</Table.HeaderRow>
}
>
{data.migrations.map((migration) => (
<Table.Row key={migration.name}>
<Table.Cell>
<span className="text-text-neutral-primary max-w-80 truncate font-mono text-xs">{migration.name}</span>
</Table.Cell>
<Table.Cell>
<BadgeFeedback farbe={migrationTone(migration.state)} size="small">
{t(`admin.deploys.migrationState.${migration.state}`)}
</BadgeFeedback>
</Table.Cell>
<Table.Cell>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">
{migration.appliedAt ?? t("shared.notSet")}
</span>
</Table.Cell>
</Table.Row>
))}
</Table>
</Card>
);
}
function DeploysEmpty() {
const { t } = useTranslation();
return <EmptyState outlined title={t("admin.deploys.empty.headline")} body={t("admin.deploys.empty.body")} />;
}
const FULL_COMMIT_HASH = /^[0-9a-f]{13,}$/iu;
const SHORT_HASH_LENGTH = 12;
/**
* Shortens a full commit hash to its readable prefix. Anything that is not a
* long hex string (image tags, branch names, `local`) is left untouched, and the
* complete value stays available through the cell's `title`.
*/
export function shortenRevision(value: string): string {
return FULL_COMMIT_HASH.test(value) ? value.slice(0, SHORT_HASH_LENGTH) : value;
}
function statusTone(status: DeploySignal["status"]): "subtle-positive" | "subtle-warning" | "subtle-negative" | "subtle-neutral" {
if (status === "ok") return "subtle-positive";
if (status === "warning") return "subtle-warning";
if (status === "critical") return "subtle-negative";
return "subtle-neutral";
}
function migrationTone(state: MigrationState): "subtle-positive" | "subtle-warning" | "subtle-negative" | "subtle-neutral" {
if (state === "applied") return "subtle-positive";
if (state === "pending") return "subtle-warning";
if (state === "failed" || state === "rolledBack") return "subtle-negative";
return "subtle-neutral";
}