Files
suwayomi-material-you-webui/src/components/MangaGrid.tsx
T

397 lines
13 KiB
TypeScript
Raw Normal View History

/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
2021-01-26 23:32:12 +03:30
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
2021-01-26 23:32:12 +03:30
import React, { ForwardedRef, forwardRef, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
2023-06-17 16:50:17 +02:00
import Grid, { GridTypeMap } from '@mui/material/Grid';
2024-04-14 15:50:12 +02:00
import Box from '@mui/material/Box';
import { GridItemProps, GridStateSnapshot, VirtuosoGrid } from 'react-virtuoso';
import { useLocation } from 'react-router-dom';
2024-04-29 15:00:37 +02:00
import { useTranslation } from 'react-i18next';
2024-04-29 15:29:33 +02:00
import { EmptyViewAbsoluteCentered } from '@/components/util/EmptyViewAbsoluteCentered.tsx';
2023-10-28 00:32:02 +02:00
import { LoadingPlaceholder } from '@/components/util/LoadingPlaceholder';
2024-04-07 21:30:47 +02:00
import { MangaCard } from '@/components/MangaCard';
2023-06-05 01:42:39 +02:00
import { GridLayout } from '@/components/context/LibraryOptionsContext';
import { useLocalStorage, useSessionStorage } from '@/util/useStorage.tsx';
2023-12-25 21:37:45 +01:00
import { TManga, TPartialManga } from '@/typings.ts';
import { SelectableCollectionReturnType } from '@/components/collection/useSelectableCollection.ts';
import { DEFAULT_FULL_FAB_HEIGHT } from '@/components/util/StyledFab.tsx';
import { AppStorage } from '@/util/AppStorage.ts';
2024-04-07 21:30:47 +02:00
import { MangaCardProps } from '@/components/manga/MangaCard.types.tsx';
2023-06-17 16:50:17 +02:00
const GridContainer = React.forwardRef<HTMLDivElement, GridTypeMap['props']>(({ children, ...props }, ref) => (
<Grid {...props} ref={ref} container sx={{ paddingLeft: '5px', paddingRight: '13px' }}>
{children}
</Grid>
));
2023-06-17 20:53:08 +02:00
const GridItemContainerWithDimension = (
dimensions: number,
itemWidth: number,
gridLayout?: GridLayout,
maxColumns: number = 12,
) => {
const itemsPerRow = Math.ceil(dimensions / itemWidth);
const columnsPerItem = gridLayout === GridLayout.List ? maxColumns : maxColumns / itemsPerRow;
2023-06-17 16:50:17 +02:00
2024-03-31 23:34:40 +02:00
// MUI GridProps and Virtuoso GridItemProps use different types for the "ref" prop which conflict with each other
return ({ children, ...itemProps }: GridTypeMap['props'] & Omit<Partial<GridItemProps>, 'ref'>) => (
<Grid {...itemProps} item xs={columnsPerItem} sx={{ width: '100%', paddingTop: '8px', paddingLeft: '8px' }}>
2023-06-17 20:53:08 +02:00
{children}
</Grid>
);
};
2023-06-17 16:50:17 +02:00
2023-12-25 21:37:45 +01:00
const createMangaCard = (
manga: TPartialManga,
gridLayout?: GridLayout,
inLibraryIndicator?: boolean,
isSelectModeActive: boolean = false,
selectedMangaIds?: TManga['id'][],
handleSelection?: DefaultGridProps['handleSelection'],
2024-01-26 20:50:51 +01:00
mode?: MangaCardProps['mode'],
2023-12-25 21:37:45 +01:00
) => (
<MangaCard
key={manga.id}
manga={manga}
gridLayout={gridLayout}
inLibraryIndicator={inLibraryIndicator}
selected={isSelectModeActive ? selectedMangaIds?.includes(manga.id) : null}
handleSelection={handleSelection}
2024-01-26 20:50:51 +01:00
mode={mode}
2023-12-25 21:37:45 +01:00
/>
2023-06-17 16:50:17 +02:00
);
2024-01-26 20:50:51 +01:00
type DefaultGridProps = Pick<MangaCardProps, 'mode'> & {
2023-06-17 16:50:17 +02:00
isLoading: boolean;
2023-10-15 16:03:08 +02:00
mangas: TPartialManga[];
2023-06-17 16:50:17 +02:00
inLibraryIndicator?: boolean;
GridItemContainer: (props: GridTypeMap['props'] & Partial<GridItemProps>) => JSX.Element;
gridLayout?: GridLayout;
2023-12-25 21:37:45 +01:00
isSelectModeActive?: boolean;
selectedMangaIds?: Required<TManga['id']>[];
handleSelection?: SelectableCollectionReturnType<TManga['id']>['handleSelection'];
2023-06-17 16:50:17 +02:00
};
const HorizontalGrid = forwardRef(
(
{
isLoading,
mangas,
inLibraryIndicator,
GridItemContainer,
gridLayout,
isSelectModeActive,
selectedMangaIds,
handleSelection,
mode,
}: DefaultGridProps,
ref: ForwardedRef<HTMLDivElement | null>,
) => (
<Grid
ref={ref}
container
spacing={1}
style={{
margin: 0,
width: '100%',
padding: '5px',
overflowX: 'auto',
display: '-webkit-inline-box',
flexWrap: 'nowrap',
}}
>
{isLoading ? (
<LoadingPlaceholder />
) : (
mangas.map((manga) => (
<GridItemContainer key={manga.id}>
{createMangaCard(
manga,
gridLayout,
inLibraryIndicator,
isSelectModeActive,
selectedMangaIds,
handleSelection,
mode,
)}
</GridItemContainer>
))
)}
</Grid>
),
2023-06-17 16:50:17 +02:00
);
export const getGridSnapshotKey = (location: ReturnType<typeof useLocation>) =>
`MangaGrid-snapshot-location-${location.key}`;
const VerticalGrid = forwardRef(
(
{
isLoading,
mangas,
inLibraryIndicator,
GridItemContainer,
gridLayout,
hasNextPage,
loadMore,
isSelectModeActive,
selectedMangaIds,
handleSelection,
mode,
}: DefaultGridProps & {
hasNextPage: boolean;
loadMore: () => void;
},
ref: ForwardedRef<HTMLDivElement | null>,
) => {
const location = useLocation<{ snapshot?: GridStateSnapshot }>();
const snapshotSessionKey = getGridSnapshotKey(location);
const [snapshot] = useSessionStorage<GridStateSnapshot | undefined>(snapshotSessionKey, undefined);
2023-06-17 16:50:17 +02:00
const persistGridStateTimeout = useRef<NodeJS.Timeout | undefined>();
const persistGridState = (gridState: GridStateSnapshot) => {
const currentUrl = window.location.href;
clearTimeout(persistGridStateTimeout.current);
persistGridStateTimeout.current = setTimeout(() => {
const didLocationChange = currentUrl !== window.location.href;
if (didLocationChange) {
return;
2023-12-25 21:37:45 +01:00
}
AppStorage.session.setItem(snapshotSessionKey, gridState, false);
}, 250);
};
useEffect(() => clearTimeout(persistGridStateTimeout.current), [location.key, persistGridStateTimeout.current]);
return (
<>
<Box ref={ref}>
<VirtuosoGrid
useWindowScroll
overscan={window.innerHeight * 0.25}
totalCount={mangas.length}
components={{
List: GridContainer,
Item: GridItemContainer,
}}
restoreStateFrom={snapshot}
stateChanged={persistGridState}
endReached={() => loadMore()}
itemContent={(index) =>
createMangaCard(
mangas[index],
gridLayout,
inLibraryIndicator,
isSelectModeActive,
selectedMangaIds,
handleSelection,
mode,
)
}
/>
</Box>
{/* render div to prevent UI jumping around when showing/hiding loading placeholder */
/* eslint-disable-next-line no-nested-ternary */}
{isSelectModeActive && gridLayout === GridLayout.List ? (
<Box sx={{ paddingBottom: DEFAULT_FULL_FAB_HEIGHT }} />
) : // eslint-disable-next-line no-nested-ternary
isLoading ? (
<LoadingPlaceholder />
) : hasNextPage ? (
<div style={{ height: '75px' }} />
) : null}
</>
);
},
);
2021-01-20 15:26:52 +03:30
2024-04-27 22:29:23 +02:00
export interface IMangaGridProps
extends Omit<DefaultGridProps, 'GridItemContainer'>,
2024-04-29 15:29:33 +02:00
Partial<React.ComponentProps<typeof EmptyViewAbsoluteCentered>> {
2023-02-06 10:06:33 +01:00
hasNextPage: boolean;
2023-06-17 16:50:17 +02:00
loadMore: () => void;
horizontal?: boolean | undefined;
2023-02-06 10:06:33 +01:00
noFaces?: boolean | undefined;
2021-01-20 15:26:52 +03:30
}
2024-04-29 15:00:37 +02:00
export const MangaGrid: React.FC<IMangaGridProps> = ({
mangas,
isLoading,
message,
messageExtra,
hasNextPage,
loadMore,
gridLayout,
horizontal,
noFaces,
inLibraryIndicator,
isSelectModeActive,
selectedMangaIds,
handleSelection,
mode,
retry,
}) => {
const { t } = useTranslation();
2022-04-02 01:34:10 +01:00
const gridRef = useRef<HTMLDivElement>(null);
const [dimensions, setDimensions] = useState(document.documentElement.offsetWidth);
2023-06-17 16:50:17 +02:00
const [gridItemWidth] = useLocalStorage<number>('ItemWidth', 300);
const gridWrapperRef = useRef<HTMLDivElement>(null);
2023-06-17 16:50:17 +02:00
const GridItemContainer = useMemo(
() => GridItemContainerWithDimension(dimensions, gridItemWidth, gridLayout),
2023-06-17 16:50:17 +02:00
[dimensions, gridItemWidth, gridLayout],
);
2022-04-02 01:34:10 +01:00
2023-06-17 16:50:17 +02:00
const updateGridWidth = () => {
const getDimensions = () => {
const gridWidth = gridWrapperRef.current?.offsetWidth;
if (!gridWidth) {
return document.documentElement.offsetWidth;
}
return gridWidth;
};
setDimensions(getDimensions());
2022-04-02 01:34:10 +01:00
};
useLayoutEffect(updateGridWidth, []);
2022-04-02 01:34:10 +01:00
// always show vertical scrollbar to prevent https://github.com/Suwayomi/Suwayomi-WebUI/issues/758
useLayoutEffect(() => {
// in case "overflow" is currently set to "hidden" that (most likely) means that a MUI modal is open and locks the scrollbar
// once this modal is closed MUI restores the previous "overflow" value, thus, reverting the just set "overflow" value
let timeout: NodeJS.Timeout;
const changeStyle = (timeoutMS: number) => {
timeout = setTimeout(() => {
if (document.documentElement.style.overflow.includes('hidden')) {
changeStyle(250);
return;
}
document.documentElement.style.overflowY = gridLayout === GridLayout.List ? 'auto' : 'scroll';
}, timeoutMS);
};
changeStyle(0);
return () => {
clearTimeout(timeout);
};
}, [gridLayout]);
useEffect(
() => () => {
document.documentElement.style.overflowY = 'auto';
},
[],
);
2023-06-17 16:50:17 +02:00
useEffect(() => {
let movementTimer: NodeJS.Timeout;
2022-04-02 01:34:10 +01:00
2023-06-17 16:50:17 +02:00
const onResize = () => {
clearInterval(movementTimer);
movementTimer = setTimeout(updateGridWidth, 100);
};
2022-04-02 01:34:10 +01:00
2023-06-17 16:50:17 +02:00
window.addEventListener('resize', onResize);
return () => window.removeEventListener('resize', onResize);
}, []);
useEffect(() => {
if (!gridRef.current) {
return () => {};
}
if (gridRef.current.offsetHeight > document.documentElement.clientHeight) {
return () => {};
}
2024-04-07 15:21:46 +02:00
const resizeObserver = new ResizeObserver((entries) => {
const gridHeight = entries[0].target.clientHeight;
const isScrollbarVisible = gridHeight > document.documentElement.clientHeight;
if (isLoading) {
return;
}
if (!gridHeight) {
return;
}
if (isScrollbarVisible) {
resizeObserver.disconnect();
return;
}
loadMore();
resizeObserver.disconnect();
});
resizeObserver.observe(gridRef.current);
return () => resizeObserver.disconnect();
}, [loadMore, isLoading]);
2023-06-17 16:50:17 +02:00
const hasNoItems = !isLoading && mangas.length === 0;
if (hasNoItems) {
2024-04-29 15:29:33 +02:00
return (
2024-04-29 15:00:37 +02:00
<EmptyViewAbsoluteCentered
noFaces={noFaces}
message={message ?? t('manga.error.label.no_mangas_found')}
messageExtra={messageExtra}
retry={retry}
/>
2024-04-29 15:29:33 +02:00
);
2021-01-20 15:26:52 +03:30
}
2021-01-22 21:11:00 +03:30
return (
2023-06-17 16:50:17 +02:00
<div
ref={gridWrapperRef}
2023-06-17 16:50:17 +02:00
style={{
overflow: 'hidden',
paddingBottom: '13px',
}}
>
{horizontal ? (
<HorizontalGrid
ref={gridRef}
2023-06-17 16:50:17 +02:00
isLoading={isLoading}
mangas={mangas}
inLibraryIndicator={inLibraryIndicator}
GridItemContainer={GridItemContainer}
gridLayout={gridLayout}
2023-12-25 21:37:45 +01:00
isSelectModeActive={isSelectModeActive}
selectedMangaIds={selectedMangaIds}
handleSelection={handleSelection}
2024-01-26 20:50:51 +01:00
mode={mode}
2023-06-17 16:50:17 +02:00
/>
) : (
<VerticalGrid
ref={gridRef}
2023-06-17 16:50:17 +02:00
isLoading={isLoading}
mangas={mangas}
inLibraryIndicator={inLibraryIndicator}
2023-06-17 16:50:17 +02:00
GridItemContainer={GridItemContainer}
hasNextPage={hasNextPage}
loadMore={loadMore}
gridLayout={gridLayout}
2023-12-25 21:37:45 +01:00
isSelectModeActive={isSelectModeActive}
selectedMangaIds={selectedMangaIds}
handleSelection={handleSelection}
2024-01-26 20:50:51 +01:00
mode={mode}
2023-06-17 16:50:17 +02:00
/>
)}
2022-04-02 01:34:10 +01:00
</div>
2021-01-22 21:11:00 +03:30
);
2022-11-24 21:04:30 +01:00
};