Fix bug with cleaned up assets not reloading

This commit is contained in:
Mitchell McCaffrey 2021-06-25 20:38:52 +10:00
parent 8b1d208a72
commit 90aa43dbf0

View File

@ -132,18 +132,21 @@ export function AssetURLsProvider({ children }) {
const { database } = useDatabase(); const { database } = useDatabase();
// Keep track of when the asset keys change so we can update the URLs // Keep track of when the asset keys change so we can update the URLs
const [assetKeys, setAssetKeys] = useState(new Set()); const [assetKeys, setAssetKeys] = useState([]);
useEffect(() => { useEffect(() => {
const keys = Object.keys(assetURLs); const keys = Object.keys(assetURLs);
let newKeys = keys.filter((key) => !assetKeys.has(key)); let newKeys = keys.filter((key) => !assetKeys.includes(key));
if (newKeys.length > 0) { let deletedKeys = assetKeys.filter((key) => !keys.includes(key));
setAssetKeys((prevKeys) => new Set([...prevKeys, ...newKeys])); if (newKeys.length > 0 || deletedKeys.length > 0) {
setAssetKeys((prevKeys) =>
[...prevKeys, ...newKeys].filter((key) => !deletedKeys.includes(key))
);
} }
}, [assetURLs, assetKeys]); }, [assetURLs, assetKeys]);
// Get the new assets whenever the keys change // Get the new assets whenever the keys change
const assets = useLiveQuery( const assets = useLiveQuery(
() => database?.table("assets").where(":id").anyOf(assetKeys).toArray(), () => database?.table("assets").where("id").anyOf(assetKeys).toArray(),
[database, assetKeys] [database, assetKeys]
); );
@ -155,10 +158,13 @@ export function AssetURLsProvider({ children }) {
setAssetURLs((prevURLs) => { setAssetURLs((prevURLs) => {
let newURLs = { ...prevURLs }; let newURLs = { ...prevURLs };
for (let asset of assets) { for (let asset of assets) {
if (!newURLs[asset.id].url) { if (newURLs[asset.id].url === null) {
newURLs[asset.id].url = URL.createObjectURL( newURLs[asset.id] = {
new Blob([asset.file], { type: asset.mime }) ...newURLs[asset.id],
); url: URL.createObjectURL(
new Blob([asset.file], { type: asset.mime })
),
};
} }
} }
return newURLs; return newURLs;
@ -213,8 +219,6 @@ export function useAssetURL(assetId, type, defaultSources, unknownSource) {
throw new Error("useAssetURL must be used within a AssetURLsProvider"); throw new Error("useAssetURL must be used within a AssetURLsProvider");
} }
const { getAsset } = useAssets();
useEffect(() => { useEffect(() => {
if (!assetId || type !== "file") { if (!assetId || type !== "file") {
return; return;
@ -265,7 +269,7 @@ export function useAssetURL(assetId, type, defaultSources, unknownSource) {
} }
}); });
}; };
}, [assetId, setAssetURLs, getAsset, type]); }, [assetId, setAssetURLs, type]);
if (!assetId) { if (!assetId) {
return unknownSource; return unknownSource;