refactor: support i18n
This commit is contained in:
@@ -15,6 +15,7 @@ import {
|
||||
import { createRealtimeConnectionManager } from "@/lib/realtime-connection"
|
||||
import { getNotificationBody, showNotification } from "@/lib/services/notification"
|
||||
import { useAgentConversationsStore } from "@/lib/stores/agent-conversations"
|
||||
import { useI18n } from "@/i18n/provider"
|
||||
|
||||
type AgentRealtimeConnection = ReturnType<typeof createRealtimeConnectionManager>
|
||||
type AgentRealtimeEnvelope = {
|
||||
@@ -29,6 +30,7 @@ type AgentRealtimeEnvelope = {
|
||||
}
|
||||
|
||||
export function useAgentConversationRealtime() {
|
||||
const t = useI18n()
|
||||
const selectedConversationId = useAgentConversationsStore(
|
||||
(state) => state.selectedConversationId
|
||||
)
|
||||
@@ -91,7 +93,7 @@ export function useAgentConversationRealtime() {
|
||||
const store = useAgentConversationsStore.getState()
|
||||
if (eventType === "resyncRequired") {
|
||||
void store.resyncRealtimeData(conversationId).catch((error) => {
|
||||
toast.error(error instanceof Error ? error.message : "同步会话数据失败")
|
||||
toast.error(error instanceof Error ? error.message : t("conversation.syncConversationDataFailed"))
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -100,7 +102,7 @@ export function useAgentConversationRealtime() {
|
||||
const message = normalizeRealtimeMessage<AgentMessage>(payload)
|
||||
if (!message) {
|
||||
void store.resyncRealtimeData(conversationId).catch((error) => {
|
||||
toast.error(error instanceof Error ? error.message : "同步消息失败")
|
||||
toast.error(error instanceof Error ? error.message : t("conversation.syncMessagesFailed"))
|
||||
})
|
||||
return
|
||||
}
|
||||
@@ -115,7 +117,7 @@ export function useAgentConversationRealtime() {
|
||||
document.visibilityState !== "visible"
|
||||
|
||||
if (shouldNotify) {
|
||||
showNotification("新消息", getNotificationBody(message), () => {
|
||||
showNotification(t("conversation.newMessage"), getNotificationBody(message), () => {
|
||||
void store.selectConversation(message.conversationId)
|
||||
})
|
||||
}
|
||||
@@ -134,7 +136,7 @@ export function useAgentConversationRealtime() {
|
||||
store.applyRealtimeConversationChanged(payload)
|
||||
if (shouldReloadConversationListForRealtimePatch(payload)) {
|
||||
void store.resyncRealtimeData(conversationId).catch((error) => {
|
||||
toast.error(error instanceof Error ? error.message : "同步会话列表失败")
|
||||
toast.error(error instanceof Error ? error.message : t("conversation.syncConversationListFailed"))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -159,7 +161,7 @@ export function useAgentConversationRealtime() {
|
||||
})
|
||||
},
|
||||
onConnectError: (error) => {
|
||||
toast.error(error instanceof Error ? error.message : "连接实时服务失败")
|
||||
toast.error(error instanceof Error ? error.message : t("conversation.realtimeConnectFailed"))
|
||||
},
|
||||
})
|
||||
|
||||
@@ -171,7 +173,7 @@ export function useAgentConversationRealtime() {
|
||||
realtime.disconnect()
|
||||
subscribedConversationIdRef.current = null
|
||||
}
|
||||
}, [setRealtimeStatus])
|
||||
}, [setRealtimeStatus, t])
|
||||
|
||||
useEffect(() => {
|
||||
const socket = realtimeRef.current?.getSocket()
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
import { useSyncExternalStore } from "react";
|
||||
|
||||
/** 与 Tailwind 默认 `lg` 断点一致(1024px) */
|
||||
/** Matches Tailwind's default `lg` breakpoint (1024px). */
|
||||
const LG_MEDIA_QUERY = "(min-width: 1024px)";
|
||||
|
||||
function subscribe(onStoreChange: () => void) {
|
||||
@@ -19,7 +19,7 @@ function getServerSnapshot() {
|
||||
return false;
|
||||
}
|
||||
|
||||
/** 仅客户端在 lg 及以上为 true;SSR 恒为 false,与 mobile-first 一致 */
|
||||
/** Client-only true at lg and above. SSR always returns false for mobile-first rendering. */
|
||||
export function useIsLgUp() {
|
||||
return useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user