lemmy-ui/src/shared/utils/app/selectable-languages.ts
Alec Armbruster 043b522ff1
utils.ts organization, round two (#1427)
* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* wip

* Update src/shared/utils/app/convert-comment-sort-type.ts

Co-authored-by: SleeplessOne1917 <abias1122@gmail.com>

* prettier pass

---------

Co-authored-by: SleeplessOne1917 <abias1122@gmail.com>
2023-06-21 18:28:24 -04:00

34 lines
1 KiB
TypeScript

import { Language } from "lemmy-js-client";
import { UserService } from "../../services";
/**
* This shows what language you can select
*
* Use showAll for the site form
* Use showSite for the profile and community forms
* Use false for both those to filter on your profile and site ones
*/
export default function selectableLanguages(
allLanguages: Language[],
siteLanguages: number[],
showAll?: boolean,
showSite?: boolean,
myUserInfo = UserService.Instance.myUserInfo
): Language[] {
const allLangIds = allLanguages.map(l => l.id);
let myLangs = myUserInfo?.discussion_languages ?? allLangIds;
myLangs = myLangs.length == 0 ? allLangIds : myLangs;
const siteLangs = siteLanguages.length == 0 ? allLangIds : siteLanguages;
if (showAll) {
return allLanguages;
} else {
if (showSite) {
return allLanguages.filter(x => siteLangs.includes(x.id));
} else {
return allLanguages
.filter(x => siteLangs.includes(x.id))
.filter(x => myLangs.includes(x.id));
}
}
}