diff --git a/frontend/application/components/SupportChat.tsx b/frontend/application/components/SupportChat.tsx index 214701bc..858f5880 100644 --- a/frontend/application/components/SupportChat.tsx +++ b/frontend/application/components/SupportChat.tsx @@ -1,6 +1,8 @@ 'use client'; import { FormEvent, useEffect, useRef, useState } from 'react'; +type PersistedMessage = { role: 'user' | 'assistant'; content: string }; + const unavailable = 'پشتیبان هوشمند فعلاً در دسترس نیست. لطفاً کمی بعد دوباره تلاش کنید یا تیکت پشتیبانی ثبت کنید.'; const token = () => { try { @@ -39,6 +41,7 @@ export default function SupportChat() { const identity = useRef(''); const pending = useRef(null); const chatScrollRef = useRef(null); + const historyKey = useRef(''); useEffect(() => { identity.current = token(); @@ -70,6 +73,54 @@ export default function SupportChat() { } }, [rows, busy]); + useEffect(() => { + if (!conversationId || !config?.enabled) return; + const current = token(); + const key = `${current}:${conversationId}`; + if (historyKey.current === key) return; + historyKey.current = key; + const controller = new AbortController(); + pending.current = controller; + const loadHistory = async () => { + try { + const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL || '/api'}/support-chat/${encodeURIComponent(conversationId)}`, { + credentials: 'include', + headers: { 'X-Requested-With': 'XMLHttpRequest', ...(current ? { Authorization: `Bearer ${current}` } : {}) }, + signal: controller.signal + }); + if (!response.ok) throw new Error('history'); + const data = await response.json(); + if (!Array.isArray(data?.messages) || data.conversationId !== conversationId) throw new Error('history'); + const messages = data.messages.filter((message: unknown): message is PersistedMessage => { + if (!message || typeof message !== 'object') return false; + const value = message as Record; + return (value.role === 'user' || value.role === 'assistant') && typeof value.content === 'string'; + }); + const restored: { question: string; answer: string }[] = []; + for (let i = 0; i < messages.length; i += 1) { + const message = messages[i]; + if (message.role === 'user') { + const next = messages[i + 1]; + if (next?.role === 'assistant') i += 1; + restored.push({ question: message.content, answer: next?.role === 'assistant' ? next.content : '' }); + } else if (!restored.length || restored[restored.length - 1].answer) { + restored.push({ question: '', answer: message.content }); + } + } + if (!controller.signal.aborted && token() === current) setRows(restored); + } catch { + if (!controller.signal.aborted && token() === current) { + setRows([]); setConversationId(''); setContextLimited(false); + try { localStorage.removeItem('canina-support-conversation'); } catch {} + } + } finally { + if (pending.current === controller) pending.current = null; + } + }; + void loadHistory(); + return () => controller.abort(); + }, [conversationId, config?.enabled]); + async function send(event: FormEvent) { event.preventDefault(); if (busy || !message.trim() || !config?.enabled) return;