Files
suwayomi-material-you-webui/src/modules/source/screens/Sources.tsx

120 lines
4.8 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/.
*/
2021-01-26 23:32:12 +03:30
2025-05-03 13:24:55 +02:00
import { Fragment, useMemo } from 'react';
import IconButton from '@mui/material/IconButton';
import Typography from '@mui/material/Typography';
import TravelExploreIcon from '@mui/icons-material/TravelExplore';
import { useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { CustomTooltip } from '@/modules/core/components/CustomTooltip.tsx';
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-09 00:44:48 +02:00
import { 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';
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() {
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 {
settings: { showNsfw },
} = 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],
);
const sourcesByLanguageTuple = useMemo(
() => Object.entries(SourceService.groupByLanguage(filteredSources)),
[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],
);
const navigate = useNavigate();
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
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'))}
/>
);
}
if (sources?.length === 0) {
return <EmptyViewAbsoluteCentered message={t('source.error.label.no_sources_found')} />;
2021-01-19 14:47:07 +03:30
}
2021-03-09 16:44:09 +03:30
return (
<>
2025-04-21 17:46:19 +02:00
{sourcesByLanguageTuple.map(([languages, sourcesOfLanguage]) => (
<Fragment key={languages}>
<Typography
key={languages}
variant="h5"
component="h2"
sx={{
paddingLeft: 3,
paddingTop: 1,
paddingBottom: 2,
fontWeight: 'bold',
}}
>
{translateExtensionLanguage(languages)}
</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
}