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
|
|
|
|
2025-05-03 13:24:55 +02:00
|
|
|
import { useCallback, useEffect, useMemo, 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';
|
2023-04-21 00:27:42 +02:00
|
|
|
import { StringParam, useQueryParam } from 'use-query-params';
|
2024-04-14 15:50:12 +02:00
|
|
|
import Button from '@mui/material/Button';
|
|
|
|
|
import Stack from '@mui/material/Stack';
|
|
|
|
|
import Typography from '@mui/material/Typography';
|
2023-03-23 13:59:32 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2025-06-22 23:12:54 +02:00
|
|
|
import { Link } from 'react-router-dom';
|
2025-05-02 17:56:56 +02:00
|
|
|
import { useWindowEvent } from '@mantine/hooks';
|
2025-08-16 02:48:46 +02:00
|
|
|
import { CustomTooltip } from '@/base/components/CustomTooltip.tsx';
|
2024-10-06 01:27:51 +02:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
2025-08-16 02:48:46 +02:00
|
|
|
import { AppbarSearch } from '@/base/components/AppbarSearch.tsx';
|
|
|
|
|
import { LoadingPlaceholder } from '@/base/components/feedback/LoadingPlaceholder.tsx';
|
|
|
|
|
import { makeToast } from '@/base/utils/Toast.ts';
|
|
|
|
|
import { LanguageSelect } from '@/base/components/inputs/LanguageSelect.tsx';
|
2025-08-15 22:02:58 +02:00
|
|
|
import { ExtensionCard } from '@/features/extension/components/ExtensionCard.tsx';
|
2025-08-16 02:48:46 +02:00
|
|
|
import { StyledGroupedVirtuoso } from '@/base/components/virtuoso/StyledGroupedVirtuoso.tsx';
|
|
|
|
|
import { StyledGroupHeader } from '@/base/components/virtuoso/StyledGroupHeader.tsx';
|
|
|
|
|
import { StyledGroupItemWrapper } from '@/base/components/virtuoso/StyledGroupItemWrapper.tsx';
|
|
|
|
|
import { EmptyViewAbsoluteCentered } from '@/base/components/feedback/EmptyViewAbsoluteCentered.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
2024-10-12 18:41:42 +02:00
|
|
|
import { VirtuosoUtil } from '@/lib/virtuoso/Virtuoso.util.tsx';
|
2024-11-04 01:21:50 +01:00
|
|
|
import {
|
2025-04-21 01:23:27 +02:00
|
|
|
groupExtensionsByLanguage,
|
|
|
|
|
getLanguagesFromExtensions,
|
2024-11-04 01:21:50 +01:00
|
|
|
translateExtensionLanguage,
|
2025-04-21 01:48:25 +02:00
|
|
|
filterExtensions,
|
2025-08-15 22:02:58 +02:00
|
|
|
} from '@/features/extension/Extensions.utils.ts';
|
2025-03-24 23:43:07 +01:00
|
|
|
import {
|
|
|
|
|
ExtensionAction,
|
|
|
|
|
ExtensionGroupState,
|
|
|
|
|
ExtensionState,
|
|
|
|
|
TExtension,
|
2025-08-15 22:02:58 +02:00
|
|
|
} from '@/features/extension/Extensions.types.ts';
|
|
|
|
|
import { EXTENSION_ACTION_TO_FAILURE_TRANSLATION_KEY_MAP } from '@/features/extension/Extensions.constants.ts';
|
2025-08-16 02:48:46 +02:00
|
|
|
import { AppRoutes } from '@/base/AppRoute.constants.ts';
|
2024-12-20 17:24:40 +01:00
|
|
|
import { getErrorMessage } from '@/lib/HelperFunctions.ts';
|
2025-04-21 15:37:54 +02:00
|
|
|
import {
|
|
|
|
|
createUpdateMetadataServerSettings,
|
|
|
|
|
useMetadataServerSettings,
|
2025-08-15 22:02:58 +02:00
|
|
|
} from '@/features/settings/services/ServerSettingsMetadata.ts';
|
|
|
|
|
import { MetadataBrowseSettings } from '@/features/browse/Browse.types.ts';
|
|
|
|
|
import { useAppAction } from '@/features/navigation-bar/hooks/useAppAction.ts';
|
2025-08-16 02:48:46 +02:00
|
|
|
import { SearchParam } from '@/base/Base.types.ts';
|
2021-03-08 21:04:42 +03:30
|
|
|
|
2021-12-19 18:06:27 +03:30
|
|
|
const LANGUAGE = 0;
|
|
|
|
|
const EXTENSIONS = 1;
|
|
|
|
|
|
2025-04-21 01:58:33 +02:00
|
|
|
const GroupHeader = ({
|
|
|
|
|
groupName,
|
|
|
|
|
isFirstItem,
|
|
|
|
|
groupExtensionIds,
|
|
|
|
|
isUpdateGroup,
|
|
|
|
|
updatingExtensionIds,
|
|
|
|
|
setUpdatingExtensionIds,
|
|
|
|
|
handleExtensionUpdate,
|
|
|
|
|
}: {
|
|
|
|
|
groupName: string;
|
|
|
|
|
isFirstItem: boolean;
|
|
|
|
|
groupExtensionIds: TExtension['pkgName'][];
|
|
|
|
|
isUpdateGroup: boolean;
|
|
|
|
|
updatingExtensionIds: TExtension['pkgName'][];
|
|
|
|
|
setUpdatingExtensionIds: (ids: TExtension['pkgName'][]) => void;
|
|
|
|
|
handleExtensionUpdate: () => void;
|
|
|
|
|
}) => {
|
|
|
|
|
const { t } = useTranslation();
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<StyledGroupHeader
|
|
|
|
|
key={groupName}
|
|
|
|
|
isFirstItem={isFirstItem}
|
|
|
|
|
sx={{ flexDirection: 'row', justifyContent: 'space-between', alignItems: 'center', pr: 1 }}
|
|
|
|
|
>
|
|
|
|
|
<Typography variant="h5" component="h2">
|
|
|
|
|
{translateExtensionLanguage(groupName)}
|
|
|
|
|
</Typography>
|
|
|
|
|
{isUpdateGroup && (
|
|
|
|
|
<Button
|
|
|
|
|
disabled={!!updatingExtensionIds.length}
|
|
|
|
|
variant="contained"
|
|
|
|
|
onClick={() => {
|
|
|
|
|
setUpdatingExtensionIds(groupExtensionIds);
|
|
|
|
|
|
|
|
|
|
requestManager
|
|
|
|
|
.updateExtensions(groupExtensionIds, { update: true })
|
|
|
|
|
.response.then(() => handleExtensionUpdate())
|
|
|
|
|
.catch((e) =>
|
|
|
|
|
makeToast(
|
|
|
|
|
t(EXTENSION_ACTION_TO_FAILURE_TRANSLATION_KEY_MAP[ExtensionAction.UPDATE], {
|
|
|
|
|
count: groupExtensionIds.length,
|
|
|
|
|
}),
|
|
|
|
|
'error',
|
|
|
|
|
getErrorMessage(e),
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.finally(() => setUpdatingExtensionIds([]));
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
{t('extension.action.label.update_all')}
|
|
|
|
|
</Button>
|
|
|
|
|
)}
|
|
|
|
|
</StyledGroupHeader>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2024-07-11 17:22:52 +02:00
|
|
|
export function Extensions({ tabsMenuHeight }: { tabsMenuHeight: number }) {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
2025-03-24 23:43:07 +01:00
|
|
|
|
2024-05-05 00:45:50 +02:00
|
|
|
const {
|
|
|
|
|
data: serverSettingsData,
|
|
|
|
|
loading: areServerSettingsLoading,
|
|
|
|
|
error: serverSettingsError,
|
|
|
|
|
refetch: refetchServerSettings,
|
|
|
|
|
} = requestManager.useGetServerSettings({ notifyOnNetworkStatusChange: true });
|
2025-04-21 02:07:23 +02:00
|
|
|
const [fetchExtensions, { data, loading: areExtensionsLoading, error: extensionsError }] =
|
|
|
|
|
requestManager.useExtensionListFetch();
|
2024-01-06 18:38:16 +01:00
|
|
|
|
2025-04-21 15:37:54 +02:00
|
|
|
const {
|
2025-04-21 19:18:42 +02:00
|
|
|
settings: { extensionLanguages: shownLangs, showNsfw },
|
2025-04-21 15:37:54 +02:00
|
|
|
} = useMetadataServerSettings();
|
|
|
|
|
const updateMetadataServerSettings = createUpdateMetadataServerSettings<
|
|
|
|
|
keyof Pick<MetadataBrowseSettings, 'extensionLanguages'>
|
|
|
|
|
>((e) => makeToast(t('global.error.label.failed_to_save_changes'), 'error', getErrorMessage(e)));
|
|
|
|
|
|
2025-07-23 01:52:41 +02:00
|
|
|
const [query] = useQueryParam(SearchParam.QUERY, StringParam);
|
2021-03-08 21:04:42 +03:30
|
|
|
|
2024-11-04 01:26:58 +01:00
|
|
|
const [updatingExtensionIds, setUpdatingExtensionIds] = useState<string[]>([]);
|
2025-04-21 02:07:23 +02:00
|
|
|
const [refetchExtensions, setRefetchExtensions] = useState({});
|
2024-11-04 01:26:58 +01:00
|
|
|
|
2025-04-21 02:07:23 +02:00
|
|
|
const isLoading = areServerSettingsLoading || areExtensionsLoading;
|
|
|
|
|
const error = serverSettingsError ?? extensionsError;
|
2023-11-24 03:20:16 +01:00
|
|
|
|
2025-04-21 02:07:23 +02:00
|
|
|
const areReposDefined = !!serverSettingsData?.settings.extensionRepos.length;
|
|
|
|
|
const areMultipleReposInUse = (serverSettingsData?.settings.extensionRepos.length ?? 0) > 1;
|
2023-02-06 10:06:33 +01:00
|
|
|
|
2025-04-21 02:07:23 +02:00
|
|
|
const allExtensions = data?.fetchExtensions?.extensions;
|
2025-04-21 01:23:27 +02:00
|
|
|
const allLangs = useMemo(() => getLanguagesFromExtensions(allExtensions ?? []), [allExtensions]);
|
|
|
|
|
|
2023-02-06 10:06:33 +01:00
|
|
|
const filteredExtensions = useMemo(
|
2025-04-24 23:53:49 +02:00
|
|
|
() => filterExtensions(allExtensions ?? [], { selectedLanguages: shownLangs, showNsfw, query }),
|
2025-04-24 13:38:35 +02:00
|
|
|
[allExtensions, shownLangs, showNsfw, query],
|
2023-02-06 10:06:33 +01:00
|
|
|
);
|
2025-04-21 01:23:27 +02:00
|
|
|
const groupedExtensions = useMemo(() => groupExtensionsByLanguage(filteredExtensions), [filteredExtensions]);
|
2024-01-02 23:32:49 +01:00
|
|
|
const groupCounts = useMemo(
|
2025-04-21 01:48:25 +02:00
|
|
|
() => groupedExtensions.map((extensionGroup) => extensionGroup[EXTENSIONS].length),
|
|
|
|
|
[groupedExtensions],
|
2024-01-02 23:32:49 +01:00
|
|
|
);
|
|
|
|
|
const visibleExtensions = useMemo(
|
2025-04-21 01:48:25 +02:00
|
|
|
() => groupedExtensions.map(([, extensions]) => extensions).flat(1),
|
|
|
|
|
[groupedExtensions],
|
2024-01-02 23:32:49 +01:00
|
|
|
);
|
2021-03-08 21:04:42 +03:30
|
|
|
|
2024-10-12 18:41:42 +02:00
|
|
|
const computeItemKey = VirtuosoUtil.useCreateGroupedComputeItemKey(
|
|
|
|
|
groupCounts,
|
2025-04-21 01:48:25 +02:00
|
|
|
useCallback((index) => groupedExtensions[index][LANGUAGE], [groupedExtensions]),
|
2024-10-12 18:41:42 +02:00
|
|
|
useCallback((index) => visibleExtensions[index].pkgName, [visibleExtensions]),
|
|
|
|
|
);
|
|
|
|
|
|
2025-04-21 02:07:23 +02:00
|
|
|
const handleExtensionUpdate = useCallback(() => setRefetchExtensions({}), []);
|
|
|
|
|
|
2021-09-09 06:03:59 +04:30
|
|
|
const submitExternalExtension = (file: File) => {
|
2025-04-21 02:07:23 +02:00
|
|
|
if (!file.name.toLowerCase().endsWith('apk')) {
|
2023-03-23 13:59:32 +01:00
|
|
|
makeToast(t('global.error.label.invalid_file_type'), 'error');
|
2025-04-21 02:07:23 +02:00
|
|
|
return;
|
2021-09-09 05:14:17 +04:30
|
|
|
}
|
2025-04-21 02:07:23 +02:00
|
|
|
|
|
|
|
|
makeToast(t('extension.label.installing_file'), 'info');
|
|
|
|
|
requestManager
|
|
|
|
|
.installExternalExtension(file)
|
|
|
|
|
.response.then(() => {
|
|
|
|
|
handleExtensionUpdate();
|
|
|
|
|
makeToast(t('extension.label.installed_successfully'), 'success');
|
|
|
|
|
})
|
|
|
|
|
.catch((e) => makeToast(t('extension.label.installation_failed'), 'error', getErrorMessage(e)));
|
2021-09-09 05:14:17 +04:30
|
|
|
};
|
|
|
|
|
|
2025-04-21 02:07:23 +02:00
|
|
|
useEffect(() => {
|
|
|
|
|
fetchExtensions();
|
|
|
|
|
}, [refetchExtensions]);
|
|
|
|
|
|
2025-05-03 13:24:55 +02:00
|
|
|
useAppAction(
|
|
|
|
|
<>
|
|
|
|
|
<AppbarSearch />
|
|
|
|
|
<CustomTooltip title={t('extension.action.label.install_external')}>
|
|
|
|
|
<IconButton
|
|
|
|
|
onClick={() => {
|
|
|
|
|
const input = document.createElement('input');
|
|
|
|
|
input.style.display = 'none';
|
|
|
|
|
input.type = 'file';
|
|
|
|
|
input.onchange = () => {
|
|
|
|
|
const file = input.files?.[0];
|
|
|
|
|
if (file) {
|
|
|
|
|
submitExternalExtension(file);
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-11-05 17:22:29 +01:00
|
|
|
|
2025-05-03 13:24:55 +02:00
|
|
|
document.documentElement.appendChild(input);
|
|
|
|
|
input.click();
|
|
|
|
|
document.documentElement.removeChild(input);
|
|
|
|
|
}}
|
|
|
|
|
color="inherit"
|
|
|
|
|
>
|
|
|
|
|
<AddIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</CustomTooltip>
|
2024-01-26 20:50:51 +01:00
|
|
|
|
2025-05-03 13:24:55 +02:00
|
|
|
<LanguageSelect
|
|
|
|
|
selectedLanguages={shownLangs}
|
|
|
|
|
setSelectedLanguages={(languages: string[]) =>
|
|
|
|
|
updateMetadataServerSettings('extensionLanguages', languages)
|
|
|
|
|
}
|
|
|
|
|
languages={allLangs}
|
|
|
|
|
/>
|
|
|
|
|
</>,
|
|
|
|
|
[t, shownLangs, allLangs],
|
|
|
|
|
);
|
2023-06-23 00:05:33 +02:00
|
|
|
|
2025-05-02 17:56:56 +02:00
|
|
|
useWindowEvent('drop', async (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
const files = await fromEvent(e);
|
|
|
|
|
submitExternalExtension(files[0] as File);
|
|
|
|
|
});
|
|
|
|
|
useWindowEvent('dragover', (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
});
|
2021-09-09 05:14:17 +04:30
|
|
|
|
2024-04-27 22:28: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
|
|
|
|
2024-04-27 22:28:55 +02:00
|
|
|
if (error) {
|
|
|
|
|
return (
|
2024-04-29 15:29:33 +02:00
|
|
|
<EmptyViewAbsoluteCentered
|
2024-04-27 22:28:55 +02:00
|
|
|
message={t('global.error.label.failed_to_load_data')}
|
2024-12-21 23:27:03 +01:00
|
|
|
messageExtra={getErrorMessage(error)}
|
2024-05-05 00:45:50 +02:00
|
|
|
retry={() => {
|
|
|
|
|
if (serverSettingsError) {
|
|
|
|
|
refetchServerSettings().catch(defaultPromiseErrorHandler('Extensions::refetchServerSettings'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (extensionsError) {
|
|
|
|
|
fetchExtensions().catch(defaultPromiseErrorHandler('Extensions::refetchExtensions'));
|
|
|
|
|
}
|
|
|
|
|
}}
|
2024-04-27 22:28:55 +02:00
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-15 23:22:35 +01:00
|
|
|
const showAddRepoInfo = !allExtensions?.length && !areReposDefined;
|
|
|
|
|
if (showAddRepoInfo) {
|
|
|
|
|
return (
|
2025-04-21 02:07:23 +02:00
|
|
|
<Stack
|
|
|
|
|
sx={{
|
|
|
|
|
alignItems: 'center',
|
|
|
|
|
justifyContent: 'center',
|
|
|
|
|
rowGap: '10px',
|
|
|
|
|
paddingTop: '20px',
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Typography>{t('extension.label.add_repository_info')}</Typography>
|
2025-05-03 11:36:13 +02:00
|
|
|
<Button component={Link} variant="contained" to={AppRoutes.settings.childRoutes.browse.path}>
|
2025-04-21 02:07:23 +02:00
|
|
|
{t('settings.title')}
|
|
|
|
|
</Button>
|
|
|
|
|
</Stack>
|
2024-01-15 23:22:35 +01:00
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2021-03-08 21:04:42 +03:30
|
|
|
return (
|
2025-06-22 23:12:54 +02:00
|
|
|
<StyledGroupedVirtuoso
|
|
|
|
|
persistKey="extensions"
|
|
|
|
|
heightToSubtract={tabsMenuHeight}
|
|
|
|
|
overscan={window.innerHeight * 0.5}
|
|
|
|
|
groupCounts={groupCounts}
|
|
|
|
|
groupContent={(index) => {
|
|
|
|
|
const [groupName, groupExtensions] = groupedExtensions[index];
|
|
|
|
|
const isUpdateGroup = groupName === ExtensionGroupState.UPDATE_PENDING;
|
2024-01-02 23:32:49 +01:00
|
|
|
|
2025-06-22 23:12:54 +02:00
|
|
|
return (
|
|
|
|
|
<GroupHeader
|
|
|
|
|
groupName={groupName}
|
|
|
|
|
isFirstItem={index === 0}
|
|
|
|
|
groupExtensionIds={groupExtensions.map((extension) => extension.pkgName)}
|
|
|
|
|
isUpdateGroup={isUpdateGroup}
|
|
|
|
|
updatingExtensionIds={updatingExtensionIds}
|
|
|
|
|
setUpdatingExtensionIds={setUpdatingExtensionIds}
|
|
|
|
|
handleExtensionUpdate={handleExtensionUpdate}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}}
|
|
|
|
|
computeItemKey={computeItemKey}
|
|
|
|
|
itemContent={(index) => {
|
|
|
|
|
const item = visibleExtensions[index];
|
2023-09-01 20:28:23 +02:00
|
|
|
|
2025-06-22 23:12:54 +02:00
|
|
|
return (
|
|
|
|
|
<StyledGroupItemWrapper>
|
|
|
|
|
<ExtensionCard
|
|
|
|
|
extension={item}
|
|
|
|
|
handleUpdate={handleExtensionUpdate}
|
|
|
|
|
showSourceRepo={areMultipleReposInUse}
|
|
|
|
|
forcedState={
|
|
|
|
|
updatingExtensionIds.includes(item.pkgName) ? ExtensionState.UPDATING : undefined
|
|
|
|
|
}
|
|
|
|
|
/>
|
|
|
|
|
</StyledGroupItemWrapper>
|
|
|
|
|
);
|
|
|
|
|
}}
|
|
|
|
|
/>
|
2021-03-08 21:04:42 +03:30
|
|
|
);
|
2021-01-19 14:47:07 +03:30
|
|
|
}
|