diff --git a/app/src/pages/Conversations.tsx b/app/src/pages/Conversations.tsx index aad5f537b..3b9f89e59 100644 --- a/app/src/pages/Conversations.tsx +++ b/app/src/pages/Conversations.tsx @@ -245,9 +245,17 @@ const Conversations = ({ variant = 'page' }: ConversationsProps = {}) => { return; } if (data.threads.length > 0) { - const mostRecent = data.threads[0]; - dispatch(setSelectedThread(mostRecent.id)); - void dispatch(loadThreadMessages(mostRecent.id)); + // Prefer the thread the user was last viewing (persisted across + // reloads via redux-persist on the `thread` slice). Only fall + // through to "most recent" if that thread no longer exists + // server-side (deleted, purged, or different user). + const persistedId = threadStateForSelect.selectedThreadId; + const resumeId = + persistedId && data.threads.some(t => t.id === persistedId) + ? persistedId + : data.threads[0].id; + dispatch(setSelectedThread(resumeId)); + void dispatch(loadThreadMessages(resumeId)); } else { void handleCreateNewThread(); } diff --git a/app/src/store/index.ts b/app/src/store/index.ts index 7cecb252f..d0f82740f 100644 --- a/app/src/store/index.ts +++ b/app/src/store/index.ts @@ -52,10 +52,17 @@ const notificationPersistConfig = { }; const persistedNotificationReducer = persistReducer(notificationPersistConfig, notificationReducer); +// Persist only the user's last-viewed thread id so a reload resumes where +// they were instead of falling through to "create a new thread". The +// thread list and per-thread message caches are re-fetched from the core +// on boot, so we deliberately don't persist them. +const threadPersistConfig = { key: 'thread', storage, whitelist: ['selectedThreadId'] }; +const persistedThreadReducer = persistReducer(threadPersistConfig, threadReducer); + export const store = configureStore({ reducer: { socket: socketReducer, - thread: threadReducer, + thread: persistedThreadReducer, chatRuntime: chatRuntimeReducer, channelConnections: persistedChannelConnectionsReducer, accounts: persistedAccountsReducer,