Files
HealthLog/src/components/documents/sources/use-document-sources.ts
T
Marc-André Bombeck 6534c3f9e8 fix(documents): picker and settings card follow-ups
The settings card stays visible, read-only, while a connection is
stored but the operator list is gone or Documents is off, so the token
can still be removed; a new address asks for the token again. The
picker clears a selection when the search or a filter changes, says
when the 25-document limit is reached, steps back the rows a finished
run did not touch and offers Pick more, shows an empty state, and
announces each row's result to screen readers. Both system switches
are radio groups with one tab stop and arrow-key movement. Strings in
all seven languages.
2026-09-26 14:32:46 +02:00

59 lines
2.1 KiB
TypeScript

"use client";
/**
* The document picker's availability for the person on this screen (#1038).
*
* Reads `GET /api/documents/sources` only in the person's own record: a
* delegate or guardian never asks (the route would refuse them anyway), so
* the import buttons and the settings card render nothing for them.
* `connected` is the list of systems with a usable connection (the documents
* module on, the operator's list set, the origin still on it), in a stable
* order; the import buttons read only that.
*/
import { useQuery } from "@tanstack/react-query";
import { useAuth } from "@/hooks/use-auth";
import { useRecordCapabilities } from "@/hooks/use-record-capabilities";
import { apiGet } from "@/lib/api/api-fetch";
import type {
DocumentPickerSystem,
DocumentSourcesStatusDto,
} from "@/lib/documents/sources/types";
import { queryKeys } from "@/lib/query-keys";
export function useDocumentSourcesStatus() {
const { user } = useAuth();
const { canManageDomain, inSharedRecord } = useRecordCapabilities();
// The status read touches only the person's own rows and never the
// network, so it runs in one's own record whatever the module says: a
// connection stored before Documents was switched off, or before the
// operator removed the list, must stay visible so it can be deleted.
const ownRecord =
user !== null &&
user !== undefined &&
!inSharedRecord &&
canManageDomain("documents");
const eligible = ownRecord && user?.modules?.inboundDocuments === true;
const query = useQuery({
queryKey: queryKeys.documentSourcesStatus(),
enabled: ownRecord,
queryFn: () => apiGet<DocumentSourcesStatusDto>("/api/documents/sources"),
staleTime: 60_000,
});
const connected: DocumentPickerSystem[] =
eligible && query.data?.available
? query.data.connections
.filter((c) => c.originAllowed)
.map((c) => c.system)
: [];
return { ownRecord, eligible, query, connected };
}
/** Display name of a system; product names, not translated. */
export function systemName(system: DocumentPickerSystem): string {
return system === "PAPERLESS" ? "Paperless-ngx" : "Papra";
}