fix(Map): add key for cache changes detecting

Fixes #6
This commit is contained in:
achichenkov
2024-09-21 11:26:25 +03:00
parent bee64c2570
commit c7866a1270
2 changed files with 7 additions and 3 deletions

View File

@@ -38,7 +38,7 @@ export const RoutesWidgetContent = () => {
const { loading } = useLoadRoutes(); const { loading } = useLoadRoutes();
const { systems: systemStatics, loadSystems } = useLoadSystemStatic({ systems: hubs ?? [] }); const { systems: systemStatics, loadSystems, lastUpdateKey } = useLoadSystemStatic({ systems: hubs ?? [] });
const { open, ...systemCtxProps } = useContextMenuSystemInfoHandlers({ const { open, ...systemCtxProps } = useContextMenuSystemInfoHandlers({
outCommand, outCommand,
hubs, hubs,
@@ -51,7 +51,8 @@ export const RoutesWidgetContent = () => {
return { ...systemStatics.get(parseInt(x))!, ...(sys && { customName: sys.name ?? '' }) }; return { ...systemStatics.get(parseInt(x))!, ...(sys && { customName: sys.name ?? '' }) };
}); });
}, [hubs, systems, systemStatics]); // eslint-disable-next-line react-hooks/exhaustive-deps
}, [hubs, systems, systemStatics, lastUpdateKey]);
const preparedRoutes = useMemo(() => { const preparedRoutes = useMemo(() => {
return ( return (

View File

@@ -27,12 +27,14 @@ interface UseLoadSystemStaticProps {
export const useLoadSystemStatic = ({ systems }: UseLoadSystemStaticProps) => { export const useLoadSystemStatic = ({ systems }: UseLoadSystemStaticProps) => {
const { outCommand } = useMapRootState(); const { outCommand } = useMapRootState();
const [loading, setLoading] = useState(false); const [loading, setLoading] = useState(false);
const [lastUpdateKey, setLastUpdateKey] = useState(0);
const ref = useRef({ outCommand }); const ref = useRef({ outCommand });
ref.current = { outCommand }; ref.current = { outCommand };
const addSystemStatic = useCallback((static_info: SolarSystemStaticInfoRaw) => { const addSystemStatic = useCallback((static_info: SolarSystemStaticInfoRaw) => {
cache.set(static_info.solar_system_id, static_info); cache.set(static_info.solar_system_id, static_info);
setLastUpdateKey(new Date().getTime());
}, []); }, []);
const loadSystems = useCallback(async (systems: (number | string)[]) => { const loadSystems = useCallback(async (systems: (number | string)[]) => {
@@ -43,6 +45,7 @@ export const useLoadSystemStatic = ({ systems }: UseLoadSystemStaticProps) => {
if (toLoad.length > 0) { if (toLoad.length > 0) {
const res = await loadSystemStaticInfo(ref.current.outCommand, toLoad); const res = await loadSystemStaticInfo(ref.current.outCommand, toLoad);
res.forEach(x => cache.set(x.solar_system_id, x)); res.forEach(x => cache.set(x.solar_system_id, x));
setLastUpdateKey(new Date().getTime());
} }
setLoading(false); setLoading(false);
}, []); }, []);
@@ -52,5 +55,5 @@ export const useLoadSystemStatic = ({ systems }: UseLoadSystemStaticProps) => {
// eslint-disable-next-line // eslint-disable-next-line
}, [systems]); }, [systems]);
return { addSystemStatic, systems: cache, loading, loadSystems }; return { addSystemStatic, systems: cache, lastUpdateKey, loading, loadSystems };
}; };