-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring
LocaleSelect
& SystemStore
.
- Loading branch information
1 parent
201052b
commit 1073c44
Showing
3 changed files
with
86 additions
and
89 deletions.
There are no files selected for viewing
85 changes: 0 additions & 85 deletions
85
graylog2-web-interface/src/components/common/LocaleSelect.jsx
This file was deleted.
Oops, something went wrong.
76 changes: 76 additions & 0 deletions
76
graylog2-web-interface/src/components/common/LocaleSelect.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
/* | ||
* Copyright (C) 2020 Graylog, Inc. | ||
* | ||
* This program is free software: you can redistribute it and/or modify | ||
* it under the terms of the Server Side Public License, version 1, | ||
* as published by MongoDB, Inc. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* Server Side Public License for more details. | ||
* | ||
* You should have received a copy of the Server Side Public License | ||
* along with this program. If not, see | ||
* <http://www.mongodb.com/licensing/server-side-public-license>. | ||
*/ | ||
import * as React from 'react'; | ||
|
||
import Select from 'components/common/Select'; | ||
import type { Locales } from 'stores/system/SystemStore'; | ||
import { SystemStore } from 'stores/system/SystemStore'; | ||
import Spinner from 'components/common/Spinner'; | ||
import { useStore } from 'stores/connect'; | ||
|
||
const _formatLocales = (locales: Array<Locales>) => { | ||
const sortedLocales = Object.values(locales) | ||
.filter((locale) => locale.language_tag !== 'und') | ||
.map((locale) => ({ value: locale.language_tag, label: locale.display_name })) | ||
.sort((a, b) => { | ||
const nameA = a.label.toUpperCase(); | ||
const nameB = b.label.toUpperCase(); | ||
|
||
if (nameA < nameB) { | ||
return -1; | ||
} | ||
|
||
if (nameA > nameB) { | ||
return 1; | ||
} | ||
|
||
return 0; | ||
}); | ||
|
||
return [{ value: 'und', label: 'Default locale' }].concat(sortedLocales); | ||
}; | ||
|
||
type Option = { | ||
value: string, | ||
label: string, | ||
} | ||
const _renderOption = (option: Option) => ( | ||
<span key={option.value} title="{option.value} [{option.value}]">{option.label} [{option.value}]</span> | ||
); | ||
|
||
/** | ||
* Component that renders a form input with all available locale settings. It also makes easy to filter | ||
* values to quickly find the locale needed. | ||
*/ | ||
const LocaleSelect = (props: Omit<React.ComponentProps<typeof Select>, 'placeholder' | 'options' | 'optionRenderer'>) => { | ||
const { locales } = useStore(SystemStore); | ||
|
||
if (!locales) { | ||
return <Spinner />; | ||
} | ||
|
||
const _locales = _formatLocales(locales); | ||
|
||
return ( | ||
<Select {...props} | ||
placeholder="Pick a locale" | ||
options={_locales} | ||
optionRenderer={_renderOption} /> | ||
); | ||
}; | ||
|
||
export default LocaleSelect; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters