From b2abde638f52c933bf82fa9eb6dc5ec24f1edd56 Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Mon, 30 Sep 2024 03:38:49 +0200 Subject: [PATCH] Handle "local source" as source with language "other" Same as in mihon --- src/screens/Sources.tsx | 45 ++++++++++++++++++++++------------ src/screens/util/Extensions.ts | 2 +- src/util/language.tsx | 2 -- 3 files changed, 31 insertions(+), 18 deletions(-) diff --git a/src/screens/Sources.tsx b/src/screens/Sources.tsx index 3d254979..8dc61ee9 100644 --- a/src/screens/Sources.tsx +++ b/src/screens/Sources.tsx @@ -15,7 +15,7 @@ import { useNavigate } from 'react-router-dom'; import { useTranslation } from 'react-i18next'; import { requestManager } from '@/lib/requests/RequestManager.ts'; import { useLocalStorage } from '@/util/useStorage.tsx'; -import { sourceDefualtLangs, sourceForcedDefaultLangs, langSortCmp } from '@/util/language'; +import { sourceDefualtLangs, sourceForcedDefaultLangs, langSortCmp, DefaultLanguage } from '@/util/language'; import { translateExtensionLanguage } from '@/screens/util/Extensions'; import { LoadingPlaceholder } from '@/components/util/LoadingPlaceholder'; import { SourceCard } from '@/components/SourceCard'; @@ -25,26 +25,28 @@ import { EmptyViewAbsoluteCentered } from '@/components/util/EmptyViewAbsoluteCe import { defaultPromiseErrorHandler } from '@/util/defaultPromiseErrorHandler.ts'; import { SourceType } from '@/lib/graphql/generated/graphql.ts'; -function sourceToLangList(sources: Pick[]) { - const result: string[] = []; +function sourceToLangList(sources: Pick[]) { + const result = new Set(); sources.forEach((source) => { - if (result.indexOf(source.lang) === -1) { - result.push(source.lang); - } + const isLocalSource = Number(source.id) === 0; + const lang = isLocalSource ? DefaultLanguage.OTHER : source.lang; + + result.add(lang); }); - result.sort(langSortCmp); - return result; + return [...result].sort(langSortCmp); } -function groupByLang>(sources: Source[]): Record { +function groupByLang>(sources: Source[]): Record { const result: Record = {}; + sources.forEach((source) => { - if (result[source.lang] === undefined) { - result[source.lang] = []; - } - result[source.lang].push(source); + const isLocalSource = Number(source.id) === 0; + const lang = isLocalSource ? DefaultLanguage.OTHER : source.lang; + + result[lang] ??= []; + result[lang].push(source); }); return result; @@ -136,7 +138,7 @@ export function Sources() { .sort((a, b) => langSortCmp(a[0], b[0])) .map( ([lang, list]) => - shownLangs.indexOf(lang) !== -1 && ( + (lang === DefaultLanguage.OTHER || shownLangs.includes(lang)) && ( {list - .filter((source) => showNsfw || !source.isNsfw) + .filter((source) => { + const isLangOther = lang === DefaultLanguage.OTHER; + if (isLangOther) { + const isLocalSource = Number(source.id) === 0; + const isLangShown = shownLangs.includes(lang); + + const isLangOtherSourceShown = isLangShown || isLocalSource; + if (!isLangOtherSourceShown) { + return false; + } + } + + return showNsfw || !source.isNsfw; + }) .map((source) => ( diff --git a/src/util/language.tsx b/src/util/language.tsx index 0c30456e..0e6771fc 100644 --- a/src/util/language.tsx +++ b/src/util/language.tsx @@ -73,8 +73,6 @@ export const langSortCmp = (a: string, b: string) => { if (a === 'en') return -1; if (b === 'en') return 1; - if (a === DefaultLanguage.LOCAL_SOURCE) return 1; - if (b === DefaultLanguage.LOCAL_SOURCE) return -1; return aLang > bLang ? 1 : -1; };