Files
suwayomi-material-you-webui/src/components/ExtensionCard.tsx

135 lines
4.2 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/. */
2020-12-25 03:08:08 +03:30
import React, { 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';
import client from 'util/client';
import useLocalStorage from 'util/useLocalStorage';
import { Box } from '@mui/system';
2020-12-24 16:50:50 +01:00
interface IProps {
extension: IExtension
2021-03-08 21:04:42 +03:30
notifyInstall: () => void
2020-12-24 16:50:50 +01:00
}
2020-12-25 03:08:08 +03:30
export default function ExtensionCard(props: IProps) {
const {
extension: {
2021-03-29 02:47:51 +04:30
name, lang, versionName, installed, hasUpdate, obsolete, pkgName, iconUrl,
2020-12-25 03:08:08 +03:30
},
2021-03-08 21:04:42 +03:30
notifyInstall,
2020-12-25 03:08:08 +03:30
} = props;
2021-03-29 02:47:51 +04:30
const [installedState, setInstalledState] = useState<string>(
() => {
if (obsolete) { return 'obsolete'; }
if (hasUpdate) { return 'update'; }
return (installed ? 'uninstall' : 'install');
},
);
2020-12-25 03:08:08 +03:30
2021-03-07 22:35:27 +03:30
const [serverAddress] = useLocalStorage<String>('serverBaseURL', '');
2021-09-18 20:59:23 +04:30
const [useCache] = useLocalStorage<boolean>('useCache', true);
2021-03-07 22:35:27 +03:30
2020-12-24 16:50:50 +01:00
const langPress = lang === 'all' ? 'All' : lang.toUpperCase();
2020-12-25 03:08:08 +03:30
function install() {
setInstalledState('installing');
client.get(`/api/v1/extension/install/${pkgName}`)
.then(() => {
setInstalledState('uninstall');
2021-03-08 21:04:42 +03:30
notifyInstall();
});
2020-12-25 03:08:08 +03:30
}
2021-03-29 02:47:51 +04:30
function update() {
setInstalledState('updating');
client.get(`/api/v1/extension/update/${pkgName}`)
.then(() => {
setInstalledState('uninstall');
notifyInstall();
});
}
2021-01-29 15:23:29 +03:30
function uninstall() {
setInstalledState('uninstalling');
client.get(`/api/v1/extension/uninstall/${pkgName}`)
.then(() => {
2021-03-08 21:04:42 +03:30
// setInstalledState('install');
notifyInstall();
});
2021-01-29 15:23:29 +03:30
}
function handleButtonClick() {
2021-03-29 02:47:51 +04:30
switch (installedState) {
case 'install':
install();
break;
case 'update':
update();
break;
case 'obsolete':
uninstall();
setTimeout(() => window.location.reload(), 3000);
break;
case 'uninstall':
uninstall();
break;
default:
break;
2021-01-29 15:23:29 +03:30
}
}
2020-12-24 16:50:50 +01:00
return (
<Card sx={{ margin: '10px' }}>
<CardContent sx={{
display: 'flex',
justifyContent: 'space-between',
alignItems: 'center',
p: 2,
}}
>
<Box sx={{ display: 'flex' }}>
2020-12-24 16:50:50 +01:00
<Avatar
variant="rounded"
sx={{
width: '56px',
height: '56px',
flex: '0 0 auto',
mr: 2,
}}
2020-12-24 16:50:50 +01:00
alt={name}
2021-09-18 20:59:23 +04:30
src={`${serverAddress}${iconUrl}?useCache=${useCache}`}
2020-12-24 16:50:50 +01:00
/>
<Box sx={{ display: 'flex', flexDirection: 'column' }}>
2020-12-24 16:50:50 +01:00
<Typography variant="h5" component="h2">
{name}
</Typography>
<Typography variant="caption" display="block" gutterBottom>
{langPress}
{' '}
{versionName}
</Typography>
</Box>
</Box>
2020-12-24 16:50:50 +01:00
2021-03-29 02:47:51 +04:30
<Button
variant="outlined"
style={{ color: installedState === 'obsolete' ? 'red' : 'inherit' }}
onClick={() => handleButtonClick()}
>
{installedState}
</Button>
2020-12-24 16:50:50 +01:00
</CardContent>
</Card>
);
}