Files
suwayomi-material-you-webui/src/screens/Extensions.tsx
T

230 lines
8.5 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/. */
2023-04-21 00:27:42 +02:00
import React, { useContext, useEffect, useMemo, useRef, useState } from 'react';
2021-09-09 05:14:17 +04:30
import { fromEvent } from 'file-selector';
2021-09-09 17:51:22 +04:30
import IconButton from '@mui/material/IconButton';
import AddIcon from '@mui/icons-material/Add';
2021-10-30 16:10:34 +03:30
import ExtensionCard from 'components/ExtensionCard';
import NavbarContext from 'components/context/NavbarContext';
2022-11-02 21:48:22 +01:00
import client, { useQuery } from 'util/client';
2021-05-27 05:13:01 +04:30
import useLocalStorage from 'util/useLocalStorage';
2021-10-30 16:10:34 +03:30
import LangSelect from 'components/navbar/action/LangSelect';
2023-04-21 00:27:42 +02:00
import { extensionDefaultLangs, DefaultLanguage, langSortCmp } from 'util/language';
2021-10-30 16:10:34 +03:30
import { makeToaster } from 'components/util/Toast';
import LoadingPlaceholder from 'components/util/LoadingPlaceholder';
2021-12-19 18:06:27 +03:30
import AppbarSearch from 'components/util/AppbarSearch';
2023-04-21 00:27:42 +02:00
import { StringParam, useQueryParam } from 'use-query-params';
2021-12-19 18:06:27 +03:30
import { Virtuoso } from 'react-virtuoso';
2021-12-19 13:25:35 +05:30
import { Typography, useMediaQuery, useTheme } from '@mui/material';
import { IExtension } from 'typings';
2023-03-23 13:59:32 +01:00
import { useTranslation } from 'react-i18next';
2023-04-21 00:27:42 +02:00
import {
ExtensionState,
GroupedExtensions,
GroupedExtensionsResult,
isExtensionStateOrLanguage,
translateExtensionLanguage,
} from 'screens/util/Extensions';
2021-03-08 21:04:42 +03:30
2021-12-19 18:06:27 +03:30
const LANGUAGE = 0;
const EXTENSIONS = 1;
2021-03-08 21:04:42 +03:30
const allLangs: string[] = [];
2023-04-21 00:27:42 +02:00
function groupExtensions(extensions: IExtension[]): GroupedExtensionsResult {
2021-03-08 21:04:42 +03:30
allLangs.length = 0; // empty the array
2023-04-21 00:27:42 +02:00
const sortedExtenions: GroupedExtensions = {
[ExtensionState.INSTALLED]: [],
[ExtensionState.UPDATE_PENDING]: [],
[DefaultLanguage.ALL]: [],
[DefaultLanguage.OTHER]: [],
[DefaultLanguage.LOCAL_SOURCE]: [],
};
2021-03-08 21:04:42 +03:30
extensions.forEach((extension) => {
2021-12-19 13:25:35 +05:30
if (sortedExtenions[extension.lang] === undefined) {
2021-12-19 18:06:27 +03:30
sortedExtenions[extension.lang] = [];
2023-02-06 10:06:33 +01:00
if (extension.lang !== 'all') {
allLangs.push(extension.lang);
}
2021-03-08 21:04:42 +03:30
}
if (extension.installed) {
2021-03-29 02:47:51 +04:30
if (extension.hasUpdate) {
2023-04-21 00:27:42 +02:00
sortedExtenions[ExtensionState.UPDATE_PENDING].push(extension);
2021-03-29 02:47:51 +04:30
} else {
2023-04-21 00:27:42 +02:00
sortedExtenions[ExtensionState.INSTALLED].push(extension);
2021-03-29 02:47:51 +04:30
}
2021-03-08 21:04:42 +03:30
} else {
2021-12-19 13:25:35 +05:30
sortedExtenions[extension.lang].push(extension);
2021-03-08 21:04:42 +03:30
}
});
2021-03-09 16:44:09 +03:30
allLangs.sort(langSortCmp);
2023-04-21 00:27:42 +02:00
const result: GroupedExtensionsResult<ExtensionState | DefaultLanguage | string> = [
[ExtensionState.UPDATE_PENDING, sortedExtenions[ExtensionState.UPDATE_PENDING]],
[ExtensionState.INSTALLED, sortedExtenions[ExtensionState.INSTALLED]],
[DefaultLanguage.ALL, sortedExtenions[DefaultLanguage.ALL]],
[DefaultLanguage.OTHER, sortedExtenions[DefaultLanguage.OTHER]],
[DefaultLanguage.LOCAL_SOURCE, sortedExtenions[DefaultLanguage.LOCAL_SOURCE]],
2021-12-19 13:25:35 +05:30
];
2021-03-08 21:04:42 +03:30
2023-04-21 00:27:42 +02:00
const langExt: GroupedExtensionsResult = allLangs.map((lang) => [lang, sortedExtenions[lang]]);
2021-12-19 13:25:35 +05:30
2023-04-21 00:27:42 +02:00
return (result as GroupedExtensionsResult).concat(langExt);
2021-03-08 21:04:42 +03:30
}
2021-01-19 14:47:07 +03:30
2021-05-27 05:13:01 +04:30
export default function MangaExtensions() {
2023-03-23 13:59:32 +01:00
const { t } = useTranslation();
2022-11-02 21:48:22 +01:00
const inputRef = useRef<HTMLInputElement>(null);
2021-03-08 21:04:42 +03:30
const { setTitle, setAction } = useContext(NavbarContext);
2023-02-08 18:19:26 +01:00
const [shownLangs, setShownLangs] = useLocalStorage<string[]>('shownExtensionLangs', extensionDefaultLangs());
2021-09-05 01:25:37 +04:30
const [showNsfw] = useLocalStorage<boolean>('showNsfw', true);
2021-12-19 13:25:35 +05:30
const theme = useTheme();
const isMobile = useMediaQuery(theme.breakpoints.down('sm'));
const [query] = useQueryParam('query', StringParam);
2021-03-08 21:04:42 +03:30
useEffect(() => {
2023-03-23 13:59:32 +01:00
setTitle(t('extension.title'));
2021-03-08 21:04:42 +03:30
setAction(
<>
2021-12-19 18:06:27 +03:30
<AppbarSearch />
2023-02-06 10:06:33 +01:00
<IconButton onClick={() => inputRef.current?.click()} size="large">
<AddIcon />
</IconButton>
2023-02-08 18:19:26 +01:00
<LangSelect shownLangs={shownLangs} setShownLangs={setShownLangs} allLangs={allLangs} />
</>,
2021-03-08 21:04:42 +03:30
);
}, [t, shownLangs]);
2021-03-08 21:04:42 +03:30
2023-05-15 11:56:55 +02:00
const { data: allExtensions, mutate, isLoading } = useQuery<IExtension[]>('/api/v1/extension/list');
2021-03-08 21:04:42 +03:30
2023-02-06 10:06:33 +01:00
const filteredExtensions = useMemo(
() =>
(allExtensions ?? []).filter((ext) => {
const nsfwFilter = showNsfw || !ext.isNsfw;
if (!query) return nsfwFilter;
return nsfwFilter && ext.name.toLowerCase().includes(query.toLowerCase());
}),
[allExtensions, showNsfw, query],
);
2021-01-19 14:47:07 +03:30
2023-02-06 10:06:33 +01:00
const groupedExtensions = useMemo(
() =>
groupExtensions(filteredExtensions)
.filter((group) => group[EXTENSIONS].length > 0)
2023-04-21 00:27:42 +02:00
.filter((group) => isExtensionStateOrLanguage(group[LANGUAGE]) || shownLangs.includes(group[LANGUAGE])),
2023-02-06 10:06:33 +01:00
[shownLangs, filteredExtensions],
);
2022-11-02 21:48:22 +01:00
const flatRenderItems: (IExtension | string)[] = groupedExtensions.flat(2);
2021-03-08 21:04:42 +03:30
2021-09-09 05:14:17 +04:30
const [toasts, makeToast] = makeToaster(useState<React.ReactElement[]>([]));
const submitExternalExtension = (file: File) => {
2021-09-09 05:14:17 +04:30
if (file.name.toLowerCase().endsWith('apk')) {
const formData = new FormData();
formData.append('file', file);
2022-11-02 21:48:22 +01:00
if (inputRef.current) {
inputRef.current.value = '';
}
2023-03-23 13:59:32 +01:00
makeToast(t('extension.label.installing_file'), 'info');
2023-02-06 10:06:33 +01:00
client
.post('/api/v1/extension/install', formData, {
headers: { 'Content-Type': 'multipart/form-data' },
})
2021-09-09 05:14:17 +04:30
.then(() => {
2023-03-23 13:59:32 +01:00
makeToast(t('extension.label.installed_successfully'), 'success');
2022-11-02 21:48:22 +01:00
mutate();
2021-09-09 05:14:17 +04:30
})
2023-03-23 13:59:32 +01:00
.catch(() => makeToast(t('extension.label.installation_failed'), 'error'));
2021-09-09 05:14:17 +04:30
} else {
2023-03-23 13:59:32 +01:00
makeToast(t('global.error.label.invalid_file_type'), 'error');
2021-09-09 05:14:17 +04:30
}
};
useEffect(() => {
2022-11-02 21:48:22 +01:00
const dropHandler = async (e: Event) => {
e.preventDefault();
const files = await fromEvent(e);
submitExternalExtension(files[0] as File);
};
2022-11-02 21:48:22 +01:00
const dragOverHandler = (e: Event) => {
e.preventDefault();
};
document.addEventListener('drop', dropHandler);
document.addEventListener('dragover', dragOverHandler);
2021-09-09 05:14:17 +04:30
return () => {
document.removeEventListener('drop', dropHandler);
document.removeEventListener('dragover', dragOverHandler);
};
2022-11-02 21:48:22 +01:00
}, []);
2021-09-09 05:14:17 +04:30
2023-05-15 11:56:55 +02:00
if (isLoading) {
2021-10-24 22:56:10 +03:30
return <LoadingPlaceholder />;
2021-01-19 14:47:07 +03:30
}
2021-12-19 13:25:35 +05:30
2021-03-08 21:04:42 +03:30
return (
<>
2021-09-09 05:14:17 +04:30
{toasts}
<input
type="file"
style={{ display: 'none' }}
2022-11-02 21:48:22 +01:00
ref={inputRef}
onChange={(e) => {
const file = e.target.files?.[0];
if (file) {
submitExternalExtension(file);
}
}}
/>
2021-12-19 18:06:27 +03:30
<Virtuoso
style={{
height: isMobile ? 'calc(100vh - 64px - 64px)' : 'calc(100vh - 64px)',
}}
totalCount={flatRenderItems.length}
itemContent={(index) => {
2023-02-06 10:06:33 +01:00
if (typeof flatRenderItems[index] === 'string') {
2021-12-19 18:06:27 +03:30
const item = flatRenderItems[index] as string;
return (
<Typography
key={item}
variant="h2"
style={{
paddingLeft: 25,
paddingBottom: '0.83em',
paddingTop: '0.83em',
fontSize: '2em',
fontWeight: 'bold',
}}
>
2023-04-21 00:27:42 +02:00
{translateExtensionLanguage(item)}
2021-12-19 18:06:27 +03:30
</Typography>
);
}
const item = flatRenderItems[index] as IExtension;
return (
<ExtensionCard
key={item.apkName}
extension={item}
notifyInstall={() => {
2022-11-02 21:48:22 +01:00
mutate();
2021-12-19 18:06:27 +03:30
}}
/>
);
}}
2021-12-19 13:25:35 +05:30
/>
2021-03-08 21:04:42 +03:30
</>
);
2021-01-19 14:47:07 +03:30
}