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

305 lines
12 KiB
TypeScript

import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import {
FeatherArrowLeft,
FeatherExternalLink,
FeatherPhoneCall,
FeatherSave,
FeatherUserCheck,
} from "@subframe/core";
import type {
TelephonyCallDetailData,
TelephonyCallDetailVM,
TelephonyCallStatus,
TelephonyMatchConfidence,
TelephonyMatchOption,
} 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 { EmptyState } from "~/ui/components/EmptyState";
import { Select } from "~/ui/components/Select";
import { TextArea } from "~/ui/components/TextArea";
import { PAGE_HEADER_ICON_CLASS, PageBody, PageHeader, PageShell } from "../shared/PageShell";
export type TelephonyCallDetailActions = {
readonly onBack: (listHref: string) => void;
readonly onOverrideMatch: (callId: string, option: TelephonyMatchOption | null) => void | Promise<void>;
readonly onSaveNote: (callId: string, noteMarkdown: string) => void | Promise<void>;
readonly onCallBack: (callId: string) => void;
readonly onRetry?: () => void | Promise<void>;
};
export type TelephonyCallDetailPageProps = {
readonly vm: TelephonyCallDetailVM;
readonly actions: TelephonyCallDetailActions;
readonly busy?: boolean;
};
const NO_MATCH = "__no_match__";
type BadgeTone = "subtle-positive" | "subtle-warning" | "subtle-negative" | "subtle-information" | "subtle-neutral";
function callStatusTone(status: TelephonyCallStatus): BadgeTone {
if (status === "answered" || status === "completed") return "subtle-positive";
if (status === "ringing") return "subtle-information";
if (status === "missed" || status === "failed") return "subtle-negative";
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 matchValue(call: TelephonyCallDetailData | null): string {
if (!call?.matchedEntityType || !call.matchedEntityId) return NO_MATCH;
return `${call.matchedEntityType}:${call.matchedEntityId}`;
}
function optionValue(option: TelephonyMatchOption): string {
return `${option.type}:${option.id}`;
}
function formatDuration(seconds: number | null, emptyLabel: string): string {
if (seconds === null) return emptyLabel;
const minutes = Math.floor(seconds / 60);
return `${minutes}:${String(seconds % 60).padStart(2, "0")}`;
}
function DetailShell({ children }: { readonly children: React.ReactNode }): React.JSX.Element {
const { t } = useTranslation();
return (
<PageShell uiId="ui.telephony.callDetail.page">
<PageHeader
icon={<FeatherPhoneCall className={PAGE_HEADER_ICON_CLASS} />}
title={t("telephony.callDetail.title")}
subtitle={t("telephony.callDetail.subtitle")}
/>
<PageBody>{children}</PageBody>
</PageShell>
);
}
function MetaItem({ label, value }: { readonly label: string; readonly value: React.ReactNode }): React.JSX.Element {
return (
<div className="flex flex-col gap-1">
<span className="text-body-small-medium-0 font-body-small-medium-0 text-text-neutral-tertiary">{label}</span>
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-primary">{value}</span>
</div>
);
}
export function TelephonyCallDetailPage({ vm, actions, busy = false }: TelephonyCallDetailPageProps): React.JSX.Element {
const { t } = useTranslation();
const formatters = useLocaleFormatters();
const call = vm.status === "ready" ? vm.data : null;
const [noteDraft, setNoteDraft] = useState(call?.noteMarkdown ?? "");
const [selectedMatch, setSelectedMatch] = useState(matchValue(call));
useEffect(() => {
setNoteDraft(call?.noteMarkdown ?? "");
setSelectedMatch(matchValue(call));
}, [call?.id, call?.matchedEntityId, call?.matchedEntityType, call?.noteMarkdown]);
if (vm.status === "idle" || vm.status === "loading") {
return (
<DetailShell>
<Alert
variant="neutral"
title={t("telephony.callDetail.loading.title")}
description={t("telephony.callDetail.loading.body")}
notDismissible
/>
</DetailShell>
);
}
if (vm.status === "error") {
return (
<DetailShell>
<EmptyState
outlined
variant="error"
title={t("telephony.callDetail.error.title")}
body={t(vm.error.messageKey)}
action={
actions.onRetry ? (
<Button variant="tertiary" onClick={() => void actions.onRetry?.()}>
{t("telephony.actions.retry")}
</Button>
) : undefined
}
/>
</DetailShell>
);
}
if (vm.status === "empty" || !call) {
return (
<DetailShell>
<EmptyState outlined variant="no-results" title={t("telephony.callDetail.empty.title")} body={t("telephony.callDetail.empty.body")} />
</DetailShell>
);
}
const selectedOption = call.matchOptions.find((option) => optionValue(option) === selectedMatch) ?? null;
const noteChanged = noteDraft !== call.noteMarkdown;
const matchChanged = selectedMatch !== matchValue(call);
return (
<DetailShell>
<div className="flex w-full flex-wrap items-center justify-between gap-3">
<Button variant="tertiary" icon={<FeatherArrowLeft />} onClick={() => actions.onBack(call.listHref)}>
{t("telephony.actions.backToCalls")}
</Button>
<div className="flex flex-wrap items-center gap-2">
<BadgeFeedback farbe={call.direction === "inbound" ? "subtle-information" : "subtle-neutral"}>
{t(`telephony.call.direction.${call.direction}`)}
</BadgeFeedback>
<BadgeFeedback farbe={callStatusTone(call.status)}>{t(`telephony.call.status.${call.status}`)}</BadgeFeedback>
{call.canCallBack ? (
<Button icon={<FeatherPhoneCall />} onClick={() => actions.onCallBack(call.id)}>
{t("telephony.actions.callBack")}
</Button>
) : null}
</div>
</div>
<div className="grid w-full grid-cols-1 gap-4 lg:grid-cols-2">
<Card data-ui-id="ui.telephony.callDetail.summary">
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">{t("telephony.callDetail.summary.title")}</span>
<div className="grid w-full grid-cols-1 gap-4 sm:grid-cols-2">
<MetaItem label={t("telephony.callDetail.fields.from")} value={call.fromNumber} />
<MetaItem label={t("telephony.callDetail.fields.to")} value={call.toNumber} />
<MetaItem label={t("telephony.callDetail.fields.startedAt")} value={formatters.dateTime(call.startedAt)} />
<MetaItem
label={t("telephony.callDetail.fields.duration")}
value={formatDuration(call.durationSeconds, t("telephony.common.notAvailable"))}
/>
<MetaItem
label={t("telephony.callDetail.fields.user")}
value={call.boundUserLabel ?? t("telephony.common.notAssigned")}
/>
<MetaItem label={t("telephony.callDetail.fields.provider")} value={t(`telephony.provider.${call.provider}`)} />
</div>
</Card>
<Card data-ui-id="ui.telephony.callDetail.recording">
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">{t("telephony.callDetail.recording.title")}</span>
{call.recordingUrl ? (
<div className="flex flex-col gap-2">
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">
{t("telephony.callDetail.recording.body")}
</span>
<a
href={call.recordingUrl}
target="_blank"
rel="noreferrer"
className="inline-flex items-center gap-2 text-body-default-medium-0 font-body-default-medium-0 text-text-feedback-link-default underline"
data-ui-id="ui.telephony.callDetail.recordingLink"
>
<FeatherExternalLink className="text-body-default-regular-0 font-body-default-regular-0" />
{t("telephony.callDetail.recording.open")}
</a>
</div>
) : (
<Alert variant="neutral" description={t("telephony.callDetail.recording.unavailable")} notDismissible />
)}
</Card>
</div>
<Card data-ui-id="ui.telephony.callDetail.match">
<div className="flex w-full flex-wrap items-start justify-between gap-3">
<div className="flex items-start gap-2">
<FeatherUserCheck className="text-heading-3 font-heading-3 text-icon-neutral-tertiary" />
<div className="flex flex-col gap-1">
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">{t("telephony.callDetail.match.title")}</span>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">
{t("telephony.callDetail.match.body")}
</span>
</div>
</div>
<BadgeFeedback farbe={matchTone(call.matchConfidence)}>{t(`telephony.call.match.${call.matchConfidence}`)}</BadgeFeedback>
</div>
{call.canOverrideMatch ? (
<div className="flex w-full flex-wrap items-end gap-3">
<Select
className="max-w-[480px] flex-1"
showLabel
label={t("telephony.callDetail.match.label")}
value={selectedMatch}
disabled={busy}
onValueChange={setSelectedMatch}
>
<Select.Item value={NO_MATCH}>{t("telephony.callDetail.match.none")}</Select.Item>
{call.matchOptions.map((option) => (
<Select.Item key={optionValue(option)} value={optionValue(option)}>
{option.label} · {t(`telephony.entityType.${option.type}`)}
</Select.Item>
))}
</Select>
<Button
variant="secondary"
disabled={busy || !matchChanged}
loading={busy}
onClick={() => void actions.onOverrideMatch(call.id, selectedMatch === NO_MATCH ? null : selectedOption)}
>
{t("telephony.callDetail.match.apply")}
</Button>
</div>
) : (
<span className="text-body-default-regular-0 font-body-default-regular-0 text-text-neutral-primary">
{call.matchedEntityLabel ?? t("telephony.common.notAssigned")}
</span>
)}
</Card>
<Card data-ui-id="ui.telephony.callDetail.note">
<div className="flex w-full flex-col gap-1">
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">{t("telephony.callDetail.note.title")}</span>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-secondary">
{t("telephony.callDetail.note.markdownHint")}
</span>
</div>
{call.canEditNote ? (
<>
<TextArea className="max-w-none" showLabel label={t("telephony.callDetail.note.label")}>
<TextArea.Input
value={noteDraft}
onChange={(event) => setNoteDraft(event.currentTarget.value)}
placeholder={t("telephony.callDetail.note.placeholder")}
disabled={busy}
/>
</TextArea>
<div className="flex w-full justify-end">
<Button
icon={<FeatherSave />}
disabled={busy || !noteChanged}
loading={busy}
onClick={() => void actions.onSaveNote(call.id, noteDraft)}
>
{t("telephony.callDetail.note.save")}
</Button>
</div>
</>
) : call.noteMarkdown ? (
<pre className="w-full whitespace-pre-wrap font-body-default-regular-0 text-body-default-regular-0 text-text-neutral-primary">
{call.noteMarkdown}
</pre>
) : (
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-tertiary">
{t("telephony.callDetail.note.empty")}
</span>
)}
</Card>
</DetailShell>
);
}