Files
ThreadNet-Web/src/components/views/elements/LanguageDropdown.tsx
T

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

121 lines
3.9 KiB
TypeScript
Raw Normal View History

/*
2024-09-09 14:57:16 +01:00
Copyright 2024 New Vector Ltd.
Copyright 2017 Marcel Radzio (MTRNord)
2017-05-25 12:02:05 +01:00
Copyright 2017 Vector Creations Ltd.
SPDX-License-Identifier: AGPL-3.0-only OR GPL-3.0-only OR LicenseRef-Element-Commercial
2024-09-09 14:57:16 +01:00
Please see LICENSE files in the repository root for full details.
*/
2025-02-05 13:25:06 +00:00
import React, { type ReactElement } from "react";
2023-08-22 15:07:16 +01:00
import classNames from "classnames";
2017-05-23 18:32:45 +01:00
import * as languageHandler from "../../../languageHandler";
import { _t } from "../../../languageHandler";
2021-09-15 20:35:30 +02:00
import Spinner from "./Spinner";
import Dropdown from "./Dropdown";
2025-02-05 13:25:06 +00:00
import { type NonEmptyArray } from "../../../@types/common";
2023-08-22 15:07:16 +01:00
type Languages = Awaited<ReturnType<typeof languageHandler.getAllLanguagesWithLabels>>;
function languageMatchesSearchQuery(query: string, language: Languages[0]): boolean {
2023-08-22 15:07:16 +01:00
if (language.labelInTargetLanguage.toUpperCase().includes(query.toUpperCase())) return true;
2020-03-31 14:05:56 -06:00
if (language.label.toUpperCase().includes(query.toUpperCase())) return true;
if (language.value.toUpperCase() === query.toUpperCase()) return true;
return false;
}
2021-09-15 20:35:30 +02:00
interface IProps {
className?: string;
onOptionChange: (language: string) => void;
value: string;
2021-09-15 20:35:30 +02:00
disabled?: boolean;
}
interface IState {
searchQuery: string;
langs: Languages | null;
2021-09-15 20:35:30 +02:00
}
export default class LanguageDropdown extends React.Component<IProps, IState> {
public constructor(props: IProps) {
super(props);
this.state = {
searchQuery: "",
2017-05-24 11:25:06 +01:00
langs: null,
2017-10-11 17:56:17 +01:00
};
}
2021-09-15 20:35:30 +02:00
public componentDidMount(): void {
languageHandler
2023-08-22 15:07:16 +01:00
.getAllLanguagesWithLabels()
.then((langs) => {
2017-10-11 17:56:17 +01:00
langs.sort(function (a, b) {
2023-08-22 15:07:16 +01:00
if (a.labelInTargetLanguage < b.labelInTargetLanguage) return -1;
if (a.labelInTargetLanguage > b.labelInTargetLanguage) return 1;
2017-05-24 11:25:06 +01:00
return 0;
2022-12-12 12:24:14 +01:00
});
2021-06-29 13:11:58 +01:00
this.setState({ langs });
2022-12-12 12:24:14 +01:00
})
2017-05-24 11:25:06 +01:00
.catch(() => {
2023-08-22 15:07:16 +01:00
this.setState({
langs: [
{
value: "en",
label: "English",
labelInTargetLanguage: "English",
},
],
});
2017-05-24 11:25:06 +01:00
});
if (!this.props.value) {
2023-08-22 15:07:16 +01:00
// If no value is given, we start with the first country selected,
// but our parent component doesn't know this, therefore we do this.
2021-05-12 14:08:32 -06:00
const language = languageHandler.getUserLanguage();
this.props.onOptionChange(language);
}
}
2021-09-15 20:35:30 +02:00
private onSearchChange = (search: string): void => {
this.setState({
searchQuery: search,
});
2021-09-15 20:35:30 +02:00
};
public render(): React.ReactNode {
2017-05-24 11:25:06 +01:00
if (this.state.langs === null) {
return <Spinner />;
}
2023-08-22 15:07:16 +01:00
let displayedLanguages: Awaited<ReturnType<typeof languageHandler.getAllLanguagesWithLabels>>;
if (this.state.searchQuery) {
2017-05-24 11:25:06 +01:00
displayedLanguages = this.state.langs.filter((lang) => {
return languageMatchesSearchQuery(this.state.searchQuery, lang);
});
} else {
2017-05-24 11:25:06 +01:00
displayedLanguages = this.state.langs;
}
const options = displayedLanguages.map((language) => {
2023-08-22 15:07:16 +01:00
return <div key={language.value}>{language.labelInTargetLanguage}</div>;
}) as NonEmptyArray<ReactElement & { key: string }>;
return (
<Dropdown
id="mx_LanguageDropdown"
2023-08-22 15:07:16 +01:00
className={classNames("mx_LanguageDropdown", this.props.className)}
onOptionChange={this.props.onOptionChange}
2021-09-15 20:35:30 +02:00
onSearchChange={this.onSearchChange}
searchEnabled={true}
value={this.props.value}
label={_t("language_dropdown_label")}
2020-04-15 14:19:47 +01:00
disabled={this.props.disabled}
>
2017-10-11 17:56:17 +01:00
{options}
</Dropdown>
);
}
}