Add toolbox spaces and recover from stale asset bundles
All checks were successful
Deploy Sokko G / deploy (push) Successful in 18s
All checks were successful
Deploy Sokko G / deploy (push) Successful in 18s
This commit is contained in:
parent
0959c6740b
commit
0acf5ecf21
30 changed files with 1252 additions and 179 deletions
|
|
@ -47,6 +47,7 @@ const TOOLBOX_ICON_FILES = [
|
|||
];
|
||||
export const TOOLBOX_ICONS = TOOLBOX_ICON_FILES.map((file) => `${TOOLBOX_ICON_BASE}${file}`);
|
||||
export const DEFAULT_TOOLBOX_ICON = `${TOOLBOX_ICON_BASE}toolbox.png`;
|
||||
export const MAX_TOOLBOX_SPACES = 5;
|
||||
|
||||
export function normalizeToolboxIcon(icon) {
|
||||
const value = String(icon || "").trim();
|
||||
|
|
@ -123,9 +124,74 @@ export function remapModuleOrderIds(moduleOrder, moduleIdMap) {
|
|||
};
|
||||
}
|
||||
|
||||
function getSpaceModuleIds(space, knownModuleIds) {
|
||||
const candidates = [
|
||||
...(Array.isArray(space?.moduleOrder?.one) ? space.moduleOrder.one : []),
|
||||
...(Array.isArray(space?.moduleOrder?.two?.[0]) ? space.moduleOrder.two[0] : []),
|
||||
...(Array.isArray(space?.moduleOrder?.two?.[1]) ? space.moduleOrder.two[1] : [])
|
||||
];
|
||||
return uniqueKnownIds(candidates, knownModuleIds);
|
||||
}
|
||||
|
||||
export function normalizeToolboxSpaces(toolbox, modules) {
|
||||
const moduleIds = modules.map((module) => module.id);
|
||||
const rawSpaces = Array.isArray(toolbox?.spaces) ? toolbox.spaces.filter((space) => space && typeof space === "object") : [];
|
||||
if (!rawSpaces.length) {
|
||||
const id = uid("space");
|
||||
return {
|
||||
spaces: [{ id, name: "Défaut", moduleOrder: normalizeModuleOrder(toolbox?.moduleOrder, modules) }],
|
||||
defaultSpaceId: id,
|
||||
activeSpaceId: id
|
||||
};
|
||||
}
|
||||
|
||||
const seenSpaceIds = new Set();
|
||||
const prepared = rawSpaces.slice(0, MAX_TOOLBOX_SPACES).map((space, index) => {
|
||||
let id = String(space.id || "").trim() || uid("space");
|
||||
while (seenSpaceIds.has(id)) id = uid("space");
|
||||
seenSpaceIds.add(id);
|
||||
return {
|
||||
id,
|
||||
name: String(space.name || "").trim() || (index === 0 ? "Défaut" : `Espace ${index + 1}`),
|
||||
source: space
|
||||
};
|
||||
});
|
||||
const requestedDefaultId = String(toolbox?.defaultSpaceId || "");
|
||||
const defaultIndex = prepared.findIndex((space) => space.id === requestedDefaultId);
|
||||
if (defaultIndex > 0) prepared.unshift(prepared.splice(defaultIndex, 1)[0]);
|
||||
|
||||
const assigned = new Set();
|
||||
const memberships = new Map();
|
||||
prepared.forEach((space) => {
|
||||
const ids = getSpaceModuleIds(space.source, moduleIds).filter((id) => {
|
||||
if (assigned.has(id)) return false;
|
||||
assigned.add(id);
|
||||
return true;
|
||||
});
|
||||
memberships.set(space.id, ids);
|
||||
});
|
||||
const defaultId = prepared[0].id;
|
||||
memberships.set(defaultId, [...memberships.get(defaultId), ...moduleIds.filter((id) => !assigned.has(id))]);
|
||||
|
||||
const spaces = prepared.map((space) => {
|
||||
const memberIds = memberships.get(space.id);
|
||||
const memberSet = new Set(memberIds);
|
||||
const spaceModules = modules.filter((module) => memberSet.has(module.id));
|
||||
return {
|
||||
id: space.id,
|
||||
name: space.name,
|
||||
moduleOrder: normalizeModuleOrder(space.source.moduleOrder, spaceModules)
|
||||
};
|
||||
});
|
||||
const requestedActiveId = String(toolbox?.activeSpaceId || "");
|
||||
const activeSpaceId = spaces.some((space) => space.id === requestedActiveId) ? requestedActiveId : defaultId;
|
||||
return { spaces, defaultSpaceId: defaultId, activeSpaceId };
|
||||
}
|
||||
|
||||
export function normalizeToolbox(toolbox) {
|
||||
if (!toolbox || typeof toolbox !== "object") return null;
|
||||
const modules = (Array.isArray(toolbox.modules) ? toolbox.modules : []).map(normalizeToolboxModule).filter(Boolean);
|
||||
const spaceState = normalizeToolboxSpaces(toolbox, modules);
|
||||
const normalized = {
|
||||
id: toolbox.id || uid("tbx"),
|
||||
name: String(toolbox.name || "Nouvelle toolbox").trim() || "Nouvelle toolbox",
|
||||
|
|
@ -133,7 +199,7 @@ export function normalizeToolbox(toolbox) {
|
|||
customCover: normalizeToolboxCustomCover(toolbox.customCover),
|
||||
moduleColumns: Number(toolbox.moduleColumns) === 1 ? 1 : 2,
|
||||
modules,
|
||||
moduleOrder: normalizeModuleOrder(toolbox.moduleOrder, modules),
|
||||
...spaceState,
|
||||
updatedAt: toolbox.updatedAt || new Date().toISOString()
|
||||
};
|
||||
if (toolbox.tutorial === true) normalized.tutorial = true;
|
||||
|
|
@ -147,7 +213,9 @@ export function compactToolboxForStorage(toolbox) {
|
|||
id: normalized.id,
|
||||
name: normalized.name,
|
||||
modules: normalized.modules,
|
||||
moduleOrder: normalized.moduleOrder,
|
||||
spaces: normalized.spaces,
|
||||
defaultSpaceId: normalized.defaultSpaceId,
|
||||
activeSpaceId: normalized.activeSpaceId,
|
||||
updatedAt: normalized.updatedAt
|
||||
};
|
||||
if (normalized.moduleColumns === 1) compact.moduleColumns = 1;
|
||||
|
|
@ -402,6 +470,7 @@ export function createToolboxExportPayload(toolbox, moduleData) {
|
|||
const source = normalizeToolbox(toolbox);
|
||||
const nextId = createExportIdFactory();
|
||||
const moduleIdMap = new Map();
|
||||
const spaceIdMap = new Map(source.spaces.map((space) => [space.id, nextId("space")]));
|
||||
const exportedToolbox = compactToolboxForStorage({
|
||||
...source,
|
||||
id: nextId("tbx"),
|
||||
|
|
@ -410,7 +479,13 @@ export function createToolboxExportPayload(toolbox, moduleData) {
|
|||
moduleIdMap.set(module.id, id);
|
||||
return { ...module, id };
|
||||
}),
|
||||
moduleOrder: remapModuleOrderIds(source.moduleOrder, moduleIdMap)
|
||||
spaces: source.spaces.map((space) => ({
|
||||
...space,
|
||||
id: spaceIdMap.get(space.id),
|
||||
moduleOrder: remapModuleOrderIds(space.moduleOrder, moduleIdMap)
|
||||
})),
|
||||
defaultSpaceId: spaceIdMap.get(source.defaultSpaceId),
|
||||
activeSpaceId: spaceIdMap.get(source.activeSpaceId)
|
||||
});
|
||||
const modules = Object.fromEntries(source.modules
|
||||
.map((module) => [
|
||||
|
|
@ -418,7 +493,7 @@ export function createToolboxExportPayload(toolbox, moduleData) {
|
|||
remapModuleDataForExport(module.type, moduleData[moduleStorageKey(source.id, module.id)] || null, nextId)
|
||||
])
|
||||
.filter(([, data]) => data != null));
|
||||
return { toolbox: exportedToolbox, modules };
|
||||
return { version: 2, toolbox: exportedToolbox, modules };
|
||||
}
|
||||
|
||||
export function createGlobalExportPayload(toolboxes, links, moduleData) {
|
||||
|
|
@ -430,7 +505,7 @@ export function createGlobalExportPayload(toolboxes, links, moduleData) {
|
|||
});
|
||||
});
|
||||
return {
|
||||
version: 1,
|
||||
version: 2,
|
||||
exportedAt: new Date().toISOString(),
|
||||
toolboxes: compactToolboxesForStorage(toolboxes),
|
||||
modules,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue