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 <tukon479@gmail.com>
This commit is contained in:
Innei 2025-11-06 22:24:24 +08:00
parent 3492de2c3c
commit ec672495b3
No known key found for this signature in database
GPG Key ID: 0F62D33977F021F7
3 changed files with 16 additions and 19 deletions

View File

@ -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")

View File

@ -38,19 +38,26 @@ export const RateLimitNotice = ({ error, chatConfig, className }: RateLimitNotic
}
return (
<m.div
<m.button
initial={{ opacity: 0, y: -10 }}
animate={{ opacity: 1, y: 0 }}
exit={{ opacity: 0, y: -10 }}
transition={{ duration: 0.2 }}
className={cn("mb-3", className)}
className={cn("mb-3 block w-full text-left", className)}
onClick={() => settingModalPresent("plan")}
>
<div className="flex items-center gap-2 rounded-lg border border-border bg-material-ultra-thick px-3 py-2 backdrop-blur-background">
<i className="i-mgc-power size-4 flex-shrink-0 text-text" />
<span className="min-w-0 flex-1 truncate text-xs text-text-secondary">{message}</span>
<button
type="button"
className="cursor-button text-xs text-accent duration-200 hover:opacity-80"
>
Upgrade Plan
</button>
</div>
</m.div>
</m.button>
)
}

View File

@ -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)
}
})