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