From f201130276a51fb2a33a4a1ebb51b3823b1f4da7 Mon Sep 17 00:00:00 2001 From: Konstantin Wohlwend Date: Fri, 20 Dec 2024 10:51:40 -0800 Subject: [PATCH] Improve globe perf --- .../projects/[projectId]/metrics/globe.tsx | 50 ++++++++++++++++--- .../[projectId]/metrics/page-client.tsx | 5 +- 2 files changed, 46 insertions(+), 9 deletions(-) diff --git a/apps/dashboard/src/app/(main)/(protected)/projects/[projectId]/metrics/globe.tsx b/apps/dashboard/src/app/(main)/(protected)/projects/[projectId]/metrics/globe.tsx index 317f018f3..8c431f8c4 100644 --- a/apps/dashboard/src/app/(main)/(protected)/projects/[projectId]/metrics/globe.tsx +++ b/apps/dashboard/src/app/(main)/(protected)/projects/[projectId]/metrics/globe.tsx @@ -31,6 +31,29 @@ export function GlobeSection({ countryData, children }: {countryData: Record(null); + const resumeRender = () => { + if (!globeRef.current) { + return; + } + const old = resumeRenderIntervalRef.current; + if (old !== null) { + clearTimeout(old); + } + + // pause again after a bit + resumeRenderIntervalRef.current = setTimeout(() => { + globeRef.current!.pauseAnimation(); + resumeRenderIntervalRef.current = null; + }, 200); + + // resume animation + // we only resume if we haven't already resumed before to prevent a StackOverflow: resumeAnimation -> onZoom -> resumeRender -> resumeAnimation, etc etc + if (old === null) { + globeRef.current.resumeAnimation(); + } + }; + const [isLightMode, setIsLightMode] = useState(null); // eslint-disable-next-line react-hooks/exhaustive-deps useEffect(() => { @@ -39,9 +62,10 @@ export function GlobeSection({ countryData, children }: {countryData: Record clearInterval(interval); }, [isLightMode]); @@ -72,8 +96,14 @@ export function GlobeSection({ countryData, children }: {countryData: Record v !== null)); - return
-
+ return
+
{ + resumeRender(); + }} + > {!isGlobeReady && ( )} {isLightMode !== null && { + resumeRender(); }} animateIn={false} @@ -112,6 +144,7 @@ export function GlobeSection({ countryData, children }: {countryData: Record "transparent"} polygonAltitude={0.002} onPolygonHover={(d: any) => { + resumeRender(); if (d) { setPolygonSelectedCountry({ code: d.properties.ISO_A2_EH, name: d.properties.NAME }); } else { @@ -142,15 +175,16 @@ export function GlobeSection({ countryData, children }: {countryData: Record { + resumeRender(); if (d) { setHexSelectedCountry({ code: d.properties.ISO_A2_EH, name: d.properties.NAME }); } else { @@ -161,7 +195,7 @@ export function GlobeSection({ countryData, children }: {countryData: Record} -
+