Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add navigator back in analytics #591

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"@mui/lab": "6.0.0-beta.22",
"@mui/material": "6.3.1",
"@sentry/react": "8.48.0",
"@tanstack/react-query": "5.63.0",
"@tanstack/react-router": "1.95.1",
"@tanstack/router-devtools": "1.95.1",
"@tanstack/zod-adapter": "1.95.1",
Expand Down
24 changes: 21 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

25 changes: 25 additions & 0 deletions src/components/ui/MenuItemLink.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import React from 'react';

import { MenuItem, MenuItemProps } from '@mui/material';

import { LinkComponent, createLink } from '@tanstack/react-router';

// eslint-disable-next-line @typescript-eslint/no-empty-object-type
interface MUIMenuItemProps extends Omit<MenuItemProps, 'href'> {
// Add any additional props you want to pass to the typography
}

const MUIMenuItemComponent = React.forwardRef<
HTMLAnchorElement,
MUIMenuItemProps
>((props, ref) => {
return <MenuItem component={'a'} ref={ref} {...props} />;
});

const CreatedLinkComponent = createLink(MUIMenuItemComponent);

export const MenuItemLink: LinkComponent<typeof MUIMenuItemComponent> = (
props,
) => {
return <CreatedLinkComponent preload="intent" {...props} />;
};
48 changes: 48 additions & 0 deletions src/components/ui/Navigator/CurrentItemNavigation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { DiscriminatedItem, ItemType } from '@graasp/sdk';

import { TypographyLink } from '../TypographyLink.js';
import ItemMenu, { ItemMenuProps } from './ItemMenu.js';
import CenterAlignWrapper from './common/CenterAlignWrapper.js';

export type CurrentItemProps = {
item: DiscriminatedItem;
buildBreadcrumbsItemLinkId?: (id: string) => string;
buildIconId?: (id: string) => string;
buildMenuId?: (id: string) => string;
buildMenuItemId?: (id: string) => string;
useChildren: ItemMenuProps['useChildren'];
showArrow: boolean;
};

export function CurrentItemNavigation({
item,
buildBreadcrumbsItemLinkId,
useChildren,
buildIconId,
buildMenuId,
buildMenuItemId,
showArrow,
}: Readonly<CurrentItemProps>): JSX.Element | null {
return (
<CenterAlignWrapper>
<TypographyLink
id={buildBreadcrumbsItemLinkId?.(item.id)}
key={item.id}
to="."
params={{ itemId: item.id }}
>
{item.name}
</TypographyLink>
{(item.type === ItemType.FOLDER || showArrow) && (
<ItemMenu
useChildren={useChildren}
itemId={item.id}
buildIconId={buildIconId}
buildMenuItemId={buildMenuItemId}
buildMenuId={buildMenuId}
renderArrow={showArrow}
/>
)}
</CenterAlignWrapper>
);
}
62 changes: 62 additions & 0 deletions src/components/ui/Navigator/ExtraItemsMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { useState } from 'react';

import { IconButton, IconButtonProps, Menu, Typography } from '@mui/material';

import { ChevronRightIcon } from 'lucide-react';

import { MenuItemLink } from '../MenuItemLink';
import { MenuItemType } from './Navigator';

export type ExtraItemsMenuProps = {
icon?: JSX.Element;
menuItems: MenuItemType[];
};

const Separator = <ChevronRightIcon data-testid="NavigateNextIcon" />;

export function ExtraItemsMenu({
icon = Separator,
menuItems,
}: Readonly<ExtraItemsMenuProps>): JSX.Element {
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
const open = Boolean(anchorEl);
const handleClick: IconButtonProps['onClick'] = (event) => {
setAnchorEl(event.currentTarget);
};

const handleClose = (): void => {
setAnchorEl(null);
};

return (
<>
<IconButton
onClick={handleClick}
aria-haspopup="true"
aria-expanded={open ? true : undefined}
>
{icon}
</IconButton>
<Menu
anchorEl={anchorEl}
open={open}
onClose={handleClose}
onClick={handleClose}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'right',
}}
transformOrigin={{
vertical: 'top',
horizontal: 'left',
}}
>
{menuItems?.map(({ name, path }) => (
<MenuItemLink key={name} to={path}>
<Typography>{name}</Typography>
</MenuItemLink>
))}
</Menu>
</>
);
}
30 changes: 30 additions & 0 deletions src/components/ui/Navigator/ExtraItemsNavigation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Box, Stack } from '@mui/material';

import { TypographyLink } from '../TypographyLink';
import { ExtraItemsMenu } from './ExtraItemsMenu';
import { MenuItemType } from './Navigator';

export interface ExtraItem {
name: string;
path: string;
icon?: JSX.Element;
menuItems?: MenuItemType[];
}

export function ExtraItemsNavigation({
extraItems,
}: Readonly<{
extraItems: ExtraItem[];
}>): JSX.Element[] {
return extraItems.map(({ icon, name, path, menuItems }) => (
<Stack key={name} direction="row" alignItems="center">
<Box display="flex" gap={1}>
{icon}
<TypographyLink to={path}>{name}</TypographyLink>
</Box>
{menuItems && menuItems.length > 0 && (
<ExtraItemsMenu menuItems={menuItems} />
)}
</Stack>
));
}
92 changes: 92 additions & 0 deletions src/components/ui/Navigator/HomeMenu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { useState } from 'react';

import {
IconButton,
IconButtonProps,
Menu,
MenuProps,
Typography,
} from '@mui/material';

import { ChevronRightIcon, HomeIcon } from 'lucide-react';

import { MenuItemLink } from '../MenuItemLink';
import { TypographyLink } from '../TypographyLink';

const Separator = <ChevronRightIcon data-testid="NavigateNextIcon" />;

type Props = {
selected: { name: string; id: string; to: string };
elements: {
name: string;
id: string;
to: string;
}[];
menuId?: string;
buildMenuItemId?: (itemId: string) => string;
homeDropdownId?: string;
};

const HomeMenu = ({
buildMenuItemId,
elements,
homeDropdownId,
menuId,
selected,
}: Props): JSX.Element => {
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
const open = Boolean(anchorEl);

const handleClick: IconButtonProps['onClick'] = (event) => {
setAnchorEl(event.currentTarget);
};

const handleClose = (): void => {
setAnchorEl(null);
};

const onClick: MenuProps['onClick'] = (): void => {
handleClose();
};

return (
<>
<HomeIcon />
<IconButton
onClick={handleClick}
id={homeDropdownId}
aria-controls={open ? 'root' : undefined}
aria-haspopup="true"
aria-expanded={open ? true : undefined}
>
{Separator}
</IconButton>
<Menu
anchorEl={anchorEl}
open={open}
id={menuId}
onClose={handleClose}
onClick={onClick}
anchorOrigin={{
vertical: 'bottom',
horizontal: 'right',
}}
transformOrigin={{
vertical: 'top',
horizontal: 'left',
}}
>
{elements.map(({ name, id, to }) => (
<MenuItemLink key={id} to={to} id={buildMenuItemId?.(id)}>
<Typography>{name}</Typography>
</MenuItemLink>
))}
</Menu>
<TypographyLink to={selected.to} key={selected.id}>
{selected.name}
</TypographyLink>
</>
);
};

export default HomeMenu;
Loading
Loading