From a412895d5d4f46c094d824c981f3833acabd2c43 Mon Sep 17 00:00:00 2001 From: Max Date: Thu, 6 Mar 2025 00:42:02 +0100 Subject: [PATCH] Simplify the ContextWrapper (#182) Co-authored-by: Anton Tranelis <31516529+antontranelis@users.noreply.github.com> --- src/Components/AppShell/ContextWrapper.tsx | 42 ++++++---------------- 1 file changed, 11 insertions(+), 31 deletions(-) diff --git a/src/Components/AppShell/ContextWrapper.tsx b/src/Components/AppShell/ContextWrapper.tsx index ece603ad..67620cdf 100644 --- a/src/Components/AppShell/ContextWrapper.tsx +++ b/src/Components/AppShell/ContextWrapper.tsx @@ -1,6 +1,6 @@ import { QueryClient, QueryClientProvider } from '@tanstack/react-query' import { useContext, createContext } from 'react' -import { BrowserRouter as Router, useLocation } from 'react-router-dom' +import { BrowserRouter as Router, useInRouterContext } from 'react-router-dom' import { ToastContainer } from 'react-toastify' import { QuestsProvider } from '#components/Gaming/hooks/useQuests' @@ -21,43 +21,23 @@ const ContextCheckContext = createContext(false) export const ContextWrapper = ({ children }: { children: React.ReactNode }) => { const isWrapped = useContext(ContextCheckContext) - // Check if we are already inside a Router - let location - try { - // eslint-disable-next-line react-hooks/rules-of-hooks - location = useLocation() - // eslint-disable-next-line no-catch-all/no-catch-all - } catch (e) { - location = null - } + const isInsideRouter = useInRouterContext() - // Case 1: Only the Router is missing, but ContextWrapper is already provided - if (!location && isWrapped) { - return {children} - } + let returnValue = children - // Case 2: Neither Router nor ContextWrapper is present - if (!location && !isWrapped) { - return ( - - - {children} - - - ) - } - - // Case 3: Only ContextWrapper is missing - if (location && !isWrapped) { - return ( + if (!isWrapped) { + returnValue = ( - {children} + {returnValue} ) } - // Case 4: Both Router and ContextWrapper are already present - return children + if (!isInsideRouter) { + returnValue = {returnValue} + } + + return returnValue } // eslint-disable-next-line react/prop-types