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

57 lines
2.7 KiB
TypeScript

// Loader-only CSV resource for the cross-pipeline project list. Keeping the
// download outside the UI route prevents React Router from rendering the list
// component with a CSV string as loader data.
import type { LoaderFunctionArgs } from "react-router";
import { PROJECT_READ_PERMISSIONS, requireProjectView } from "~/utils/integrations/projects/projectAccess.server";
import { exportProjectListCsv, listPipelines, ProjectServiceError, type ProjectListSort } from "~/utils/integrations/projects/projectService.server";
import { parseBoardFacetParams, pickListPipeline } from "~/utils/integrations/projects/projectBoardRoute.server";
import { isDemoDataUiEnabled } from "~/utils/services/.server/demoDataUi.server";
function listSort(value: string | null): ProjectListSort {
if (value === "updated_asc" || value === "name_asc" || value === "name_desc" || value === "number_asc" || value === "value_desc") return value;
return "updated_desc";
}
export async function loader({ request, params }: LoaderFunctionArgs): Promise<Response> {
const ctx = await requireProjectView({ request, params }, PROJECT_READ_PERMISSIONS);
const url = new URL(request.url);
const demoDataUiEnabled = isDemoDataUiEnabled(undefined, { tenantId: ctx.tenantId, tenantSlug: params.tenant });
// The list is bound to one board, so the download is too — resolved by the
// same rule, otherwise a CSV taken from the default board would silently
// contain every board.
const pipelines = await listPipelines({ tenantId: ctx.tenantId, ctx });
const activePipeline = pickListPipeline(pipelines, url.searchParams.get("pipelineId"));
let csv: string;
try {
csv = await exportProjectListCsv({
tenantId: ctx.tenantId,
ctx,
filters: {
pipelineId: activePipeline?.id ?? null,
q: url.searchParams.get("q"),
onlyMine: url.searchParams.get("onlyMine") === "1",
includeDemoData: demoDataUiEnabled && url.searchParams.get("demo") === "show",
// Same facets the list is showing, so the download matches the screen.
facets: parseBoardFacetParams(url.searchParams),
},
sort: listSort(url.searchParams.get("sort")),
});
} catch (error) {
if (error instanceof ProjectServiceError && error.messageKey === "projects.error.exportTooLarge") {
return Response.json(
{ error: error.messageKey },
{ status: 413, headers: { "Cache-Control": "private, no-store" } },
);
}
throw error;
}
return new Response(csv, {
headers: {
"Content-Type": "text/csv; charset=utf-8",
"Content-Disposition": 'attachment; filename="projects.csv"',
"Cache-Control": "private, no-store",
},
});
}