Watch
0
0
Fork
You've already forked hyperhive
0

swarm-ui: the report, review and classifier-key pages read through useResource

Part of the unification order in the UI sweep issue (step d, the rest).

The pull-request and issue reports (the repo list and the report), the review
try-out's repo list and classifier cap, and the classifier key move to the
shared read hook. A failed report still drops its rows, now by reading
'error ? null : loaded' instead of a handler that cleared them. The reads that
seed a draft, post, or are not a plain request stay as they were.
This commit is contained in:
iris 2026-11-25 23:03:16 +01:00 • committed by mara
commit c5cc961028
8 changed files with 103 additions and 191 deletions

View file

@ -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<string[] | null>(null);
const [repoFilter, setRepoFilter] = useLocalSetting(
REPO_FILTER_KEY,
ALL_REPOS,
);
const [rows, setRows] = useState<IssueReportRow[] | null>(null);
// One slot per request, so a report that loads does not clear a repo
// list that failed.
const [reposError, setReposError] = useState<ProblemDetails | null>(null);
const [error, setError] = useState<ProblemDetails | null>(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<string[]>("/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<IssueReportRow[]>(reportUrl(repoFilter), signal),
[repoFilter],
);
const rows = reportRows(error, loaded);
const visibleRows = useMemo(() => {
let out = rows ?? [];

View file

@ -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<PrReportRow>[] = [
];
function PrReportBody() {
const [repos, setRepos] = useState<string[] | null>(null);
const [repoFilter, setRepoFilter] = useLocalSetting(
REPO_FILTER_KEY,
ALL_REPOS,
);
const [rows, setRows] = useState<PrReportRow[] | null>(null);
// One slot per request, so a report that loads does not clear a repo list
// that failed.
const [reposError, setReposError] = useState<ProblemDetails | null>(null);
const [error, setError] = useState<ProblemDetails | null>(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<string[]>("/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<PrReportRow[]>(prReportUrl(repoFilter), signal),
[repoFilter],
);
const rows = reportRows(error, loaded);
return (
<>

View file

@ -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<T>(error: unknown, loaded: T[] | null): T[] | null {
return error ? null : loaded;
}

View file

@ -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<KeysBody | null>(null);
const [loadError, setLoadError] = useState<ProblemDetails | null>(null);
const {
data: keys,
error: loadError,
setData: setKeys,
} = useResource((signal) => fetchJson<KeysBody>(KEYS, signal), []);
const [saveError, setSaveError] = useState<ProblemDetails | null>(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) {

View file

@ -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<string[]>([]);
const [repoChoicesError, setRepoChoicesError] =
useState<ProblemDetails | null>(null);
const [pulls, setPulls] = useState<OpenPull[]>([]);
const [pullsError, setPullsError] = useState<ProblemDetails | null>(null);
const [maxClassifications, setMaxClassifications] = useState<number | null>(
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<string[]>(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.

View file

@ -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;
}

View file

@ -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);
});

View file

@ -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);
});