feat(chat): sync multi-role backend APIs
This commit is contained in:
@@ -16,6 +16,11 @@ import {
|
||||
useAuthSelector,
|
||||
} from "@/stores/auth/auth-context";
|
||||
import { useChatDispatch } from "@/stores/chat/chat-context";
|
||||
import { syncGuestHistoriesToUser } from "@/stores/chat/guest-history-sync";
|
||||
import { useCharacterCatalog } from "@/providers/character-catalog-provider";
|
||||
import { resolveChatCacheOwnerKey } from "@/data/repositories/chat_cache_identity";
|
||||
import { NavigationStorage } from "@/data/storage/navigation";
|
||||
import { Result } from "@/utils/result";
|
||||
|
||||
export function ChatAuthSync() {
|
||||
const authState = useAuthSelector(
|
||||
@@ -27,6 +32,7 @@ export function ChatAuthSync() {
|
||||
shallowEqual,
|
||||
);
|
||||
const chatDispatch = useChatDispatch();
|
||||
const characterCatalog = useCharacterCatalog();
|
||||
const prevSessionKeyRef = useRef<string | null>(null);
|
||||
|
||||
useEffect(() => {
|
||||
@@ -42,14 +48,27 @@ export function ChatAuthSync() {
|
||||
}
|
||||
|
||||
if (authState.loginStatus === "guest") {
|
||||
chatDispatch({ type: "ChatGuestLogin" });
|
||||
return;
|
||||
let cancelled = false;
|
||||
void (async () => {
|
||||
const ownerResult = await resolveChatCacheOwnerKey();
|
||||
if (Result.isOk(ownerResult)) {
|
||||
await NavigationStorage.saveGuestChatOwnerKey(ownerResult.data);
|
||||
}
|
||||
if (!cancelled) chatDispatch({ type: "ChatGuestLogin" });
|
||||
})();
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}
|
||||
|
||||
let cancelled = false;
|
||||
void (async () => {
|
||||
const tokenR = await AuthStorage.getInstance().getLoginToken();
|
||||
if (!cancelled && tokenR.success && tokenR.data) {
|
||||
await syncGuestHistoriesToUser(
|
||||
characterCatalog.characters.map((character) => character.id),
|
||||
);
|
||||
if (cancelled) return;
|
||||
chatDispatch({
|
||||
type: "ChatUserLogin",
|
||||
token: tokenR.data,
|
||||
@@ -64,6 +83,7 @@ export function ChatAuthSync() {
|
||||
authState.hasInitialized,
|
||||
authState.isLoading,
|
||||
authState.loginStatus,
|
||||
characterCatalog.characters,
|
||||
chatDispatch,
|
||||
]);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user