2024-06-11 14:27:59 +02:00
|
|
|
import {
|
2024-06-13 09:43:39 +02:00
|
|
|
Icon,
|
2024-06-11 14:27:59 +02:00
|
|
|
List,
|
|
|
|
ListItemButton,
|
|
|
|
ListItemIcon,
|
|
|
|
ListItemText,
|
|
|
|
styled,
|
|
|
|
Typography,
|
|
|
|
} from '@mui/material';
|
|
|
|
import { Link } from 'react-router-dom';
|
2024-06-13 09:43:39 +02:00
|
|
|
import {
|
|
|
|
IconRenderer,
|
|
|
|
StyledProjectIcon,
|
|
|
|
} from 'component/layout/MainLayout/NavigationSidebar/IconRenderer';
|
2024-06-11 14:27:59 +02:00
|
|
|
import type { LastViewedPage } from 'hooks/useRecentlyVisited';
|
|
|
|
import type { Theme } from '@mui/material/styles/createTheme';
|
2024-06-14 11:22:55 +02:00
|
|
|
import useProjectOverview from 'hooks/api/getters/useProjectOverview/useProjectOverview';
|
|
|
|
import { ConditionallyRender } from 'component/common/ConditionallyRender/ConditionallyRender';
|
2024-06-27 11:48:57 +02:00
|
|
|
import { usePlausibleTracker } from 'hooks/usePlausibleTracker';
|
2024-06-11 14:27:59 +02:00
|
|
|
|
|
|
|
const listItemButtonStyle = (theme: Theme) => ({
|
2024-06-25 14:30:39 +02:00
|
|
|
border: `1px solid transparent`,
|
2024-06-11 14:27:59 +02:00
|
|
|
borderLeft: `${theme.spacing(0.5)} solid transparent`,
|
2024-06-25 14:30:39 +02:00
|
|
|
'&:hover': {
|
|
|
|
border: `1px solid ${theme.palette.primary.main}`,
|
2024-06-11 14:27:59 +02:00
|
|
|
borderLeft: `${theme.spacing(0.5)} solid ${theme.palette.primary.main}`,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2024-06-25 14:30:39 +02:00
|
|
|
const StyledContainer = styled('div')(({ theme }) => ({
|
|
|
|
marginBottom: theme.spacing(3),
|
|
|
|
}));
|
|
|
|
|
|
|
|
const StyledButtonTypography = styled(Typography)(({ theme }) => ({
|
|
|
|
fontSize: theme.fontSizes.smallerBody,
|
|
|
|
}));
|
|
|
|
|
|
|
|
const ColoredStyledProjectIcon = styled(StyledProjectIcon)(({ theme }) => ({
|
|
|
|
fill: theme.palette.primary.main,
|
|
|
|
stroke: theme.palette.primary.main,
|
|
|
|
}));
|
|
|
|
|
2024-06-11 14:27:59 +02:00
|
|
|
const StyledTypography = styled(Typography)(({ theme }) => ({
|
2024-06-25 14:30:39 +02:00
|
|
|
fontSize: theme.fontSizes.smallBody,
|
|
|
|
padding: theme.spacing(0, 2.5),
|
2024-06-11 14:27:59 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
const StyledListItemIcon = styled(ListItemIcon)(({ theme }) => ({
|
2024-06-25 14:30:39 +02:00
|
|
|
minWidth: theme.spacing(0.5),
|
|
|
|
margin: theme.spacing(0, 1, 0, 0),
|
|
|
|
color: theme.palette.primary.main,
|
2024-06-11 14:27:59 +02:00
|
|
|
}));
|
|
|
|
|
|
|
|
const StyledListItemText = styled(ListItemText)(({ theme }) => ({
|
|
|
|
margin: 0,
|
2024-06-25 14:30:39 +02:00
|
|
|
fontSize: theme.fontSizes.smallBody,
|
2024-06-11 14:27:59 +02:00
|
|
|
}));
|
|
|
|
|
2024-06-14 11:22:55 +02:00
|
|
|
const toListItemButton = (
|
|
|
|
item: LastViewedPage,
|
|
|
|
routes: Record<string, { path: string; route: string; title: string }>,
|
|
|
|
index: number,
|
|
|
|
) => {
|
|
|
|
const key = `recently-visited-${index}`;
|
|
|
|
if (item.featureId && item.projectId) {
|
|
|
|
return (
|
|
|
|
<RecentlyVisitedFeatureButton
|
|
|
|
key={key}
|
|
|
|
featureId={item.featureId}
|
|
|
|
projectId={item.projectId}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (item.projectId) {
|
|
|
|
return (
|
|
|
|
<RecentlyVisitedProjectButton
|
|
|
|
key={key}
|
|
|
|
projectId={item.projectId}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
if (!item.pathName) return null;
|
|
|
|
const name = routes[item.pathName]?.title ?? item.pathName;
|
|
|
|
return (
|
|
|
|
<RecentlyVisitedPathButton key={key} path={item.pathName} name={name} />
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const RecentlyVisitedFeatureButton = ({
|
|
|
|
key,
|
|
|
|
projectId,
|
|
|
|
featureId,
|
|
|
|
}: { key: string; projectId: string; featureId: string }) => {
|
2024-06-27 11:48:57 +02:00
|
|
|
const { trackEvent } = usePlausibleTracker();
|
|
|
|
|
|
|
|
const onClick = () => {
|
|
|
|
trackEvent('command-bar', {
|
|
|
|
props: {
|
|
|
|
eventType: `click`,
|
|
|
|
source: 'recently-visited',
|
|
|
|
eventTarget: 'Flags',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
2024-06-14 11:22:55 +02:00
|
|
|
return (
|
|
|
|
<ListItemButton
|
|
|
|
key={key}
|
|
|
|
dense={true}
|
|
|
|
component={Link}
|
2024-06-27 11:48:57 +02:00
|
|
|
onClick={onClick}
|
2024-06-14 11:22:55 +02:00
|
|
|
to={`/projects/${projectId}/features/${featureId}`}
|
|
|
|
sx={listItemButtonStyle}
|
|
|
|
>
|
|
|
|
<StyledListItemIcon>
|
|
|
|
<Icon>{'flag'}</Icon>
|
|
|
|
</StyledListItemIcon>
|
|
|
|
<StyledListItemText>
|
2024-06-25 14:30:39 +02:00
|
|
|
<StyledButtonTypography color='textPrimary'>
|
|
|
|
{featureId}
|
|
|
|
</StyledButtonTypography>
|
2024-06-14 11:22:55 +02:00
|
|
|
</StyledListItemText>
|
|
|
|
</ListItemButton>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const RecentlyVisitedPathButton = ({
|
|
|
|
path,
|
|
|
|
key,
|
|
|
|
name,
|
|
|
|
}: { path: string; key: string; name: string }) => {
|
2024-06-27 11:48:57 +02:00
|
|
|
const { trackEvent } = usePlausibleTracker();
|
|
|
|
|
|
|
|
const onClick = () => {
|
|
|
|
trackEvent('command-bar', {
|
|
|
|
props: {
|
|
|
|
eventType: `click`,
|
|
|
|
source: 'recently-visited',
|
|
|
|
eventTarget: 'Pages',
|
|
|
|
pageType: name,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
2024-06-14 11:22:55 +02:00
|
|
|
return (
|
|
|
|
<ListItemButton
|
|
|
|
key={key}
|
|
|
|
dense={true}
|
|
|
|
component={Link}
|
|
|
|
to={path}
|
2024-06-27 11:48:57 +02:00
|
|
|
onClick={onClick}
|
2024-06-14 11:22:55 +02:00
|
|
|
sx={listItemButtonStyle}
|
|
|
|
>
|
2024-06-25 14:30:39 +02:00
|
|
|
<StyledListItemIcon
|
|
|
|
sx={(theme) => ({ color: theme.palette.primary.main })}
|
|
|
|
>
|
2024-06-14 11:22:55 +02:00
|
|
|
<ConditionallyRender
|
|
|
|
condition={path === '/projects'}
|
2024-06-25 14:30:39 +02:00
|
|
|
show={<ColoredStyledProjectIcon />}
|
2024-06-14 11:22:55 +02:00
|
|
|
elseShow={<IconRenderer path={path} />}
|
|
|
|
/>
|
|
|
|
</StyledListItemIcon>
|
|
|
|
<StyledListItemText>
|
2024-06-25 14:30:39 +02:00
|
|
|
<StyledButtonTypography color='textPrimary'>
|
|
|
|
{name}
|
|
|
|
</StyledButtonTypography>
|
2024-06-14 11:22:55 +02:00
|
|
|
</StyledListItemText>
|
|
|
|
</ListItemButton>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const RecentlyVisitedProjectButton = ({
|
|
|
|
projectId,
|
|
|
|
key,
|
|
|
|
}: { projectId: string; key: string }) => {
|
2024-06-27 11:48:57 +02:00
|
|
|
const { trackEvent } = usePlausibleTracker();
|
2024-06-14 11:22:55 +02:00
|
|
|
const { project, loading } = useProjectOverview(projectId);
|
|
|
|
const projectDeleted = !project.name && !loading;
|
2024-06-27 11:48:57 +02:00
|
|
|
|
|
|
|
const onClick = () => {
|
|
|
|
trackEvent('command-bar', {
|
|
|
|
props: {
|
|
|
|
eventType: `click`,
|
|
|
|
source: 'recently-visited',
|
|
|
|
eventTarget: 'Projects',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2024-06-14 11:22:55 +02:00
|
|
|
if (projectDeleted) return null;
|
|
|
|
return (
|
|
|
|
<ListItemButton
|
|
|
|
key={key}
|
|
|
|
dense={true}
|
|
|
|
component={Link}
|
|
|
|
to={`/projects/${projectId}`}
|
2024-06-27 11:48:57 +02:00
|
|
|
onClick={onClick}
|
2024-06-14 11:22:55 +02:00
|
|
|
sx={listItemButtonStyle}
|
|
|
|
>
|
|
|
|
<StyledListItemIcon>
|
2024-06-25 14:30:39 +02:00
|
|
|
<ColoredStyledProjectIcon />
|
2024-06-14 11:22:55 +02:00
|
|
|
</StyledListItemIcon>
|
|
|
|
<StyledListItemText>
|
2024-06-25 14:30:39 +02:00
|
|
|
<StyledButtonTypography color='textPrimary'>
|
|
|
|
{project.name}
|
|
|
|
</StyledButtonTypography>
|
2024-06-14 11:22:55 +02:00
|
|
|
</StyledListItemText>
|
|
|
|
</ListItemButton>
|
|
|
|
);
|
2024-06-13 09:43:39 +02:00
|
|
|
};
|
|
|
|
|
2024-06-28 12:18:27 +02:00
|
|
|
export const CommandRecent = ({
|
2024-06-11 14:27:59 +02:00
|
|
|
lastVisited,
|
2024-06-14 11:22:55 +02:00
|
|
|
routes,
|
|
|
|
}: {
|
|
|
|
lastVisited: LastViewedPage[];
|
|
|
|
routes: Record<string, { path: string; route: string; title: string }>;
|
|
|
|
}) => {
|
|
|
|
const buttons = lastVisited.map((item, index) =>
|
|
|
|
toListItemButton(item, routes, index),
|
|
|
|
);
|
2024-06-11 14:27:59 +02:00
|
|
|
return (
|
2024-06-25 14:30:39 +02:00
|
|
|
<StyledContainer>
|
2024-06-11 14:27:59 +02:00
|
|
|
<StyledTypography color='textSecondary'>
|
|
|
|
Recently visited
|
|
|
|
</StyledTypography>
|
2024-06-14 11:22:55 +02:00
|
|
|
<List>{buttons}</List>
|
2024-06-25 14:30:39 +02:00
|
|
|
</StyledContainer>
|
2024-06-11 14:27:59 +02:00
|
|
|
);
|
|
|
|
};
|