Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 | 28x 27x 27x 1x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 76x 1x 76x 1x 76x 1x 1x 1x 76x 28x 1x 1x 1x 2x 2x 3x 1x 1x 1x 3x 2x 1x 1x | import { MAX_WHATSAPP_RECIPIENTS_PER_PRO } from "@interioring/utils/constants/whatsapp";
import { MessageCircle, Plus, Star, Trash2, ShieldCheck, ShieldAlert } from "lucide-react";
import { useState } from "react";
import {
useDeleteWhatsAppRecipient,
useUpdateWhatsAppRecipient,
useWhatsAppRecipients,
} from "../../../hooks/queries/useWhatsAppRecipientQueries";
import type { WhatsAppRecipient } from "../../../lib/api";
import { Button } from "../../ui/button";
import { ConfirmDialog } from "../../ui/confirm-dialog";
import { SectionCard } from "../SectionCard";
import { AddRecipientDialog } from "./AddRecipientDialog";
import { EditRecipientDialog } from "./EditRecipientDialog";
import { OtpVerifyDialog } from "./OtpVerifyDialog";
interface Props {
proId: string;
canEdit?: boolean;
}
function formatNumber(e164: string): string {
if (e164.startsWith("+91") && e164.length === 13) {
const n = e164.slice(3);
return `+91 ${n.slice(0, 5)} ${n.slice(5)}`;
}
return e164;
}
export function WhatsAppRecipientsCard({ proId, canEdit = true }: Props) {
const { data, isLoading } = useWhatsAppRecipients(proId);
const updateMut = useUpdateWhatsAppRecipient(proId);
const deleteMut = useDeleteWhatsAppRecipient(proId);
const [showAdd, setShowAdd] = useState(false);
const [verifyId, setVerifyId] = useState<number | null>(null);
const [editId, setEditId] = useState<number | null>(null);
const [confirmDeleteId, setConfirmDeleteId] = useState<number | null>(null);
const recipients = data?.recipients ?? [];
const max = data?.maxAllowed ?? MAX_WHATSAPP_RECIPIENTS_PER_PRO;
const atCap = recipients.length >= max;
const handleToggleNotifications = async (r: WhatsAppRecipient) => {
await updateMut.mutateAsync({
id: r.id,
patch: { notificationsEnabled: !r.notificationsEnabled },
});
};
const handleMakePrimary = async (r: WhatsAppRecipient) => {
await updateMut.mutateAsync({ id: r.id, patch: { isPrimary: true } });
};
const handleDelete = async () => {
Iif (confirmDeleteId === null) return;
await deleteMut.mutateAsync(confirmDeleteId);
setConfirmDeleteId(null);
};
return (
<SectionCard
title="WhatsApp Recipients for Inquiry Alerts"
icon={<MessageCircle className="h-5 w-5" />}
isEmpty={!isLoading && recipients.length === 0}
className="xl:col-span-2"
>
<div className="flex flex-col gap-3">
{isLoading && (
<p className="text-sm text-foreground-muted">Loading…</p>
)}
{recipients.map((r) => (
<div
key={r.id}
className="flex items-center justify-between gap-3 rounded-md border border-border-default bg-background-elevated p-3"
>
<div className="flex flex-col gap-1 min-w-0">
<div className="flex items-center gap-2 flex-wrap">
<span className="font-medium text-foreground-default truncate">
{r.label}
</span>
{r.isPrimary && (
<span className="inline-flex items-center gap-1 rounded-full bg-primary-100 px-2 py-0.5 text-xs font-semibold text-primary-700">
<Star className="h-3 w-3" /> Primary
</span>
)}
{r.isVerified ? (
<span className="inline-flex items-center gap-1 rounded-full bg-success/10 px-2 py-0.5 text-xs font-semibold text-success">
<ShieldCheck className="h-3 w-3" /> Verified
</span>
) : (
<button
type="button"
className="inline-flex items-center gap-1 rounded-full bg-warning/10 px-2 py-0.5 text-xs font-semibold text-warning hover:underline"
onClick={() => setVerifyId(r.id)}
>
<ShieldAlert className="h-3 w-3" /> Verify now
</button>
)}
</div>
<span className="text-sm text-foreground-muted">
{formatNumber(r.number)}
</span>
</div>
{canEdit && (
<div className="flex items-center gap-2 shrink-0">
<label className="inline-flex items-center gap-1 text-xs cursor-pointer">
<input
type="checkbox"
checked={r.notificationsEnabled}
onChange={() => handleToggleNotifications(r)}
aria-label={
r.notificationsEnabled
? "Disable notifications"
: "Enable notifications"
}
/>
<span>Alerts</span>
</label>
{!r.isPrimary && r.isVerified && (
<Button
size="sm"
variant="ghost"
onClick={() => handleMakePrimary(r)}
>
Make primary
</Button>
)}
<Button
size="sm"
variant="ghost"
onClick={() => setEditId(r.id)}
>
Edit
</Button>
<Button
size="sm"
variant="ghost"
onClick={() => setConfirmDeleteId(r.id)}
aria-label={`Delete ${r.label}`}
>
<Trash2 className="h-4 w-4" />
</Button>
</div>
)}
</div>
))}
{canEdit && (
<div className="flex items-center gap-3 pt-2">
<Button
size="sm"
variant="outline"
disabled={atCap}
onClick={() => setShowAdd(true)}
title={
atCap
? `Cap reached — up to ${max} numbers per pro`
: undefined
}
>
<Plus className="h-4 w-4" /> Add recipient
</Button>
<span className="text-xs text-foreground-muted">
{recipients.length} of {max}
</span>
</div>
)}
</div>
{showAdd && (
<AddRecipientDialog
proId={proId}
onClose={() => setShowAdd(false)}
onCreated={(id) => {
setShowAdd(false);
setVerifyId(id);
}}
/>
)}
{verifyId !== null && (
<OtpVerifyDialog
proId={proId}
recipient={recipients.find((r) => r.id === verifyId)}
onClose={() => setVerifyId(null)}
/>
)}
{editId !== null && (
<EditRecipientDialog
proId={proId}
recipient={recipients.find((r) => r.id === editId)}
onClose={() => setEditId(null)}
/>
)}
<ConfirmDialog
open={confirmDeleteId !== null}
title="Remove recipient?"
description="This number will no longer receive new-inquiry WhatsApp alerts."
confirmLabel="Remove"
onConfirm={handleDelete}
onCancel={() => setConfirmDeleteId(null)}
/>
</SectionCard>
);
}
|