Files
suwayomi-material-you-webui/src/screens/Sources.tsx
schroda db65b9df44 Feature/merge source and extensions screen on desktop (#535)
* Merge "source" and "extension" screen for desktop

* Move "tab" related components into new folder

* Reuse "tab" components for "browse" screen

* Prevent extension list from being taller than 100% of the parents height

- remove margin - virtuoso has issues calculating the height otherwise
- change group title styling, fontSize caused issues

* Align "source" list group header with "extension"

* Remove bottom padding from "ExtensionCard"

* Move "GroupedVirtuoso" related components into folder

* Use "GroupedVirtuoso" for "extensions"

There was an issue with the height calculation of the virtuoso list, which apparently was caused by the conditional rendering of the group title and item as list items.
Due to this, while scrolling down, the virtuoso list caused an overflow, which rendered the scrollbar, which in turn caused the list items to shrink in width.
The end result of this was, that the list items "jumped" slightly on the initial render of the items, due to the scrollbar appearing and disappearing
2024-01-02 23:32:49 +01:00

133 lines
4.8 KiB
TypeScript

/*
* Copyright (C) Contributors to the Suwayomi project
*
* This Source Code Form is subject to the terms of the Mozilla Public
* 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/.
*/
import { Fragment, useContext, useEffect } from 'react';
import { IconButton, Tooltip, Typography } from '@mui/material';
import TravelExploreIcon from '@mui/icons-material/TravelExplore';
import { useNavigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { ISource } from '@/typings';
import { requestManager } from '@/lib/requests/RequestManager.ts';
import { useLocalStorage } from '@/util/useLocalStorage';
import { sourceDefualtLangs, sourceForcedDefaultLangs, langSortCmp } from '@/util/language';
import { translateExtensionLanguage } from '@/screens/util/Extensions';
import { LoadingPlaceholder } from '@/components/util/LoadingPlaceholder';
import { SourceCard } from '@/components/SourceCard';
import { LangSelect } from '@/components/navbar/action/LangSelect';
import { NavBarContext } from '@/components/context/NavbarContext.tsx';
function sourceToLangList(sources: ISource[]) {
const result: string[] = [];
sources.forEach((source) => {
if (result.indexOf(source.lang) === -1) {
result.push(source.lang);
}
});
result.sort(langSortCmp);
return result;
}
function groupByLang(sources: ISource[]) {
const result = {} as any;
sources.forEach((source) => {
if (result[source.lang] === undefined) {
result[source.lang] = [] as ISource[];
}
result[source.lang].push(source);
});
return result;
}
export function Sources() {
const { t } = useTranslation();
const { setTitle, setAction } = useContext(NavBarContext);
const [shownLangs, setShownLangs] = useLocalStorage<string[]>('shownSourceLangs', sourceDefualtLangs());
const [showNsfw] = useLocalStorage<boolean>('showNsfw', true);
const { data, loading: isLoading } = requestManager.useGetSourceList();
const sources = data?.sources.nodes;
const navigate = useNavigate();
useEffect(() => {
// make sure all of forcedDefaultLangs() exists in shownLangs
sourceForcedDefaultLangs().forEach((forcedLang) => {
let hasLang = false;
shownLangs.forEach((lang) => {
if (lang === forcedLang) hasLang = true;
});
if (!hasLang) {
setShownLangs((shownLangsCopy) => {
shownLangsCopy.push(forcedLang);
return shownLangsCopy;
});
}
});
}, []);
useEffect(() => {
setTitle(t('source.title'));
setAction(
<>
<Tooltip title={t('search.title.global_search')}>
<IconButton onClick={() => navigate('/sources/all/search/')} size="large">
<TravelExploreIcon />
</IconButton>
</Tooltip>
<LangSelect
shownLangs={shownLangs}
setShownLangs={setShownLangs}
allLangs={sourceToLangList(sources ?? [])}
forcedLangs={sourceForcedDefaultLangs()}
/>
</>,
);
}, [t, shownLangs, sources]);
if (isLoading) return <LoadingPlaceholder />;
if (sources?.length === 0) {
return <h3>{t('source.error.label.no_sources_found')}</h3>;
}
return (
<>
{Object.entries(groupByLang(sources ?? []))
.sort((a, b) => langSortCmp(a[0], b[0]))
.map(
([lang, list]) =>
shownLangs.indexOf(lang) !== -1 && (
<Fragment key={lang}>
<Typography
key={lang}
variant="h4"
style={{
paddingLeft: '24px',
paddingTop: '6px',
paddingBottom: '16px',
fontWeight: 'bold',
}}
>
{translateExtensionLanguage(lang)}
</Typography>
{(list as ISource[])
.filter((source) => showNsfw || !source.isNsfw)
.map((source) => (
<SourceCard key={source.id} source={source} />
))}
</Fragment>
),
)}
</>
);
}