Fix Contexts

This commit is contained in:
KoalaSat 2024-01-12 13:05:15 -08:00
parent 51c5bad5c0
commit 963a3b9e00
No known key found for this signature in database
GPG Key ID: 2F7F61C6146AB157
5 changed files with 104 additions and 68 deletions

View File

@ -1,8 +1,7 @@
import React, { StrictMode, Suspense } from 'react'; import React, { StrictMode, Suspense } from 'react';
import ReactDOM from 'react-dom/client'; import ReactDOM from 'react-dom/client';
import Main from './basic/Main'; import Main from './basic/Main';
import { CssBaseline, ThemeProvider } from '@mui/material'; import { CssBaseline } from '@mui/material';
import { AppContext, useAppStore } from './contexts/AppContext';
import HostAlert from './components/HostAlert'; import HostAlert from './components/HostAlert';
import TorConnectionBadge from './components/TorConnection'; import TorConnectionBadge from './components/TorConnection';
@ -11,30 +10,25 @@ import i18n from './i18n/Web';
import { systemClient } from './services/System'; import { systemClient } from './services/System';
import ErrorBoundary from './components/ErrorBoundary'; import ErrorBoundary from './components/ErrorBoundary';
import { GarageContext, useGarageStore } from './contexts/GarageContext'; import { AppContextProvider } from './contexts/AppContext';
import { FederationContext, useFederationStore } from './contexts/FederationContext'; import { GarageContextProvider } from './contexts/GarageContext';
import { FederationContextProvider } from './contexts/FederationContext';
const App = (): JSX.Element => { const App = (): JSX.Element => {
const appStore = useAppStore();
const garageStore = useGarageStore();
const federationStore = useFederationStore();
return ( return (
<StrictMode> <StrictMode>
<ErrorBoundary> <ErrorBoundary>
<Suspense fallback='loading'> <Suspense fallback='loading'>
<I18nextProvider i18n={i18n}> <I18nextProvider i18n={i18n}>
<AppContext.Provider value={appStore}> <AppContextProvider>
<GarageContext.Provider value={garageStore}> <GarageContextProvider>
<FederationContext.Provider value={federationStore}> <FederationContextProvider>
<ThemeProvider theme={appStore.theme}>
<CssBaseline /> <CssBaseline />
{window.NativeRobosats === undefined ? <HostAlert /> : <TorConnectionBadge />} {window.NativeRobosats === undefined ? <HostAlert /> : <TorConnectionBadge />}
<Main /> <Main />
</ThemeProvider> </FederationContextProvider>
</FederationContext.Provider> </GarageContextProvider>
</GarageContext.Provider> </AppContextProvider>
</AppContext.Provider>
</I18nextProvider> </I18nextProvider>
</Suspense> </Suspense>
</ErrorBoundary> </ErrorBoundary>

View File

@ -25,8 +25,6 @@ const NavBar = (): JSX.Element => {
useContext<UseAppStoreType>(AppContext); useContext<UseAppStoreType>(AppContext);
const { garage, orderUpdatedAt, robotUpdatedAt } = useContext<UseGarageStoreType>(GarageContext); const { garage, orderUpdatedAt, robotUpdatedAt } = useContext<UseGarageStoreType>(GarageContext);
console.log('On NavBar "page" is:', page);
const navigate = useNavigate(); const navigate = useNavigate();
const location = useLocation(); const location = useLocation();
const smallBar = windowSize?.width < 50; const smallBar = windowSize?.width < 50;

View File

@ -1,6 +1,14 @@
import { createContext, type Dispatch, useEffect, useState, type SetStateAction } from 'react'; import React, {
createContext,
type Dispatch,
useEffect,
useState,
type SetStateAction,
ReactNode,
} from 'react';
import { type Page } from '../basic/NavBar'; import { type Page } from '../basic/NavBar';
import { type OpenDialogs } from '../basic/MainDialogs'; import { type OpenDialogs } from '../basic/MainDialogs';
import { ThemeProvider } from '@mui/material';
import { Settings, type Version, type Origin, type Favorites } from '../models'; import { Settings, type Version, type Origin, type Favorites } from '../models';
@ -95,6 +103,10 @@ export interface WindowSize {
height: number; height: number;
} }
export interface AppContextProviderProps {
children: ReactNode;
}
export interface UseAppStoreType { export interface UseAppStoreType {
theme?: Theme; theme?: Theme;
torStatus: TorStatus; torStatus: TorStatus;
@ -148,7 +160,7 @@ export const initialAppContext: UseAppStoreType = {
export const AppContext = createContext<UseAppStoreType>(initialAppContext); export const AppContext = createContext<UseAppStoreType>(initialAppContext);
export const useAppStore = (): UseAppStoreType => { export const AppContextProvider = ({ children }: AppContextProviderProps): JSX.Element => {
// State provided right at the top level of the app. A chaotic bucket of everything. // State provided right at the top level of the app. A chaotic bucket of everything.
// Contains app-wide state and functions. Triggers re-renders on the full tree often. // Contains app-wide state and functions. Triggers re-renders on the full tree often.
@ -176,8 +188,6 @@ export const useAppStore = (): UseAppStoreType => {
initialAppContext.acknowledgedWarning, initialAppContext.acknowledgedWarning,
); );
console.log('On AppContext "page" is:', page);
useEffect(() => { useEffect(() => {
setTheme(makeTheme(settings)); setTheme(makeTheme(settings));
}, [settings.fontSize, settings.mode, settings.lightQRs]); }, [settings.fontSize, settings.mode, settings.lightQRs]);
@ -218,7 +228,9 @@ export const useAppStore = (): UseAppStoreType => {
setWindowSize(getWindowSize(theme.typography.fontSize)); setWindowSize(getWindowSize(theme.typography.fontSize));
}; };
return { return (
<AppContext.Provider
value={{
theme, theme,
torStatus, torStatus,
settings, settings,
@ -238,5 +250,9 @@ export const useAppStore = (): UseAppStoreType => {
origin, origin,
fav, fav,
setFav, setFav,
}; }}
>
<ThemeProvider theme={theme}>{children}</ThemeProvider>
</AppContext.Provider>
);
}; };

View File

@ -1,4 +1,4 @@
import { import React, {
createContext, createContext,
type Dispatch, type Dispatch,
useEffect, useEffect,
@ -6,6 +6,7 @@ import {
type SetStateAction, type SetStateAction,
useMemo, useMemo,
useContext, useContext,
ReactNode,
} from 'react'; } from 'react';
import { type Coordinator, type Order, Federation } from '../models'; import { type Coordinator, type Order, Federation } from '../models';
@ -47,6 +48,10 @@ export interface fetchRobotProps {
isRefresh?: boolean; isRefresh?: boolean;
} }
export interface FederationContextProviderProps {
children: ReactNode;
}
export interface UseFederationStoreType { export interface UseFederationStoreType {
federation: Federation; federation: Federation;
sortedCoordinators: string[]; sortedCoordinators: string[];
@ -65,7 +70,9 @@ export const initialFederationContext: UseFederationStoreType = {
export const FederationContext = createContext<UseFederationStoreType>(initialFederationContext); export const FederationContext = createContext<UseFederationStoreType>(initialFederationContext);
export const useFederationStore = (): UseFederationStoreType => { export const FederationContextProvider = ({
children,
}: FederationContextProviderProps): JSX.Element => {
const { settings, page, origin, hostUrl, open, torStatus } = const { settings, page, origin, hostUrl, open, torStatus } =
useContext<UseAppStoreType>(AppContext); useContext<UseAppStoreType>(AppContext);
const { setMaker, garage, setBadOrder, robotUpdatedAt } = const { setMaker, garage, setBadOrder, robotUpdatedAt } =
@ -101,7 +108,6 @@ export const useFederationStore = (): UseFederationStoreType => {
setFederation(newFed); setFederation(newFed);
}, [settings.network, torStatus]); }, [settings.network, torStatus]);
console.log('On FederationContext "page" is:', page);
const onOrderReceived = (order: Order): void => { const onOrderReceived = (order: Order): void => {
let newDelay = defaultDelay; let newDelay = defaultDelay;
if (order?.bad_request) { if (order?.bad_request) {
@ -121,7 +127,6 @@ export const useFederationStore = (): UseFederationStoreType => {
garage.updateOrder(order); garage.updateOrder(order);
setBadOrder(undefined); setBadOrder(undefined);
} }
console.log('page:', page, 'Why remains as initial page?');
console.log('setting delay!', newDelay); console.log('setting delay!', newDelay);
setDelay(newDelay); setDelay(newDelay);
setTimer(setTimeout(fetchCurrentOrder, newDelay)); setTimer(setTimeout(fetchCurrentOrder, newDelay));
@ -167,11 +172,17 @@ export const useFederationStore = (): UseFederationStoreType => {
} }
}, [open.profile, hostUrl, robotUpdatedAt]); }, [open.profile, hostUrl, robotUpdatedAt]);
return { return (
<FederationContext.Provider
value={{
federation, federation,
sortedCoordinators, sortedCoordinators,
setDelay, setDelay,
coordinatorUpdatedAt, coordinatorUpdatedAt,
federationUpdatedAt, federationUpdatedAt,
}; }}
>
{children}
</FederationContext.Provider>
);
}; };

View File

@ -1,8 +1,19 @@
import { createContext, type Dispatch, useState, type SetStateAction, useEffect } from 'react'; import React, {
createContext,
type Dispatch,
useState,
type SetStateAction,
useEffect,
ReactNode,
} from 'react';
import { defaultMaker, type Maker, Garage } from '../models'; import { defaultMaker, type Maker, Garage } from '../models';
import { systemClient } from '../services/System'; import { systemClient } from '../services/System';
export interface GarageContextProviderProps {
children: ReactNode;
}
export interface UseGarageStoreType { export interface UseGarageStoreType {
garage: Garage; garage: Garage;
maker: Maker; maker: Maker;
@ -25,7 +36,7 @@ export const initialGarageContext: UseGarageStoreType = {
export const GarageContext = createContext<UseGarageStoreType>(initialGarageContext); export const GarageContext = createContext<UseGarageStoreType>(initialGarageContext);
export const useGarageStore = (): UseGarageStoreType => { export const GarageContextProvider = ({ children }: GarageContextProviderProps): JSX.Element => {
// All garage data structured // All garage data structured
const [garage] = useState<Garage>(initialGarageContext.garage); const [garage] = useState<Garage>(initialGarageContext.garage);
const [maker, setMaker] = useState<Maker>(initialGarageContext.maker); const [maker, setMaker] = useState<Maker>(initialGarageContext.maker);
@ -52,7 +63,9 @@ export const useGarageStore = (): UseGarageStoreType => {
} }
}, [systemClient.loading]); }, [systemClient.loading]);
return { return (
<GarageContext.Provider
value={{
garage, garage,
maker, maker,
setMaker, setMaker,
@ -60,5 +73,9 @@ export const useGarageStore = (): UseGarageStoreType => {
setBadOrder, setBadOrder,
robotUpdatedAt, robotUpdatedAt,
orderUpdatedAt, orderUpdatedAt,
}; }}
>
{children}
</GarageContext.Provider>
);
}; };