-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: isVisible item handler for NavSection
- Loading branch information
1 parent
40212a8
commit 7a8c596
Showing
5 changed files
with
107 additions
and
45 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
60 changes: 60 additions & 0 deletions
60
src/components/organisms/NewNavigatorPane/NavSectionRenderer/useNavSection.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 |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import {useMemo, useCallback} from 'react'; | ||
import {NavSection} from '@models/navsection'; | ||
|
||
export function useNavSection<ItemType, ScopeType>(navSection: NavSection<ItemType, ScopeType>) { | ||
const {name, getItems, getItemsGrouped, useScope, itemHandler, subsections} = navSection; | ||
|
||
const scope = useScope(); | ||
|
||
const items = useMemo(() => { | ||
if (getItems) { | ||
return getItems(scope); | ||
} | ||
return undefined; | ||
}, [scope, getItems]); | ||
|
||
const groupedItems = useMemo(() => { | ||
if (getItemsGrouped) { | ||
return getItemsGrouped(scope); | ||
} | ||
return undefined; | ||
}, [scope, getItemsGrouped]); | ||
|
||
const getItemIdentifier = useCallback( | ||
(item: ItemType) => { | ||
if (!itemHandler) { | ||
return null; | ||
} | ||
return itemHandler.getIdentifier(item, scope); | ||
}, | ||
[scope, itemHandler] | ||
); | ||
|
||
const visibleItems = useMemo(() => { | ||
if (!items) { | ||
return []; | ||
} | ||
const isVisible = itemHandler?.isVisible; | ||
if (!isVisible) { | ||
return items; | ||
} | ||
return items.filter(item => isVisible(item, scope)); | ||
}, [scope, itemHandler, items]); | ||
|
||
const groupedVisibleItems = useMemo<Record<string, ItemType[]> | undefined>(() => { | ||
if (!groupedItems) { | ||
return undefined; | ||
} | ||
const isVisible = itemHandler?.isVisible; | ||
if (!isVisible) { | ||
return groupedItems; | ||
} | ||
return Object.fromEntries( | ||
Object.entries(([groupName, groupItems]: [string, ItemType[]]) => { | ||
return [groupName, groupItems.filter(item => isVisible(item, scope))]; | ||
}) | ||
); | ||
}, [scope, itemHandler, groupedItems]); | ||
|
||
return {name, scope, subsections, visibleItems, groupedVisibleItems, getItemIdentifier, itemHandler}; | ||
} |
22 changes: 22 additions & 0 deletions
22
src/components/organisms/NewNavigatorPane/NavSectionRenderer/useNavSectionItem.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 |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import {useMemo} from 'react'; | ||
import {NavSectionItemHandler} from '@models/navsection'; | ||
|
||
export function useNavSectionItem<ItemType, ScopeType>( | ||
item: ItemType, | ||
scope: ScopeType, | ||
handler: NavSectionItemHandler<ItemType, ScopeType> | ||
) { | ||
const name = useMemo(() => { | ||
return handler.getName(item, scope); | ||
}, [handler, scope, item]); | ||
|
||
const isSelected = useMemo(() => { | ||
return Boolean(handler.isSelected && handler.isSelected(item, scope)); | ||
}, [handler, scope, item]); | ||
|
||
const isHighlighted = useMemo(() => { | ||
return Boolean(handler.isHighlighted && handler.isHighlighted(item, scope)); | ||
}, [handler, scope, item]); | ||
|
||
return {name, isSelected, isHighlighted}; | ||
} |
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