feat(skillSelect): 将localstorage换成sessionStorage,且不从sessionstorage自动恢复显示skill

This commit is contained in:
肖应宇 2026-04-17 09:38:58 +08:00
parent ecb26534fc
commit b88fa12214
1 changed files with 33 additions and 32 deletions

View File

@ -103,39 +103,39 @@ export function useIframeSkill(
return next; return next;
}); });
// 2) 回滚 localStoragelatest + thread // 2) 回滚 sessionStoragelatest + thread
const latestSkills = parseStoredSkills( const latestSkills = parseStoredSkills(
window.localStorage.getItem(STORAGE_KEYS.latest), window.sessionStorage.getItem(STORAGE_KEYS.latest),
); );
const nextLatestSkills = removeSkillsByIdsFromList( const nextLatestSkills = removeSkillsByIdsFromList(
latestSkills, latestSkills,
skillIds, skillIds,
); );
if (nextLatestSkills.length > 0) { if (nextLatestSkills.length > 0) {
window.localStorage.setItem( window.sessionStorage.setItem(
STORAGE_KEYS.latest, STORAGE_KEYS.latest,
JSON.stringify(nextLatestSkills), JSON.stringify(nextLatestSkills),
); );
} else { } else {
window.localStorage.removeItem(STORAGE_KEYS.latest); window.sessionStorage.removeItem(STORAGE_KEYS.latest);
} }
const threadKey = getThreadStorageKey(threadId); const threadKey = getThreadStorageKey(threadId);
if (threadKey) { if (threadKey) {
const threadSkills = parseStoredSkills( const threadSkills = parseStoredSkills(
window.localStorage.getItem(threadKey), window.sessionStorage.getItem(threadKey),
); );
const nextThreadSkills = removeSkillsByIdsFromList( const nextThreadSkills = removeSkillsByIdsFromList(
threadSkills, threadSkills,
skillIds, skillIds,
); );
if (nextThreadSkills.length > 0) { if (nextThreadSkills.length > 0) {
window.localStorage.setItem( window.sessionStorage.setItem(
threadKey, threadKey,
JSON.stringify(nextThreadSkills), JSON.stringify(nextThreadSkills),
); );
} else { } else {
window.localStorage.removeItem(threadKey); window.sessionStorage.removeItem(threadKey);
} }
} }
}, },
@ -196,38 +196,39 @@ export function useIframeSkill(
return () => window.removeEventListener("message", handleMessage); return () => window.removeEventListener("message", handleMessage);
}, []); }, []);
// 3. 首次进入时恢复 localStorage 中上次选择的 skill线程优先其次全局 // 3. 首次进入时恢复 sessionStorage 中上次选择的 skill线程优先其次全局
useEffect(() => { // 已按需求注释:关闭页签后重新打开时,不再从 sessionStorage 自动恢复。
const threadKey = getThreadStorageKey(threadId); // useEffect(() => {
const threadSkills = threadKey // const threadKey = getThreadStorageKey(threadId);
? parseStoredSkills(window.localStorage.getItem(threadKey)) // const threadSkills = threadKey
: []; // ? parseStoredSkills(window.sessionStorage.getItem(threadKey))
const latestSkills = parseStoredSkills( // : [];
window.localStorage.getItem(STORAGE_KEYS.latest), // const latestSkills = parseStoredSkills(
); // window.sessionStorage.getItem(STORAGE_KEYS.latest),
const restoredSkills = // );
threadSkills.length > 0 ? threadSkills : latestSkills; // const restoredSkills =
if (restoredSkills.length === 0) return; // threadSkills.length > 0 ? threadSkills : latestSkills;
setSelectedSkills(restoredSkills); // if (restoredSkills.length === 0) return;
setSelectedSkill(restoredSkills[0] ?? null); // setSelectedSkills(restoredSkills);
}, [threadId]); // setSelectedSkill(restoredSkills[0] ?? null);
// }, [threadId]);
// 4. 选择变化时同步到 localStorage // 4. 选择变化时同步到 sessionStorage
useEffect(() => { useEffect(() => {
const threadKey = getThreadStorageKey(threadId); const threadKey = getThreadStorageKey(threadId);
if (selectedSkills.length === 0) { if (selectedSkills.length === 0) {
// 空数组也要同步到存储,避免 UI 状态与缓存不一致 // 空数组也要同步到存储,避免 UI 状态与缓存不一致
window.localStorage.removeItem(STORAGE_KEYS.latest); window.sessionStorage.removeItem(STORAGE_KEYS.latest);
if (threadKey) { if (threadKey) {
window.localStorage.removeItem(threadKey); window.sessionStorage.removeItem(threadKey);
} }
return; return;
} }
const payload = JSON.stringify(selectedSkills); const payload = JSON.stringify(selectedSkills);
window.localStorage.setItem(STORAGE_KEYS.latest, payload); window.sessionStorage.setItem(STORAGE_KEYS.latest, payload);
if (threadKey) { if (threadKey) {
window.localStorage.setItem(threadKey, payload); window.sessionStorage.setItem(threadKey, payload);
} }
}, [selectedSkills, threadId]); }, [selectedSkills, threadId]);
@ -359,30 +360,30 @@ export function useIframeSkill(
// 同步 latest 缓存:仅删除对应 skill或全部清空 // 同步 latest 缓存:仅删除对应 skill或全部清空
const latestSkills = parseStoredSkills( const latestSkills = parseStoredSkills(
window.localStorage.getItem(STORAGE_KEYS.latest), window.sessionStorage.getItem(STORAGE_KEYS.latest),
); );
const nextLatestSkills = removeAll const nextLatestSkills = removeAll
? [] ? []
: latestSkills.filter((skill) => skill.skill_id !== String(skillId)); : latestSkills.filter((skill) => skill.skill_id !== String(skillId));
if (nextLatestSkills.length > 0) { if (nextLatestSkills.length > 0) {
window.localStorage.setItem( window.sessionStorage.setItem(
STORAGE_KEYS.latest, STORAGE_KEYS.latest,
JSON.stringify(nextLatestSkills), JSON.stringify(nextLatestSkills),
); );
} else { } else {
window.localStorage.removeItem(STORAGE_KEYS.latest); window.sessionStorage.removeItem(STORAGE_KEYS.latest);
} }
// 同步线程缓存:保存剩余数组,空则删除 key // 同步线程缓存:保存剩余数组,空则删除 key
const threadKey = getThreadStorageKey(threadId); const threadKey = getThreadStorageKey(threadId);
if (threadKey) { if (threadKey) {
if (nextSelectedSkills.length > 0) { if (nextSelectedSkills.length > 0) {
window.localStorage.setItem( window.sessionStorage.setItem(
threadKey, threadKey,
JSON.stringify(nextSelectedSkills), JSON.stringify(nextSelectedSkills),
); );
} else { } else {
window.localStorage.removeItem(threadKey); window.sessionStorage.removeItem(threadKey);
} }
} }