Stop orphan game links from blocking global imports
A game link could outlive its toolbox when one tab linked a game while another tab, unaware of that link, deleted the toolbox. Auto-backups and manual exports then copied the orphan link, and the global import rejected the whole file. - Drop links to missing toolboxes inside the IndexedDB transaction. - Remove orphan links already stored when the page loads. - Ignore a link request to a toolbox that no longer exists. - Leave orphan links out of exports and skip them on global import, so older backups import again. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
98c442a9cd
commit
d030c3f59f
9 changed files with 137 additions and 9 deletions
|
|
@ -292,9 +292,10 @@ export function validateToolboxImportPayload(payload, mode = "single") {
|
|||
});
|
||||
validateModuleData(validated, payload.modules, true);
|
||||
assertRecord(payload.links, "links");
|
||||
// Une association vers une toolbox absente vient d'une ancienne sauvegarde : elle est ignorée à l'import.
|
||||
for (const [gameId, toolboxId] of Object.entries(payload.links)) {
|
||||
assertString(gameId, "identifiant de jeu");
|
||||
assertReference(toolboxId, toolboxIds, `links.${gameId}`, { allowEmpty: false });
|
||||
assertString(toolboxId, `links.${gameId}`, { allowColon: false });
|
||||
}
|
||||
return payload;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -510,6 +510,14 @@ export function createToolboxExportPayload(toolbox, moduleData) {
|
|||
return { version: 2, toolbox: exportedToolbox, modules };
|
||||
}
|
||||
|
||||
// Ne garde que les associations de jeu dont la toolbox existe encore.
|
||||
export function pruneGameLinks(links, toolboxes) {
|
||||
if (!links || typeof links !== "object") return {};
|
||||
const toolboxIds = new Set((toolboxes || []).map((toolbox) => toolbox?.id));
|
||||
return Object.fromEntries(Object.entries(links)
|
||||
.filter(([, toolboxId]) => typeof toolboxId === "string" && toolboxIds.has(toolboxId)));
|
||||
}
|
||||
|
||||
export function createGlobalExportPayload(toolboxes, links, moduleData) {
|
||||
const modules = {};
|
||||
toolboxes.forEach((toolbox) => {
|
||||
|
|
@ -523,6 +531,6 @@ export function createGlobalExportPayload(toolboxes, links, moduleData) {
|
|||
exportedAt: new Date().toISOString(),
|
||||
toolboxes: compactToolboxesForStorage(toolboxes),
|
||||
modules,
|
||||
links
|
||||
links: pruneGameLinks(links, toolboxes)
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,13 +8,15 @@ import {
|
|||
getToolboxSnapshot as dbGetToolboxSnapshot,
|
||||
getToolboxes as dbGetToolboxes,
|
||||
mutateToolboxStorage as dbMutateToolboxStorage,
|
||||
removeOrphanLinks as dbRemoveOrphanLinks,
|
||||
setSetting as dbSetSetting,
|
||||
} from "../../../utils/indexedDbStorage.js";
|
||||
import {
|
||||
compactToolboxesForStorage,
|
||||
moduleStorageKey,
|
||||
normalizeToolbox,
|
||||
prepareModuleDataForStorage
|
||||
prepareModuleDataForStorage,
|
||||
pruneGameLinks
|
||||
} from "./toolboxStorage.js";
|
||||
import { applyModuleDataValue, removeModuleDataKeys } from "./optimisticModuleData.js";
|
||||
import { createPersistenceQueue } from "./persistenceQueue.js";
|
||||
|
|
@ -62,7 +64,7 @@ async function readStoredState({ preservedModuleData = {}, refreshedModuleKeys =
|
|||
}
|
||||
return {
|
||||
toolboxes: normalizedToolboxes,
|
||||
links: links && typeof links === "object" ? links : {},
|
||||
links: pruneGameLinks(links, normalizedToolboxes),
|
||||
moduleData,
|
||||
toastPosition: toastPosition === "left" ? "left" : "right"
|
||||
};
|
||||
|
|
@ -107,6 +109,8 @@ export function useIndexedToolboxes(onError) {
|
|||
let cancelled = false;
|
||||
async function loadStore() {
|
||||
try {
|
||||
// Le nettoyage est accessoire : un échec ne doit pas empêcher le chargement.
|
||||
await dbRemoveOrphanLinks().catch(() => 0);
|
||||
const storedState = await readStoredState();
|
||||
if (cancelled) return;
|
||||
queue.initialize(storedState);
|
||||
|
|
|
|||
|
|
@ -7,6 +7,7 @@ import {
|
|||
globalModuleKey,
|
||||
labelFromFileName,
|
||||
normalizeToolbox,
|
||||
pruneGameLinks,
|
||||
remapMemosPlannerLink,
|
||||
remapModuleOrderIds,
|
||||
uid
|
||||
|
|
@ -198,6 +199,7 @@ export function useToolboxActions({ store, notify, creationTemplate, importConte
|
|||
function linkToolboxToGame(gameId, toolboxId) {
|
||||
const updatedAt = new Date().toISOString();
|
||||
return store.persistToolboxSnapshot((current) => {
|
||||
if (toolboxId && !current.toolboxes.some((toolbox) => toolbox.id === toolboxId)) return current;
|
||||
const links = { ...current.links };
|
||||
const touched = new Set([links[gameId], toolboxId].filter(Boolean));
|
||||
if (toolboxId) links[gameId] = toolboxId;
|
||||
|
|
@ -431,7 +433,8 @@ export function useToolboxActions({ store, notify, creationTemplate, importConte
|
|||
importAllToolboxes: async (file) => {
|
||||
if (!store.ready) return false;
|
||||
try {
|
||||
const payload = validateToolboxImportPayload(JSON.parse(await file.text()), "global");
|
||||
const validated = validateToolboxImportPayload(JSON.parse(await file.text()), "global");
|
||||
const payload = { ...validated, links: pruneGameLinks(validated.links, validated.toolboxes) };
|
||||
const reassignedLinks = Object.keys(payload.links || {}).filter((gameId) => store.links?.[gameId]);
|
||||
const runImport = () => importAllToolboxesPayload(payload).then(() => true);
|
||||
const imported = reassignedLinks.length
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue