mirror of
https://github.com/utopia-os/utopia-ui.git
synced 2025-12-13 07:46:10 +00:00
Simplify the ContextWrapper (#182)
Co-authored-by: Anton Tranelis <31516529+antontranelis@users.noreply.github.com>
This commit is contained in:
parent
9f631f156c
commit
a412895d5d
@ -1,6 +1,6 @@
|
|||||||
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
import { QueryClient, QueryClientProvider } from '@tanstack/react-query'
|
||||||
import { useContext, createContext } from 'react'
|
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 { ToastContainer } from 'react-toastify'
|
||||||
|
|
||||||
import { QuestsProvider } from '#components/Gaming/hooks/useQuests'
|
import { QuestsProvider } from '#components/Gaming/hooks/useQuests'
|
||||||
@ -21,43 +21,23 @@ const ContextCheckContext = createContext(false)
|
|||||||
export const ContextWrapper = ({ children }: { children: React.ReactNode }) => {
|
export const ContextWrapper = ({ children }: { children: React.ReactNode }) => {
|
||||||
const isWrapped = useContext(ContextCheckContext)
|
const isWrapped = useContext(ContextCheckContext)
|
||||||
|
|
||||||
// Check if we are already inside a Router
|
const isInsideRouter = useInRouterContext()
|
||||||
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
|
|
||||||
}
|
|
||||||
|
|
||||||
// Case 1: Only the Router is missing, but ContextWrapper is already provided
|
let returnValue = children
|
||||||
if (!location && isWrapped) {
|
|
||||||
return <Router>{children}</Router>
|
|
||||||
}
|
|
||||||
|
|
||||||
// Case 2: Neither Router nor ContextWrapper is present
|
if (!isWrapped) {
|
||||||
if (!location && !isWrapped) {
|
returnValue = (
|
||||||
return (
|
|
||||||
<Router>
|
|
||||||
<ContextCheckContext.Provider value={true}>
|
|
||||||
<Wrappers>{children}</Wrappers>
|
|
||||||
</ContextCheckContext.Provider>
|
|
||||||
</Router>
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Case 3: Only ContextWrapper is missing
|
|
||||||
if (location && !isWrapped) {
|
|
||||||
return (
|
|
||||||
<ContextCheckContext.Provider value={true}>
|
<ContextCheckContext.Provider value={true}>
|
||||||
<Wrappers>{children}</Wrappers>
|
<Wrappers>{returnValue}</Wrappers>
|
||||||
</ContextCheckContext.Provider>
|
</ContextCheckContext.Provider>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Case 4: Both Router and ContextWrapper are already present
|
if (!isInsideRouter) {
|
||||||
return children
|
returnValue = <Router>{returnValue}</Router>
|
||||||
|
}
|
||||||
|
|
||||||
|
return returnValue
|
||||||
}
|
}
|
||||||
|
|
||||||
// eslint-disable-next-line react/prop-types
|
// eslint-disable-next-line react/prop-types
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user