-
Notifications
You must be signed in to change notification settings - Fork 117
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bugfixes(fe): fix toolbar tabs selected state (#1588)
- Loading branch information
1 parent
8ea9d90
commit 8866d3c
Showing
5 changed files
with
59 additions
and
29 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
22 changes: 13 additions & 9 deletions
22
odd-platform-ui/src/components/shared/elements/AppTabs/useSetSelectedTab.ts
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,20 +1,24 @@ | ||
import type React from 'react'; | ||
import { useEffect } from 'react'; | ||
import { useCallback, useEffect } from 'react'; | ||
import { useLocation, useResolvedPath } from 'react-router-dom'; | ||
import type { AppTabItem } from './AppTabs'; | ||
|
||
const useSetSelectedTab = ( | ||
tabs: AppTabItem[], | ||
setSelectedTab: React.Dispatch<React.SetStateAction<number>> | ||
) => { | ||
const resolvedPath = useResolvedPath(useLocation().pathname); | ||
useEffect(() => { | ||
const foundIndex = tabs.findIndex(({ link }) => { | ||
const { pathname } = resolvedPath; | ||
return link ? pathname.includes(link) || link.includes(pathname) : false; | ||
}); | ||
setSelectedTab(foundIndex); | ||
}, [tabs, resolvedPath]); | ||
const { pathname } = useResolvedPath(useLocation()); | ||
|
||
const findTabIndex = useCallback( | ||
() => | ||
tabs.findIndex(({ link }) => { | ||
if (link) return pathname.includes(link) || link.includes(pathname); | ||
return false; | ||
}), | ||
[tabs, pathname] | ||
); | ||
|
||
useEffect(() => setSelectedTab(findTabIndex()), [tabs, pathname]); | ||
}; | ||
|
||
export default useSetSelectedTab; |
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