-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client-electron): add dynamic navigation
- Loading branch information
1 parent
21213f2
commit a5bbb5d
Showing
11 changed files
with
149 additions
and
77 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
export * from './common'; | ||
export * from './daemons-i18n'; | ||
export * from './ocr'; | ||
export { | ||
BreachProtocol, | ||
export { BreachProtocol } from './solver/breach-protocol'; | ||
export type { | ||
BreachProtocolOptions, | ||
BreachProtocolResultJSON, | ||
BreachProtocolStrategy, | ||
} from './solver/breach-protocol'; | ||
export { FocusHierarchyProvider } from './solver/hierarchy/focus-hierarchy-provider'; | ||
export { HierarchyProvider } from './solver/hierarchy/hierarchy-provider'; | ||
export type { HierarchyProvider } from './solver/hierarchy/hierarchy-provider'; | ||
export { IndexHierarchyProvider } from './solver/hierarchy/index-hierarchy-provider'; | ||
export { SequenceJSON } from './solver/sequence'; | ||
export type { SequenceJSON } from './solver/sequence'; |
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
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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
import { AppSettings } from '@/electron/common'; | ||
import { | ||
createContext, | ||
PropsWithChildren, | ||
useCallback, | ||
useContext, | ||
useLayoutEffect, | ||
useState, | ||
} from 'react'; | ||
import { NavLinkProps, To, useNavigate } from 'react-router-dom'; | ||
|
||
export interface NavigationItem extends NavLinkProps { | ||
label: string; | ||
} | ||
|
||
export interface Navigation { | ||
/** Current navigation items. */ | ||
readonly items?: NavigationItem[]; | ||
|
||
/** Path for back button. */ | ||
readonly from?: To; | ||
} | ||
|
||
interface RouterExt extends Navigation { | ||
navigateToSetting: (id: keyof AppSettings) => void; | ||
|
||
/** Overrides navigation for current route. */ | ||
override(navigation: Navigation): void; | ||
|
||
/** Resets navigation to the default. */ | ||
reset(): void; | ||
} | ||
|
||
export const RouterExtContext = createContext<RouterExt>(null); | ||
|
||
export function useNavigation({ | ||
items = defaultNavigationItems, | ||
from, | ||
}: Navigation): void { | ||
const { override, reset } = useContext(RouterExtContext); | ||
|
||
useLayoutEffect(() => { | ||
override({ items, from }); | ||
|
||
return () => { | ||
reset(); | ||
}; | ||
}, []); | ||
} | ||
|
||
const defaultNavigationItems: NavigationItem[] = [ | ||
{ | ||
label: 'Dashboard', | ||
to: '/', | ||
end: true, | ||
}, | ||
{ | ||
label: 'History', | ||
to: '/history', | ||
}, | ||
{ | ||
label: 'Settings', | ||
to: '/settings', | ||
}, | ||
]; | ||
|
||
export const RouterExt = ({ children }: PropsWithChildren) => { | ||
const navigate = useNavigate(); | ||
const [items, setItems] = useState<NavigationItem[]>(defaultNavigationItems); | ||
const [from, setFrom] = useState<To>(null); | ||
|
||
const navigateToSetting = useCallback((id: keyof AppSettings) => { | ||
navigate(`/settings#${id}`); | ||
}, []); | ||
|
||
const override = useCallback(({ items, from }: Navigation) => { | ||
setFrom(from); | ||
setItems(items); | ||
}, []); | ||
|
||
const reset = useCallback(() => { | ||
setFrom(null); | ||
setItems(defaultNavigationItems); | ||
}, []); | ||
|
||
return ( | ||
<RouterExtContext.Provider | ||
value={{ navigateToSetting, override, reset, items, from }} | ||
> | ||
{children} | ||
</RouterExtContext.Provider> | ||
); | ||
}; |