|
| 1 | +"use client"; |
| 2 | + |
| 3 | +/** |
| 4 | + * One ruleset: every rule it contains, and which of them apply. |
| 5 | + * |
| 6 | + * Switching a rule off moves the ruleset's version, which the page says out |
| 7 | + * loud, because a review's report names the version it was judged against and |
| 8 | + * two different rule sets must never share one. |
| 9 | + */ |
| 10 | + |
| 11 | +import { use, useEffect, useState } from "react"; |
| 12 | +import { PageBody, PageHeader } from "@/components/page"; |
| 13 | +import { Badge, Button, Card, Mono, Problem, severityTone } from "@/components/ui"; |
| 14 | + |
| 15 | +interface Detail { |
| 16 | + ruleset: { id: string; name: string; tier: string; version: number }; |
| 17 | + directives: { section: string; title: string }[]; |
| 18 | + rules: { |
| 19 | + code: string; |
| 20 | + title: string; |
| 21 | + severity: string; |
| 22 | + tags: string[]; |
| 23 | + sweepPatterns: number; |
| 24 | + enabled: boolean; |
| 25 | + }[]; |
| 26 | +} |
| 27 | + |
| 28 | +export default function RulesetPage({ params }: { params: Promise<{ id: string }> }) { |
| 29 | + const { id } = use(params); |
| 30 | + const [detail, setDetail] = useState<Detail | null>(null); |
| 31 | + const [error, setError] = useState(""); |
| 32 | + const [busy, setBusy] = useState(""); |
| 33 | + const [showDirectives, setShowDirectives] = useState(false); |
| 34 | + |
| 35 | + useEffect(() => { |
| 36 | + let cancelled = false; |
| 37 | + void (async () => { |
| 38 | + const response = await fetch(`/api/rulesets/${id}`); |
| 39 | + if (response.ok && !cancelled) setDetail((await response.json()) as Detail); |
| 40 | + })(); |
| 41 | + return () => { |
| 42 | + cancelled = true; |
| 43 | + }; |
| 44 | + }, [id]); |
| 45 | + |
| 46 | + async function toggle(code: string, enabled: boolean) { |
| 47 | + setError(""); |
| 48 | + setBusy(code); |
| 49 | + try { |
| 50 | + const response = await fetch(`/api/rulesets/${id}/rules/${encodeURIComponent(code)}`, { |
| 51 | + method: "PATCH", |
| 52 | + body: JSON.stringify({ enabled }), |
| 53 | + }); |
| 54 | + if (!response.ok) { |
| 55 | + setError(((await response.json()) as { error?: string }).error ?? "That did not work."); |
| 56 | + return; |
| 57 | + } |
| 58 | + const refreshed = await fetch(`/api/rulesets/${id}`); |
| 59 | + if (refreshed.ok) setDetail((await refreshed.json()) as Detail); |
| 60 | + } finally { |
| 61 | + setBusy(""); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + if (!detail) return <PageBody>Loading...</PageBody>; |
| 66 | + |
| 67 | + const off = detail.rules.filter((rule) => !rule.enabled).length; |
| 68 | + |
| 69 | + return ( |
| 70 | + <> |
| 71 | + <PageHeader |
| 72 | + title={detail.ruleset.name} |
| 73 | + subtitle={ |
| 74 | + <span className="flex flex-wrap items-center gap-3"> |
| 75 | + <Badge>{detail.ruleset.tier}</Badge> |
| 76 | + <span>version {detail.ruleset.version}</span> |
| 77 | + <span> |
| 78 | + {detail.rules.length} rule(s) |
| 79 | + {off > 0 ? `, ${off} switched off` : ""} |
| 80 | + </span> |
| 81 | + </span> |
| 82 | + } |
| 83 | + actions={ |
| 84 | + <a href={`/api/rulesets/${id}/export`} download> |
| 85 | + <Button>Export document</Button> |
| 86 | + </a> |
| 87 | + } |
| 88 | + /> |
| 89 | + <PageBody> |
| 90 | + {error ? ( |
| 91 | + <div className="mb-4"> |
| 92 | + <Problem>{error}</Problem> |
| 93 | + </div> |
| 94 | + ) : null} |
| 95 | + |
| 96 | + <p className="mb-4 max-w-2xl text-sm text-[var(--color-ink-muted)]"> |
| 97 | + A switched-off rule is left out of the rules a new review is judged against, and its |
| 98 | + mechanical sweeps do not run. Reviews already started keep the rules they were frozen |
| 99 | + with. The exported document always contains every rule, because it is the document that |
| 100 | + was imported. |
| 101 | + </p> |
| 102 | + |
| 103 | + <Card className="overflow-x-auto"> |
| 104 | + <table className="w-full text-sm"> |
| 105 | + <thead> |
| 106 | + <tr className="border-b border-[var(--color-border)] text-left text-xs text-[var(--color-ink-muted)]"> |
| 107 | + <th className="px-3 py-2 font-medium">Rule</th> |
| 108 | + <th className="px-3 py-2 font-medium">Severity</th> |
| 109 | + <th className="px-3 py-2 font-medium">Tags</th> |
| 110 | + <th className="px-3 py-2 font-medium">Sweeps</th> |
| 111 | + <th className="px-3 py-2 text-right font-medium">Applies</th> |
| 112 | + </tr> |
| 113 | + </thead> |
| 114 | + <tbody> |
| 115 | + {detail.rules.map((rule) => ( |
| 116 | + <tr |
| 117 | + key={rule.code} |
| 118 | + className={`border-b border-[var(--color-border)] last:border-0 ${ |
| 119 | + rule.enabled ? "" : "opacity-55" |
| 120 | + }`} |
| 121 | + > |
| 122 | + <td className="px-3 py-2"> |
| 123 | + <Mono className="text-xs text-[var(--color-ink-muted)]">{rule.code}</Mono> |
| 124 | + <span className="ml-2">{rule.title}</span> |
| 125 | + </td> |
| 126 | + <td className="px-3 py-2"> |
| 127 | + <Badge tone={severityTone(rule.severity)}>{rule.severity}</Badge> |
| 128 | + </td> |
| 129 | + <td className="px-3 py-2 text-xs text-[var(--color-ink-muted)]"> |
| 130 | + {rule.tags.join(", ") || "any"} |
| 131 | + </td> |
| 132 | + <td className="px-3 py-2 text-[var(--color-ink-muted)]">{rule.sweepPatterns}</td> |
| 133 | + <td className="px-3 py-2 text-right"> |
| 134 | + <label className="inline-flex items-center gap-2"> |
| 135 | + <span className="text-xs text-[var(--color-ink-muted)]"> |
| 136 | + {rule.enabled ? "on" : "off"} |
| 137 | + </span> |
| 138 | + <input |
| 139 | + type="checkbox" |
| 140 | + checked={rule.enabled} |
| 141 | + disabled={busy !== ""} |
| 142 | + onChange={(event) => void toggle(rule.code, event.target.checked)} |
| 143 | + /> |
| 144 | + </label> |
| 145 | + </td> |
| 146 | + </tr> |
| 147 | + ))} |
| 148 | + </tbody> |
| 149 | + </table> |
| 150 | + </Card> |
| 151 | + |
| 152 | + <button |
| 153 | + type="button" |
| 154 | + className="mt-6 text-sm text-[var(--color-accent)] hover:underline" |
| 155 | + onClick={() => setShowDirectives((shown) => !shown)} |
| 156 | + > |
| 157 | + {showDirectives ? "Hide" : "Show"} the {detail.directives.length} process directive(s) |
| 158 | + </button> |
| 159 | + {showDirectives ? ( |
| 160 | + <ul className="mt-2 grid gap-1 text-sm text-[var(--color-ink-muted)]"> |
| 161 | + {detail.directives.map((directive) => ( |
| 162 | + <li key={`${directive.section}-${directive.title}`}> |
| 163 | + {directive.section}: {directive.title} |
| 164 | + </li> |
| 165 | + ))} |
| 166 | + </ul> |
| 167 | + ) : null} |
| 168 | + </PageBody> |
| 169 | + </> |
| 170 | + ); |
| 171 | +} |
0 commit comments