92 lines
4.1 KiB
TypeScript
92 lines
4.1 KiB
TypeScript
// R-P5 (P5) §P3 — integrations catalog route. Loader builds IntegrationCatalogVM
|
|
// from the code catalog (@hivegrid/integrations) + per-app live connection counts.
|
|
// Module off -> locked empty; missing integrations.read -> 404. Renders the
|
|
// interim IntegrationsCatalogPage from ~/ui (AD-28); final optic = D-P5 delta.
|
|
|
|
import { redirect, type LoaderFunctionArgs, useLoaderData } from "react-router";
|
|
import { buildIntegrationCatalogVM, integrationCategoryKey, type CatalogAppRow, type IntegrationCatalogVM } from "@hivegrid/view-models";
|
|
import { INTEGRATION_CATALOG } from "@hivegrid/integrations";
|
|
import { getTranslations } from "~/locale/i18next.server";
|
|
import {
|
|
loadIntegrationsContext,
|
|
integrationsHasPermission,
|
|
notFoundResponse,
|
|
requireIntegrationsPermission,
|
|
} from "~/utils/integrations/integrationsAccess.server";
|
|
import { listConnections } from "~/utils/integrations/appsEngineService.server";
|
|
import { integrationsAppPath, INTEGRATIONS_MODULE_KEY, legacyIntegrationsRedirectPath } from "~/utils/integrations/integrationsRoutePaths";
|
|
import { telephonySettingsPath } from "~/modules/telephony/routes/telephonyRoutePaths";
|
|
import { calendarConnectionsPath } from "~/modules/calendar/routes/calendarRoutePaths";
|
|
import { hasKwpIntegrationModule } from "~/modules/kwp/routes/kwpAccess.server";
|
|
import { IntegrationsCatalogPage } from "~/components/screens/integrations/IntegrationsCatalogPage";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
|
|
type LoaderData = { title: string; vm: IntegrationCatalogVM };
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
const legacyRedirect = legacyIntegrationsRedirectPath(request.url, params.tenant ?? "");
|
|
if (legacyRedirect) {
|
|
throw redirect(legacyRedirect);
|
|
}
|
|
const { t } = await getTranslations(request);
|
|
const ctx = await loadIntegrationsContext({ request, params });
|
|
const tenantSlug = params.tenant ?? "";
|
|
|
|
if (!ctx.hasModule) {
|
|
return Response.json({
|
|
title: `${t("integrations.catalog.title")} | ${process.env.APP_NAME}`,
|
|
vm: buildIntegrationCatalogVM({ rows: [], canConnect: false }),
|
|
} satisfies LoaderData);
|
|
}
|
|
try {
|
|
requireIntegrationsPermission(ctx, "integrations.read");
|
|
} catch {
|
|
throw notFoundResponse();
|
|
}
|
|
|
|
// Live connection counts per app (tenant-scoped, no credentials selected).
|
|
const [connections, hasKwpIntegration] = await Promise.all([
|
|
listConnections(ctx.tenantId),
|
|
hasKwpIntegrationModule(ctx),
|
|
]);
|
|
const countByApp = new Map<string, number>();
|
|
for (const connection of connections) {
|
|
countByApp.set(connection.appId, (countByApp.get(connection.appId) ?? 0) + 1);
|
|
}
|
|
|
|
const rows: CatalogAppRow[] = INTEGRATION_CATALOG.filter(
|
|
(app) => !app.internal && (app.key !== "kwp" || hasKwpIntegration),
|
|
).map((app) => ({
|
|
key: app.key,
|
|
titleKey: app.titleKey,
|
|
category: app.category,
|
|
categoryKey: integrationCategoryKey(app.category),
|
|
connectionType: app.connectionType,
|
|
connectionCount: countByApp.get(app.key) ?? 0,
|
|
// Telephony providers and calendar have dedicated lifecycles (provider
|
|
// setup, webhook authentication and optional user bindings; calendar:
|
|
// per-user CalendarConnection rows via the Google OAuth flow) — link their
|
|
// cards straight to those surfaces instead of generic DataConnection detail.
|
|
detailHref:
|
|
app.key === "aircall" || app.key === "hallopetra"
|
|
? telephonySettingsPath(tenantSlug)
|
|
: app.key === "google-calendar"
|
|
? calendarConnectionsPath(tenantSlug)
|
|
: integrationsAppPath(tenantSlug, app.key),
|
|
}));
|
|
|
|
const vm = buildIntegrationCatalogVM({ rows, canConnect: integrationsHasPermission(ctx, "integrations.connect") });
|
|
return Response.json({ title: `${t("integrations.catalog.title")} | ${process.env.APP_NAME}`, vm } satisfies LoaderData);
|
|
};
|
|
|
|
export default function IntegrationsCatalogRoute() {
|
|
const { vm } = useLoaderData<LoaderData>();
|
|
return <IntegrationsCatalogPage vm={vm} embedded />;
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|
|
|
|
export const handle = { moduleKey: INTEGRATIONS_MODULE_KEY };
|