86 lines
3.7 KiB
TypeScript
86 lines
3.7 KiB
TypeScript
// R-P5 (P5) §P3 — sync history for one connection. Loader builds SyncHistoryVM
|
|
// from the AppSyncRun list. Requires integrations.view_logs. Manual re-sync posts
|
|
// back to the app route's action. Renders the interim SyncHistoryPage from ~/ui
|
|
// (AD-28); final optic = D-P5 delta.
|
|
|
|
import { type LoaderFunctionArgs, useFetcher, useLoaderData } from "react-router";
|
|
import { buildSyncHistoryVM, syncKindKey, syncStatusKey, type SyncRunRow, type SyncHistoryVM } from "@hivegrid/view-models";
|
|
import { getCatalogEntry } from "@hivegrid/integrations";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import { integrationsHasPermission, loadIntegrationsContext, notFoundResponse, requireIntegrationsPermission } from "~/utils/integrations/integrationsAccess.server";
|
|
import { getConnectionOrThrow, listSyncRuns } from "~/utils/integrations/appsEngineService.server";
|
|
import { integrationsAppPath, integrationsSyncsPath } from "~/utils/integrations/integrationsRoutePaths";
|
|
import { SyncHistoryPage, type SyncHistoryActions } from "~/components/screens/integrations/SyncHistoryPage";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
|
|
type LoaderData = { title: string; backHref: string; actionHref: string; runHrefBase: string; vm: SyncHistoryVM };
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const { t } = await getTranslations(request);
|
|
const ctx = await loadIntegrationsContext({ request, params });
|
|
const tenantSlug = params.tenant ?? "";
|
|
const appKey = params.app ?? "";
|
|
const connectionId = params.connectionId ?? "";
|
|
|
|
if (!ctx.hasModule) {
|
|
throw notFoundResponse();
|
|
}
|
|
try {
|
|
requireIntegrationsPermission(ctx, "integrations.view_logs");
|
|
} catch {
|
|
throw notFoundResponse();
|
|
}
|
|
if (!getCatalogEntry(appKey)) {
|
|
throw notFoundResponse();
|
|
}
|
|
// Tenant-scoped; 404 if the connection is not in this tenant.
|
|
await getConnectionOrThrow(ctx.tenantId, connectionId);
|
|
|
|
const runs = await listSyncRuns(ctx.tenantId, connectionId);
|
|
const rows: SyncRunRow[] = runs.map((run) => ({
|
|
id: run.id,
|
|
kind: run.kind,
|
|
kindKey: syncKindKey(run.kind),
|
|
status: run.status,
|
|
statusKey: syncStatusKey(run.status),
|
|
itemsTotal: run.itemsTotal,
|
|
itemsSucceeded: run.itemsSucceeded,
|
|
itemsFailed: run.itemsFailed,
|
|
summaryKey: run.summaryText,
|
|
startedIso: run.startedAt ? run.startedAt.toISOString() : null,
|
|
completedIso: run.completedAt ? run.completedAt.toISOString() : null,
|
|
}));
|
|
|
|
const vm = buildSyncHistoryVM({ connectionId, rows, canManualSync: integrationsHasPermission(ctx, "integrations.manual_sync") });
|
|
return Response.json({
|
|
title: `${t("integrations.syncs.title")} | ${process.env.APP_NAME}`,
|
|
backHref: integrationsAppPath(tenantSlug, appKey),
|
|
actionHref: integrationsAppPath(tenantSlug, appKey),
|
|
runHrefBase: integrationsSyncsPath(tenantSlug, appKey, connectionId),
|
|
vm,
|
|
} satisfies LoaderData);
|
|
};
|
|
|
|
type MutationResponse = { ok?: boolean; error?: string };
|
|
|
|
export default function IntegrationsSyncHistoryRoute() {
|
|
const { backHref, actionHref, runHrefBase, vm } = useLoaderData<LoaderData>();
|
|
const fetcher = useFetcher<MutationResponse>();
|
|
const submitting = fetcher.state !== "idle";
|
|
|
|
const actions: SyncHistoryActions = {
|
|
onManualSync: (connectionId: string) => {
|
|
const formData = new FormData();
|
|
formData.set("intent", "manual-sync");
|
|
formData.set("connectionId", connectionId);
|
|
fetcher.submit(formData, { method: "post", action: actionHref });
|
|
},
|
|
};
|
|
|
|
return <SyncHistoryPage vm={vm} backHref={backHref} runHrefBase={runHrefBase} actions={actions} submitting={submitting} />;
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|