Coalesce quota refreshes and consolidate shared helpers
Some checks failed
Deploy Sokko G / deploy (push) Failing after 2s
Some checks failed
Deploy Sokko G / deploy (push) Failing after 2s
This commit is contained in:
parent
a4cf633ab4
commit
748415082b
19 changed files with 216 additions and 64 deletions
|
|
@ -1,5 +1,5 @@
|
|||
// Rôle : synchronise l'état optimiste des toolboxes avec une file d'écritures IndexedDB.
|
||||
import { useCallback, useEffect, useState } from "react";
|
||||
import { useCallback, useEffect, useRef, useState } from "react";
|
||||
import {
|
||||
getAllModuleData as dbGetAllModuleData,
|
||||
getLinks as dbGetLinks,
|
||||
|
|
@ -22,6 +22,7 @@ import {
|
|||
} from "./toolboxStorage.js";
|
||||
import { applyModuleDataValue, removeModuleDataKeys } from "./optimisticModuleData.js";
|
||||
import { createPersistenceQueue } from "./persistenceQueue.js";
|
||||
import { createQuotaRefresh } from "./quotaRefresh.js";
|
||||
|
||||
function normalizeToolboxes(toolboxes) {
|
||||
return compactToolboxesForStorage(toolboxes).map(normalizeToolbox).filter(Boolean);
|
||||
|
|
@ -32,20 +33,29 @@ export function useIndexedToolboxes(onError) {
|
|||
const [state, setState] = useState({ toolboxes: [], links: {}, moduleData: {}, toastPosition: "right" });
|
||||
const [queue] = useState(() => createPersistenceQueue(state, setState));
|
||||
const [storageUsage, setStorageUsage] = useState({ used: 0, limit: 0, ratio: 0 });
|
||||
const quotaRefreshRef = useRef(null);
|
||||
const reportStorageError = useCallback((error) => {
|
||||
onError(error?.message || "Erreur de stockage IndexedDB.");
|
||||
}, [onError]);
|
||||
|
||||
const refreshQuota = useCallback(async () => {
|
||||
try {
|
||||
const estimate = await getStorageEstimate();
|
||||
const used = estimate.usage || 0;
|
||||
const limit = estimate.quota || 0;
|
||||
setStorageUsage({ used, limit, ratio: limit ? Math.min(1, used / limit) : 0 });
|
||||
} catch {
|
||||
setStorageUsage({ used: 0, limit: 0, ratio: 0 });
|
||||
}
|
||||
useEffect(() => {
|
||||
const refresh = createQuotaRefresh({
|
||||
estimate: getStorageEstimate,
|
||||
onResult: (estimate) => {
|
||||
const used = estimate.usage || 0;
|
||||
const limit = estimate.quota || 0;
|
||||
setStorageUsage((current) => current.used === used && current.limit === limit
|
||||
? current
|
||||
: { used, limit, ratio: limit ? Math.min(1, used / limit) : 0 });
|
||||
}
|
||||
});
|
||||
quotaRefreshRef.current = refresh;
|
||||
return () => {
|
||||
refresh.dispose();
|
||||
quotaRefreshRef.current = null;
|
||||
};
|
||||
}, []);
|
||||
const refreshQuota = useCallback(() => quotaRefreshRef.current?.request(), []);
|
||||
|
||||
useEffect(() => {
|
||||
let cancelled = false;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue