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

164 lines
6.6 KiB
TypeScript

import { useEffect, useState } from "react";
import { useTranslation } from "react-i18next";
import { FeatherPhoneCall } from "@subframe/core";
import type { ClickToCallMode, ClickToCallNumber, ClickToCallVM } from "@hivegrid/view-models";
import { Alert } from "~/ui/components/Alert";
import { BadgeFeedback } from "~/ui/components/BadgeFeedback";
import { Button } from "~/ui/components/Button";
import { Dialog } from "~/ui/components/Dialog";
import { EmptyState } from "~/ui/components/EmptyState";
export type ClickToCallActions = {
readonly onOpenChange: (open: boolean) => void;
readonly onCall: (number: ClickToCallNumber, mode: Exclude<ClickToCallMode, "disabled">) => void | Promise<void>;
};
export type ClickToCallDialogProps = {
readonly open: boolean;
readonly vm: ClickToCallVM;
readonly actions: ClickToCallActions;
readonly busy?: boolean;
};
export type ClickToCallEntryPointProps = {
readonly vm: ClickToCallVM;
readonly onCall: ClickToCallActions["onCall"];
readonly busy?: boolean;
readonly onOpenChange?: (open: boolean) => void;
/**
* Look of the trigger. It defaults to the solid button of the detail headers;
* a bar of equal actions (WF-P8 S02 quick actions) passes "tertiary" so the
* call does not shout over the five buttons next to it.
*/
readonly variant?: "primary" | "tertiary";
};
function preferredNumber(numbers: readonly ClickToCallNumber[]): ClickToCallNumber | null {
return numbers.find((number) => number.primary) ?? numbers[0] ?? null;
}
export function ClickToCallDialog({ open, vm, actions, busy = false }: ClickToCallDialogProps): React.JSX.Element {
const { t } = useTranslation();
const [selectedId, setSelectedId] = useState(preferredNumber(vm.numbers)?.id ?? null);
useEffect(() => {
if (open) setSelectedId(preferredNumber(vm.numbers)?.id ?? null);
}, [open, vm.numbers]);
const selectedNumber = vm.numbers.find((number) => number.id === selectedId) ?? preferredNumber(vm.numbers);
const disabled = vm.mode === "disabled" || !selectedNumber;
return (
<Dialog open={open} onOpenChange={actions.onOpenChange}>
<Dialog.Content className="w-[520px] max-w-[90vw] p-6" aria-label={t("telephony.clickToCall.title")}>
<div className="flex w-full flex-col gap-6" data-ui-id="ui.telephony.clickToCall.dialog">
<div className="flex flex-col gap-1">
<span className="text-heading-3 font-heading-3 text-text-neutral-primary">{t("telephony.clickToCall.title")}</span>
<span className="text-body-default-regular-150 font-body-default-regular-150 text-text-neutral-secondary">
{t(vm.requiresSelection ? "telephony.clickToCall.selectBody" : "telephony.clickToCall.confirmBody")}
</span>
</div>
{vm.reasonKey ? (
<Alert
variant={vm.mode === "disabled" ? "error" : "warning"}
title={t(vm.mode === "disabled" ? "telephony.clickToCall.unavailableTitle" : "telephony.clickToCall.fallbackTitle")}
description={t(vm.reasonKey)}
notDismissible
/>
) : null}
{vm.numbers.length === 0 ? (
<EmptyState
variant="no-results"
outlined
title={t("telephony.clickToCall.empty.title")}
body={t("telephony.clickToCall.empty.body")}
/>
) : (
<div className="flex w-full flex-col gap-2" role="radiogroup" aria-label={t("telephony.clickToCall.numberGroupLabel")}>
{vm.numbers.map((number) => {
const selected = number.id === selectedNumber?.id;
return (
<Button
key={number.id}
variant={selected ? "secondary" : "tertiary"}
className="h-auto w-full justify-start py-3"
onClick={() => setSelectedId(number.id)}
role="radio"
aria-checked={selected}
aria-label={t("telephony.clickToCall.selectNumber", { label: number.label, number: number.e164 })}
data-number-id={number.id}
>
<span className="flex w-full items-center justify-between gap-3">
<span className="flex flex-col items-start gap-0.5">
<span>{number.label}</span>
<span className="text-body-small-regular-150 font-body-small-regular-150 text-text-neutral-tertiary">
{number.e164}
</span>
</span>
{number.primary ? (
<BadgeFeedback farbe="subtle-information" size="small">
{t("telephony.clickToCall.primary")}
</BadgeFeedback>
) : null}
</span>
</Button>
);
})}
</div>
)}
<div className="flex w-full items-center justify-end gap-2">
<Button variant="tertiary" disabled={busy} onClick={() => actions.onOpenChange(false)}>
{t("telephony.actions.cancel")}
</Button>
<Button
icon={<FeatherPhoneCall />}
loading={busy}
disabled={busy || disabled}
onClick={() => {
if (selectedNumber && vm.mode !== "disabled") void actions.onCall(selectedNumber, vm.mode);
}}
>
{t(vm.mode === "tel-fallback" ? "telephony.clickToCall.openPhoneApp" : "telephony.clickToCall.startCall")}
</Button>
</div>
</div>
</Dialog.Content>
</Dialog>
);
}
/**
* Reusable detail-screen entry point. It owns presentation state only; the
* route decides how a connected provider or tel-fallback call is started.
*/
export function ClickToCallEntryPoint({ vm, onCall, busy = false, onOpenChange, variant = "primary" }: ClickToCallEntryPointProps): React.JSX.Element {
const { t } = useTranslation();
const [open, setOpen] = useState(false);
const changeOpen = (nextOpen: boolean) => {
setOpen(nextOpen);
onOpenChange?.(nextOpen);
};
return (
<>
<Button
variant={variant === "tertiary" ? "tertiary" : undefined}
size={variant === "tertiary" ? "small" : undefined}
icon={<FeatherPhoneCall />}
disabled={busy}
loading={busy}
onClick={() => changeOpen(true)}
data-ui-id="ui.telephony.clickToCall.open"
>
{t("telephony.clickToCall.open")}
</Button>
<ClickToCallDialog open={open} vm={vm} busy={busy} actions={{ onOpenChange: changeOpen, onCall }} />
</>
);
}