diff --git a/frontend/packages/swarm-ui/src/pages/IssueReportPage.tsx b/frontend/packages/swarm-ui/src/pages/IssueReportPage.tsx index df24375fe..02fd108ad 100644 --- a/frontend/packages/swarm-ui/src/pages/IssueReportPage.tsx +++ b/frontend/packages/swarm-ui/src/pages/IssueReportPage.tsx @@ -27,18 +27,24 @@ // checkbox row — folded into the "labels" column's own `Table` filter // (`filterMode: "multiselect"`) per mara's "fold filters into table" // request, one mechanism instead of two. -import { useEffect, useMemo, useState } from "preact/hooks"; +import { useMemo } from "preact/hooks"; import { ApiErrorPanel } from "@hive/shared/api-error-panel.js"; -import { readApiError, type ProblemDetails } from "@hive/shared/api-error.js"; import { Badge } from "@hive/shared/badge.js"; import { useLocalSetting } from "@hive/shared/settings-storage.js"; +import { fetchJson } from "../ui/polled/usePolled.js"; +import { useResource } from "../ui/polled/useResource.js"; import { ExternalLink } from "../ui/external-link/ExternalLink.js"; import { LazyDisclosure } from "../ui/disclosure/LazyDisclosure.js"; import { CheckboxField } from "../ui/checkbox-field/CheckboxField.js"; import { SelectField } from "../ui/select-field/SelectField.js"; import { Table, type TableColumn } from "../ui/table/Table.js"; import { TableSkeleton } from "../ui/table/TableSkeleton.js"; -import { ALL_REPOS, reportUrl, repoOptions } from "./issue-report-repo.js"; +import { + ALL_REPOS, + reportRows, + reportUrl, + repoOptions, +} from "./issue-report-repo.js"; import "./IssueReportPage.css"; interface IssueReportRow { @@ -131,17 +137,10 @@ function SortHeader({ } function IssueReportBody() { - const [repos, setRepos] = useState(null); const [repoFilter, setRepoFilter] = useLocalSetting( REPO_FILTER_KEY, ALL_REPOS, ); - const [rows, setRows] = useState(null); - // One slot per request, so a report that loads does not clear a repo - // list that failed. - const [reposError, setReposError] = useState(null); - const [error, setError] = useState(null); - const [loading, setLoading] = useState(false); const [hideBlocked, setHideBlocked] = useLocalSetting( HIDE_BLOCKED_KEY, false, @@ -154,62 +153,24 @@ function IssueReportBody() { // Repo dropdown source, fetched once — this page has no refresh // cadence, and a repo gaining/losing its first/last open issue between - // visits is rare enough that a manual reload covers it. Same - // `cancelled` guard as the repo-filter effect below, so an unmount - // mid-flight (navigating away before this resolves) doesn't call - // `setRepos`/`setReposError` on a component that's already gone. - useEffect(() => { - let cancelled = false; - (async () => { - const r = await fetch("/api/repos"); - if (!r.ok) { - if (!cancelled) setReposError(await readApiError(r)); - return; - } - const data = (await r.json()) as string[]; - if (!cancelled) setRepos(data); - })().catch((e: unknown) => { - if (!cancelled) setReposError({ detail: String(e) }); - }); - return () => { - cancelled = true; - }; - }, []); + // visits is rare enough that a manual reload covers it. + const { data: repos, error: reposError } = useResource( + (signal) => fetchJson("/api/repos", signal), + [], + ); - // The one fetch this page re-runs on state change — everything else - // (sort, hide-blocked, the table's own per-column filters) works over - // what's already loaded. - useEffect(() => { - let cancelled = false; - setLoading(true); - // A failed report drops the rows, so the table never shows another - // repo's issues under this repo's error. - const fail = (problem: ProblemDetails) => { - setRows(null); - setError(problem); - }; - (async () => { - const r = await fetch(reportUrl(repoFilter)); - if (!r.ok) { - const problem = await readApiError(r); - if (!cancelled) fail(problem); - return; - } - const data = (await r.json()) as IssueReportRow[]; - if (cancelled) return; - setRows(data); - setError(null); - })() - .catch((e: unknown) => { - if (!cancelled) fail({ detail: String(e) }); - }) - .finally(() => { - if (!cancelled) setLoading(false); - }); - return () => { - cancelled = true; - }; - }, [repoFilter]); + // The one read this page re-runs on a state change: the repo choice. A failed + // report drops the rows, so the table never shows another repo's rows under + // this repo's error. + const { + data: loaded, + error, + loading, + } = useResource( + (signal) => fetchJson(reportUrl(repoFilter), signal), + [repoFilter], + ); + const rows = reportRows(error, loaded); const visibleRows = useMemo(() => { let out = rows ?? []; diff --git a/frontend/packages/swarm-ui/src/pages/PrReportPage.tsx b/frontend/packages/swarm-ui/src/pages/PrReportPage.tsx index af84b661e..54a973f6d 100644 --- a/frontend/packages/swarm-ui/src/pages/PrReportPage.tsx +++ b/frontend/packages/swarm-ui/src/pages/PrReportPage.tsx @@ -10,18 +10,18 @@ // loaded; only the repo selection fetches again. No default filter is applied. // The filters the user sets are kept in the browser (the table's storage key, // and the repo choice). -import { useEffect, useState } from "preact/hooks"; import { ApiErrorPanel } from "@hive/shared/api-error-panel.js"; -import { readApiError, type ProblemDetails } from "@hive/shared/api-error.js"; import { Badge, type BadgeTone } from "@hive/shared/badge.js"; import { useLocalSetting } from "@hive/shared/settings-storage.js"; import { LazyDisclosure } from "../ui/disclosure/LazyDisclosure.js"; import { ExternalLink } from "../ui/external-link/ExternalLink.js"; +import { fetchJson } from "../ui/polled/usePolled.js"; +import { useResource } from "../ui/polled/useResource.js"; import { RelativeTime } from "../ui/relative-time/RelativeTime.js"; import { SelectField } from "../ui/select-field/SelectField.js"; import { Table, type TableColumn } from "../ui/table/Table.js"; import { TableSkeleton } from "../ui/table/TableSkeleton.js"; -import { ALL_REPOS, repoOptions } from "./issue-report-repo.js"; +import { ALL_REPOS, repoOptions, reportRows } from "./issue-report-repo.js"; import { approvalsText, diffSize, @@ -173,70 +173,30 @@ const COLUMNS: TableColumn[] = [ ]; function PrReportBody() { - const [repos, setRepos] = useState(null); const [repoFilter, setRepoFilter] = useLocalSetting( REPO_FILTER_KEY, ALL_REPOS, ); - const [rows, setRows] = useState(null); - // One slot per request, so a report that loads does not clear a repo list - // that failed. - const [reposError, setReposError] = useState(null); - const [error, setError] = useState(null); - const [loading, setLoading] = useState(false); // The dropdown's source, read once: a repo gaining or losing its first or // last open PR between visits is rare enough that a reload covers it. - useEffect(() => { - let cancelled = false; - (async () => { - const r = await fetch("/api/pulls/repos"); - if (!r.ok) { - if (!cancelled) setReposError(await readApiError(r)); - return; - } - const data = (await r.json()) as string[]; - if (!cancelled) setRepos(data); - })().catch((e: unknown) => { - if (!cancelled) setReposError({ detail: String(e) }); - }); - return () => { - cancelled = true; - }; - }, []); + const { data: repos, error: reposError } = useResource( + (signal) => fetchJson("/api/pulls/repos", signal), + [], + ); - // The one fetch that re-runs on a state change: the repo choice. - useEffect(() => { - let cancelled = false; - setLoading(true); - // A failed report drops the rows, so the table never shows another - // repo's PRs under this repo's error. - const fail = (problem: ProblemDetails) => { - setRows(null); - setError(problem); - }; - (async () => { - const r = await fetch(prReportUrl(repoFilter)); - if (!r.ok) { - const problem = await readApiError(r); - if (!cancelled) fail(problem); - return; - } - const data = (await r.json()) as PrReportRow[]; - if (cancelled) return; - setRows(data); - setError(null); - })() - .catch((e: unknown) => { - if (!cancelled) fail({ detail: String(e) }); - }) - .finally(() => { - if (!cancelled) setLoading(false); - }); - return () => { - cancelled = true; - }; - }, [repoFilter]); + // The one read this page re-runs on a state change: the repo choice. A failed + // report drops the rows, so the table never shows another repo's rows under + // this repo's error. + const { + data: loaded, + error, + loading, + } = useResource( + (signal) => fetchJson(prReportUrl(repoFilter), signal), + [repoFilter], + ); + const rows = reportRows(error, loaded); return ( <> diff --git a/frontend/packages/swarm-ui/src/pages/issue-report-repo.ts b/frontend/packages/swarm-ui/src/pages/issue-report-repo.ts index 2c6760c48..0427ba9e4 100644 --- a/frontend/packages/swarm-ui/src/pages/issue-report-repo.ts +++ b/frontend/packages/swarm-ui/src/pages/issue-report-repo.ts @@ -42,3 +42,11 @@ export function repoOptions( } return options; } + +/** + * The rows a report shows: none while the report is in error, so the table + * never keeps the last repo's rows under this repo's error. + */ +export function reportRows(error: unknown, loaded: T[] | null): T[] | null { + return error ? null : loaded; +} diff --git a/frontend/packages/swarm-ui/src/pages/review-settings/ClassifierKey.tsx b/frontend/packages/swarm-ui/src/pages/review-settings/ClassifierKey.tsx index a10ab8b68..c7218a76f 100644 --- a/frontend/packages/swarm-ui/src/pages/review-settings/ClassifierKey.tsx +++ b/frontend/packages/swarm-ui/src/pages/review-settings/ClassifierKey.tsx @@ -10,6 +10,8 @@ import { readApiError, type ProblemDetails } from "@hive/shared/api-error.js"; import { ActionRow } from "../../ui/action-row/ActionRow.js"; import { Button } from "../../ui/button/Button.js"; import { Hint } from "../../ui/hint/Hint.js"; +import { fetchJson } from "../../ui/polled/usePolled.js"; +import { useResource } from "../../ui/polled/useResource.js"; import { TextField } from "../../ui/text-field/TextField.js"; import { keyOf, keyProblem, keyStatus, type KeysBody } from "./KeyTypes.js"; import type { Runtime } from "./SettingsTypes.js"; @@ -17,35 +19,17 @@ import type { Runtime } from "./SettingsTypes.js"; const KEYS = "/api/review-classifier/keys"; export function ClassifierKey({ runtime }: { runtime: Runtime }) { - const [keys, setKeys] = useState(null); - const [loadError, setLoadError] = useState(null); + const { + data: keys, + error: loadError, + setData: setKeys, + } = useResource((signal) => fetchJson(KEYS, signal), []); const [saveError, setSaveError] = useState(null); // The key as typed. Never kept after a save. const [text, setText] = useState(""); const [saving, setSaving] = useState(false); const [justSaved, setJustSaved] = useState(false); - useEffect(() => { - let cancelled = false; - void (async () => { - try { - const res = await fetch(KEYS); - if (cancelled) return; - if (!res.ok) { - setLoadError(await readApiError(res)); - return; - } - const body = (await res.json()) as KeysBody; - if (!cancelled) setKeys(body); - } catch (e: unknown) { - if (!cancelled) setLoadError({ detail: String(e) }); - } - })(); - return () => { - cancelled = true; - }; - }, []); - // A key typed for one runtime must not be saved to the other. useEffect(() => { setText(""); @@ -71,7 +55,8 @@ export function ClassifierKey({ runtime }: { runtime: Runtime }) { setSaveError(await readApiError(res)); return; } - setKeys((await res.json()) as KeysBody); + const saved = (await res.json()) as KeysBody; + setKeys(() => saved); setText(""); setJustSaved(true); } catch (err: unknown) { diff --git a/frontend/packages/swarm-ui/src/pages/review-tryout/ReviewTryOut.tsx b/frontend/packages/swarm-ui/src/pages/review-tryout/ReviewTryOut.tsx index 174164be6..0e2d5adcf 100644 --- a/frontend/packages/swarm-ui/src/pages/review-tryout/ReviewTryOut.tsx +++ b/frontend/packages/swarm-ui/src/pages/review-tryout/ReviewTryOut.tsx @@ -33,6 +33,8 @@ import { useConfirm } from "../../ui/confirm-dialog/useConfirm.js"; import { Disclosure } from "../../ui/disclosure/Disclosure.js"; import { ExternalLink } from "../../ui/external-link/ExternalLink.js"; import { Panel } from "../../ui/panel/Panel.js"; +import { fetchJson } from "../../ui/polled/usePolled.js"; +import { useResource } from "../../ui/polled/useResource.js"; import { TextField } from "../../ui/text-field/TextField.js"; import { ClassificationEditor } from "./ClassificationEditor.js"; import { TryOutResult } from "../review-common/TryOutResult.js"; @@ -40,6 +42,7 @@ import { addProblem, draftFromRule, hasTypedText, + maxClassificationsOf, prNumber, repoProblem, rowsProblem, @@ -109,14 +112,8 @@ export function ReviewTryOut() { // What the two fields offer to complete to. The fields stay free text, so a // repo or a PR the lists lack, a merged one say, can still be typed. - const [repoChoices, setRepoChoices] = useState([]); - const [repoChoicesError, setRepoChoicesError] = - useState(null); const [pulls, setPulls] = useState([]); const [pullsError, setPullsError] = useState(null); - const [maxClassifications, setMaxClassifications] = useState( - null, - ); const running = starting || runId !== null; @@ -125,46 +122,19 @@ export function ReviewTryOut() { const [open, setOpen] = useState(false); // The repos with an open PR, read once when the page opens. - useEffect(() => { - let cancelled = false; - void (async () => { - try { - const res = await fetch(PULL_REPOS); - if (cancelled) return; - if (!res.ok) { - setRepoChoicesError(await readApiError(res)); - return; - } - setRepoChoices((await res.json()) as string[]); - } catch (e: unknown) { - if (!cancelled) setRepoChoicesError({ detail: String(e) }); - } - })(); - return () => { - cancelled = true; - }; - }, []); + const { data: repoList, error: repoChoicesError } = useResource( + (signal) => fetchJson(PULL_REPOS, signal), + [], + ); + const repoChoices = repoList ?? []; // How many rows a run takes: the classifier settings', read once. Until it // is known, or when it cannot be read, the run endpoint is the only cap. - useEffect(() => { - let cancelled = false; - void (async () => { - try { - const res = await fetch(SETTINGS); - if (cancelled || !res.ok) return; - const body = (await res.json()) as { max_classifications?: number }; - if (!cancelled && typeof body.max_classifications === "number") { - setMaxClassifications(body.max_classifications); - } - } catch { - // The endpoint still refuses a run that is too long. - } - })(); - return () => { - cancelled = true; - }; - }, []); + const { data: settings } = useResource( + (signal) => fetchJson<{ max_classifications?: number }>(SETTINGS, signal), + [], + ); + const maxClassifications = maxClassificationsOf(settings); // The open PRs of the repo once it is typed or picked and has stopped // changing. A repo that is not valid yet has no list. diff --git a/frontend/packages/swarm-ui/src/pages/review-tryout/TryOutTypes.ts b/frontend/packages/swarm-ui/src/pages/review-tryout/TryOutTypes.ts index d6f3e27eb..fed23695c 100644 --- a/frontend/packages/swarm-ui/src/pages/review-tryout/TryOutTypes.ts +++ b/frontend/packages/swarm-ui/src/pages/review-tryout/TryOutTypes.ts @@ -197,3 +197,15 @@ export function toBody(d: ClassificationDraft): ClassificationBody { threshold: thresholdValue(d.threshold) ?? 0, }; } + +/** + * The classifier settings' cap on rows per run, or `null` when it is not known + * yet or the settings lack one: then the run endpoint is the only cap. + */ +export function maxClassificationsOf( + settings: { max_classifications?: unknown } | null, +): number | null { + return typeof settings?.max_classifications === "number" + ? settings.max_classifications + : null; +} diff --git a/frontend/packages/swarm-ui/test/issue-report-repo.test.mjs b/frontend/packages/swarm-ui/test/issue-report-repo.test.mjs index 25535a853..ab3a0b1f5 100644 --- a/frontend/packages/swarm-ui/test/issue-report-repo.test.mjs +++ b/frontend/packages/swarm-ui/test/issue-report-repo.test.mjs @@ -2,6 +2,7 @@ import { test } from "node:test"; import assert from "node:assert/strict"; import { ALL_REPOS, + reportRows, reportUrl, repoOptions, } from "../src/pages/issue-report-repo.ts"; @@ -53,3 +54,10 @@ test("repoOptions: a remembered repo missing from the list says why", () => { label: "a/x (no open issues)", }); }); + +test("reportRows: a failed report shows no rows, a good one shows them", () => { + const rows = [{ n: 1 }]; + assert.equal(reportRows({ detail: "boom" }, rows), null); + assert.equal(reportRows(null, rows), rows); + assert.equal(reportRows(null, null), null); +}); diff --git a/frontend/packages/swarm-ui/test/tryout-types.test.mjs b/frontend/packages/swarm-ui/test/tryout-types.test.mjs index d5e32449e..f1b902dbb 100644 --- a/frontend/packages/swarm-ui/test/tryout-types.test.mjs +++ b/frontend/packages/swarm-ui/test/tryout-types.test.mjs @@ -10,6 +10,7 @@ import { draftProblem, duplicateName, hasTypedText, + maxClassificationsOf, prNumber, repoProblem, rowsProblem, @@ -154,3 +155,10 @@ test("the page's one hint names what disables opening a PR before the PR number" assert.equal(rowsProblem("o/r", [row()]), null); assert.equal(runProblem("o/r", "7", [row()]), null); }); + +test("maxClassificationsOf: the settings cap when it is a number, else none", () => { + assert.equal(maxClassificationsOf({ max_classifications: 20 }), 20); + assert.equal(maxClassificationsOf({}), null); + assert.equal(maxClassificationsOf({ max_classifications: "20" }), null); + assert.equal(maxClassificationsOf(null), null); +});