import { useState } from "react"; import { useTranslation } from "react-i18next"; import { FeatherKey, FeatherLink, FeatherPhoneCall, FeatherRefreshCw, FeatherTrash2, FeatherUsers } from "@subframe/core"; import type { TelephonyConnectionStatus, TelephonySettingsVM, TelephonyUserBindingRow, } from "@hivegrid/view-models"; import { useLocaleFormatters } from "~/locale/format"; import { Alert } from "~/ui/components/Alert"; import { BadgeFeedback } from "~/ui/components/BadgeFeedback"; import { Button } from "~/ui/components/Button"; import { Card } from "~/ui/components/Card"; import { Dialog } from "~/ui/components/Dialog"; import { EmptyState } from "~/ui/components/EmptyState"; import { Select } from "~/ui/components/Select"; import { Table } from "~/ui/components/Table"; import { TextInput } from "~/ui/components/TextInput"; import { PAGE_HEADER_ICON_CLASS, PageBackLink, PageBody, PageHeader, PageIntro, PageShell } from "../shared/PageShell"; export type TelephonySettingsActions = { readonly onConnect: (connectHref: string) => void | Promise; readonly onHealthCheck: (connectionId: string) => void | Promise; readonly onConnectHalloPetra?: (apiKey: string) => void | Promise; readonly onBindingChange: (bindingId: string, hivegridUserId: string | null) => void | Promise; readonly onDisconnect: (connectionId: string) => void | Promise; readonly onRetry?: () => void | Promise; }; export type TelephonySettingsPageProps = { readonly vm: TelephonySettingsVM; readonly actions: TelephonySettingsActions; readonly busy?: boolean; /** Path back to the integrations catalog (settings/integrations). */ readonly backHref?: string; /** Inside the Erweiterungen hub (AK-SET-01) the hub owns the header. */ readonly embedded?: boolean; readonly hallopetraSetup?: { readonly webhookUrl: string; readonly webhookSecret: string } | null; }; const UNASSIGNED = "__unassigned__"; type BadgeTone = "subtle-positive" | "subtle-warning" | "subtle-negative" | "subtle-information" | "subtle-neutral"; function connectionTone(status: TelephonyConnectionStatus): BadgeTone { switch (status) { case "active": return "subtle-positive"; case "connecting": return "subtle-information"; case "error": return "subtle-negative"; case "revoked": default: return "subtle-warning"; } } function bindingTone(match: TelephonyUserBindingRow["match"]): BadgeTone { if (match === "email") return "subtle-positive"; if (match === "manual") return "subtle-information"; return "subtle-warning"; } function SettingsShell({ backHref, embedded = false, children, }: { readonly backHref?: string; /** Inside the Erweiterungen hub (AK-SET-01) the hub owns the header. */ readonly embedded?: boolean; readonly children: React.ReactNode; }): React.JSX.Element { const { t } = useTranslation(); return ( {embedded ? null : ( } title={t("telephony.settings.title")} subtitle={t("telephony.settings.subtitle")} /> )} {embedded ? ( {t("telephony.settings.subtitle")} ) : backHref ? ( ) : null} {children} ); } export function TelephonySettingsPage({ vm, actions, busy = false, backHref, embedded = false, hallopetraSetup = null, }: TelephonySettingsPageProps): React.JSX.Element { const { t } = useTranslation(); const formatters = useLocaleFormatters(); const [disconnectOpen, setDisconnectOpen] = useState(false); const [hallopetraOpen, setHalloPetraOpen] = useState(false); const [hallopetraApiKey, setHalloPetraApiKey] = useState(""); if (vm.status === "idle" || vm.status === "loading") { return ( ); } if (vm.status === "error") { return ( void actions.onRetry?.()}> {t("telephony.actions.retry")} ) : undefined } /> ); } if (vm.status === "empty" || !vm.data) { return ( ); } const { connection, canManage, providerConfigured, connectHref, providerOptions, userOptions } = vm.data; const currentProviderConfigured = connection ? (providerOptions.find((option) => option.provider === connection.provider)?.configured ?? providerConfigured) : providerConfigured; return ( {connection?.provider === "aircall" ? ( ) : null} {!connection ? (
{t("telephony.settings.providers.title")} {t("telephony.settings.providers.body")}
{providerOptions.map((option) => (
{t(option.labelKey)} {t(`telephony.settings.providers.${option.provider}.body`)}
{t(`telephony.settings.providers.connectionType.${option.connectionType}`)}
{!option.configured ? ( ) : null} {canManage ? ( ) : null}
))}
) : ( <>
{connection.label} {t(`telephony.connection.status.${connection.status}`)}
{connection.externalCompanyName ?? t("telephony.settings.connection.companyUnknown")}
{canManage ? (
{connection.status === "error" || connection.status === "revoked" ? ( ) : null}
) : null}
{t("telephony.settings.connection.provider")} {t(`telephony.provider.${connection.provider}`)}
{t("telephony.settings.connection.company")} {connection.externalCompanyName ?? t("telephony.settings.connection.companyUnknown")}
{t("telephony.settings.connection.lastHealthCheck")} {connection.lastHealthCheckAt ? formatters.dateTime(connection.lastHealthCheckAt) : t("telephony.settings.connection.neverChecked")}
{connection.errorKey ? ( ) : null}
{connection.provider === "hallopetra" ? ( ) : (
{t("telephony.settings.bindings.title")} {t("telephony.settings.bindings.subtitle")}
{connection.bindings.length === 0 ? ( ) : (
{t("telephony.settings.bindings.columns.aircallUser")}{t("telephony.settings.bindings.columns.hivegridUser")}{t("telephony.settings.bindings.columns.match")} } > {connection.bindings.map((binding) => (
{binding.providerName} {binding.providerEmail ? ( {binding.providerEmail} ) : null}
{t(`telephony.binding.match.${binding.match}`)}
))}
)}
)}
{t("telephony.settings.disconnect.title")} {t("telephony.settings.disconnect.body")}
)}
{t("telephony.settings.hallopetra.title")} {t("telephony.settings.hallopetra.body")}
{hallopetraSetup ? ( <>
{t("telephony.settings.hallopetra.webhookUrl")} {hallopetraSetup.webhookUrl} {t("telephony.settings.hallopetra.webhookHeader")} Authorization: Bearer {hallopetraSetup.webhookSecret}
) : ( setHalloPetraApiKey(event.target.value)} autoComplete="off" data-testid="hallopetra-api-key" /> )}
{!hallopetraSetup ? ( ) : null}
); }