Skip to content

Commit

Permalink
fix: mixed collapse/expand state of nav sections
Browse files Browse the repository at this point in the history
  • Loading branch information
devcatalin committed Sep 17, 2021
1 parent ba86233 commit 413d4e2
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export const List = styled.ol<{height: number}>`
background: transparent;
}
${props => `height: ${props.height}px;`}
padding-bottom: 20px;
`;

export const TitleBar = styled.div`
Expand Down
4 changes: 2 additions & 2 deletions src/components/organisms/NewNavigatorPane/NavPane.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ const NavPane = () => {
};

return (
<nav>
<>
<S.TitleBar>
<MonoPaneTitle>Navigator</MonoPaneTitle>
<S.TitleBarRightButtons>
Expand All @@ -62,7 +62,7 @@ const NavPane = () => {
<NavSectionRenderer<K8sResource, KustomizationNavSectionScope> navSection={KustomizationNavSection} level={0} />
<NavSectionRenderer<K8sResource, K8sResourceNavSectionScope> navSection={K8sResourceNavSection} level={0} />
</S.List>
</nav>
</>
);
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,45 +52,56 @@ function NavSectionRenderer<ItemType, ScopeType>(props: NavSectionRendererProps<
const [isHovered, setIsHovered] = useState<boolean>(false);
const collapsedNavSectionNames = useAppSelector(state => state.ui.navPane.collapsedNavSectionNames);

const allNestedSubsectionNames = useMemo(() => {
if (!subsections) {
return undefined;
}
const nestedSubsectionNames: string[] = [];
loopSubsectionNamesDeep(subsections, subsectionName => nestedSubsectionNames.push(subsectionName));
return nestedSubsectionNames;
}, [subsections]);

const isCollapsedMode = useMemo(() => {
if (collapsedNavSectionNames.includes(name)) {
return 'collapsed';
if (allNestedSubsectionNames) {
if (
collapsedNavSectionNames.includes(name) &&
allNestedSubsectionNames.every(s => collapsedNavSectionNames.includes(s))
) {
return 'collapsed';
}
if (
!collapsedNavSectionNames.includes(name) &&
allNestedSubsectionNames.every(s => !collapsedNavSectionNames.includes(s))
) {
return 'expanded';
}
return 'mixed';
}
if (subsections && subsections.every(s => collapsedNavSectionNames.includes(s.name))) {
if (collapsedNavSectionNames.includes(name)) {
return 'collapsed';
}
if (subsections && subsections.every(s => !collapsedNavSectionNames.includes(s.name))) {
return 'expanded';
}
if (!subsections || subsections.length === 0) {
return 'expanded';
}
return 'mixed';
}, [collapsedNavSectionNames, name]);
return 'expanded';
}, [collapsedNavSectionNames, name, allNestedSubsectionNames]);

const isCollapsed = useMemo(() => {
return isCollapsedMode === 'collapsed';
}, [isCollapsedMode]);

const expandSection = useCallback(() => {
if (!subsections || subsections.length === 0) {
if (!allNestedSubsectionNames || allNestedSubsectionNames.length === 0) {
dispatch(expandNavSections([name]));
} else {
const allSubsectionNames: string[] = [];
loopSubsectionNamesDeep(subsections, subsectionName => allSubsectionNames.push(subsectionName));
dispatch(expandNavSections([name, ...allSubsectionNames]));
dispatch(expandNavSections([name, ...allNestedSubsectionNames]));
}
}, [name, subsections, dispatch, expandNavSections]);
}, [name, allNestedSubsectionNames, dispatch, expandNavSections]);

const collapseSection = useCallback(() => {
if (!subsections || subsections.length === 0) {
if (!allNestedSubsectionNames || allNestedSubsectionNames.length === 0) {
dispatch(collapseNavSections([name]));
} else {
const allSubsectionNames: string[] = [];
loopSubsectionNamesDeep(subsections, subsectionName => allSubsectionNames.push(subsectionName));
dispatch(collapseNavSections([name, ...allSubsectionNames]));
dispatch(collapseNavSections([name, ...allNestedSubsectionNames]));
}
}, [name, subsections, dispatch, collapseNavSections]);
}, [name, allNestedSubsectionNames, dispatch, collapseNavSections]);

useEffect(() => {
if (shouldSectionExpand) {
Expand Down
2 changes: 1 addition & 1 deletion src/constants/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ export const ROOT_FILE_ENTRY = '<root>';
export const APP_MIN_WIDTH = 800;
export const APP_MIN_HEIGHT = 600;
export const TOOLTIP_DELAY = 1.0;
export const NAVIGATOR_HEIGHT_OFFSET = 112;
export const NAVIGATOR_HEIGHT_OFFSET = 110;
export const FILE_TREE_HEIGHT_OFFSET = 122;
export const REF_PATH_SEPARATOR = '#';

0 comments on commit 413d4e2

Please sign in to comment.