Files
suwayomi-material-you-webui/src/features/browse/sources/components/SourceCard.tsx

134 lines
5.4 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 CardActionArea from '@mui/material/CardActionArea';
import Button from '@mui/material/Button';
2021-09-09 17:51:22 +04:30
import Card from '@mui/material/Card';
import Typography from '@mui/material/Typography';
import React from 'react';
import { Link } from 'react-router-dom';
2024-08-30 21:32:21 +02:00
import Stack from '@mui/material/Stack';
2025-05-17 22:17:15 +02:00
import PushPinIcon from '@mui/icons-material/PushPin';
import PushPinOutlinedIcon from '@mui/icons-material/PushPinOutlined';
import IconButton from '@mui/material/IconButton';
import { useLingui } from '@lingui/react/macro';
import { requestManager } from '@/lib/requests/RequestManager.ts';
2026-03-11 00:11:29 +01:00
import type { GetSourcesListQuery } from '@/lib/graphql/generated/graphql.ts';
import { AppRoutes } from '@/base/AppRoute.constants.ts';
import { MUIUtil } from '@/lib/mui/MUI.util.ts';
2025-08-15 22:02:58 +02:00
import { Sources } from '@/features/source/services/Sources.ts';
import { ListCardAvatar } from '@/base/components/lists/cards/ListCardAvatar.tsx';
import { ListCardContent } from '@/base/components/lists/cards/ListCardContent.tsx';
import { CustomTooltip } from '@/base/components/CustomTooltip.tsx';
2025-08-15 22:02:58 +02:00
import { createUpdateSourceMetadata, useGetSourceMetadata } from '@/features/source/services/SourceMetadata.ts';
import { makeToast } from '@/base/utils/Toast.ts';
2025-05-17 22:17:15 +02:00
import { getErrorMessage } from '@/lib/HelperFunctions.ts';
import { languageCodeToName } from '@/base/utils/Languages.ts';
2026-05-27 23:59:58 +02:00
import { SourceContentType } from '@/features/source/Source.types.ts';
2020-12-25 06:36:34 +03:30
interface IProps {
source: GetSourcesListQuery['sources']['nodes'][number];
showSourceRepo: boolean;
showLanguage: boolean;
2020-12-25 06:36:34 +03:30
}
2023-10-28 00:32:02 +02:00
export const SourceCard: React.FC<IProps> = (props: IProps) => {
const { t } = useLingui();
const { source, showSourceRepo, showLanguage } = props;
2020-12-25 06:36:34 +03:30
const {
2025-04-21 17:00:13 +02:00
id,
name,
lang,
iconUrl,
supportsLatest,
isNsfw,
extension: { repo },
} = source;
2020-12-25 06:36:34 +03:30
2025-05-17 22:17:15 +02:00
const { isPinned } = useGetSourceMetadata(source);
const sourceName = Sources.isLocalSource(source) ? t`Local source` : name;
2025-05-17 22:17:15 +02:00
const updateSetting = createUpdateSourceMetadata(source, (e) =>
makeToast(t`Failed to save changes`, 'error', getErrorMessage(e)),
2025-05-17 22:17:15 +02:00
);
2020-12-25 06:36:34 +03:30
return (
2025-06-23 00:36:32 +02:00
<Card>
<CardActionArea
component={Link}
2026-05-28 00:01:54 +02:00
to={AppRoutes.sources.children.browse.path(id)}
state={AppRoutes.sources.children.browse.state({
2026-05-27 23:59:58 +02:00
contentType: SourceContentType.POPULAR,
clearCache: true,
})}
>
2025-04-25 01:05:43 +02:00
<ListCardContent>
<ListCardAvatar
iconUrl={requestManager.getValidImgUrlFor(iconUrl)}
alt={sourceName}
slots={{
spinnerImageProps: {
ignoreQueue: true,
},
}}
/>
<Stack
sx={{
justifyContent: 'center',
flexGrow: 1,
flexShrink: 1,
wordBreak: 'break-word',
}}
>
<Typography variant="h6" component="h3">
{sourceName}
</Typography>
<Typography variant="caption">
{showLanguage && languageCodeToName(lang)}
{isNsfw && (
<Typography variant="caption" color="error">
{' 18+'}
</Typography>
)}
</Typography>
{showSourceRepo && <Typography variant="caption">{repo}</Typography>}
</Stack>
{supportsLatest && (
<Button
{...MUIUtil.preventRippleProp()}
variant="outlined"
component={Link}
2026-05-28 00:01:54 +02:00
to={AppRoutes.sources.children.browse.path(id)}
state={AppRoutes.sources.children.browse.state({
2026-05-27 23:59:58 +02:00
contentType: SourceContentType.LATEST,
clearCache: true,
})}
>
{t`Latest`}
</Button>
)}
<CustomTooltip title={isPinned ? t`Unpin source` : t`Pin source`}>
2025-05-17 22:17:15 +02:00
<IconButton
{...MUIUtil.preventRippleProp()}
onClick={(e) => {
e.preventDefault();
updateSetting('isPinned', !isPinned);
}}
color={isPinned ? 'primary' : 'inherit'}
>
{isPinned ? <PushPinIcon /> : <PushPinOutlinedIcon />}
</IconButton>
</CustomTooltip>
2025-04-25 01:05:43 +02:00
</ListCardContent>
</CardActionArea>
2020-12-25 06:36:34 +03:30
</Card>
);
};