novo fix
This commit is contained in:
1 parent
822ee2d557
commit
ad0025ef84
7 files changed
+173
-106
No files matched your search
@@ -0,0 +1,48 @@
|
||||
import { useEffect, useMemo, useReducer, useRef } from "react";
|
||||
import { api, type Structure } from "../lib/api.js";
|
||||
|
||||
/**
|
||||
* Carrega estruturas SOMENTE dos sistemas atualmente visíveis (uma
|
||||
* requisição por sistema ainda não cacheado), em vez de buscar as ~5000
|
||||
* estruturas do banco de uma vez — isso não escala (estourava timeout de
|
||||
* proxy e sobrecarregava o pool de conexões do Postgres) desde que o
|
||||
* dataset passou a usar malhas reais do Z-Anatomy.
|
||||
*/
|
||||
export function useSystemStructures(visibleSystems: Set<string>) {
|
||||
const cacheRef = useRef<Map<string, Structure[]>>(new Map());
|
||||
const [version, bump] = useReducer((x: number) => x + 1, 0);
|
||||
|
||||
useEffect(() => {
|
||||
const missing = [...visibleSystems].filter((slug) => !cacheRef.current.has(slug));
|
||||
if (missing.length === 0) return;
|
||||
let cancelled = false;
|
||||
|
||||
Promise.all(
|
||||
missing.map((slug) =>
|
||||
api.structures({ system: slug }).then((data) => [slug, data] as const),
|
||||
),
|
||||
).then((pairs) => {
|
||||
if (cancelled) return;
|
||||
for (const [slug, data] of pairs) cacheRef.current.set(slug, data);
|
||||
bump();
|
||||
});
|
||||
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [visibleSystems]);
|
||||
|
||||
const structures = useMemo(
|
||||
() => [...cacheRef.current.values()].flat(),
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
[version, visibleSystems],
|
||||
);
|
||||
|
||||
const bySlug = useMemo(() => {
|
||||
const map = new Map<string, Structure>();
|
||||
for (const s of structures) map.set(s.slug, s);
|
||||
return map;
|
||||
}, [structures]);
|
||||
|
||||
return { structures, bySlug };
|
||||
}
|
||||
Reference in new issue
Block a user