- store unsent draft text per chat in zustand - restore draft when switching chats - clear draft after successful send
This commit is contained in:
@@ -6,6 +6,7 @@ interface ChatState {
|
||||
chats: Chat[];
|
||||
activeChatId: number | null;
|
||||
messagesByChat: Record<number, Message[]>;
|
||||
draftsByChat: Record<number, string>;
|
||||
typingByChat: Record<number, number[]>;
|
||||
replyToByChat: Record<number, Message | null>;
|
||||
unreadBoundaryByChat: Record<number, number>;
|
||||
@@ -38,12 +39,15 @@ interface ChatState {
|
||||
setReplyToMessage: (chatId: number, message: Message | null) => void;
|
||||
updateChatPinnedMessage: (chatId: number, pinnedMessageId: number | null) => void;
|
||||
applyPresenceEvent: (chatId: number, userId: number, isOnline: boolean, lastSeenAt?: string) => void;
|
||||
setDraft: (chatId: number, text: string) => void;
|
||||
clearDraft: (chatId: number) => void;
|
||||
}
|
||||
|
||||
export const useChatStore = create<ChatState>((set, get) => ({
|
||||
chats: [],
|
||||
activeChatId: null,
|
||||
messagesByChat: {},
|
||||
draftsByChat: {},
|
||||
typingByChat: {},
|
||||
replyToByChat: {},
|
||||
unreadBoundaryByChat: {},
|
||||
@@ -272,5 +276,21 @@ export const useChatStore = create<ChatState>((set, get) => ({
|
||||
}
|
||||
return chat;
|
||||
})
|
||||
}))
|
||||
})),
|
||||
setDraft: (chatId, text) =>
|
||||
set((state) => ({
|
||||
draftsByChat: {
|
||||
...state.draftsByChat,
|
||||
[chatId]: text
|
||||
}
|
||||
})),
|
||||
clearDraft: (chatId) =>
|
||||
set((state) => {
|
||||
if (!(chatId in state.draftsByChat)) {
|
||||
return state;
|
||||
}
|
||||
const next = { ...state.draftsByChat };
|
||||
delete next[chatId];
|
||||
return { draftsByChat: next };
|
||||
})
|
||||
}));
|
||||
|
||||
Reference in New Issue
Block a user