- store unsent draft text per chat in zustand - restore draft when switching chats - clear draft after successful send
This commit is contained in:
@@ -7,6 +7,9 @@ import { buildWsUrl } from "../utils/ws";
|
||||
export function MessageComposer() {
|
||||
const activeChatId = useChatStore((s) => s.activeChatId);
|
||||
const me = useAuthStore((s) => s.me);
|
||||
const draftsByChat = useChatStore((s) => s.draftsByChat);
|
||||
const setDraft = useChatStore((s) => s.setDraft);
|
||||
const clearDraft = useChatStore((s) => s.clearDraft);
|
||||
const addOptimisticMessage = useChatStore((s) => s.addOptimisticMessage);
|
||||
const confirmMessageByClientId = useChatStore((s) => s.confirmMessageByClientId);
|
||||
const removeOptimisticMessage = useChatStore((s) => s.removeOptimisticMessage);
|
||||
@@ -25,6 +28,19 @@ export function MessageComposer() {
|
||||
const [previewUrl, setPreviewUrl] = useState<string | null>(null);
|
||||
const [isRecording, setIsRecording] = useState(false);
|
||||
|
||||
useEffect(() => {
|
||||
if (!activeChatId) {
|
||||
if (text !== "") {
|
||||
setText("");
|
||||
}
|
||||
return;
|
||||
}
|
||||
const draft = draftsByChat[activeChatId] ?? "";
|
||||
if (draft !== text) {
|
||||
setText(draft);
|
||||
}
|
||||
}, [activeChatId, draftsByChat, text]);
|
||||
|
||||
useEffect(() => {
|
||||
return () => {
|
||||
if (previewUrl) {
|
||||
@@ -64,6 +80,7 @@ export function MessageComposer() {
|
||||
const message = await sendMessageWithClientId(activeChatId, textValue, "text", clientMessageId, replyToMessageId);
|
||||
confirmMessageByClientId(activeChatId, clientMessageId, message);
|
||||
setText("");
|
||||
clearDraft(activeChatId);
|
||||
setReplyToMessage(activeChatId, null);
|
||||
const ws = getWs();
|
||||
ws?.send(JSON.stringify({ event: "typing_stop", payload: { chat_id: activeChatId } }));
|
||||
@@ -284,7 +301,11 @@ export function MessageComposer() {
|
||||
placeholder="Write a message..."
|
||||
value={text}
|
||||
onChange={(e) => {
|
||||
setText(e.target.value);
|
||||
const next = e.target.value;
|
||||
setText(next);
|
||||
if (activeChatId) {
|
||||
setDraft(activeChatId, next);
|
||||
}
|
||||
if (activeChatId) {
|
||||
const ws = getWs();
|
||||
ws?.send(JSON.stringify({ event: "typing_start", payload: { chat_id: activeChatId } }));
|
||||
|
||||
Reference in New Issue
Block a user