diff --git a/frontend/src/store/index.ts b/frontend/src/store/index.ts index 3418150..3c84188 100644 --- a/frontend/src/store/index.ts +++ b/frontend/src/store/index.ts @@ -53,10 +53,20 @@ export const useStore = create((set, get) => ({ setChats: (chats) => set({ chats, chatsLoading: false }), setChatsLoading: (v) => set({ chatsLoading: v }), - updateChat: (partial) => set(state => ({ - chats: state.chats.map(c => c.id === partial.id ? { ...c, ...partial } : c), - activeChat: state.activeChat?.id === partial.id ? { ...state.activeChat, ...partial } : state.activeChat, - })), + updateChat: (partial) => set(state => { + const updated = state.chats.map(c => c.id === partial.id ? { ...c, ...partial } : c); + updated.sort((a, b) => { + if (a.isPinned && !b.isPinned) return -1; + if (!a.isPinned && b.isPinned) return 1; + const ta = a.lastMessageAt || a.createdAt; + const tb = b.lastMessageAt || b.createdAt; + return new Date(tb).getTime() - new Date(ta).getTime(); + }); + return { + chats: updated, + activeChat: state.activeChat?.id === partial.id ? { ...state.activeChat, ...partial } : state.activeChat, + }; + }), setActiveChat: (chat) => set({ activeChat: chat }),