Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | 26x 99x 99x 46x 99x 99x 99x 150x 99x 97x 90x 450x 90x 99x 99x 150x | import {
EvaluationScorer,
ScorerCatalogue,
ScorerGroupInfo,
ScorerInfo,
} from "@/objects/Evaluation";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { useQuery } from "@tanstack/react-query";
import { useMemo } from "react";
/** The fallback catalogue, used only until the request lands. */
const EMPTY: ScorerCatalogue = {
scorers: [],
groups: [],
default_pass_threshold: 0.7,
};
/**
* The scorer vocabulary, as svc-core describes it.
*
* Four surfaces choose a scorer — creating an evaluation, editing one, a
* Quick try, and the Reports filter — and all four asked for this list
* separately. It never changes within a session, so it is one query with an
* infinite stale time, and the lookups every caller built by hand
* (`find(s => s.kind === …)`) live here once.
*
* `offered` is what a picker shows: superseded scorers are still described —
* an evaluation that already selected one has to be able to say what it is —
* but are never offered for a new one.
*/
export function useScorerCatalogue() {
const { getApiHandler } = useApiHandler();
const { data, isLoading } = useQuery<ScorerCatalogue>({
queryKey: [QUERY_KEYS.GET.EVALUATION_SCORERS],
queryFn: () => getApiHandler().evaluation_router.listScorers(),
staleTime: Infinity,
});
// Bound to an explicitly typed const for the same reason as elsewhere: the
// pinned TypeScript cannot infer react-query v5's return type.
//
// Normalised rather than trusted: the endpoint answers an object, but a
// page must still render when it answers a bare array — an empty-API smoke
// test does exactly that, and so does a browser holding a cached response
// from before `/scorers` grew its groups.
const answer = (data ?? EMPTY) as Partial<ScorerCatalogue>;
const catalogue: ScorerCatalogue = {
scorers: Array.isArray(answer.scorers) ? answer.scorers : [],
groups: Array.isArray(answer.groups) ? answer.groups : [],
default_pass_threshold:
typeof answer.default_pass_threshold === "number"
? answer.default_pass_threshold
: EMPTY.default_pass_threshold,
};
const byKind = useMemo(
() =>
Object.fromEntries(catalogue.scorers.map((s) => [s.kind, s])) as Partial<
Record<EvaluationScorer, ScorerInfo>
>,
[catalogue.scorers],
);
/**
* The scorers a picker offers, grouped under their headings, with empty
* groups dropped so a heading never stands over nothing.
*/
const grouped = useMemo(
(): { group: ScorerGroupInfo; scorers: ScorerInfo[] }[] =>
catalogue.groups
.map((group) => ({
group,
scorers: catalogue.scorers.filter(
(s) => s.group === group.key && !s.superseded_by,
),
}))
.filter((section) => section.scorers.length > 0),
[catalogue.groups, catalogue.scorers],
);
/** What a scorer is called, for a row that shows one without offering it. */
const labelOf = useMemo(
() => (kind: EvaluationScorer | null | undefined) =>
kind ? (byKind[kind]?.label ?? kind.replace(/_/g, " ")) : "—",
[byKind],
);
return {
...catalogue,
/** Every scorer, superseded ones included — for reading a stored value. */
all: catalogue.scorers,
/** What a new evaluation may choose. */
offered: catalogue.scorers.filter((s) => !s.superseded_by),
grouped,
byKind,
labelOf,
isLoading,
};
}
export default useScorerCatalogue;
|