Files
suwayomi-material-you-webui/src/components/manga/MangaDetails.tsx

234 lines
8.6 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
2021-09-09 17:51:22 +04:30
import FavoriteIcon from '@mui/icons-material/Favorite';
import FavoriteBorderIcon from '@mui/icons-material/FavoriteBorder';
import { styled } from '@mui/material/styles';
2024-08-30 04:34:28 +02:00
import { ComponentProps, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import { t as translate } from 'i18next';
import Link from '@mui/material/Link';
2024-05-23 00:40:20 +02:00
import Typography from '@mui/material/Typography';
2024-05-23 02:28:37 +02:00
import { useLongPress } from 'use-long-press';
2024-08-30 04:34:28 +02:00
import Chip from '@mui/material/Chip';
import Tooltip from '@mui/material/Tooltip';
import LaunchIcon from '@mui/icons-material/Launch';
import Collapse from '@mui/material/Collapse';
2024-08-30 04:34:28 +02:00
import Stack from '@mui/material/Stack';
import IconButton from '@mui/material/IconButton';
import ExpandLessIcon from '@mui/icons-material/ExpandLess';
import ExpandMoreIcon from '@mui/icons-material/ExpandMore';
2023-10-28 00:32:02 +02:00
import { makeToast } from '@/components/util/Toast';
2024-07-08 18:02:50 +02:00
import { Mangas, MangaThumbnailInfo, MangaTrackRecordInfo } from '@/lib/data/Mangas.ts';
import { SpinnerImage } from '@/components/util/SpinnerImage.tsx';
import { CustomIconButton } from '@/components/atoms/CustomIconButton';
2024-03-10 17:36:22 +01:00
import { TrackMangaButton } from '@/components/manga/TrackMangaButton.tsx';
import { useManageMangaLibraryState } from '@/components/manga/useManageMangaLibraryState.tsx';
2024-05-23 00:40:20 +02:00
import { Metadata as BaseMetadata } from '@/components/atoms/Metadata.tsx';
2024-05-23 02:28:37 +02:00
import { defaultPromiseErrorHandler } from '@/util/defaultPromiseErrorHandler.ts';
2024-07-08 18:02:50 +02:00
import { MangaType, SourceType } from '@/lib/graphql/generated/graphql.ts';
import { useLocalStorage } from '@/util/useStorage.tsx';
2021-02-21 04:27:41 +03:30
const DetailsWrapper = styled('div')(({ theme }) => ({
2024-08-30 04:34:28 +02:00
display: 'flex',
flexDirection: 'column',
gap: theme.spacing(2),
padding: theme.spacing(1),
[theme.breakpoints.up('md')]: {
2024-08-30 04:34:28 +02:00
flexBasis: '40%',
height: 'calc(100vh - 64px)',
overflowY: 'auto',
},
}));
2024-08-30 04:34:28 +02:00
const TopContentWrapper = styled('div')(() => ({}));
const ThumbnailMetadataWrapper = styled('div')(() => ({
display: 'flex',
}));
2024-08-30 04:34:28 +02:00
const Thumbnail = styled('div')(({ theme }) => ({
'& img': {
borderRadius: 4,
2024-08-30 04:34:28 +02:00
width: '150px',
height: 'auto',
2024-08-30 04:34:28 +02:00
[theme.breakpoints.up('lg')]: {
width: '200px',
},
[theme.breakpoints.up('xl')]: {
width: '300px',
},
},
}));
2024-05-23 00:40:20 +02:00
const MetadataContainer = styled('div')(({ theme }) => ({
2024-08-30 04:34:28 +02:00
marginLeft: theme.spacing(1),
}));
2024-05-23 00:40:20 +02:00
2024-08-30 04:34:28 +02:00
const Metadata = (props: ComponentProps<typeof BaseMetadata>) => <BaseMetadata {...props} />;
2024-05-23 00:40:20 +02:00
const MangaButtonsContainer = styled('div')(({ theme }) => ({
display: 'flex',
2024-08-30 04:34:28 +02:00
gap: theme.spacing(1),
}));
2021-01-19 20:20:28 +03:30
2023-09-08 00:44:01 +02:00
const OpenSourceButton = ({ url }: { url?: string | null }) => {
const { t } = useTranslation();
2024-08-30 04:34:28 +02:00
return (
<Tooltip title={t('global.button.open_site')}>
<CustomIconButton
2024-08-30 04:34:28 +02:00
size="medium"
disabled={!url}
component={Link}
href={url ?? undefined}
target="_blank"
rel="noreferrer"
2024-08-30 04:34:28 +02:00
variant="outlined"
>
2024-08-30 04:34:28 +02:00
<LaunchIcon />
</CustomIconButton>
2024-08-30 04:34:28 +02:00
</Tooltip>
);
2023-09-08 00:44:01 +02:00
};
function getSourceName(source?: Pick<SourceType, 'id' | 'displayName'> | null): string {
if (!source) {
return translate('global.label.unknown');
}
return source.displayName ?? source.id;
2021-01-19 20:20:28 +03:30
}
2023-09-08 00:44:01 +02:00
function getValueOrUnknown(val?: string | null) {
2024-05-23 01:21:20 +02:00
return val ?? translate('global.label.unknown');
}
const DESCRIPTION_COLLAPSED_SIZE = 75;
2024-08-30 04:34:28 +02:00
const DescriptionGenre = ({
manga: { description, genre: genres },
}: {
manga: Pick<MangaType, 'description' | 'genre'>;
}) => {
const [isCollapsed, setIsCollapsed] = useLocalStorage('isDescriptionGenreCollapsed', true);
return (
<>
<Stack sx={{ position: 'relative' }}>
<Collapse collapsedSize={DESCRIPTION_COLLAPSED_SIZE} in={!isCollapsed}>
<Typography style={{ whiteSpace: 'pre-line', textAlign: 'justify', textJustify: 'inter-word' }}>
{description}
</Typography>
</Collapse>
<Stack
onClick={() => setIsCollapsed(!isCollapsed)}
sx={{
pt: 1,
alignItems: 'center',
cursor: 'pointer',
position: isCollapsed ? 'absolute' : null,
width: '100%',
bottom: 0,
background: (theme) => `linear-gradient(transparent 1px, ${theme.palette.background.paper})`,
}}
>
<IconButton sx={{ color: (theme) => (theme.palette.mode === 'light' ? 'black' : 'text') }}>
{isCollapsed ? <ExpandMoreIcon /> : <ExpandLessIcon />}
</IconButton>
</Stack>
</Stack>
<Stack
sx={{
flexDirection: 'row',
flexWrap: isCollapsed ? 'no-wrap' : 'wrap',
gap: 1,
overflowX: isCollapsed ? 'auto' : null,
}}
>
{genres.map((genre) => (
<Chip label={genre} variant="outlined" />
))}
</Stack>
</>
);
};
2024-08-30 04:34:28 +02:00
2024-07-08 18:02:50 +02:00
export const MangaDetails = ({
manga,
}: {
manga: Pick<
MangaType,
'id' | 'title' | 'author' | 'artist' | 'status' | 'inLibrary' | 'realUrl' | 'description' | 'genre'
> &
MangaThumbnailInfo &
MangaTrackRecordInfo & {
source?: Pick<SourceType, 'id' | 'displayName'> | null;
};
}) => {
const { t } = useTranslation();
useEffect(() => {
if (!manga.source) {
makeToast(translate('source.error.label.source_not_found'), 'error');
}
}, [manga.source]);
const { CategorySelectComponent, updateLibraryState } = useManageMangaLibraryState(manga);
2021-01-19 20:20:28 +03:30
2024-05-23 02:28:37 +02:00
const copyTitleLongPressBind = useLongPress(async () => {
try {
await navigator.clipboard.writeText(manga.title);
makeToast(t('global.label.copied'), 'info');
} catch (e) {
defaultPromiseErrorHandler('MangaDetails::copyTitleLongPress')(e);
}
});
2021-01-19 20:20:28 +03:30
return (
<>
<DetailsWrapper>
<TopContentWrapper>
<ThumbnailMetadataWrapper>
<Thumbnail>
<SpinnerImage src={Mangas.getThumbnailUrl(manga)} alt="Manga Thumbnail" />
</Thumbnail>
2024-05-23 00:40:20 +02:00
<MetadataContainer>
2024-05-23 02:28:37 +02:00
<Typography
2024-08-30 04:34:28 +02:00
variant="h5"
component="h2"
2024-05-23 02:28:37 +02:00
sx={{ mb: 1, '@media not (pointer: fine)': { userSelect: 'none' } }}
{...copyTitleLongPressBind()}
>
2024-05-23 00:40:20 +02:00
{manga.title}
</Typography>
<Metadata title={t('manga.label.author')} value={getValueOrUnknown(manga.author)} />
<Metadata title={t('manga.label.artist')} value={getValueOrUnknown(manga.artist)} />
<Metadata title={t('manga.label.status')} value={getValueOrUnknown(manga.status)} />
2024-07-01 19:51:27 +02:00
<Metadata title={t('source.title_one')} value={getSourceName(manga.source)} />
2024-05-23 00:40:20 +02:00
</MetadataContainer>
</ThumbnailMetadataWrapper>
<MangaButtonsContainer>
<CustomIconButton
2024-08-30 04:34:28 +02:00
size="medium"
onClick={updateLibraryState}
2024-08-30 04:34:28 +02:00
variant={manga.inLibrary ? 'contained' : 'outlined'}
>
{manga.inLibrary ? <FavoriteIcon /> : <FavoriteBorderIcon />}
{manga.inLibrary ? t('manga.button.in_library') : t('manga.button.add_to_library')}
</CustomIconButton>
2024-03-10 17:36:22 +01:00
<TrackMangaButton manga={manga} />
<OpenSourceButton url={manga.realUrl} />
</MangaButtonsContainer>
</TopContentWrapper>
2024-08-30 04:34:28 +02:00
<DescriptionGenre manga={manga} />
</DetailsWrapper>
{CategorySelectComponent}
</>
2021-01-19 20:20:28 +03:30
);
};