Files
suwayomi-material-you-webui/src/modules/reader/components/ReaderPageNumber.tsx

118 lines
4.0 KiB
TypeScript
Raw Normal View History

2024-12-07 00:13:00 +01:00
/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* 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/.
*/
import { useMemo } from 'react';
import Typography from '@mui/material/Typography';
import Stack from '@mui/material/Stack';
import Box from '@mui/material/Box';
import { ReaderService } from '@/modules/reader/services/ReaderService.ts';
2024-12-21 03:46:55 +01:00
import {
IReaderSettings,
ProgressBarType,
ReadingDirection,
TReaderScrollbarContext,
} from '@/modules/reader/types/Reader.types.ts';
2024-12-07 00:13:00 +01:00
import { userReaderStatePagesContext } from '@/modules/reader/contexts/state/ReaderStatePagesContext.tsx';
import { getPage } from '@/modules/reader/utils/ReaderProgressBar.utils.tsx';
import { useNavBarContext } from '@/modules/navigation-bar/contexts/NavbarContext.tsx';
import { useReaderProgressBarContext } from '@/modules/reader/contexts/ReaderProgressBarContext.tsx';
2024-11-04 18:00:35 +01:00
import { useReaderScrollbarContext } from '@/modules/reader/contexts/ReaderScrollbarContext.tsx';
2024-11-06 13:00:36 +01:00
import { reverseString } from '@/util/Strings.ts';
2024-12-21 03:46:55 +01:00
import { NavbarContextType } from '@/modules/navigation-bar/NavigationBar.types.ts';
import { ReaderStatePages, TReaderProgressBarContext } from '@/modules/reader/types/ReaderProgressBar.types.ts';
import { withPropsFrom } from '@/modules/core/hoc/withPropsFrom.tsx';
2024-12-07 00:13:00 +01:00
2024-12-21 03:46:55 +01:00
const BaseReaderPageNumber = ({
isDesktop,
scrollbarXSize,
readerNavBarWidth,
isMaximized,
currentPageIndex,
pages,
totalPages,
progressBarType,
shouldShowPageNumber,
readingDirection,
}: Pick<TReaderScrollbarContext, 'scrollbarXSize'> &
Pick<ReturnType<typeof ReaderService.useOverlayMode>, 'isDesktop'> &
Pick<NavbarContextType, 'readerNavBarWidth'> &
Pick<TReaderProgressBarContext, 'isMaximized'> &
Pick<ReaderStatePages, 'currentPageIndex' | 'pages' | 'totalPages'> &
Pick<IReaderSettings, 'progressBarType' | 'shouldShowPageNumber' | 'readingDirection'>) => {
2024-11-06 13:00:36 +01:00
const pageName = useMemo(() => {
const currentPageName = getPage(currentPageIndex, pages).name;
const SEPARATOR = '/';
const tmpPageName = `${currentPageName}${SEPARATOR}${totalPages}`;
2024-12-21 03:46:55 +01:00
return readingDirection === ReadingDirection.LTR ? tmpPageName : reverseString(tmpPageName, SEPARATOR);
}, [currentPageIndex, pages, totalPages, readingDirection]);
2024-12-07 00:13:00 +01:00
2024-11-04 18:00:35 +01:00
if (!shouldShowPageNumber) {
2024-12-07 00:13:00 +01:00
return null;
}
if (isMaximized) {
return null;
}
2024-11-04 18:00:35 +01:00
if (isDesktop && progressBarType === ProgressBarType.STANDARD) {
2024-12-07 00:13:00 +01:00
return null;
}
if (isMaximized && progressBarType === ProgressBarType.HIDDEN) {
return null;
}
return (
<Stack
sx={{
position: 'fixed',
left: readerNavBarWidth,
right: 0,
bottom: (theme) => `max(calc(${theme.spacing(1)} + ${scrollbarXSize}px), env(safe-area-inset-bottom))`,
2024-12-07 00:13:00 +01:00
alignItems: 'center',
transition: (theme) => `left 0.${theme.transitions.duration.shortest}s`,
}}
>
<Box
sx={{
p: 0.5,
borderRadius: 1,
backgroundColor: 'rgba(0, 0, 0, 0.3)',
}}
>
2024-11-06 13:00:36 +01:00
<Typography sx={{ color: 'white' }}>{pageName}</Typography>
2024-12-07 00:13:00 +01:00
</Box>
</Stack>
);
};
2024-12-21 03:46:55 +01:00
export const ReaderPageNumber = withPropsFrom(
BaseReaderPageNumber,
[
ReaderService.useOverlayMode,
useReaderScrollbarContext,
useNavBarContext,
useReaderProgressBarContext,
userReaderStatePagesContext,
ReaderService.useSettingsWithoutDefaultFlag,
],
[
'isDesktop',
'scrollbarXSize',
'readerNavBarWidth',
'isMaximized',
'currentPageIndex',
'pages',
'totalPages',
'progressBarType',
'shouldShowPageNumber',
'readingDirection',
],
);