Files
HiveGrid-Wireframes/03 Quellen und Referenzen/hivegrid-cloud/apps/web/app/components/screens/telephony/TelephonySettingsPage.tsx
T

466 lines
21 KiB
TypeScript

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<void>;
readonly onHealthCheck: (connectionId: string) => void | Promise<void>;
readonly onConnectHalloPetra?: (apiKey: string) => void | Promise<void>;
readonly onBindingChange: (bindingId: string, hivegridUserId: string | null) => void | Promise<void>;
readonly onDisconnect: (connectionId: string) => void | Promise<void>;
readonly onRetry?: () => void | Promise<void>;
};
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 (
<PageShell uiId="ui.telephony.settings.page">
{embedded ? null : (
<PageHeader
icon={<FeatherPhoneCall className={PAGE_HEADER_ICON_CLASS} />}
title={t("telephony.settings.title")}
subtitle={t("telephony.settings.subtitle")}
/>
)}
<PageBody>
{embedded ? (
<PageIntro uiId="ui.telephony.settings.intro">{t("telephony.settings.subtitle")}</PageIntro>
) : backHref ? (
<PageBackLink to={backHref} label={t("integrations.connections.back")} uiId="ui.telephony.settings.back" />
) : null}
{children}
</PageBody>
</PageShell>
);
}
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 (
<SettingsShell backHref={backHref} embedded={embedded}>
<Alert
variant="neutral"
title={t("telephony.settings.loading.title")}
description={t("telephony.settings.loading.body")}
notDismissible
/>
</SettingsShell>
);
}
if (vm.status === "error") {
return (
<SettingsShell backHref={backHref} embedded={embedded}>
<EmptyState
outlined
variant="error"
title={t("telephony.settings.error.title")}
body={t(vm.error.messageKey)}
action={
actions.onRetry ? (
<Button variant="tertiary" onClick={() => void actions.onRetry?.()}>
{t("telephony.actions.retry")}
</Button>
) : undefined
}
/>
</SettingsShell>
);
}
if (vm.status === "empty" || !vm.data) {
return (
<SettingsShell backHref={backHref} embedded={embedded}>
<EmptyState outlined title={t("telephony.settings.unavailable.title")} body={t("telephony.settings.unavailable.body")} />
</SettingsShell>
);
}
const { connection, canManage, providerConfigured, connectHref, providerOptions, userOptions } = vm.data;
const currentProviderConfigured = connection
? (providerOptions.find((option) => option.provider === connection.provider)?.configured ?? providerConfigured)
: providerConfigured;
return (
<SettingsShell backHref={backHref} embedded={embedded}>
{connection?.provider === "aircall" ? (
<Alert
variant="information"
title={t("telephony.settings.companyAdmin.title")}
description={t("telephony.settings.companyAdmin.body")}
notDismissible
/>
) : null}
{!connection ? (
<div className="flex w-full flex-col gap-4" data-ui-id="ui.telephony.settings.providerSelection">
<div className="flex flex-col gap-1">
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">{t("telephony.settings.providers.title")}</span>
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">
{t("telephony.settings.providers.body")}
</span>
</div>
<div className="grid w-full grid-cols-1 gap-4 lg:grid-cols-2">
{providerOptions.map((option) => (
<Card key={option.provider} data-provider={option.provider}>
<div className="flex w-full items-start justify-between gap-4">
<div className="flex flex-col gap-1">
<span className="text-body-large-0 font-body-large-0 text-text-neutral-primary">{t(option.labelKey)}</span>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">
{t(`telephony.settings.providers.${option.provider}.body`)}
</span>
</div>
<BadgeFeedback farbe={option.connectionType === "api_key" ? "subtle-information" : "subtle-neutral"} size="small">
{t(`telephony.settings.providers.connectionType.${option.connectionType}`)}
</BadgeFeedback>
</div>
{!option.configured ? (
<Alert variant="warning" title={t("telephony.settings.providers.notConfigured")} notDismissible />
) : null}
{canManage ? (
<Button
icon={option.provider === "hallopetra" ? <FeatherKey /> : <FeatherLink />}
loading={busy}
disabled={busy || !option.configured}
onClick={() => {
if (option.provider === "hallopetra") setHalloPetraOpen(true);
else void actions.onConnect(option.connectHref);
}}
>
{t("telephony.actions.connectProvider", { provider: t(option.labelKey) })}
</Button>
) : null}
</Card>
))}
</div>
</div>
) : (
<>
<Card data-ui-id="ui.telephony.settings.connection">
<div className="flex w-full flex-wrap items-start justify-between gap-4">
<div className="flex flex-col gap-1">
<div className="flex flex-wrap items-center gap-2">
<span className="text-body-large-0 font-body-large-0 text-text-neutral-primary">{connection.label}</span>
<BadgeFeedback farbe={connectionTone(connection.status)}>{t(`telephony.connection.status.${connection.status}`)}</BadgeFeedback>
</div>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">
{connection.externalCompanyName ?? t("telephony.settings.connection.companyUnknown")}
</span>
</div>
{canManage ? (
<div className="flex flex-wrap items-center gap-2">
{connection.status === "error" || connection.status === "revoked" ? (
<Button
variant="secondary"
icon={<FeatherLink />}
loading={busy}
disabled={busy || !currentProviderConfigured}
onClick={() => {
if (connection.provider === "hallopetra") setHalloPetraOpen(true);
else void actions.onConnect(connectHref);
}}
>
{t("telephony.actions.reconnect")}
</Button>
) : null}
<Button
variant="tertiary"
icon={<FeatherRefreshCw />}
loading={busy}
disabled={busy}
onClick={() => void actions.onHealthCheck(connection.id)}
>
{t("telephony.actions.checkHealth")}
</Button>
<Button
variant="destructive"
icon={<FeatherTrash2 />}
disabled={busy}
onClick={() => setDisconnectOpen(true)}
data-testid="telephony-disconnect-trigger"
>
{t("telephony.actions.disconnect")}
</Button>
</div>
) : null}
</div>
<div className="grid w-full grid-cols-1 gap-3 sm:grid-cols-3">
<div className="flex flex-col gap-1 rounded-md border border-solid border-border-default bg-surface-neutral-primary px-4 py-3">
<span className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-tertiary">
{t("telephony.settings.connection.provider")}
</span>
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-primary">
{t(`telephony.provider.${connection.provider}`)}
</span>
</div>
<div className="flex flex-col gap-1 rounded-md border border-solid border-border-default bg-surface-neutral-primary px-4 py-3">
<span className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-tertiary">
{t("telephony.settings.connection.company")}
</span>
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-primary">
{connection.externalCompanyName ?? t("telephony.settings.connection.companyUnknown")}
</span>
</div>
<div className="flex flex-col gap-1 rounded-md border border-solid border-border-default bg-surface-neutral-primary px-4 py-3">
<span className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-tertiary">
{t("telephony.settings.connection.lastHealthCheck")}
</span>
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-primary">
{connection.lastHealthCheckAt
? formatters.dateTime(connection.lastHealthCheckAt)
: t("telephony.settings.connection.neverChecked")}
</span>
</div>
</div>
{connection.errorKey ? (
<Alert variant="error" title={t("telephony.settings.connection.errorTitle")} description={t(connection.errorKey)} notDismissible />
) : null}
</Card>
{connection.provider === "hallopetra" ? (
<Alert
variant="information"
title={t("telephony.settings.hallopetra.inboundOnly.title")}
description={t("telephony.settings.hallopetra.inboundOnly.body")}
notDismissible
/>
) : (
<div className="flex w-full flex-col gap-3" data-ui-id="ui.telephony.settings.bindings">
<div className="flex items-center gap-2">
<FeatherUsers className="text-body-large font-body-large text-icon-neutral-tertiary" />
<div className="flex flex-col gap-0.5">
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">{t("telephony.settings.bindings.title")}</span>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">
{t("telephony.settings.bindings.subtitle")}
</span>
</div>
</div>
{connection.bindings.length === 0 ? (
<EmptyState outlined title={t("telephony.settings.bindings.empty.title")} body={t("telephony.settings.bindings.empty.body")} />
) : (
<div className="w-full overflow-x-auto rounded-md">
<Table
header={
<Table.HeaderRow>
<Table.HeaderCell>{t("telephony.settings.bindings.columns.aircallUser")}</Table.HeaderCell>
<Table.HeaderCell>{t("telephony.settings.bindings.columns.hivegridUser")}</Table.HeaderCell>
<Table.HeaderCell>{t("telephony.settings.bindings.columns.match")}</Table.HeaderCell>
</Table.HeaderRow>
}
>
{connection.bindings.map((binding) => (
<Table.Row key={binding.id} data-binding-id={binding.id}>
<Table.Cell>
<div className="flex flex-col gap-0.5">
<span className="text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary">
{binding.providerName}
</span>
{binding.providerEmail ? (
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">
{binding.providerEmail}
</span>
) : null}
</div>
</Table.Cell>
<Table.Cell>
<Select
className="min-w-[240px] max-w-[320px]"
disabled={!canManage || busy}
value={binding.hivegridUserId ?? UNASSIGNED}
onValueChange={(value: string) =>
void actions.onBindingChange(binding.id, value === UNASSIGNED ? null : value)
}
aria-label={t("telephony.settings.bindings.assignAria", { name: binding.providerName })}
>
<Select.Item value={UNASSIGNED}>{t("telephony.settings.bindings.unassigned")}</Select.Item>
{userOptions.map((user) => (
<Select.Item key={user.id} value={user.id}>
{user.label} · {user.email}
</Select.Item>
))}
</Select>
</Table.Cell>
<Table.Cell>
<BadgeFeedback farbe={bindingTone(binding.match)} size="small">
{t(`telephony.binding.match.${binding.match}`)}
</BadgeFeedback>
</Table.Cell>
</Table.Row>
))}
</Table>
</div>
)}
</div>
)}
<Dialog open={disconnectOpen} onOpenChange={setDisconnectOpen}>
<Dialog.Content className="w-[420px] max-w-[90vw] p-6">
<div className="flex w-full flex-col gap-6" data-ui-id="ui.telephony.settings.disconnectConfirm">
<div className="flex flex-col gap-2">
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">
{t("telephony.settings.disconnect.title")}
</span>
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">
{t("telephony.settings.disconnect.body")}
</span>
</div>
<div className="flex w-full items-center justify-end gap-2">
<Button variant="tertiary" onClick={() => setDisconnectOpen(false)}>
{t("telephony.actions.cancel")}
</Button>
<Button
variant="destructive"
loading={busy}
disabled={busy}
onClick={() => void actions.onDisconnect(connection.id)}
>
{t("telephony.actions.disconnect")}
</Button>
</div>
</div>
</Dialog.Content>
</Dialog>
</>
)}
<Dialog open={hallopetraOpen} onOpenChange={setHalloPetraOpen}>
<Dialog.Content className="w-[620px] max-w-[90vw] p-6">
<div className="flex w-full flex-col gap-5" data-ui-id="ui.telephony.settings.hallopetraConnect">
<div className="flex flex-col gap-1">
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">{t("telephony.settings.hallopetra.title")}</span>
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">
{t("telephony.settings.hallopetra.body")}
</span>
</div>
{hallopetraSetup ? (
<>
<Alert
variant="success"
title={t("telephony.settings.hallopetra.connected.title")}
description={t("telephony.settings.hallopetra.connected.body")}
notDismissible
/>
<div className="flex flex-col gap-3 rounded-md border border-solid border-border-default bg-surface-neutral-primary p-4">
<span className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-secondary">
{t("telephony.settings.hallopetra.webhookUrl")}
</span>
<code className="break-all text-body-small-regular-150 text-text-neutral-primary">{hallopetraSetup.webhookUrl}</code>
<span className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-secondary">
{t("telephony.settings.hallopetra.webhookHeader")}
</span>
<code className="break-all text-body-small-regular-150 text-text-neutral-primary">
Authorization: Bearer {hallopetraSetup.webhookSecret}
</code>
</div>
<Alert variant="warning" title={t("telephony.settings.hallopetra.secretOnce")} notDismissible />
</>
) : (
<TextInput className="w-full" label={t("telephony.settings.hallopetra.apiKey")} showLabel type="password">
<TextInput.Input
type="password"
value={hallopetraApiKey}
onChange={(event) => setHalloPetraApiKey(event.target.value)}
autoComplete="off"
data-testid="hallopetra-api-key"
/>
</TextInput>
)}
<div className="flex w-full justify-end gap-2">
<Button variant="tertiary" disabled={busy} onClick={() => setHalloPetraOpen(false)}>
{t(hallopetraSetup ? "telephony.actions.close" : "telephony.actions.cancel")}
</Button>
{!hallopetraSetup ? (
<Button
icon={<FeatherKey />}
loading={busy}
disabled={busy || !hallopetraApiKey.trim()}
onClick={() => void actions.onConnectHalloPetra?.(hallopetraApiKey)}
>
{t("telephony.actions.connect")}
</Button>
) : null}
</div>
</div>
</Dialog.Content>
</Dialog>
</SettingsShell>
);
}