登录/刷新/发短信直接解析根级字段,移除 data 包装与 mergeUser 等多余逻辑。 首页侧栏用户信息随 localStorage 与 chatone-user-changed 更新,优先展示 nickname。 Made-with: Cursor
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import type { CSSProperties } from "react";
|
||||
import { useEffect, useMemo, useRef, useState } from "react";
|
||||
import { useEffect, useMemo, useRef, useState, useSyncExternalStore } from "react";
|
||||
import {
|
||||
ArrowUpOutlined,
|
||||
LogoutOutlined,
|
||||
@@ -44,13 +44,13 @@ type UiMessage = {
|
||||
thinking?: string;
|
||||
};
|
||||
|
||||
function readStoredUserProfile(): { displayName: string; avatar?: string } {
|
||||
function parseStoredUserProfileJson(raw: string | null): { displayName: string; avatar?: string } {
|
||||
try {
|
||||
const raw = window.localStorage.getItem(USER_KEY);
|
||||
if (!raw) return { displayName: "用户" };
|
||||
const u = JSON.parse(raw) as Record<string, unknown>;
|
||||
const nickname = typeof u.nickname === "string" && u.nickname.trim() ? u.nickname.trim() : "";
|
||||
const displayName =
|
||||
(typeof u.nickname === "string" && u.nickname) ||
|
||||
nickname ||
|
||||
(typeof u.name === "string" && u.name) ||
|
||||
(typeof u.username === "string" && u.username) ||
|
||||
(typeof u.phone === "string" && u.phone) ||
|
||||
@@ -65,6 +65,19 @@ function readStoredUserProfile(): { displayName: string; avatar?: string } {
|
||||
}
|
||||
}
|
||||
|
||||
/** 与 `persistUser` 内触发的 `chatone-user-changed` 及跨标签 `storage` 对齐,用于刷新侧栏昵称 */
|
||||
function subscribeUserProfile(onChange: () => void) {
|
||||
const onStorage = (e: StorageEvent) => {
|
||||
if (e.key === USER_KEY || e.key === null) onChange();
|
||||
};
|
||||
window.addEventListener("chatone-user-changed", onChange);
|
||||
window.addEventListener("storage", onStorage);
|
||||
return () => {
|
||||
window.removeEventListener("chatone-user-changed", onChange);
|
||||
window.removeEventListener("storage", onStorage);
|
||||
};
|
||||
}
|
||||
|
||||
const INITIAL_MESSAGES: UiMessage[] = [
|
||||
{
|
||||
id: "init-assistant",
|
||||
@@ -108,7 +121,15 @@ export default function HomePage() {
|
||||
[],
|
||||
);
|
||||
|
||||
const userProfile = useMemo(() => readStoredUserProfile(), []);
|
||||
const userJsonSnapshot = useSyncExternalStore(
|
||||
subscribeUserProfile,
|
||||
() => window.localStorage.getItem(USER_KEY) ?? "",
|
||||
() => "",
|
||||
);
|
||||
const userProfile = useMemo(
|
||||
() => parseStoredUserProfileJson(userJsonSnapshot || null),
|
||||
[userJsonSnapshot],
|
||||
);
|
||||
|
||||
/** 用户信息下拉:略增大行高与项间距 */
|
||||
const userMenuItemStyle: CSSProperties = {
|
||||
|
||||
Reference in New Issue
Block a user