Skip to content

Commit

Permalink
feat: show next-prev buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
pyphilia committed Nov 23, 2023
1 parent 7ad78e7 commit 3176b92
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 0 deletions.
4 changes: 4 additions & 0 deletions src/modules/item/Item.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ import { useCurrentMemberContext } from '@/contexts/CurrentMemberContext';
import { PLAYER } from '@/langs/constants';
import { isHidden, paginationContentFilter } from '@/utils/item';

import NavigationButton from './NavigationButton';
import PinnedFolderItem from './PinnedFolderItem';

const {
Expand Down Expand Up @@ -460,6 +461,9 @@ const Item = ({
</Fragment>
))}
{showLoadMoreButton}
{!hasNextPage && !isFetchingNextPage && (
<NavigationButton item={item} />
)}
</>
)}

Expand Down
68 changes: 68 additions & 0 deletions src/modules/item/NavigationButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { useParams } from 'react-router';

import ArrowBackIcon from '@mui/icons-material/ArrowBack';
import ArrowForwardIcon from '@mui/icons-material/ArrowForward';
import { Box } from '@mui/material';

import { DiscriminatedItem, ItemType } from '@graasp/sdk';
import { Button } from '@graasp/ui';

import { hooks } from '@/config/queryClient';

const NavigationButton = ({
item,
}: {
item: DiscriminatedItem;
}): JSX.Element | null => {
const { rootId } = useParams();

const { data: descendants, isLoading } = hooks.useDescendants({
// not correct but enabled
id: rootId ?? '',
enabled: Boolean(rootId),
});

if (isLoading) {
return null;
}

let prev = null;
let next = null;

const folderHierarchy = descendants?.filter(
({ type }) => type === ItemType.FOLDER,
);

if (item.id === rootId && folderHierarchy?.length) {
[next] = folderHierarchy;
} else {
const idx = folderHierarchy?.findIndex(({ id }) => id === item.id) ?? -1;

if (!folderHierarchy || idx < 0) {
return null;
}

prev = folderHierarchy[idx - 1];
next = folderHierarchy[idx + 1];
}
return (
<Box flexDirection="row" display="flex" justifyContent="space-between">
{prev ? (
<Button sx={{}} variant="outlined" startIcon={<ArrowBackIcon />}>
{prev.name}
</Button>
) : (
<p />
)}
{next ? (
<Button sx={{}} endIcon={<ArrowForwardIcon />}>
{next.name}
</Button>
) : (
<p />
)}
</Box>
);
};

export default NavigationButton;

0 comments on commit 3176b92

Please sign in to comment.