42 lines
2.3 KiB
TypeScript
42 lines
2.3 KiB
TypeScript
// PEP-224: a feature that depends on a failing service (Observability) — grey, inactive, with a
|
||
// plain caption and the time it failed. Available — its children as they are; unstable — the
|
||
// children with a note «может работать с задержками». For any new feature: pass `featureGateView`.
|
||
import type { ReactNode } from "react";
|
||
import { CircleSlash, Clock } from "lucide-react";
|
||
import { serverTimezone, type FeatureGateView } from "@peppa/core";
|
||
import { getLocale, getT } from "@/lib/i18n";
|
||
import { intlLocale } from "@peppa/shared/i18n";
|
||
import { cn } from "@/lib/utils";
|
||
|
||
export async function FeatureGate({ view, label, title, children, className }: { view: FeatureGateView; /** The grey button's text (a button-like control). */ label?: ReactNode; /** A grey status instead (an instruction, a section). */ title?: ReactNode; children: ReactNode; className?: string }) {
|
||
if (view.state === "available") return <>{children}</>;
|
||
const t = await getT();
|
||
if (view.state === "degraded")
|
||
return (
|
||
<>
|
||
{children}
|
||
<p className="m-0 flex items-center gap-1.5 text-xs text-warn" data-feature={view.name} data-feature-state="degraded">
|
||
<Clock className="size-3.5 shrink-0" /> {t.text(view.text)}
|
||
</p>
|
||
</>
|
||
);
|
||
const since = view.since ? new Date(view.since).toLocaleTimeString(intlLocale(await getLocale()), { hour: "2-digit", minute: "2-digit", timeZone: serverTimezone() }) : null;
|
||
return (
|
||
<div className={cn("flex w-full flex-col gap-1.5", className)} data-feature={view.name} data-feature-state="unavailable">
|
||
{label ? (
|
||
<button type="button" disabled aria-disabled="true" className="inline-flex h-10 w-full cursor-not-allowed items-center justify-center gap-2 rounded-md border border-dashed bg-muted px-4 text-sm font-medium text-muted-foreground opacity-80">
|
||
<CircleSlash className="size-4" /> {label}
|
||
</button>
|
||
) : (
|
||
<div role="status" aria-disabled="true" className="flex items-center gap-2 rounded-lg border border-dashed bg-muted px-4 py-3 text-sm font-medium text-muted-foreground">
|
||
<CircleSlash className="size-4 shrink-0" /> {title}
|
||
</div>
|
||
)}
|
||
<p className="m-0 text-xs text-muted-foreground">
|
||
{t.text(view.text)}
|
||
{since ? " · " + t("с {time}", { time: since }) : ""}
|
||
</p>
|
||
</div>
|
||
);
|
||
}
|