Clarify local persistence and backup states
This commit is contained in:
parent
2f2e79d200
commit
1171eee510
24 changed files with 678 additions and 269 deletions
|
|
@ -29,7 +29,32 @@ function getLibraryUrl() {
|
|||
return `${window.location.origin}${window.location.pathname}#/library`;
|
||||
}
|
||||
|
||||
export function useToolboxActions({ store, notify, creationTemplate, setConfirmModal, setCreateModal, setLinkModalGameId, setDrawerGameId, setImage }) {
|
||||
export function useToolboxActions({ store, notify, creationTemplate, importContent, setConfirmModal, setCreateModal, setLinkModalGameId, setDrawerGameId, setImage }) {
|
||||
const importText = {
|
||||
replaceLinksTitle: importContent?.replaceLinksTitle || "Réattribuer des associations de jeu ?",
|
||||
replaceLinksMessage: importContent?.replaceLinksMessage || "Des associations de jeu existantes ({count}) seront réattribuées aux copies importées. Les toolboxes déjà présentes seront conservées.",
|
||||
replaceLinksConfirm: importContent?.replaceLinksConfirm || "Importer et réattribuer",
|
||||
cancel: importContent?.cancel || "Annuler"
|
||||
};
|
||||
|
||||
function confirmLinkReplacement(count, onConfirm) {
|
||||
return new Promise((resolve, reject) => {
|
||||
setConfirmModal({
|
||||
title: importText.replaceLinksTitle,
|
||||
message: importText.replaceLinksMessage.replace("{count}", String(count)),
|
||||
confirmLabel: importText.replaceLinksConfirm,
|
||||
cancelLabel: importText.cancel,
|
||||
onResolve: (confirmed) => {
|
||||
if (!confirmed) {
|
||||
resolve(false);
|
||||
return;
|
||||
}
|
||||
Promise.resolve(onConfirm()).then(resolve, reject);
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function createToolbox(name, gameId = "", options = {}) {
|
||||
if (!store.ready) return null;
|
||||
const now = new Date().toISOString();
|
||||
|
|
@ -181,8 +206,7 @@ export function useToolboxActions({ store, notify, creationTemplate, setConfirmM
|
|||
return true;
|
||||
}
|
||||
|
||||
async function importToolboxPayload(file, gameId = "") {
|
||||
const payload = validateToolboxImportPayload(JSON.parse(await file.text()), "single");
|
||||
async function importToolboxPayload(payload, gameId = "") {
|
||||
const imported = normalizeToolbox({ ...payload.toolbox, id: uid("tbx"), updatedAt: new Date().toISOString() });
|
||||
const moduleIdMap = new Map();
|
||||
imported.modules = imported.modules.map((module) => {
|
||||
|
|
@ -218,8 +242,7 @@ export function useToolboxActions({ store, notify, creationTemplate, setConfirmM
|
|||
return normalizedImport;
|
||||
}
|
||||
|
||||
async function importAllToolboxesPayload(file) {
|
||||
const payload = validateToolboxImportPayload(JSON.parse(await file.text()), "global");
|
||||
async function importAllToolboxesPayload(payload) {
|
||||
const toolboxIdMap = new Map();
|
||||
const moduleIdMap = new Map();
|
||||
const importedToolboxes = payload.toolboxes.map((toolbox) => {
|
||||
|
|
@ -301,7 +324,11 @@ export function useToolboxActions({ store, notify, creationTemplate, setConfirmM
|
|||
importToolbox: async (file, gameId = "") => {
|
||||
if (!store.ready) return null;
|
||||
try {
|
||||
const toolbox = await importToolboxPayload(file, gameId);
|
||||
const payload = validateToolboxImportPayload(JSON.parse(await file.text()), "single");
|
||||
const runImport = () => importToolboxPayload(payload, gameId);
|
||||
const toolbox = gameId && store.links?.[gameId]
|
||||
? await confirmLinkReplacement(1, runImport)
|
||||
: await runImport();
|
||||
if (toolbox) notify("Toolbox importée.");
|
||||
return toolbox;
|
||||
} catch (error) {
|
||||
|
|
@ -318,7 +345,13 @@ export function useToolboxActions({ store, notify, creationTemplate, setConfirmM
|
|||
importAllToolboxes: async (file) => {
|
||||
if (!store.ready) return false;
|
||||
try {
|
||||
await importAllToolboxesPayload(file);
|
||||
const payload = validateToolboxImportPayload(JSON.parse(await file.text()), "global");
|
||||
const reassignedLinks = Object.keys(payload.links || {}).filter((gameId) => store.links?.[gameId]);
|
||||
const runImport = () => importAllToolboxesPayload(payload).then(() => true);
|
||||
const imported = reassignedLinks.length
|
||||
? await confirmLinkReplacement(reassignedLinks.length, runImport)
|
||||
: await runImport();
|
||||
if (!imported) return false;
|
||||
notify("Import global terminé.");
|
||||
return true;
|
||||
} catch (error) {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue