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
|
|
|
|
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
2021-01-22 17:00:33 +03:30
|
|
|
import React, { useContext, useEffect, 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';
|
2021-05-27 05:13:01 +04:30
|
|
|
import client from 'util/client';
|
|
|
|
|
import useLocalStorage from 'util/useLocalStorage';
|
2021-10-30 16:10:34 +03:30
|
|
|
import LangSelect from 'components/navbar/action/LangSelect';
|
2021-10-18 14:02:51 +03:30
|
|
|
import { extensionDefaultLangs, langCodeToName, 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 13:25:35 +05:30
|
|
|
import ExtensionSearch from 'components/ExtensionSearch';
|
|
|
|
|
import { useQueryParam, StringParam } from 'use-query-params';
|
|
|
|
|
import { GroupedVirtuoso } from 'react-virtuoso';
|
|
|
|
|
import { Typography, useMediaQuery, useTheme } from '@mui/material';
|
2021-03-08 21:04:42 +03:30
|
|
|
|
|
|
|
|
const allLangs: string[] = [];
|
|
|
|
|
|
2021-12-19 13:25:35 +05:30
|
|
|
interface GroupedExtension {
|
|
|
|
|
[key: string]: IExtension[]
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 21:04:42 +03:30
|
|
|
function groupExtensions(extensions: IExtension[]) {
|
|
|
|
|
allLangs.length = 0; // empty the array
|
2021-12-19 13:25:35 +05:30
|
|
|
const sortedExtenions: GroupedExtension = { installed: [], 'updates pending': [], all: [] };
|
2021-03-08 21:04:42 +03:30
|
|
|
extensions.forEach((extension) => {
|
2021-12-19 13:25:35 +05:30
|
|
|
if (sortedExtenions[extension.lang] === undefined) {
|
|
|
|
|
if (sortedExtenions[extension.lang] === undefined) {
|
|
|
|
|
sortedExtenions[extension.lang] = [];
|
|
|
|
|
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) {
|
2021-12-19 13:25:35 +05:30
|
|
|
sortedExtenions['updates pending'].push(extension);
|
2021-03-29 02:47:51 +04:30
|
|
|
} else {
|
2021-12-19 13:25:35 +05:30
|
|
|
sortedExtenions.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);
|
2021-12-19 13:25:35 +05:30
|
|
|
const result: [string, IExtension[]][] = [
|
|
|
|
|
['updates pending', sortedExtenions['updates pending']],
|
|
|
|
|
['installed', sortedExtenions.installed],
|
|
|
|
|
['all', sortedExtenions.all],
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
const langExt: [string, IExtension[]][] = allLangs.map((lang) => [lang, sortedExtenions[lang]]);
|
2021-03-08 21:04:42 +03:30
|
|
|
|
2021-12-19 13:25:35 +05:30
|
|
|
return result.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() {
|
2021-03-08 21:04:42 +03:30
|
|
|
const { setTitle, setAction } = useContext(NavbarContext);
|
2021-03-16 16:09:20 +03:30
|
|
|
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'));
|
|
|
|
|
|
|
|
|
|
// VirtuosoGroup: ExtArr, LangArr, langCountArr
|
|
|
|
|
const [extArr, setExtArr] = useState<IExtension[]>([]);
|
|
|
|
|
const [langArr, setLangArr] = useState<string[]>([]);
|
|
|
|
|
const [langCountArr, setLangCountArr] = useState<number[]>([]);
|
|
|
|
|
const [query] = useQueryParam('query', StringParam);
|
2021-03-08 21:04:42 +03:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
setTitle('Extensions');
|
|
|
|
|
setAction(
|
2021-09-09 06:03:59 +04:30
|
|
|
<>
|
2021-12-19 13:25:35 +05:30
|
|
|
<ExtensionSearch />
|
2021-09-09 06:03:59 +04:30
|
|
|
<IconButton
|
|
|
|
|
onClick={
|
|
|
|
|
() => document.getElementById('external-extension-file')?.click()
|
|
|
|
|
}
|
2021-09-09 17:51:22 +04:30
|
|
|
size="large"
|
2021-09-09 06:03:59 +04:30
|
|
|
>
|
|
|
|
|
<AddIcon />
|
|
|
|
|
</IconButton>
|
2021-10-18 14:24:11 +03:30
|
|
|
<LangSelect
|
2021-09-09 06:03:59 +04:30
|
|
|
shownLangs={shownLangs}
|
|
|
|
|
setShownLangs={setShownLangs}
|
|
|
|
|
allLangs={allLangs}
|
|
|
|
|
/>
|
|
|
|
|
</>,
|
2021-03-08 21:04:42 +03:30
|
|
|
);
|
|
|
|
|
}, [shownLangs]);
|
|
|
|
|
|
|
|
|
|
const [extensionsRaw, setExtensionsRaw] = useState<IExtension[]>([]);
|
|
|
|
|
|
|
|
|
|
const [updateTriggerHolder, setUpdateTriggerHolder] = useState(0); // just a hack
|
|
|
|
|
const triggerUpdate = () => setUpdateTriggerHolder(updateTriggerHolder + 1); // just a hack
|
2021-01-19 14:47:07 +03:30
|
|
|
|
|
|
|
|
useEffect(() => {
|
2021-03-07 22:25:29 +03:30
|
|
|
client.get('/api/v1/extension/list')
|
|
|
|
|
.then((response) => response.data)
|
2021-03-08 21:04:42 +03:30
|
|
|
.then((data) => setExtensionsRaw(data));
|
|
|
|
|
}, [updateTriggerHolder]);
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
if (extensionsRaw.length > 0) {
|
2021-12-19 13:25:35 +05:30
|
|
|
const filtered = extensionsRaw.filter((ext) => {
|
|
|
|
|
const nsfwFilter = showNsfw || !ext.isNsfw;
|
|
|
|
|
if (!query) return nsfwFilter;
|
|
|
|
|
return nsfwFilter && ext.name.toLowerCase().includes(query.toLowerCase());
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
const groupedExtensions: [string, IExtension[]][] = groupExtensions(filtered)
|
|
|
|
|
.filter((group) => group[1].length !== 0)
|
|
|
|
|
.filter((group) => group[0] === 'installed' || 'updates pending' || 'all'
|
|
|
|
|
|| shownLangs.includes(group[0]));
|
|
|
|
|
|
|
|
|
|
// The Virtual List set up
|
|
|
|
|
setExtArr(groupedExtensions.reduce((p, c) => p.concat(...c[1]), [] as IExtension[]));
|
|
|
|
|
setLangArr(groupedExtensions.map((g) => g[0]));
|
|
|
|
|
setLangCountArr(groupedExtensions.map((lang) => lang[1].length));
|
2021-03-08 21:04:42 +03:30
|
|
|
}
|
2021-12-19 13:25:35 +05:30
|
|
|
}, [extensionsRaw, query, shownLangs]);
|
2021-01-19 14:47:07 +03:30
|
|
|
|
2021-09-09 05:14:17 +04:30
|
|
|
const [toasts, makeToast] = makeToaster(useState<React.ReactElement[]>([]));
|
|
|
|
|
|
2021-09-09 06:03:59 +04:30
|
|
|
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);
|
|
|
|
|
|
2021-09-09 06:03:59 +04:30
|
|
|
// empty the input
|
|
|
|
|
// @ts-ignore
|
|
|
|
|
document.getElementById('external-extension-file').value = null;
|
|
|
|
|
|
2021-09-09 05:14:17 +04:30
|
|
|
makeToast('Installing Extension File....', 'info');
|
|
|
|
|
client.post('/api/v1/extension/install',
|
|
|
|
|
formData, { headers: { 'Content-Type': 'multipart/form-data' } })
|
|
|
|
|
.then(() => {
|
|
|
|
|
makeToast('Installed extension successfully!', 'success');
|
|
|
|
|
triggerUpdate();
|
|
|
|
|
})
|
|
|
|
|
.catch(() => makeToast('Extension installion failed!', 'error'));
|
|
|
|
|
} else {
|
|
|
|
|
makeToast('invalid file type!', 'error');
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const dropHandler = async (e: Event) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
const files = await fromEvent(e);
|
|
|
|
|
|
2021-09-09 06:03:59 +04:30
|
|
|
submitExternalExtension(files[0] as File);
|
2021-09-09 05:14:17 +04:30
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const dragOverHandler = (e: Event) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
|
document.addEventListener('drop', dropHandler);
|
|
|
|
|
document.addEventListener('dragover', dragOverHandler);
|
|
|
|
|
|
2021-09-09 06:03:59 +04:30
|
|
|
const changeHandler = async (evt: Event) => {
|
|
|
|
|
const files = await fromEvent(evt);
|
|
|
|
|
submitExternalExtension(files[0] as File);
|
|
|
|
|
};
|
|
|
|
|
const input = document.getElementById('external-extension-file');
|
|
|
|
|
input?.addEventListener('change', changeHandler);
|
|
|
|
|
|
2021-09-09 05:14:17 +04:30
|
|
|
return () => {
|
|
|
|
|
document.removeEventListener('drop', dropHandler);
|
|
|
|
|
document.removeEventListener('dragover', dragOverHandler);
|
2021-09-09 06:03:59 +04:30
|
|
|
input?.removeEventListener('change', changeHandler);
|
2021-09-09 05:14:17 +04:30
|
|
|
};
|
2021-12-19 13:25:35 +05:30
|
|
|
}, [extArr]); // useEffect only after <input> renders
|
2021-09-09 05:14:17 +04:30
|
|
|
|
2021-12-19 13:25:35 +05:30
|
|
|
if (extensionsRaw.length === 0) {
|
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}
|
2021-09-09 06:03:59 +04:30
|
|
|
<input
|
|
|
|
|
type="file"
|
|
|
|
|
id="external-extension-file"
|
|
|
|
|
style={{ display: 'none' }}
|
|
|
|
|
/>
|
2021-12-19 13:25:35 +05:30
|
|
|
<GroupedVirtuoso
|
|
|
|
|
fixedItemHeight={57}
|
|
|
|
|
groupCounts={langCountArr}
|
|
|
|
|
groupContent={(index) => (
|
|
|
|
|
<Typography
|
|
|
|
|
key={langArr[index][0]}
|
|
|
|
|
variant="h4"
|
|
|
|
|
style={{
|
|
|
|
|
paddingLeft: 25, margin: 0, paddingBottom: 5, backgroundColor: 'rgb(18, 18, 18)',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{langCodeToName(langArr[index])}
|
|
|
|
|
</Typography>
|
|
|
|
|
)}
|
|
|
|
|
style={{ height: isMobile ? 'calc(100vh - 64px - 64px)' : 'calc(100vh - 64px)' }}
|
|
|
|
|
itemContent={(index) => (
|
|
|
|
|
<ExtensionCard
|
|
|
|
|
key={extArr[index].apkName}
|
|
|
|
|
extension={extArr[index]}
|
|
|
|
|
notifyInstall={() => {
|
|
|
|
|
triggerUpdate();
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
)}
|
|
|
|
|
/>
|
2021-03-08 21:04:42 +03:30
|
|
|
</>
|
|
|
|
|
);
|
2021-01-19 14:47:07 +03:30
|
|
|
}
|