/** * Site › Settings › Design rules (RFC-0021 §5): the site's DESIGN.md, the Markdown every agent and * Build chat reads before building, next to the theme's own rules (read-only). Saved to the site * (snapshotted with it); `design_lint` warnings show after loading and saving. Agents propose * changes with `set_design`, which waits for a person in Approvals. */ import { formatNumber } from "@cloudhal/messages/format"; import { Banner, Button, Collapsible, InputArea, SkeletonLine, Text } from "@cloudhal/ui"; import { WarningCircle } from "@phosphor-icons/react"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import { useEffect, useState } from "react"; import { useI18n } from "../i18n/index.tsx"; import { ApiRequestError, api } from "../lib/api.ts"; import { apiErrorText } from "../lib/api-errors.ts"; import { SettingsSection } from "./Settings.tsx"; import { useToast } from "./Toast.ts"; import { useGate } from "./useMe.ts"; export const DESIGN_RULES_SECTION = "design"; /** The contract's limit (`Site.design.markdown`). */ const MAX = 24_000; /** The recommended headings (RFC-0021 §1), for an empty start. */ const TEMPLATE = [ "## Brand", "", "## Colors", "", "## Typography", "", "## Layout", "", "## Components", "", "## Imagery", "", "## Pages", "", "## Do", "", "## Don't", "", ].join("\n"); const isConflict = (e: unknown) => e instanceof ApiRequestError && e.status === 409; export function DesignRulesSettingsSection({ siteId, theme, }: { siteId: string; /** The site's theme name, for the wording. */ theme: string; }) { const { t, locale } = useI18n(); const qc = useQueryClient(); const toast = useToast(); const gate = useGate("settings.write"); const key = ["design-rules", siteId] as const; const rules = useQuery({ queryKey: key, queryFn: () => api.design.get(siteId) }); const saved = rules.data; const [text, setText] = useState(); const [warnings, setWarnings] = useState([]); useEffect(() => { if (saved) { setText(saved.site ?? ""); setWarnings(saved.warnings); } }, [saved]); const save = useMutation({ mutationFn: (markdown: string) => api.design.save(siteId, { rev: saved?.rev ?? 0, markdown: markdown.trim() || null }), onSuccess: async (result) => { setWarnings(result.warnings); toast.success(t("siteSettings.design.saved")); await qc.invalidateQueries({ queryKey: key }); await qc.invalidateQueries({ queryKey: ["site", siteId] }); }, }); const title = t("siteSettings.sections.design"); if (!saved || text === undefined) return ( ); const dirty = text.trim() !== (saved.site ?? "").trim(); const disabled = !gate.allowed; const count = (n: number) => formatNumber(locale, n); return ( {dirty && ( )} } > {save.error && ( } title={t("siteSettings.design.failed")} description={ isConflict(save.error) ? t("siteSettings.conflictDescription") : save.error instanceof Error ? apiErrorText(t, save.error) : String(save.error) } /> )} {warnings.length > 0 && ( } title={t("siteSettings.design.warnings")} description={warnings.join(" ")} data-testid="design-warnings" /> )}
{!text.trim() && !disabled && (
)} {saved.theme ? ( {t("siteSettings.design.theme", { theme })}
{t("siteSettings.design.themeHint")}
) : ( {t("siteSettings.design.noTheme", { theme })} )} {disabled && gate.reason ? gate.reason : t("siteSettings.design.agentHint")}
); }