2021-03-26 04:17:02 +04:30
|
|
|
/*
|
|
|
|
|
* 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
|
2023-05-18 13:17:41 +02:00
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
|
|
|
*/
|
2021-01-26 23:32:12 +03:30
|
|
|
|
2024-11-04 01:26:58 +01:00
|
|
|
import { useEffect, useState } from 'react';
|
2021-09-09 17:51:22 +04:30
|
|
|
import Card from '@mui/material/Card';
|
|
|
|
|
import Button from '@mui/material/Button';
|
|
|
|
|
import Typography from '@mui/material/Typography';
|
2023-03-23 13:59:32 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2025-03-24 23:24:12 +01:00
|
|
|
import IconButton from '@mui/material/IconButton';
|
|
|
|
|
import SettingsIcon from '@mui/icons-material/Settings';
|
2025-04-25 00:27:28 +02:00
|
|
|
import Stack from '@mui/material/Stack';
|
2025-06-22 23:12:54 +02:00
|
|
|
import { Link } from 'react-router-dom';
|
2024-10-06 01:27:51 +02:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
2024-11-04 01:21:50 +01:00
|
|
|
import {
|
|
|
|
|
ExtensionAction,
|
|
|
|
|
ExtensionState,
|
|
|
|
|
InstalledState,
|
|
|
|
|
InstalledStates,
|
|
|
|
|
TExtension,
|
|
|
|
|
} from '@/modules/extension/Extensions.types.ts';
|
|
|
|
|
import {
|
|
|
|
|
EXTENSION_ACTION_TO_NEXT_ACTION_MAP,
|
|
|
|
|
EXTENSION_ACTION_TO_STATE_MAP,
|
|
|
|
|
INSTALLED_STATE_TO_TRANSLATION_KEY_MAP,
|
|
|
|
|
} from '@/modules/extension/Extensions.constants.ts';
|
2025-06-22 23:12:54 +02:00
|
|
|
import { getInstalledState, updateExtension } from '@/modules/extension/Extensions.utils.ts';
|
2025-03-24 23:24:12 +01:00
|
|
|
import { CustomTooltip } from '@/modules/core/components/CustomTooltip';
|
2025-05-03 12:14:05 +02:00
|
|
|
import { ListCardAvatar } from '@/modules/core/components/lists/cards/ListCardAvatar.tsx';
|
|
|
|
|
import { ListCardContent } from '@/modules/core/components/lists/cards/ListCardContent.tsx';
|
2025-06-22 23:12:54 +02:00
|
|
|
import { AppRoutes } from '@/modules/core/AppRoute.constants.ts';
|
|
|
|
|
import { MUIUtil } from '@/lib/mui/MUI.util.ts';
|
|
|
|
|
import { OptionalCardActionAreaLink } from '@/modules/core/components/lists/cards/OptionalCardActionAreaLink.tsx';
|
2020-12-24 16:50:50 +01:00
|
|
|
|
|
|
|
|
interface IProps {
|
2024-07-06 15:55:58 +02:00
|
|
|
extension: TExtension;
|
2023-11-24 03:20:16 +01:00
|
|
|
handleUpdate: () => void;
|
2024-01-09 22:09:31 +01:00
|
|
|
showSourceRepo: boolean;
|
2024-11-04 01:26:58 +01:00
|
|
|
forcedState?: ExtensionState;
|
2020-12-24 16:50:50 +01:00
|
|
|
}
|
|
|
|
|
|
2023-10-28 00:32:02 +02:00
|
|
|
export function ExtensionCard(props: IProps) {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
2020-12-25 03:08:08 +03:30
|
|
|
const {
|
2024-01-06 18:38:16 +01:00
|
|
|
extension: { name, lang, versionName, isInstalled, hasUpdate, isObsolete, pkgName, iconUrl, isNsfw, repo },
|
2023-11-24 03:20:16 +01:00
|
|
|
handleUpdate,
|
2024-01-09 22:09:31 +01:00
|
|
|
showSourceRepo,
|
2024-11-04 01:26:58 +01:00
|
|
|
forcedState,
|
2020-12-25 03:08:08 +03:30
|
|
|
} = props;
|
2024-11-04 01:26:58 +01:00
|
|
|
const [localInstalledState, setInstalledState] = useState<InstalledStates>(
|
2023-12-31 01:46:53 +01:00
|
|
|
getInstalledState(isInstalled, isObsolete, hasUpdate),
|
|
|
|
|
);
|
2024-11-04 01:26:58 +01:00
|
|
|
const installedState = forcedState ?? localInstalledState;
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setInstalledState(getInstalledState(isInstalled, isObsolete, hasUpdate));
|
|
|
|
|
}, [getInstalledState(isInstalled, isObsolete, hasUpdate)]);
|
2020-12-25 03:08:08 +03:30
|
|
|
|
2023-03-23 13:59:32 +01:00
|
|
|
const langPress = lang === 'all' ? t('extension.language.all') : lang.toUpperCase();
|
2020-12-24 16:50:50 +01:00
|
|
|
|
2023-03-03 15:21:11 +01:00
|
|
|
const requestExtensionAction = async (action: ExtensionAction): Promise<void> => {
|
|
|
|
|
const nextAction = EXTENSION_ACTION_TO_NEXT_ACTION_MAP[action];
|
|
|
|
|
const state = EXTENSION_ACTION_TO_STATE_MAP[action];
|
2020-12-25 03:08:08 +03:30
|
|
|
|
2023-12-31 01:46:53 +01:00
|
|
|
try {
|
|
|
|
|
setInstalledState(state);
|
2025-06-22 23:12:54 +02:00
|
|
|
|
|
|
|
|
await updateExtension(pkgName, isObsolete, action);
|
|
|
|
|
|
2023-12-31 01:46:53 +01:00
|
|
|
setInstalledState(nextAction);
|
|
|
|
|
|
|
|
|
|
handleUpdate();
|
2025-06-22 23:12:54 +02:00
|
|
|
} catch (_) {
|
2023-12-31 01:46:53 +01:00
|
|
|
setInstalledState(getInstalledState(isInstalled, isObsolete, hasUpdate));
|
2023-05-18 13:25:19 +02:00
|
|
|
}
|
2023-03-03 15:21:11 +01:00
|
|
|
};
|
2021-01-29 15:23:29 +03:30
|
|
|
|
|
|
|
|
function handleButtonClick() {
|
2021-03-29 02:47:51 +04:30
|
|
|
switch (installedState) {
|
2023-03-03 15:21:11 +01:00
|
|
|
case ExtensionAction.INSTALL:
|
|
|
|
|
case ExtensionAction.UPDATE:
|
|
|
|
|
case ExtensionAction.UNINSTALL:
|
2023-12-31 01:42:54 +01:00
|
|
|
requestExtensionAction(installedState).catch(
|
|
|
|
|
defaultPromiseErrorHandler(`ExtensionCard:handleButtonClick(${installedState})`),
|
|
|
|
|
);
|
2021-03-29 02:47:51 +04:30
|
|
|
break;
|
2023-03-03 15:21:11 +01:00
|
|
|
case ExtensionState.OBSOLETE:
|
2023-12-31 01:42:54 +01:00
|
|
|
requestExtensionAction(ExtensionAction.UNINSTALL).catch(
|
|
|
|
|
defaultPromiseErrorHandler(`ExtensionCard:handleButtonClick(${installedState})`),
|
|
|
|
|
);
|
2021-03-29 02:47:51 +04:30
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
break;
|
2021-01-29 15:23:29 +03:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-12-24 16:50:50 +01:00
|
|
|
return (
|
2024-01-02 23:32:49 +01:00
|
|
|
<Card>
|
2025-06-22 23:12:54 +02:00
|
|
|
<OptionalCardActionAreaLink disabled={!isInstalled} to={AppRoutes.extension.childRoutes.info.path(pkgName)}>
|
|
|
|
|
<ListCardContent>
|
|
|
|
|
<ListCardAvatar iconUrl={requestManager.getValidImgUrlFor(iconUrl)} alt={name} />
|
|
|
|
|
<Stack
|
|
|
|
|
sx={{
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
flexGrow: 1,
|
|
|
|
|
flexShrink: 1,
|
|
|
|
|
wordBreak: 'break-word',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Typography variant="h6" component="h3">
|
|
|
|
|
{name}
|
|
|
|
|
</Typography>
|
|
|
|
|
<Typography variant="caption">
|
|
|
|
|
{langPress} {versionName}
|
|
|
|
|
{isNsfw && (
|
|
|
|
|
<Typography variant="caption" color="error">
|
|
|
|
|
{' 18+'}
|
|
|
|
|
</Typography>
|
|
|
|
|
)}
|
|
|
|
|
</Typography>
|
|
|
|
|
{showSourceRepo && <Typography variant="caption">{repo}</Typography>}
|
|
|
|
|
</Stack>
|
|
|
|
|
{isInstalled && (
|
|
|
|
|
<CustomTooltip title={t('settings.title')}>
|
|
|
|
|
<IconButton
|
|
|
|
|
component={Link}
|
|
|
|
|
to={AppRoutes.extension.childRoutes.info.path(pkgName)}
|
|
|
|
|
color="inherit"
|
|
|
|
|
{...MUIUtil.preventRippleProp()}
|
|
|
|
|
>
|
|
|
|
|
<SettingsIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</CustomTooltip>
|
|
|
|
|
)}
|
|
|
|
|
<Button
|
|
|
|
|
variant="outlined"
|
|
|
|
|
sx={{
|
|
|
|
|
color: installedState === InstalledState.OBSOLETE ? 'red' : 'inherit',
|
|
|
|
|
flexShrink: 0,
|
|
|
|
|
}}
|
|
|
|
|
onClick={() => handleButtonClick()}
|
|
|
|
|
>
|
|
|
|
|
{t(INSTALLED_STATE_TO_TRANSLATION_KEY_MAP[installedState])}
|
|
|
|
|
</Button>
|
|
|
|
|
</ListCardContent>
|
|
|
|
|
</OptionalCardActionAreaLink>
|
2020-12-24 16:50:50 +01:00
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
}
|