feat(support-chat): persist conversations and analytics
Deploy Canina / deploy (push) Failing after 28s

This commit is contained in:
devops committed 2026-10-15 07:28:02 +00:00
1 parent 59ab154d0f
commit def903734f
7 files changed
+97 -10

No files matched your search

@@ -34,15 +34,19 @@ export default function SupportChat() {
const [message, setMessage] = useState('');
const [busy, setBusy] = useState(false);
const [rows, setRows] = useState<{ question: string; answer: string }[]>([]);
const [conversationId, setConversationId] = useState('');
const [contextLimited, setContextLimited] = useState(false);
const identity = useRef('');
const pending = useRef<AbortController | null>(null);
const chatScrollRef = useRef<HTMLDivElement | null>(null);
useEffect(() => {
identity.current = token();
try { const saved = localStorage.getItem('canina-support-conversation'); if (saved) setConversationId(saved); } catch {}
const check = () => {
if (identity.current !== token()) {
identity.current = token();
try { const saved = localStorage.getItem('canina-support-conversation'); if (saved) setConversationId(saved); } catch {}
pending.current?.abort();
setRows([]);
setMessage('');
@@ -77,6 +81,7 @@ export default function SupportChat() {
const question = message.trim();
setMessage('');
setBusy(true);
setRows((r) => [...r.slice(-9), { question, answer: 'در حال بررسی…' }]);
const controller = new AbortController();
pending.current = controller;
let answer = unavailable;
@@ -89,7 +94,7 @@ export default function SupportChat() {
...(current ? { Authorization: `Bearer ${current}` } : {})
},
credentials: 'include',
body: JSON.stringify({ message: question }),
body: JSON.stringify({ message: question, ...(conversationId ? { conversationId } : {}), entryPath: window.location.pathname }),
signal: AbortSignal.any([controller.signal, AbortSignal.timeout(50000)])
});
if (response.status === 401) {
@@ -97,12 +102,14 @@ export default function SupportChat() {
} else if (response.ok) {
const data = await response.json();
if (typeof data.answer === 'string') answer = data.answer;
if (typeof data.conversationId === 'string') { setConversationId(data.conversationId); try { localStorage.setItem('canina-support-conversation', data.conversationId); } catch {} }
setContextLimited(data.contextLimited === true);
}
} catch {
// safe fallback kept
}
if (!controller.signal.aborted && token() === current) {
setRows((r) => [...r.slice(-9), { question, answer }]);
setRows((r) => { const next = [...r]; const i = next.length - 1; if (i >= 0 && next[i].question === question && next[i].answer === 'در حال بررسی…') next[i] = { question, answer }; return next; });
}
if (pending.current === controller) setBusy(false);
}
@@ -156,6 +163,7 @@ export default function SupportChat() {
<span className="text-[10px] bg-canina-gold/20 text-canina-gold px-1.5 py-0.5 rounded border border-canina-gold/30">AI</span>
</h3>
<p className="text-[11px] text-white/70">راهنمای محصولات و سلامت پت</p>
<button type="button" className="text-[11px] text-canina-gold hover:underline" onClick={() => { setRows([]); setConversationId(''); setContextLimited(false); try { localStorage.removeItem('canina-support-conversation'); } catch {} }}>گفتگوی جدید</button>
</div>
</div>
<button
@@ -180,7 +188,7 @@ export default function SupportChat() {
<svg className="h-4 w-4 text-canina-blue shrink-0 mt-0.5" fill="none" viewBox="0 0 24 24" stroke="currentColor">
<path strokeLinecap="round" strokeLinejoin="round" strokeWidth={2} d="M13 16h-1v-4h-1m1-4h.01M21 12c0 4.418-4.03 8-9 8a9.863 9.863 0 01-4.255-.949L3 20l1.395-3.72C3.512 15.042 3 13.574 3 12c0-4.418 4.03-8 9-8s9 3.582 9 8z" />
</svg>
<span>پشتیبان هوشمند کنینا · هر پیام مستقل است. اطلاعات تغذیه و مکمل‌ها جایگزین تشخیص دامپزشک نیست.</span>
<span>پشتیبان هوشمند کنینا · پیام‌ها در همین گفت‌وگو حفظ می‌شوند. اطلاعات تغذیه و مکمل‌ها جایگزین تشخیص دامپزشک نیست.</span>
</div>
{rows.length === 0 && (
@@ -252,6 +260,7 @@ export default function SupportChat() {
</button>
</form>
{contextLimited && <div role="status" className="mb-2 rounded bg-amber-50 p-2 text-[11px] text-amber-800">برای ادامه دقیق‌تر، گفتگوی جدیدی شروع کنید یا با پشتیبانی تماس بگیرید.</div>}
<div className="mt-2 flex items-center justify-between px-1 text-[11px] text-medical-gray-500">
<span>Shift+Enter خط جدید</span>
<a