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 { Fragment, useMemo } from 'react';
|
2024-04-14 15:50:12 +02:00
|
|
|
import IconButton from '@mui/material/IconButton';
|
|
|
|
|
import Typography from '@mui/material/Typography';
|
2022-03-12 04:46:55 +00:00
|
|
|
import TravelExploreIcon from '@mui/icons-material/TravelExplore';
|
2023-06-04 21:30:15 +02:00
|
|
|
import { useNavigate } from 'react-router-dom';
|
2023-03-23 13:59:32 +01:00
|
|
|
import { useTranslation } from 'react-i18next';
|
2025-02-01 14:24:38 +01:00
|
|
|
import { CustomTooltip } from '@/modules/core/components/CustomTooltip.tsx';
|
2024-10-06 01:27:51 +02:00
|
|
|
import { requestManager } from '@/lib/requests/RequestManager.ts';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { useLocalStorage } from '@/modules/core/hooks/useStorage.tsx';
|
2025-05-17 23:37:00 +02:00
|
|
|
import { DefaultLanguage, getDefaultLanguages } from '@/modules/core/utils/Languages.ts';
|
2025-05-03 12:14:05 +02:00
|
|
|
import { LoadingPlaceholder } from '@/modules/core/components/feedback/LoadingPlaceholder.tsx';
|
2024-10-05 21:22:12 +02:00
|
|
|
import { SourceCard } from '@/modules/source/components/SourceCard.tsx';
|
2025-04-21 18:18:13 +02:00
|
|
|
import { LanguageSelect } from '@/modules/core/components/inputs/LanguageSelect.tsx';
|
2025-05-03 12:14:05 +02:00
|
|
|
import { EmptyViewAbsoluteCentered } from '@/modules/core/components/feedback/EmptyViewAbsoluteCentered.tsx';
|
2024-10-05 16:09:34 +02:00
|
|
|
import { defaultPromiseErrorHandler } from '@/lib/DefaultPromiseErrorHandler.ts';
|
2024-11-04 01:21:50 +01:00
|
|
|
import { translateExtensionLanguage } from '@/modules/extension/Extensions.utils.ts';
|
2024-12-11 20:10:59 +01:00
|
|
|
import { AppRoutes } from '@/modules/core/AppRoute.constants.ts';
|
2024-12-21 23:27:03 +01:00
|
|
|
import { getErrorMessage } from '@/lib/HelperFunctions.ts';
|
2025-04-21 17:00:13 +02:00
|
|
|
import { Sources as SourceService } from '@/modules/source/services/Sources.ts';
|
2025-04-21 19:18:42 +02:00
|
|
|
import { useMetadataServerSettings } from '@/modules/settings/services/ServerSettingsMetadata.ts';
|
2025-05-03 13:24:55 +02:00
|
|
|
import { useAppAction } from '@/modules/navigation-bar/hooks/useAppAction.ts';
|
2021-03-09 16:44:09 +03:30
|
|
|
|
2024-12-22 21:45:34 +01:00
|
|
|
export function Sources() {
|
2023-03-23 13:59:32 +01:00
|
|
|
const { t } = useTranslation();
|
2021-03-09 16:44:09 +03:30
|
|
|
|
2025-05-09 00:44:48 +02:00
|
|
|
const [shownLangs, setShownLangs] = useLocalStorage<string[]>('shownSourceLangs', getDefaultLanguages());
|
2025-04-21 19:18:42 +02:00
|
|
|
const {
|
2025-05-17 23:37:00 +02:00
|
|
|
settings: { showNsfw, lastUsedSourceId },
|
2025-04-21 19:18:42 +02:00
|
|
|
} = useMetadataServerSettings();
|
2021-03-09 16:44:09 +03:30
|
|
|
|
2024-04-27 22:28:55 +02:00
|
|
|
const {
|
|
|
|
|
data,
|
|
|
|
|
loading: isLoading,
|
|
|
|
|
error,
|
|
|
|
|
refetch,
|
|
|
|
|
} = requestManager.useGetSourceList({ notifyOnNetworkStatusChange: true });
|
2023-09-01 20:29:19 +02:00
|
|
|
const sources = data?.sources.nodes;
|
2025-04-21 17:46:19 +02:00
|
|
|
const filteredSources = useMemo(
|
|
|
|
|
() => SourceService.filter(sources ?? [], { showNsfw, languages: shownLangs, keepLocalSource: true }),
|
|
|
|
|
[sources, shownLangs],
|
|
|
|
|
);
|
2025-05-17 23:37:00 +02:00
|
|
|
const sourcesByLanguage = useMemo(() => {
|
|
|
|
|
const lastUsedSource = SourceService.getLastUsedSource(lastUsedSourceId, filteredSources);
|
|
|
|
|
const groupedByLanguageTuple = Object.entries(SourceService.groupByLanguage(filteredSources));
|
|
|
|
|
|
|
|
|
|
if (lastUsedSource) {
|
|
|
|
|
return [
|
|
|
|
|
[DefaultLanguage.LAST_USED_SOURCE, [lastUsedSource]],
|
|
|
|
|
...groupedByLanguageTuple,
|
|
|
|
|
] satisfies typeof groupedByLanguageTuple;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return groupedByLanguageTuple;
|
|
|
|
|
}, [filteredSources]);
|
2021-01-19 14:47:07 +03:30
|
|
|
|
2025-04-21 17:46:19 +02:00
|
|
|
const sourceLanguages = useMemo(() => SourceService.getLanguages(sources ?? []), [sources]);
|
|
|
|
|
const areSourcesFromDifferentRepos = useMemo(
|
|
|
|
|
() => SourceService.areFromMultipleRepos(filteredSources),
|
|
|
|
|
[filteredSources],
|
|
|
|
|
);
|
2024-01-09 22:09:31 +01:00
|
|
|
|
2023-06-04 21:30:15 +02:00
|
|
|
const navigate = useNavigate();
|
2022-03-12 04:46:55 +00:00
|
|
|
|
2025-05-03 13:24:55 +02:00
|
|
|
useAppAction(
|
|
|
|
|
<>
|
|
|
|
|
<CustomTooltip title={t('search.title.global_search')}>
|
|
|
|
|
<IconButton onClick={() => navigate(AppRoutes.sources.childRoutes.searchAll.path)} color="inherit">
|
|
|
|
|
<TravelExploreIcon />
|
|
|
|
|
</IconButton>
|
|
|
|
|
</CustomTooltip>
|
|
|
|
|
<LanguageSelect
|
|
|
|
|
selectedLanguages={shownLangs}
|
|
|
|
|
setSelectedLanguages={setShownLangs}
|
|
|
|
|
languages={sourceLanguages}
|
|
|
|
|
/>
|
|
|
|
|
</>,
|
|
|
|
|
[t, shownLangs, sourceLanguages],
|
|
|
|
|
);
|
2021-03-09 16:44:09 +03:30
|
|
|
|
2023-05-15 11:56:55 +02:00
|
|
|
if (isLoading) return <LoadingPlaceholder />;
|
2021-01-19 14:47:07 +03: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-04-27 22:28:55 +02:00
|
|
|
retry={() => refetch().catch(defaultPromiseErrorHandler('Sources::refetch'))}
|
|
|
|
|
/>
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-02 21:48:22 +01:00
|
|
|
if (sources?.length === 0) {
|
2024-12-21 23:21:55 +01:00
|
|
|
return <EmptyViewAbsoluteCentered message={t('source.error.label.no_sources_found')} />;
|
2021-01-19 14:47:07 +03:30
|
|
|
}
|
2022-11-02 21:48:22 +01:00
|
|
|
|
2021-03-09 16:44:09 +03:30
|
|
|
return (
|
|
|
|
|
<>
|
2025-05-17 23:37:00 +02:00
|
|
|
{sourcesByLanguage.map(([language, sourcesOfLanguage]) => (
|
|
|
|
|
<Fragment key={language}>
|
2025-04-21 17:46:19 +02:00
|
|
|
<Typography
|
2025-05-17 23:37:00 +02:00
|
|
|
key={language}
|
2025-04-21 17:46:19 +02:00
|
|
|
variant="h5"
|
|
|
|
|
component="h2"
|
|
|
|
|
sx={{
|
|
|
|
|
paddingLeft: 3,
|
|
|
|
|
paddingTop: 1,
|
|
|
|
|
paddingBottom: 2,
|
|
|
|
|
fontWeight: 'bold',
|
|
|
|
|
}}
|
|
|
|
|
>
|
2025-05-17 23:37:00 +02:00
|
|
|
{translateExtensionLanguage(language)}
|
2025-04-21 17:46:19 +02:00
|
|
|
</Typography>
|
|
|
|
|
{sourcesOfLanguage.map((source) => (
|
|
|
|
|
<SourceCard key={source.id} source={source} showSourceRepo={areSourcesFromDifferentRepos} />
|
|
|
|
|
))}
|
|
|
|
|
</Fragment>
|
|
|
|
|
))}
|
2021-03-09 16:44:09 +03:30
|
|
|
</>
|
|
|
|
|
);
|
2021-01-19 14:47:07 +03:30
|
|
|
}
|