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 | 269x 269x 69x 269x 269x 269x 100x 100x 120x 100x 100x 16x 16x 16x 16x | import { useQuery } from "@tanstack/react-query";
import { useMemo } from "react";
import {
AccessCatalogue,
AccessLevel,
AccessMap,
LEVEL_RANK,
} from "@/objects/Organisation";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
/** The access vocabulary, from svc-core's registry: which area each kind of
* resource is in, what each level allows, and the presets. Never changes in
* a session. */
export function useAccessCatalogue() {
const { getApiHandler } = useApiHandler();
const query = useQuery<AccessCatalogue>({
queryKey: [QUERY_KEYS.GET.ACCESS_CATALOGUE],
queryFn: () => getApiHandler().access_router.catalogue(),
staleTime: Infinity,
});
// Typed here: TypeScript 5.0 reads useQuery's data as `any`. Anything
// not shaped like a catalogue counts as still loading, so consumers need
// only check that it is there.
const data: AccessCatalogue | undefined = query.data;
const catalogue = Array.isArray(data?.areas) ? data : undefined;
return useMemo(() => {
const areaOfKind = new Map<string, string>();
for (const area of catalogue?.areas ?? [])
for (const kind of area.kinds) areaOfKind.set(kind, area.key);
const editorOnly = new Set(catalogue?.editor_only ?? []);
return {
catalogue,
/** The level `access` must hold to create a `kind` there, and in which
* area — or null while the catalogue is loading. */
needs: (kind: string): { area: string; level: AccessLevel } | null => {
const area = areaOfKind.get(kind);
Iif (!area) return null;
return { area, level: editorOnly.has(kind) ? "editor" : "writer" };
},
allows: (access: AccessMap | null | undefined, area: string, level: AccessLevel) =>
!!access && LEVEL_RANK[access[area] ?? "reader"] >= LEVEL_RANK[level],
};
}, [catalogue]);
}
|