242 lines
9.3 KiB
TypeScript
242 lines
9.3 KiB
TypeScript
import { useTranslation } from "react-i18next";
|
|
import { FeatherPhoneCall } from "@subframe/core";
|
|
import type {
|
|
TelephonyCallListRow,
|
|
TelephonyCallListVM,
|
|
TelephonyCallStatus,
|
|
TelephonyMatchConfidence,
|
|
} 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 { EmptyState } from "~/ui/components/EmptyState";
|
|
import { LinkButton } from "~/ui/components/LinkButton";
|
|
import { Table } from "~/ui/components/Table";
|
|
import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageShell } from "../shared/PageShell";
|
|
import { RefreshIconButton } from "../shared/RefreshIconButton";
|
|
|
|
export type TelephonyCallsActions = {
|
|
readonly onOpenCall: (callId: string, detailHref: string) => void;
|
|
readonly onRefresh?: () => void | Promise<void>;
|
|
readonly onRetry?: () => void | Promise<void>;
|
|
};
|
|
|
|
export type TelephonyCallsPageProps = {
|
|
readonly vm: TelephonyCallListVM;
|
|
readonly actions: TelephonyCallsActions;
|
|
readonly busy?: boolean;
|
|
};
|
|
|
|
type BadgeTone = "subtle-positive" | "subtle-warning" | "subtle-negative" | "subtle-information" | "subtle-neutral";
|
|
|
|
function callStatusTone(status: TelephonyCallStatus): BadgeTone {
|
|
switch (status) {
|
|
case "answered":
|
|
case "completed":
|
|
return "subtle-positive";
|
|
case "ringing":
|
|
return "subtle-information";
|
|
case "missed":
|
|
case "failed":
|
|
return "subtle-negative";
|
|
case "unknown":
|
|
default:
|
|
return "subtle-neutral";
|
|
}
|
|
}
|
|
|
|
function matchTone(confidence: TelephonyMatchConfidence): BadgeTone {
|
|
if (confidence === "exact") return "subtle-positive";
|
|
if (confidence === "manual") return "subtle-information";
|
|
if (confidence === "soft") return "subtle-warning";
|
|
return "subtle-neutral";
|
|
}
|
|
|
|
function formatDuration(seconds: number | null, emptyLabel: string): string {
|
|
if (seconds === null) return emptyLabel;
|
|
const minutes = Math.floor(seconds / 60);
|
|
const remainder = seconds % 60;
|
|
return `${minutes}:${String(remainder).padStart(2, "0")}`;
|
|
}
|
|
|
|
function CallsShell({ actions, busy, children }: {
|
|
readonly actions: TelephonyCallsActions;
|
|
readonly busy: boolean;
|
|
readonly children: React.ReactNode;
|
|
}): React.JSX.Element {
|
|
const { t } = useTranslation();
|
|
return (
|
|
<PageShell uiId="ui.telephony.calls.page">
|
|
<PageHeader
|
|
icon={<FeatherPhoneCall className={PAGE_HEADER_ICON_CLASS} />}
|
|
title={t("telephony.calls.title")}
|
|
subtitle={t("telephony.calls.subtitle")}
|
|
actions={
|
|
actions.onRefresh ? (
|
|
<RefreshIconButton
|
|
busy={busy}
|
|
onRefresh={() => void actions.onRefresh?.()}
|
|
label={t("telephony.actions.refresh")}
|
|
dataUiId="ui.telephony.calls.refresh"
|
|
/>
|
|
) : undefined
|
|
}
|
|
/>
|
|
<PageBody>{children}</PageBody>
|
|
</PageShell>
|
|
);
|
|
}
|
|
|
|
function Counterpart({ row }: { readonly row: TelephonyCallListRow }): React.JSX.Element {
|
|
return (
|
|
<div className="flex min-w-[180px] flex-col gap-0.5">
|
|
<span className="text-body-default-medium-0 font-body-default-medium-0 text-text-neutral-primary">
|
|
{row.counterpartLabel ?? row.phoneNumber}
|
|
</span>
|
|
{row.counterpartLabel ? (
|
|
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">{row.phoneNumber}</span>
|
|
) : null}
|
|
{row.noteSnippet ? (
|
|
<span className="max-w-[320px] truncate text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">
|
|
{row.noteSnippet}
|
|
</span>
|
|
) : null}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function TelephonyCallsPage({ vm, actions, busy = false }: TelephonyCallsPageProps): React.JSX.Element {
|
|
const { t } = useTranslation();
|
|
const formatters = useLocaleFormatters();
|
|
|
|
if (vm.status === "idle" || vm.status === "loading") {
|
|
return (
|
|
<CallsShell actions={actions} busy={busy}>
|
|
<Alert variant="neutral" title={t("telephony.calls.loading.title")} description={t("telephony.calls.loading.body")} notDismissible />
|
|
</CallsShell>
|
|
);
|
|
}
|
|
|
|
if (vm.status === "error") {
|
|
return (
|
|
<CallsShell actions={actions} busy={busy}>
|
|
<EmptyState
|
|
outlined
|
|
variant="error"
|
|
title={t("telephony.calls.error.title")}
|
|
body={t(vm.error.messageKey)}
|
|
action={
|
|
actions.onRetry ? (
|
|
<Button variant="tertiary" onClick={() => void actions.onRetry?.()}>
|
|
{t("telephony.actions.retry")}
|
|
</Button>
|
|
) : undefined
|
|
}
|
|
/>
|
|
</CallsShell>
|
|
);
|
|
}
|
|
|
|
if (vm.status === "empty" || !vm.data) {
|
|
return (
|
|
<CallsShell actions={actions} busy={busy}>
|
|
<EmptyState outlined title={t("telephony.calls.unavailable.title")} body={t("telephony.calls.unavailable.body")} />
|
|
</CallsShell>
|
|
);
|
|
}
|
|
|
|
if (vm.data.rows.length === 0) {
|
|
return (
|
|
<CallsShell actions={actions} busy={busy}>
|
|
<EmptyState outlined title={t(vm.data.emptyHeadlineKey)} body={t(vm.data.emptyBodyKey)} />
|
|
</CallsShell>
|
|
);
|
|
}
|
|
|
|
return (
|
|
<CallsShell actions={actions} busy={busy}>
|
|
<Alert
|
|
variant="neutral"
|
|
title={t(vm.data.canReadAll ? "telephony.calls.scope.all.title" : "telephony.calls.scope.own.title")}
|
|
description={t(vm.data.canReadAll ? "telephony.calls.scope.all.body" : "telephony.calls.scope.own.body")}
|
|
notDismissible
|
|
/>
|
|
|
|
<div className="w-full overflow-x-auto rounded-md" data-ui-id="ui.telephony.calls.table">
|
|
<Table
|
|
header={
|
|
<Table.HeaderRow>
|
|
<Table.HeaderCell>{t("telephony.calls.columns.counterpart")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("telephony.calls.columns.direction")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("telephony.calls.columns.status")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("telephony.calls.columns.match")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("telephony.calls.columns.user")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("telephony.calls.columns.startedAt")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("telephony.calls.columns.duration")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("telephony.calls.columns.recording")}</Table.HeaderCell>
|
|
<Table.HeaderCell>{t("telephony.calls.columns.action")}</Table.HeaderCell>
|
|
</Table.HeaderRow>
|
|
}
|
|
>
|
|
{vm.data.rows.map((row) => (
|
|
<Table.Row key={row.id} data-call-id={row.id}>
|
|
<Table.Cell><Counterpart row={row} /></Table.Cell>
|
|
<Table.Cell>
|
|
<BadgeFeedback farbe={row.direction === "inbound" ? "subtle-information" : "subtle-neutral"} size="small">
|
|
{t(`telephony.call.direction.${row.direction}`)}
|
|
</BadgeFeedback>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<BadgeFeedback farbe={callStatusTone(row.status)} size="small">
|
|
{t(`telephony.call.status.${row.status}`)}
|
|
</BadgeFeedback>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<div className="flex min-w-[160px] flex-col gap-1">
|
|
<span className="text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-primary">
|
|
{row.matchedEntityLabel ?? t("telephony.common.notAssigned")}
|
|
</span>
|
|
<BadgeFeedback farbe={matchTone(row.matchConfidence)} size="small">
|
|
{t(`telephony.call.match.${row.matchConfidence}`)}
|
|
</BadgeFeedback>
|
|
</div>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<span className="whitespace-nowrap text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-secondary">
|
|
{row.boundUserLabel ?? t("telephony.common.notAssigned")}
|
|
</span>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<span className="whitespace-nowrap text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-secondary">
|
|
{formatters.dateTime(row.startedAt)}
|
|
</span>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<span className="whitespace-nowrap text-body-small-regular-0 font-body-small-regular-0 text-text-neutral-secondary">
|
|
{formatDuration(row.durationSeconds, t("telephony.common.notAvailable"))}
|
|
</span>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<BadgeFeedback farbe={row.hasRecording ? "subtle-positive" : "subtle-neutral"} size="small">
|
|
{t(row.hasRecording ? "telephony.calls.recording.available" : "telephony.calls.recording.unavailable")}
|
|
</BadgeFeedback>
|
|
</Table.Cell>
|
|
<Table.Cell>
|
|
<LinkButton
|
|
size="small"
|
|
onClick={() => actions.onOpenCall(row.id, row.detailHref)}
|
|
aria-label={t("telephony.calls.openAria", { number: row.phoneNumber })}
|
|
>
|
|
{t("telephony.actions.open")}
|
|
</LinkButton>
|
|
</Table.Cell>
|
|
</Table.Row>
|
|
))}
|
|
</Table>
|
|
</div>
|
|
</CallsShell>
|
|
);
|
|
}
|