From 80ce7d21b0d599748e251422b60b195839728c4c Mon Sep 17 00:00:00 2001 From: Eduard Gert Date: Thu, 28 Mar 2024 11:27:00 +0100 Subject: [PATCH] Fix issue where the first users cache is not populated (#366) --- src/contexts/ApplicationProvider.tsx | 31 +++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/src/contexts/ApplicationProvider.tsx b/src/contexts/ApplicationProvider.tsx index 955eae7..073b5dc 100644 --- a/src/contexts/ApplicationProvider.tsx +++ b/src/contexts/ApplicationProvider.tsx @@ -3,7 +3,14 @@ import FullScreenLoading from "@components/ui/FullScreenLoading"; import { useApiCall } from "@utils/api"; import { useIsMd } from "@utils/responsive"; import { getLatestNetbirdRelease } from "@utils/version"; -import React, { useContext, useEffect, useMemo, useRef, useState } from "react"; +import React, { + useCallback, + useContext, + useEffect, + useMemo, + useRef, + useState, +} from "react"; import { useLocalStorage } from "@/hooks/useLocalStorage"; import { User } from "@/interfaces/User"; import type { NetbirdRelease } from "@/interfaces/Version"; @@ -32,13 +39,27 @@ export default function ApplicationProvider({ children }: Props) { const userRequest = useApiCall("/users", true); const [show, setShow] = useState(false); const requestCalled = useRef(false); + const maxTries = 3; + + const populateCache = useCallback( + async (tries = 0) => { + if (tries >= maxTries) { + setShow(true); + return Promise.reject(); + } + try { + await userRequest.get().then(() => setShow(true)); + return Promise.resolve(); + } catch (e) { + setTimeout(() => populateCache(tries + 1), 500); + } + }, + [userRequest, setShow], + ); useEffect(() => { if (!requestCalled.current) { - userRequest - .get() - .then(() => setShow(true)) - .catch(() => setShow(true)); + populateCache().then(); requestCalled.current = true; } // eslint-disable-next-line react-hooks/exhaustive-deps