feat: chat send via WebSocket instead of REST
All checks were successful
Deploy Web Client / deploy (push) Successful in 36s
All checks were successful
Deploy Web Client / deploy (push) Successful in 36s
This commit is contained in:
parent
eff6724455
commit
cb5b2afe16
@ -6,7 +6,6 @@ import {
|
||||
ChatMessage,
|
||||
getProjectChat,
|
||||
getChatMessages,
|
||||
sendChatMessage,
|
||||
} from "@/lib/api";
|
||||
import { wsClient } from "@/lib/ws";
|
||||
|
||||
@ -20,7 +19,6 @@ export default function ChatPanel({ projectId }: ChatPanelProps) {
|
||||
const [input, setInput] = useState("");
|
||||
const [expanded, setExpanded] = useState(false);
|
||||
const [collapsed, setCollapsed] = useState(false);
|
||||
const [sending, setSending] = useState(false);
|
||||
const messagesEndRef = useRef<HTMLDivElement>(null);
|
||||
const inputRef = useRef<HTMLInputElement>(null);
|
||||
|
||||
@ -72,27 +70,20 @@ export default function ChatPanel({ projectId }: ChatPanelProps) {
|
||||
messagesEndRef.current?.scrollIntoView({ behavior: "smooth" });
|
||||
}, [messages]);
|
||||
|
||||
const handleSend = async () => {
|
||||
if (!chat || !input.trim() || sending) return;
|
||||
const handleSend = () => {
|
||||
if (!chat || !input.trim()) return;
|
||||
const text = input.trim();
|
||||
setInput("");
|
||||
setSending(true);
|
||||
|
||||
try {
|
||||
// Send via REST (gets saved + we get it back via WS broadcast)
|
||||
const msg = await sendChatMessage(chat.id, text);
|
||||
// Add immediately (WS might duplicate — deduplicate by id)
|
||||
setMessages((prev) => {
|
||||
if (prev.find((m) => m.id === msg.id)) return prev;
|
||||
return [...prev, msg];
|
||||
});
|
||||
} catch (e) {
|
||||
console.error("Failed to send message:", e);
|
||||
setInput(text); // restore on failure
|
||||
} finally {
|
||||
setSending(false);
|
||||
inputRef.current?.focus();
|
||||
}
|
||||
// Send via WebSocket — message will come back via chat.message event
|
||||
wsClient.send("chat.send", {
|
||||
chat_id: chat.id,
|
||||
content: text,
|
||||
sender_type: "human",
|
||||
sender_name: "admin",
|
||||
});
|
||||
|
||||
inputRef.current?.focus();
|
||||
};
|
||||
|
||||
const handleKeyDown = (e: React.KeyboardEvent) => {
|
||||
@ -180,11 +171,11 @@ export default function ChatPanel({ projectId }: ChatPanelProps) {
|
||||
onKeyDown={handleKeyDown}
|
||||
placeholder="Написать сообщение..."
|
||||
className="flex-1 bg-[var(--bg)] border border-[var(--border)] rounded px-3 py-1.5 text-sm outline-none focus:border-[var(--accent)]"
|
||||
disabled={!chat || sending}
|
||||
disabled={!chat}
|
||||
/>
|
||||
<button
|
||||
onClick={handleSend}
|
||||
disabled={!chat || !input.trim() || sending}
|
||||
disabled={!chat || !input.trim()}
|
||||
className="bg-[var(--accent)] text-white px-4 py-1.5 rounded text-sm hover:opacity-90 disabled:opacity-50"
|
||||
>
|
||||
→
|
||||
|
||||
Loading…
Reference in New Issue
Block a user