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 | 32x 17x 17x 17x 32x 32x 32x 32x 32x 32x 12x 32x 32x 32x 32x 3x 32x 3x 3x 3x 3x 32x 1x 1x 1x 1x 1x 1x 32x 2x 1x 1x 32x 4x 4x 32x 6x 4x 13x | import { useState } from "react";
import { Send, FileText, AlertTriangle } from "lucide-react";
import type { WaConversation, WaTemplate } from "../../lib/api/whatsapp";
import { TemplateParameterFields } from "./TemplateParameterFields";
import {
parseTemplateComponents,
extractParameterSlots,
buildSendComponents,
isApprovedTemplate,
} from "./template-utils";
type ReplyBoxProps = {
conversation: WaConversation;
templates: WaTemplate[];
onSendText: (message: string) => void;
onSendTemplate: (
templateName: string,
language: string,
components?: unknown[],
) => void;
isSending: boolean;
};
function isWindowOpen(lastCustomerMessageAt: string | null): boolean {
if (!lastCustomerMessageAt) return false;
const windowEnd = new Date(lastCustomerMessageAt);
windowEnd.setHours(windowEnd.getHours() + 24);
return new Date() < windowEnd;
}
export function ReplyBox({
conversation,
templates,
onSendText,
onSendTemplate,
isSending,
}: ReplyBoxProps) {
const [message, setMessage] = useState("");
const [selectedTemplate, setSelectedTemplate] = useState("");
const [paramValues, setParamValues] = useState<Record<string, string>>({});
const windowOpen = isWindowOpen(conversation.lastCustomerMessageAt);
const approvedTemplates = templates.filter(isApprovedTemplate);
const currentTemplate = approvedTemplates.find(
(t) => t.name === selectedTemplate,
);
// Detect if current template has variables
const templateComponents = currentTemplate
? parseTemplateComponents(currentTemplate.components)
: [];
const slots = extractParameterSlots(templateComponents);
const hasVariables = slots.length > 0;
const allParamsFilled =
!hasVariables ||
slots.every((s) => paramValues[`${s.componentType}_${s.index}`]?.trim());
const handleSendText = () => {
const text = message.trim();
Iif (!text) return;
onSendText(text);
setMessage("");
};
const handleSendTemplate = () => {
Iif (!selectedTemplate || !currentTemplate) return;
Iif (hasVariables && !allParamsFilled) return;
const components = hasVariables
? buildSendComponents(templateComponents, paramValues)
: undefined;
onSendTemplate(currentTemplate.name, currentTemplate.language, components);
setSelectedTemplate("");
setParamValues({});
};
const handleKeyDown = (e: React.KeyboardEvent) => {
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
handleSendText();
}
};
const handleTemplateChange = (name: string) => {
setSelectedTemplate(name);
setParamValues({});
};
return (
<div className="border-t border-border-default bg-background-default">
{!windowOpen && (
<div className="flex items-center gap-2 px-4 py-2 bg-amber-50 dark:bg-amber-900/20 border-b border-amber-200 dark:border-amber-800">
<AlertTriangle className="h-4 w-4 text-amber-600 flex-shrink-0" />
<p className="text-xs text-amber-700 dark:text-amber-400">
24-hour messaging window is closed. You can only send approved
templates.
</p>
</div>
)}
{windowOpen ? (
/* Text message input */
<div className="p-3 flex items-end gap-2">
<div className="flex-1">
<textarea
value={message}
onChange={(e) => setMessage(e.target.value)}
onKeyDown={handleKeyDown}
placeholder="Type a message..."
rows={1}
className="w-full resize-none rounded-lg border border-border-default bg-background-default px-3 py-2 text-sm placeholder:text-foreground-subtle focus:outline-none focus:ring-2 focus:ring-whatsapp-green focus:border-transparent min-h-10 max-h-[120px]"
/>
</div>
<button
type="button"
onClick={handleSendText}
disabled={!message.trim() || isSending}
aria-label="Send message"
className="flex-shrink-0 rounded-full bg-whatsapp-green p-2.5 text-foreground-inverse hover:bg-whatsapp-green-hover disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
>
<Send className="h-4 w-4" />
</button>
</div>
) : (
/* Template picker with parameter fields */
<div className="p-3 space-y-3">
<div className="flex items-center gap-2">
<FileText className="h-4 w-4 text-foreground-muted flex-shrink-0" />
<select
value={selectedTemplate}
onChange={(e) => handleTemplateChange(e.target.value)}
className="flex-1 rounded-lg border border-border-default bg-background-default px-3 py-2 text-sm focus:outline-none focus:ring-2 focus:ring-whatsapp-green"
>
<option value="">Select a template...</option>
{approvedTemplates.map((t) => (
<option key={t.id} value={t.name}>
{t.name} ({t.language})
</option>
))}
</select>
<button
type="button"
onClick={handleSendTemplate}
disabled={
!selectedTemplate || isSending || (hasVariables && !allParamsFilled)
}
aria-label="Send template"
className="flex-shrink-0 rounded-full bg-whatsapp-green p-2.5 text-foreground-inverse hover:bg-whatsapp-green-hover disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
>
<Send className="h-4 w-4" />
</button>
</div>
{/* Parameter fields + preview when template has variables */}
{currentTemplate && (
<div className="max-h-64 overflow-y-auto">
<TemplateParameterFields
template={currentTemplate}
paramValues={paramValues}
onParamChange={(key, value) =>
setParamValues((prev) => ({ ...prev, [key]: value }))
}
/>
</div>
)}
</div>
)}
</div>
);
}
|