Add local automatic backups and Aniimo regions lists
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
2255de94a7
commit
0a3bb4dffd
24 changed files with 908 additions and 16 deletions
148
website/src/features/toolboxes/storage/useAutoBackup.js
Normal file
148
website/src/features/toolboxes/storage/useAutoBackup.js
Normal file
|
|
@ -0,0 +1,148 @@
|
|||
// Rôle : expose la configuration et les actions de sauvegarde automatique à l'interface.
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import { createGlobalExportPayload } from "./toolboxStorage.js";
|
||||
import { getSetting, setSetting } from "../../../utils/indexedDbStorage.js";
|
||||
import {
|
||||
AUTO_BACKUP_SETTING,
|
||||
getBackupFileName,
|
||||
getDirectoryPermission,
|
||||
isFileSystemAccessSupported,
|
||||
normalizeAutoBackupConfig,
|
||||
saveBackup
|
||||
} from "./autoBackup.js";
|
||||
|
||||
function serializeGlobalExport(snapshot) {
|
||||
return JSON.stringify(createGlobalExportPayload(snapshot.toolboxes, snapshot.links, snapshot.moduleData), null, 2);
|
||||
}
|
||||
|
||||
export function useAutoBackup(store) {
|
||||
const supported = isFileSystemAccessSupported(typeof window === "undefined" ? undefined : window);
|
||||
const [config, setConfig] = useState(() => normalizeAutoBackupConfig());
|
||||
const [loaded, setLoaded] = useState(false);
|
||||
const [busy, setBusy] = useState(false);
|
||||
const [permission, setPermission] = useState("unknown");
|
||||
const [error, setError] = useState("");
|
||||
const runRef = useRef(false);
|
||||
const startedRef = useRef(false);
|
||||
|
||||
const persist = useCallback(async (next) => {
|
||||
const normalized = normalizeAutoBackupConfig(next);
|
||||
setConfig(normalized);
|
||||
try {
|
||||
await setSetting(AUTO_BACKUP_SETTING, normalized);
|
||||
} catch (persistError) {
|
||||
setError(persistError?.message || "Impossible d'enregistrer les préférences de sauvegarde.");
|
||||
}
|
||||
return normalized;
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
getSetting(AUTO_BACKUP_SETTING, normalizeAutoBackupConfig()).then((value) => {
|
||||
if (!cancelled) {
|
||||
setConfig(normalizeAutoBackupConfig(value));
|
||||
setLoaded(true);
|
||||
}
|
||||
}).catch((loadError) => {
|
||||
if (!cancelled) {
|
||||
setLoaded(true);
|
||||
setError(loadError?.message || "Impossible de lire les préférences de sauvegarde.");
|
||||
}
|
||||
});
|
||||
return () => { cancelled = true; };
|
||||
}, []);
|
||||
|
||||
const run = useCallback(async ({ force = false, directoryHandle = config.directoryHandle, configOverride = config } = {}) => {
|
||||
if (!supported || !directoryHandle || runRef.current) return { ok: false, reason: "unavailable" };
|
||||
runRef.current = true;
|
||||
setBusy(true);
|
||||
try {
|
||||
const result = await saveBackup({
|
||||
directoryHandle,
|
||||
force,
|
||||
generateJson: async () => {
|
||||
const snapshot = await store.getExportSnapshot(undefined, { onError: () => {} });
|
||||
if (!snapshot) throw new Error("Impossible de préparer les données à sauvegarder.");
|
||||
return serializeGlobalExport(snapshot);
|
||||
}
|
||||
});
|
||||
setPermission(result.permission || await getDirectoryPermission(directoryHandle));
|
||||
if (result.ok && !result.cleanupError) setError("");
|
||||
if (result.ok && result.created) {
|
||||
const next = { ...configOverride, directoryHandle, lastBackup: new Date().toISOString() };
|
||||
await persist(next);
|
||||
}
|
||||
if (!result.ok) setError(result.error?.message || "La sauvegarde locale a échoué.");
|
||||
if (result.ok && result.cleanupError) setError(result.cleanupError);
|
||||
return result;
|
||||
} catch (runError) {
|
||||
setError(runError?.message || "La sauvegarde locale a échoué.");
|
||||
return { ok: false, reason: "backup", error: runError };
|
||||
} finally {
|
||||
runRef.current = false;
|
||||
setBusy(false);
|
||||
}
|
||||
}, [config, persist, store, supported]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!loaded || !supported || !store.ready || !config.enabled || !config.directoryHandle || startedRef.current) return;
|
||||
startedRef.current = true;
|
||||
void run();
|
||||
}, [config.directoryHandle, config.enabled, loaded, run, store.ready, supported]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!loaded || !config.directoryHandle) return;
|
||||
void getDirectoryPermission(config.directoryHandle).then(setPermission);
|
||||
}, [config.directoryHandle, loaded]);
|
||||
|
||||
const chooseDirectory = useCallback(async () => {
|
||||
if (!supported) return false;
|
||||
try {
|
||||
const directoryHandle = await window.showDirectoryPicker({ mode: "readwrite" });
|
||||
const next = await persist({ ...config, directoryHandle });
|
||||
setPermission("granted");
|
||||
if (next.enabled) void run({ directoryHandle, configOverride: next });
|
||||
return true;
|
||||
} catch (chooseError) {
|
||||
if (chooseError?.name !== "AbortError") setError(chooseError?.message || "Impossible de choisir ce dossier.");
|
||||
return false;
|
||||
}
|
||||
}, [config, persist, run, supported]);
|
||||
|
||||
const requestPermission = useCallback(async () => {
|
||||
if (!config.directoryHandle?.requestPermission) return false;
|
||||
try {
|
||||
const result = await config.directoryHandle.requestPermission({ mode: "readwrite" });
|
||||
setPermission(result);
|
||||
if (result === "granted" && config.enabled) void run();
|
||||
return result === "granted";
|
||||
} catch (permissionError) {
|
||||
setError(permissionError?.message || "Impossible de réautoriser ce dossier.");
|
||||
return false;
|
||||
}
|
||||
}, [config.directoryHandle, config.enabled, run]);
|
||||
|
||||
const setEnabled = useCallback(async (enabled) => {
|
||||
const next = await persist({ ...config, enabled });
|
||||
if (enabled && next.directoryHandle) {
|
||||
const currentPermission = await getDirectoryPermission(next.directoryHandle);
|
||||
setPermission(currentPermission);
|
||||
if (currentPermission === "granted") void run({ configOverride: next });
|
||||
}
|
||||
}, [config, persist, run]);
|
||||
|
||||
return {
|
||||
supported,
|
||||
config,
|
||||
busy,
|
||||
permission,
|
||||
error,
|
||||
setError,
|
||||
setEnabled,
|
||||
chooseDirectory,
|
||||
requestPermission,
|
||||
saveNow: () => run({ force: true }),
|
||||
filename: getBackupFileName(),
|
||||
retention: 10
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue