Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/routes/app.$tenant/files.link-targets.tsx
T

36 lines
1.7 KiB
TypeScript

// P4 Files — loader-only JSON resource behind the search field of the
// "Datei verknüpfen" dialog.
//
// Its own resource route on purpose (same reasoning as the project's
// contact-search): the detail loader assembles the whole file including links
// and neighbours, and re-running that on every keystroke to look up a name
// would be absurd. The dialog fetches here instead.
//
// Gated on `files.write`: only someone who may create the link may browse the
// records to link to. A missing permission reads as "not found" (A.5.3).
import type { LoaderFunctionArgs } from "react-router";
import { loadFilesContext, notFoundResponse, requireFilesPermission } from "~/utils/integrations/fileAccess.server";
import { isSearchableLinkType, searchFileLinkTargets } from "~/utils/integrations/fileLinkTargets.server";
export async function loader({ request, params }: LoaderFunctionArgs): Promise<Response> {
const ctx = await loadFilesContext({ request, params, requireModule: true });
try {
requireFilesPermission(ctx, "files.write");
} catch {
throw notFoundResponse();
}
const url = new URL(request.url);
const entityType = url.searchParams.get("type") ?? "";
const search = url.searchParams.get("q") ?? "";
// An unsupported type is not an error: the dialog asks for whatever type is
// selected and falls back to its id field when nothing comes back.
if (!isSearchableLinkType(entityType)) {
return Response.json({ rows: [], searchable: false }, { headers: { "Cache-Control": "private, no-store" } });
}
const rows = await searchFileLinkTargets({ tenantId: ctx.tenantId, entityType, search });
return Response.json({ rows, searchable: true }, { headers: { "Cache-Control": "private, no-store" } });
}