Fix Location Settings (due to Normalised Cache bugs) (#2023)

It do be broke
This commit is contained in:
Oscar Beaumont 2024-01-30 23:52:05 +08:00 committed by GitHub
parent 6fe2637ae7
commit c1ae1aed37
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 38 additions and 4 deletions

4
Cargo.lock generated
View file

@ -7453,7 +7453,7 @@ dependencies = [
[[package]]
name = "sd-core"
version = "0.2.0"
version = "0.2.2"
dependencies = [
"aovec",
"async-channel",
@ -7590,7 +7590,7 @@ dependencies = [
[[package]]
name = "sd-desktop"
version = "0.2.0"
version = "0.2.2"
dependencies = [
"axum",
"futures",

View file

@ -31,7 +31,7 @@ const FlexCol = tw.label`flex flex-col flex-1`;
const ToggleSection = tw.label`flex flex-row w-full`;
const schema = z.object({
name: z.string().nullable(),
name: z.string().min(1).nullable(),
path: z.string().min(1).nullable(),
hidden: z.boolean().nullable(),
indexerRulesIds: z.array(z.number()),

View file

@ -143,7 +143,13 @@ function restore(cache: Store, subscribed: Map<string, Set<unknown>>, item: unkn
subscribed.set(item.__type, new Set([item.__id]));
}
return result;
// We call restore again for arrays and objects to deal with nested relations.
return Object.fromEntries(
Object.entries(result).map(([key, value]) => [
key,
restore(cache, subscribed, value)
])
);
}
return Object.fromEntries(
@ -186,12 +192,40 @@ function updateNodes(cache: Store, data: CacheNode[] | undefined) {
delete copy.__type;
delete copy.__id;
const original = cache.nodes?.[item.__type]?.[item.__id];
specialMerge(copy, original);
if (!cache.nodes[item.__type]) cache.nodes[item.__type] = {};
// TODO: This should be a deepmerge but that would break stuff like `size_in_bytes` or `inode` as the arrays are joined.
cache.nodes[item.__type]![item.__id] = copy;
}
}
// When using PCR's data structure if you don't fetch a relation `null` is returned.
// If two queries return a single entity but one fetches relations and the other doesn't that null might "win" over the actual data.
// Once it "wins" the normalised cache is updated causing all `useCache`'s to rerun.
//
// The `useCache` hook derives the type from the specific React Query operation.
// Due to this the result of a `useCache` might end up as `null` even when TS says it's `T` causing crashes due to no-null checks.
//
// So this merge function causes the `null` to be replaced with the original value.
function specialMerge(copy: Record<any, any>, original: unknown) {
if (
original &&
typeof original === 'object' &&
typeof copy === 'object' &&
!Array.isArray(original) &&
!Array.isArray(copy)
) {
for (const [property, value] of Object.entries(original)) {
copy[property] = copy[property] || value;
if (typeof copy[property] === 'object' && !Array.isArray(copy[property]))
specialMerge(copy[property], value);
}
}
}
export type UseCacheResult<T> = T extends (infer A)[]
? UseCacheResult<A>[]
: T extends object