feat: clearing library cache (#6621)

Co-authored-by: dwelle <luzar.david@gmail.com>
This commit is contained in:
Arnost Pleskot 2023-05-29 16:01:44 +02:00 committed by GitHub
parent 08563e7d7b
commit a91e401554
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 86 additions and 41 deletions

View file

@ -1,12 +1,13 @@
import { atom, useAtom } from "jotai";
import { useEffect, useState } from "react";
import { COLOR_PALETTE } from "../colors";
import { jotaiScope } from "../jotai";
import { exportToSvg } from "../packages/utils";
import { LibraryItem } from "../types";
export const libraryItemSvgsCache = atom<Map<LibraryItem["id"], SVGSVGElement>>(
new Map(),
);
export type SvgCache = Map<LibraryItem["id"], SVGSVGElement>;
export const libraryItemSvgsCache = atom<SvgCache>(new Map());
const exportLibraryItemToSvg = async (elements: LibraryItem["elements"]) => {
return await exportToSvg({
@ -22,8 +23,8 @@ const exportLibraryItemToSvg = async (elements: LibraryItem["elements"]) => {
export const useLibraryItemSvg = (
id: LibraryItem["id"] | null,
elements: LibraryItem["elements"] | undefined,
svgCache: SvgCache,
): SVGSVGElement | undefined => {
const [svgCache, setSvgCache] = useAtom(libraryItemSvgsCache);
const [svg, setSvg] = useState<SVGSVGElement>();
useEffect(() => {
@ -40,7 +41,7 @@ export const useLibraryItemSvg = (
const exportedSvg = await exportLibraryItemToSvg(elements);
if (exportedSvg) {
setSvgCache(svgCache.set(id, exportedSvg));
svgCache.set(id, exportedSvg);
setSvg(exportedSvg);
}
})();
@ -53,7 +54,23 @@ export const useLibraryItemSvg = (
})();
}
}
}, [id, elements, svgCache, setSvgCache, setSvg]);
}, [id, elements, svgCache, setSvg]);
return svg;
};
export const useLibraryCache = () => {
const [svgCache] = useAtom(libraryItemSvgsCache, jotaiScope);
const clearLibraryCache = () => svgCache.clear();
const deleteItemsFromLibraryCache = (items: LibraryItem["id"][]) => {
items.forEach((item) => svgCache.delete(item));
};
return {
clearLibraryCache,
deleteItemsFromLibraryCache,
svgCache,
};
};