-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Ross Bulat
committed
Mar 12, 2024
1 parent
e4b05bb
commit 0607354
Showing
51 changed files
with
158 additions
and
167 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright 2024 @rossbulat/console authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
/* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-empty-function */ | ||
|
||
import type { RouteContextInterface } from './types'; | ||
|
||
export const defaultActivePage = 0; | ||
|
||
export const defaultRouteContext: RouteContextInterface = { | ||
activePage: defaultActivePage, | ||
setActivePage: (section, persist) => {}, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2024 @rossbulat/console authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
import { createContext, useContext, useState } from 'react'; | ||
import { defaultActivePage, defaultRouteContext } from './defaults'; | ||
import type { RouteContextInterface, RouteContextProps } from './types'; | ||
import * as local from './Local'; | ||
import { useEffectIgnoreInitial } from '@w3ux/hooks'; | ||
import { useTabs } from 'contexts/Tabs'; | ||
import { useApi } from 'contexts/Api'; | ||
|
||
export const RouteContext = | ||
createContext<RouteContextInterface>(defaultRouteContext); | ||
|
||
export const useRoute = () => useContext(RouteContext); | ||
|
||
export const RouteProvider = ({ route, children }: RouteContextProps) => { | ||
const { activeTabId } = useTabs(); | ||
const { redirectCounter } = useTabs(); | ||
const { getApiActive, getApiStatus } = useApi(); | ||
|
||
const apiStatus = getApiStatus(activeTabId); | ||
const apiActive = getApiActive(activeTabId); | ||
|
||
// The active section of the page. Falls back to default section if not connected. | ||
const [activePage, setActivePageState] = useState<number>( | ||
!apiActive | ||
? defaultActivePage | ||
: local.getActivePage(route, activeTabId) || defaultActivePage | ||
); | ||
|
||
// Sets active section, and updates local storage if persisted. | ||
const setActivePage = (section: number, persist = true) => { | ||
if (persist) { | ||
local.setActivePage(route, activeTabId, section); | ||
} | ||
setActivePageState(section); | ||
}; | ||
|
||
// Handle redirects from local storage, if present. Also redirects back to default section if api | ||
// is not active. | ||
useEffectIgnoreInitial(() => { | ||
const redirect = local.getSectionRedirect(route, activeTabId); | ||
const localActive = local.getActivePage(route, activeTabId); | ||
|
||
if (redirect) { | ||
setActivePage(redirect || localActive || defaultActivePage, false); | ||
} else { | ||
setActivePage( | ||
!apiActive ? defaultActivePage : localActive || defaultActivePage, | ||
false | ||
); | ||
} | ||
}, [route, activeTabId, redirectCounter, apiActive, apiStatus]); | ||
|
||
return ( | ||
<RouteContext.Provider | ||
value={{ | ||
activePage, | ||
setActivePage, | ||
}} | ||
> | ||
{children} | ||
</RouteContext.Provider> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2024 @rossbulat/console authors & contributors | ||
// SPDX-License-Identifier: GPL-3.0-only | ||
|
||
import type { Route } from 'App'; | ||
import type { ReactNode } from 'react'; | ||
|
||
export interface RouteContextInterface { | ||
activePage: number; | ||
setActivePage: (section: number, persist?: boolean) => void; | ||
} | ||
|
||
export interface RouteContextProps { | ||
route: Route; | ||
children: ReactNode; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.