From ec672495b33a6e2bda3680c688e7a258f177df61 Mon Sep 17 00:00:00 2001 From: Innei Date: Thu, 6 Nov 2025 22:24:24 +0800 Subject: [PATCH] fix(ai): fix load ai history session - Changed RateLimitNotice from a div to a button for better accessibility and interaction. - Added an "Upgrade Plan" button to encourage users to upgrade their plan directly from the notice. - Improved layout styling for better visual clarity. fix(useChatHistory): streamline loading state checks - Simplified loading state condition in loadHistory function to improve performance. fix(ai-chat-session): remove unnecessary localization logic in fetchRemoteSessions - Cleaned up fetchRemoteSessions function by removing unused i18n imports and related code. Signed-off-by: Innei --- .../renderer/src/modules/ai-chat-session/store.ts | 15 +-------------- .../components/layouts/RateLimitNotice.tsx | 13 ++++++++++--- .../src/modules/ai-chat/hooks/useChatHistory.ts | 7 +++++-- 3 files changed, 16 insertions(+), 19 deletions(-) diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat-session/store.ts b/apps/desktop/layer/renderer/src/modules/ai-chat-session/store.ts index 6a18b3b17..084792849 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat-session/store.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat-session/store.ts @@ -1,7 +1,5 @@ import { createWithEqualityFn } from "zustand/traditional" -import { getI18n } from "~/i18n" - import type { ChatSession } from "../ai-chat/types/ChatSession" import { AIChatSessionService } from "./service" @@ -74,18 +72,7 @@ export const aiChatSessionStoreActions = { // syncing fetchRemoteSessions: async () => { try { - const { t } = getI18n() - const sessions = await AIChatSessionService.syncSessionsAndMessagesFromServer() - aiChatSessionStoreActions.setSessions( - sessions.map((session) => ({ - chatId: session.chatId, - title: session.title || t("ai:common.new_chat"), - createdAt: new Date(session.createdAt), - updatedAt: new Date(session.updatedAt), - isLocal: false, - syncStatus: "synced" as const, - })), - ) + await AIChatSessionService.syncSessionsAndMessagesFromServer() } catch (error) { console.error("fetchRemoteSessionsAndMessages: failed", error) aiChatSessionStoreActions.setError(error instanceof Error ? error.message : "fetch_failed") diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/RateLimitNotice.tsx b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/RateLimitNotice.tsx index 9ecf5f6f0..a5742edd6 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/RateLimitNotice.tsx +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/components/layouts/RateLimitNotice.tsx @@ -38,19 +38,26 @@ export const RateLimitNotice = ({ error, chatConfig, className }: RateLimitNotic } return ( - settingModalPresent("plan")} >
{message} + +
-
+ ) } diff --git a/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useChatHistory.ts b/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useChatHistory.ts index 824255ae0..f9a30a8ef 100644 --- a/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useChatHistory.ts +++ b/apps/desktop/layer/renderer/src/modules/ai-chat/hooks/useChatHistory.ts @@ -1,6 +1,7 @@ import { useMemo } from "react" import { useEventCallback } from "usehooks-ts" +import { AIChatSessionService } from "~/modules/ai-chat-session/service" import { aiChatSessionStoreActions, useAIChatSessionStore } from "~/modules/ai-chat-session/store" export const useChatHistory = () => { @@ -10,18 +11,20 @@ export const useChatHistory = () => { const loading = state.isLoading || state.isSyncing const loadHistory = useEventCallback(async () => { - if (loading) return + if (state.isLoading) return aiChatSessionStoreActions.setLoading(true) aiChatSessionStoreActions.clearError() try { + // 1) Always hydrate latest local first so UI updates immediately + await AIChatSessionService.loadSessionsFromDb() + // 2) Then sync remote → upsert into local DB → reload from DB inside service await aiChatSessionStoreActions.fetchRemoteSessions() } catch (error) { console.error("Failed to load chat history:", error) aiChatSessionStoreActions.setError(error instanceof Error ? error.message : "Unknown error") } finally { - aiChatSessionStoreActions.setSyncing(false) aiChatSessionStoreActions.setLoading(false) } })