52 lines
1.6 KiB
TypeScript
52 lines
1.6 KiB
TypeScript
export const selectedAppStorageKey = 'studio-admin:selected-app-id';
|
|
export const selectedComponentByAppStorageKey = 'studio-admin:selected-component-by-app-id';
|
|
const legacySelectedAppStorageKey = 'studio-selected-app-id';
|
|
|
|
export function getStoredSelectedAppId() {
|
|
const current = window.localStorage.getItem(selectedAppStorageKey);
|
|
if (current) return current;
|
|
|
|
const legacy = window.localStorage.getItem(legacySelectedAppStorageKey);
|
|
if (legacy) {
|
|
setStoredSelectedAppId(legacy);
|
|
window.localStorage.removeItem(legacySelectedAppStorageKey);
|
|
return legacy;
|
|
}
|
|
|
|
return '';
|
|
}
|
|
|
|
export function setStoredSelectedAppId(appId: string) {
|
|
if (!appId) return;
|
|
window.localStorage.setItem(selectedAppStorageKey, appId);
|
|
}
|
|
|
|
function readSelectedComponentMap(): Record<string, string> {
|
|
const raw = window.localStorage.getItem(selectedComponentByAppStorageKey);
|
|
if (!raw) return {};
|
|
|
|
try {
|
|
const parsed = JSON.parse(raw) as unknown;
|
|
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return {};
|
|
return Object.fromEntries(
|
|
Object.entries(parsed)
|
|
.filter((entry): entry is [string, string] => typeof entry[0] === 'string' && typeof entry[1] === 'string')
|
|
);
|
|
} catch {
|
|
return {};
|
|
}
|
|
}
|
|
|
|
export function getStoredSelectedComponentId(appId: string) {
|
|
if (!appId) return '';
|
|
return readSelectedComponentMap()[appId] ?? '';
|
|
}
|
|
|
|
export function setStoredSelectedComponentId(appId: string, componentId: string) {
|
|
if (!appId || !componentId) return;
|
|
window.localStorage.setItem(selectedComponentByAppStorageKey, JSON.stringify({
|
|
...readSelectedComponentMap(),
|
|
[appId]: componentId,
|
|
}));
|
|
}
|