Remove "forced languages" from "LangSelect"
This commit is contained in:
@@ -23,30 +23,18 @@ import { CustomTooltip } from '@/modules/core/components/CustomTooltip.tsx';
|
|||||||
import { cloneObject } from '@/util/cloneObject.tsx';
|
import { cloneObject } from '@/util/cloneObject.tsx';
|
||||||
import { translateExtensionLanguage } from '@/modules/extension/Extensions.utils.ts';
|
import { translateExtensionLanguage } from '@/modules/extension/Extensions.utils.ts';
|
||||||
|
|
||||||
function removeAll(firstList: any[], secondList: any[]) {
|
|
||||||
secondList.forEach((item) => {
|
|
||||||
const index = firstList.indexOf(item);
|
|
||||||
if (index !== -1) {
|
|
||||||
firstList.splice(index, 1);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
return firstList;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface IProps {
|
interface IProps {
|
||||||
shownLangs: string[];
|
shownLangs: string[];
|
||||||
setShownLangs: (arg0: string[]) => void;
|
setShownLangs: (arg0: string[]) => void;
|
||||||
allLangs: string[];
|
allLangs: string[];
|
||||||
forcedLangs?: string[];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export function LangSelect(props: IProps) {
|
export function LangSelect(props: IProps) {
|
||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
|
|
||||||
const { shownLangs, setShownLangs, allLangs, forcedLangs = [] } = props;
|
const { shownLangs, setShownLangs, allLangs } = props;
|
||||||
// hold a copy and only sate state on parent when OK pressed, improves performance
|
// hold a copy and only sate state on parent when OK pressed, improves performance
|
||||||
const [mShownLangs, setMShownLangs] = useState(removeAll(cloneObject(shownLangs), forcedLangs));
|
const [mShownLangs, setMShownLangs] = useState(cloneObject(shownLangs));
|
||||||
const [open, setOpen] = useState<boolean>(false);
|
const [open, setOpen] = useState<boolean>(false);
|
||||||
|
|
||||||
const handleCancel = () => {
|
const handleCancel = () => {
|
||||||
|
|||||||
@@ -252,7 +252,6 @@ export const SearchAll: React.FC = () => {
|
|||||||
shownLangs={shownLangs}
|
shownLangs={shownLangs}
|
||||||
setShownLangs={setShownLangs}
|
setShownLangs={setShownLangs}
|
||||||
allLangs={sourceToLangList(sources)}
|
allLangs={sourceToLangList(sources)}
|
||||||
forcedLangs={sourceForcedDefaultLangs()}
|
|
||||||
/>
|
/>
|
||||||
</>,
|
</>,
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -6,7 +6,7 @@
|
|||||||
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import { Fragment, useEffect, useLayoutEffect, useMemo } from 'react';
|
import { Fragment, useLayoutEffect, useMemo } from 'react';
|
||||||
import IconButton from '@mui/material/IconButton';
|
import IconButton from '@mui/material/IconButton';
|
||||||
import Typography from '@mui/material/Typography';
|
import Typography from '@mui/material/Typography';
|
||||||
import TravelExploreIcon from '@mui/icons-material/TravelExplore';
|
import TravelExploreIcon from '@mui/icons-material/TravelExplore';
|
||||||
@@ -67,7 +67,11 @@ export function Sources() {
|
|||||||
const { t } = useTranslation();
|
const { t } = useTranslation();
|
||||||
const { setAction } = useNavBarContext();
|
const { setAction } = useNavBarContext();
|
||||||
|
|
||||||
const [shownLangs, setShownLangs] = useLocalStorage<string[]>('shownSourceLangs', sourceDefualtLangs());
|
const [savedShownLangs, setShownLangs] = useLocalStorage<string[]>('shownSourceLangs', sourceDefualtLangs());
|
||||||
|
const shownLangs = useMemo(
|
||||||
|
() => [...new Set([...savedShownLangs, ...sourceDefualtLangs(), ...sourceForcedDefaultLangs()])],
|
||||||
|
[savedShownLangs],
|
||||||
|
);
|
||||||
const [showNsfw] = useLocalStorage<boolean>('showNsfw', true);
|
const [showNsfw] = useLocalStorage<boolean>('showNsfw', true);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@@ -89,22 +93,6 @@ export function Sources() {
|
|||||||
|
|
||||||
const navigate = useNavigate();
|
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;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
useLayoutEffect(() => {
|
useLayoutEffect(() => {
|
||||||
setAction(
|
setAction(
|
||||||
<>
|
<>
|
||||||
@@ -117,7 +105,6 @@ export function Sources() {
|
|||||||
shownLangs={shownLangs}
|
shownLangs={shownLangs}
|
||||||
setShownLangs={setShownLangs}
|
setShownLangs={setShownLangs}
|
||||||
allLangs={sourceToLangList(sources ?? [])}
|
allLangs={sourceToLangList(sources ?? [])}
|
||||||
forcedLangs={sourceForcedDefaultLangs()}
|
|
||||||
/>
|
/>
|
||||||
</>,
|
</>,
|
||||||
);
|
);
|
||||||
|
|||||||
Reference in New Issue
Block a user