1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-09-28 17:55:15 +02:00
unleash.unleash/frontend/src/component/common/BreadcrumbNav/BreadcrumbNav.tsx
Nuno Góis b3fcc97f93
fix: small breadcrumb adjustments (#2893)
https://linear.app/unleash/issue/2-577/small-breadcrumb-adjustments

Includes small adjustments to the breadcrumb navigation component.

### Long chain path (ellipts if width > 200px)

![image](https://user-images.githubusercontent.com/14320932/212155100-973c2b31-d990-4c0a-a67b-3d3110231569.png)

### Long final path (no longer ellipts as long as it has enough
remaining space)

![image](https://user-images.githubusercontent.com/14320932/212156184-041f671a-6bf5-4e43-9ef0-4c89015837cc.png)

### Long final path with resized window (still ellipts when needed)

![image](https://user-images.githubusercontent.com/14320932/212157091-6453c881-1c0f-4785-935e-4993ed479280.png)
2023-01-16 13:09:38 +01:00

104 lines
3.6 KiB
TypeScript

import Breadcrumbs from '@mui/material/Breadcrumbs';
import { Link, useLocation } from 'react-router-dom';
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
import AccessContext from 'contexts/AccessContext';
import { useContext } from 'react';
import { styled } from '@mui/material';
import { textTruncated } from 'themes/themeStyles';
const StyledBreadcrumbContainer = styled('div')(({ theme }) => ({
height: theme.spacing(2.5),
margin: theme.spacing(2, 0),
}));
const StyledBreadcrumbs = styled(Breadcrumbs)({
'& > ol': {
flexWrap: 'nowrap',
'& > li:last-child': {
minWidth: 0,
},
},
});
const StyledParagraph = styled('p')(textTruncated);
const StyledLink = styled(Link)(({ theme }) => ({
'& > *': {
maxWidth: theme.spacing(25),
},
}));
const BreadcrumbNav = () => {
const { isAdmin } = useContext(AccessContext);
const location = useLocation();
const paths = location.pathname
.split('/')
.filter(item => item)
.filter(
item =>
item !== 'create' &&
item !== 'edit' &&
item !== 'view' &&
item !== 'variants' &&
item !== 'logs' &&
item !== 'metrics' &&
item !== 'copy' &&
item !== 'features' &&
item !== 'features2' &&
item !== 'create-toggle' &&
item !== 'settings' &&
item !== 'profile'
);
return (
<StyledBreadcrumbContainer>
<ConditionallyRender
condition={
(location.pathname.includes('admin') && isAdmin) ||
!location.pathname.includes('admin')
}
show={
<ConditionallyRender
condition={paths.length > 1}
show={
<StyledBreadcrumbs aria-label="Breadcrumbs">
{paths.map((path, index) => {
const lastItem = index === paths.length - 1;
if (lastItem) {
return (
<StyledParagraph key={path}>
{path}
</StyledParagraph>
);
}
let link = '/';
paths.forEach((path, i) => {
if (i !== index && i < index) {
link += path + '/';
} else if (i === index) {
link += path;
}
});
return (
<StyledLink key={path} to={link}>
<StyledParagraph>
{path}
</StyledParagraph>
</StyledLink>
);
})}
</StyledBreadcrumbs>
}
/>
}
/>
</StyledBreadcrumbContainer>
);
};
export default BreadcrumbNav;