// AK-SET-01 Hub E — "Erweiterungen" bundles the integration catalog, mailboxes, // calendars, telephony, plugins and API keys. Was already the layout route for // the integrations area; it now also contributes the tab bar and stays the // "Erweiterungen" breadcrumb level (useBreadcrumbTrail reads loader titles per // match). import { Outlet, useLoaderData, useLocation, type LoaderFunctionArgs, type MetaFunction } from "react-router"; import { FeatherPlug } from "@subframe/core"; import { buildSettingsHubVM, type SettingsHubTabInput } from "@hivegrid/view-models"; import { SettingsHubShell } from "~/components/screens/settings/SettingsHubShell"; import { getTranslations } from "~/locale/i18next.server"; import UrlUtils from "~/utils/app/UrlUtils"; import { getTenantIdFromUrl } from "~/utils/services/.server/urlService"; import { getUserInfo } from "~/utils/session.server"; import { tenantHasModule } from "~/utils/connector/tenantConnector.server"; import { getAppConfiguration } from "~/utils/db/appConfiguration.db.server"; import { EMAIL_INBOX_MODULE_KEY } from "~/modules/emailInbox/routes/emailInboxRoutePaths"; import { INTEGRATIONS_MODULE_KEY } from "~/utils/integrations/integrationsRoutePaths"; import { TELEPHONY_MODULE_KEY } from "~/modules/telephony/routes/telephonyRoutePaths"; import { hasSettingsPermission, settingsHubBaseRedirect, settingsHubNotFound } from "./hub-access.server"; type LoaderData = { title: string; tabs: SettingsHubTabInput[]; }; export const loader = async ({ request, params }: LoaderFunctionArgs) => { const tenantId = await getTenantIdFromUrl(params); const [{ t }, userInfo, canReadIntegrations, canReadOwnEmail, canReadAllEmail, canConnectOwnCalendar, canAdminCalendar, canManageTelephony, canUsePlugins, canViewApiKeys] = await Promise.all([ getTranslations(request), getUserInfo(request), hasSettingsPermission(request, "integrations.read", tenantId), hasSettingsPermission(request, "email.read_own", tenantId), hasSettingsPermission(request, "email.read_all", tenantId), hasSettingsPermission(request, "calendar.connect_own", tenantId), hasSettingsPermission(request, "calendar.admin", tenantId), hasSettingsPermission(request, "telephony.manage", tenantId), hasSettingsPermission(request, "app.plugins.use", tenantId), hasSettingsPermission(request, "app.settings.apiKeys.view", tenantId), ]); const [integrationsEnabled, emailEnabled, telephonyEnabled, apiEnabled, appConfiguration] = await Promise.all([ tenantHasModule({ tenantId, userId: userInfo.userId, moduleKey: INTEGRATIONS_MODULE_KEY }), tenantHasModule({ tenantId, userId: userInfo.userId, moduleKey: EMAIL_INBOX_MODULE_KEY }), tenantHasModule({ tenantId, userId: userInfo.userId, moduleKey: TELEPHONY_MODULE_KEY }), tenantHasModule({ tenantId, userId: userInfo.userId, moduleKey: "api" }), getAppConfiguration({ request }), ]); const tabs: SettingsHubTabInput[] = []; if (canReadIntegrations && integrationsEnabled) { // The hub index href is a prefix of every sibling, but longest-prefix // matching in buildSettingsHubVM keeps sibling tabs winning on their own // routes — so the catalog tab stays lit on app detail pages // (…/integrations/) without swallowing email/calendar/… tabs. tabs.push({ id: "catalog", labelKey: "integrations.nav.title", href: UrlUtils.currentTenantUrl(params, "settings/integrations") }); } if (emailEnabled && (canReadOwnEmail || canReadAllEmail)) { tabs.push({ id: "email", labelKey: "emailInbox.settings.title", href: UrlUtils.currentTenantUrl(params, "settings/integrations/email") }); } if (integrationsEnabled && (canConnectOwnCalendar || canAdminCalendar)) { tabs.push({ id: "calendar", labelKey: "calendar.connections.title", href: UrlUtils.currentTenantUrl(params, "settings/integrations/calendar") }); } if (telephonyEnabled && canManageTelephony) { tabs.push({ id: "telephony", labelKey: "telephony.nav.title", href: UrlUtils.currentTenantUrl(params, "settings/integrations/telephony") }); } if (canUsePlugins) { tabs.push({ id: "plugins", labelKey: "settings.plugins.title", href: UrlUtils.currentTenantUrl(params, "settings/integrations/plugins") }); } if (apiEnabled && appConfiguration.app.features.tenantApiKeys && canViewApiKeys) { tabs.push({ id: "api", labelKey: "models.apiKey.plural", href: UrlUtils.currentTenantUrl(params, "settings/integrations/api") }); } if (tabs.length === 0) throw settingsHubNotFound(); const baseRedirect = settingsHubBaseRedirect(request, UrlUtils.currentTenantUrl(params, "settings/integrations"), tabs); if (baseRedirect) throw baseRedirect; return { title: `${t("settings.hubs.integrations.title")} | ${process.env.APP_NAME}`, tabs } satisfies LoaderData; }; export const meta: MetaFunction = ({ data }) => [{ title: data?.title }]; export default function IntegrationsHubRoute() { const { tabs } = useLoaderData(); const location = useLocation(); const vm = buildSettingsHubVM({ hubId: "integrations", titleKey: "settings.hubs.integrations.title", subtitleKey: "settings.hubs.integrations.subtitle", tabs, currentPath: location.pathname, }); return ( }> ); }