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
|
|
|
|
2023-08-14 21:43:06 +02:00
|
|
|
import { useState } from 'react';
|
2021-09-09 17:51:22 +04:30
|
|
|
import Card from '@mui/material/Card';
|
|
|
|
|
import CardContent from '@mui/material/CardContent';
|
|
|
|
|
import Button from '@mui/material/Button';
|
|
|
|
|
import Avatar from '@mui/material/Avatar';
|
|
|
|
|
import Typography from '@mui/material/Typography';
|
2024-04-14 15:50:12 +02:00
|
|
|
import Box from '@mui/material/Box';
|
2023-03-23 13:59:32 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
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-10-05 23:22:42 +02:00
|
|
|
import { makeToast } from '@/modules/core/utils/Toast.ts';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { SpinnerImage } from '@/modules/core/components/SpinnerImage.tsx';
|
2024-11-04 01:21:50 +01:00
|
|
|
import {
|
|
|
|
|
ExtensionAction,
|
|
|
|
|
ExtensionState,
|
|
|
|
|
InstalledState,
|
|
|
|
|
InstalledStates,
|
|
|
|
|
TExtension,
|
|
|
|
|
} from '@/modules/extension/Extensions.types.ts';
|
|
|
|
|
import {
|
|
|
|
|
EXTENSION_ACTION_TO_FAILURE_TRANSLATION_KEY_MAP,
|
|
|
|
|
EXTENSION_ACTION_TO_NEXT_ACTION_MAP,
|
|
|
|
|
EXTENSION_ACTION_TO_STATE_MAP,
|
|
|
|
|
INSTALLED_STATE_TO_TRANSLATION_KEY_MAP,
|
|
|
|
|
} from '@/modules/extension/Extensions.constants.ts';
|
|
|
|
|
import { getInstalledState } from '@/modules/extension/Extensions.utils.ts';
|
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;
|
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,
|
2020-12-25 03:08:08 +03:30
|
|
|
} = props;
|
2023-12-31 01:46:53 +01:00
|
|
|
const [installedState, setInstalledState] = useState<InstalledStates>(
|
|
|
|
|
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);
|
|
|
|
|
switch (action) {
|
|
|
|
|
case ExtensionAction.INSTALL:
|
2024-01-29 20:56:09 +01:00
|
|
|
await requestManager.updateExtension(pkgName, { install: true, isObsolete }).response;
|
2023-12-31 01:46:53 +01:00
|
|
|
break;
|
|
|
|
|
case ExtensionAction.UNINSTALL:
|
2024-01-29 20:56:09 +01:00
|
|
|
await requestManager.updateExtension(pkgName, { uninstall: true, isObsolete }).response;
|
2023-12-31 01:46:53 +01:00
|
|
|
break;
|
|
|
|
|
case ExtensionAction.UPDATE:
|
2024-01-29 20:56:09 +01:00
|
|
|
await requestManager.updateExtension(pkgName, { update: true, isObsolete }).response;
|
2023-12-31 01:46:53 +01:00
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
throw new Error(`Unexpected ExtensionAction "${action}"`);
|
|
|
|
|
}
|
|
|
|
|
setInstalledState(nextAction);
|
|
|
|
|
|
|
|
|
|
handleUpdate();
|
|
|
|
|
} catch (e) {
|
|
|
|
|
setInstalledState(getInstalledState(isInstalled, isObsolete, hasUpdate));
|
|
|
|
|
makeToast(t(EXTENSION_ACTION_TO_FAILURE_TRANSLATION_KEY_MAP[action]), 'error');
|
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>
|
2023-02-06 10:06:33 +01:00
|
|
|
<CardContent
|
|
|
|
|
sx={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
alignItems: 'center',
|
2024-08-30 21:34:58 +02:00
|
|
|
gap: 1,
|
|
|
|
|
p: 1.5,
|
2024-01-02 23:32:49 +01:00
|
|
|
'&:last-child': {
|
2024-08-30 21:34:58 +02:00
|
|
|
paddingBottom: 1.5,
|
2024-01-02 23:32:49 +01:00
|
|
|
},
|
2023-02-06 10:06:33 +01:00
|
|
|
}}
|
2021-11-17 14:29:47 +03:30
|
|
|
>
|
2024-07-13 03:05:58 +02:00
|
|
|
<Avatar
|
|
|
|
|
variant="rounded"
|
|
|
|
|
sx={{
|
|
|
|
|
width: 56,
|
|
|
|
|
height: 56,
|
|
|
|
|
flex: '0 0 auto',
|
|
|
|
|
background: 'transparent',
|
|
|
|
|
}}
|
|
|
|
|
alt={name}
|
|
|
|
|
>
|
|
|
|
|
<SpinnerImage
|
|
|
|
|
spinnerStyle={{ small: true }}
|
|
|
|
|
imgStyle={{ objectFit: 'cover', width: '100%', height: '100%' }}
|
2020-12-24 16:50:50 +01:00
|
|
|
alt={name}
|
2024-07-13 03:05:58 +02:00
|
|
|
src={requestManager.getValidImgUrlFor(iconUrl)}
|
|
|
|
|
/>
|
|
|
|
|
</Avatar>
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
display: 'flex',
|
|
|
|
|
flexDirection: 'column',
|
|
|
|
|
flexGrow: 1,
|
|
|
|
|
flexShrink: 1,
|
|
|
|
|
wordBreak: 'break-word',
|
2024-08-30 21:34:58 +02:00
|
|
|
justifyContent: 'center',
|
2024-07-13 03:05:58 +02:00
|
|
|
}}
|
|
|
|
|
>
|
2024-08-30 21:34:58 +02:00
|
|
|
<Typography variant="h6" component="h3">
|
2024-07-13 03:05:58 +02:00
|
|
|
{name}
|
|
|
|
|
</Typography>
|
2024-09-03 17:15:47 +02:00
|
|
|
<Typography
|
|
|
|
|
variant="caption"
|
|
|
|
|
sx={{
|
|
|
|
|
display: 'block',
|
|
|
|
|
}}
|
|
|
|
|
>
|
2024-07-13 03:05:58 +02:00
|
|
|
{langPress} {versionName}
|
|
|
|
|
{isNsfw && (
|
2024-09-03 17:15:47 +02:00
|
|
|
<Typography
|
|
|
|
|
variant="caption"
|
|
|
|
|
color="error"
|
|
|
|
|
sx={{
|
|
|
|
|
display: 'inline',
|
|
|
|
|
}}
|
|
|
|
|
>
|
2024-07-13 03:05:58 +02:00
|
|
|
{' 18+'}
|
2024-01-06 18:38:16 +01:00
|
|
|
</Typography>
|
|
|
|
|
)}
|
2024-07-13 03:05:58 +02:00
|
|
|
</Typography>
|
|
|
|
|
{showSourceRepo && (
|
2024-09-03 17:15:47 +02:00
|
|
|
<Typography
|
|
|
|
|
variant="caption"
|
|
|
|
|
sx={{
|
|
|
|
|
display: 'block',
|
|
|
|
|
}}
|
|
|
|
|
>
|
2024-07-13 03:05:58 +02:00
|
|
|
{repo}
|
|
|
|
|
</Typography>
|
|
|
|
|
)}
|
2021-11-17 14:29:47 +03:30
|
|
|
</Box>
|
2021-03-29 02:47:51 +04:30
|
|
|
<Button
|
|
|
|
|
variant="outlined"
|
2024-07-13 03:05:58 +02:00
|
|
|
sx={{ color: installedState === InstalledState.OBSOLETE ? 'red' : 'inherit', flexShrink: 0 }}
|
2021-03-29 02:47:51 +04:30
|
|
|
onClick={() => handleButtonClick()}
|
|
|
|
|
>
|
2023-07-04 21:53:25 +02:00
|
|
|
{t(INSTALLED_STATE_TO_TRANSLATION_KEY_MAP[installedState])}
|
2021-03-29 02:47:51 +04:30
|
|
|
</Button>
|
2020-12-24 16:50:50 +01:00
|
|
|
</CardContent>
|
|
|
|
|
</Card>
|
|
|
|
|
);
|
|
|
|
|
}
|