import { css } from '@emotion/css'; import { PureComponent } from 'react'; import * as React from 'react'; import { FeatureState, ThemeRegistryItem } from '@grafana/data'; import { selectors } from '@grafana/e2e-selectors'; import { PSEUDO_LOCALE } from '@grafana/i18n'; import { config, reportInteraction } from '@grafana/runtime'; import { Preferences as UserPreferencesDTO } from '@grafana/schema/src/raw/preferences/x/preferences_types.gen'; import { Button, Field, FieldSet, Label, stylesFactory, TimeZonePicker, WeekStartPicker, FeatureBadge, Combobox, ComboboxOption, TextLink, WeekStart, isWeekStart, } from '@grafana/ui'; import { DashboardPicker } from 'app/core/components/Select/DashboardPicker'; import { t, Trans } from 'app/core/internationalization'; import { LANGUAGES } from 'app/core/internationalization/constants'; import { LOCALES } from 'app/core/internationalization/locales'; import { PreferencesService } from 'app/core/services/PreferencesService'; import { changeTheme } from 'app/core/services/theme'; import { getSelectableThemes } from '../ThemeSelector/getSelectableThemes'; export interface Props { resourceUri: string; disabled?: boolean; preferenceType: 'org' | 'team' | 'user'; onConfirm?: () => Promise; } export type State = UserPreferencesDTO & { isLoading: boolean; }; function getLanguageOptions(): ComboboxOption[] { const languageOptions = LANGUAGES.map((v) => ({ value: v.code, label: v.name, })).sort((a, b) => { if (a.value === PSEUDO_LOCALE) { return 1; } if (b.value === PSEUDO_LOCALE) { return -1; } return a.label.localeCompare(b.label); }); const options = [ { value: '', label: t('common.locale.default', 'Default'), }, ...languageOptions, ]; return options; } function getLocaleOptions(): ComboboxOption[] { const localeOptions = LOCALES.map((v) => ({ value: v.code, label: v.name, })).sort((a, b) => { return a.label.localeCompare(b.label); }); const options = [ { value: '', label: t('common.locale.default', 'Default'), }, ...localeOptions, ]; return options; } export class SharedPreferences extends PureComponent { service: PreferencesService; themeOptions: ComboboxOption[]; languageOptions: ComboboxOption[]; localeOptions: ComboboxOption[]; constructor(props: Props) { super(props); this.service = new PreferencesService(props.resourceUri); this.state = { isLoading: false, theme: '', timezone: '', weekStart: '', language: '', locale: '', queryHistory: { homeTab: '' }, navbar: { bookmarkUrls: [] }, }; const themes = getSelectableThemes(); // Options are translated, so must be called after init but call them // in constructor to avoid memo-break of array changing every render this.themeOptions = themes.map((theme) => ({ value: theme.id, label: getTranslatedThemeName(theme), group: theme.isExtra ? t('shared-preferences.theme.experimental', 'Experimental') : undefined, })); this.languageOptions = getLanguageOptions(); this.localeOptions = getLocaleOptions(); // Add default option this.themeOptions.unshift({ value: '', label: t('shared-preferences.theme.default-label', 'Default') }); } async componentDidMount() { this.setState({ isLoading: true, }); const prefs = await this.service.load(); this.setState({ isLoading: false, homeDashboardUID: prefs.homeDashboardUID, theme: prefs.theme, timezone: prefs.timezone, weekStart: prefs.weekStart, language: prefs.language, locale: prefs.locale, queryHistory: prefs.queryHistory, navbar: prefs.navbar, }); } onSubmitForm = async (event: React.FormEvent) => { event.preventDefault(); const confirmationResult = this.props.onConfirm ? await this.props.onConfirm() : true; if (confirmationResult) { const { homeDashboardUID, theme, timezone, weekStart, language, locale, queryHistory, navbar } = this.state; reportInteraction('grafana_preferences_save_button_clicked', { preferenceType: this.props.preferenceType, theme, language, }); await this.service.update({ homeDashboardUID, theme, timezone, weekStart, language, locale, queryHistory, navbar, }); window.location.reload(); } }; onThemeChanged = (value: ComboboxOption) => { this.setState({ theme: value.value }); reportInteraction('grafana_preferences_theme_changed', { toTheme: value.value, preferenceType: this.props.preferenceType, }); if (value.value) { changeTheme(value.value, true); } }; onTimeZoneChanged = (timezone?: string) => { if (typeof timezone !== 'string') { return; } this.setState({ timezone: timezone }); }; onWeekStartChanged = (weekStart?: WeekStart) => { this.setState({ weekStart: weekStart ?? '' }); }; onHomeDashboardChanged = (dashboardUID: string) => { this.setState({ homeDashboardUID: dashboardUID }); }; onLanguageChanged = (language: string) => { this.setState({ language }); reportInteraction('grafana_preferences_language_changed', { toLanguage: language, preferenceType: this.props.preferenceType, }); }; onLocaleChanged = (locale: string) => { this.setState({ locale }); reportInteraction('grafana_preferences_locale_changed', { toLocale: locale, preferenceType: this.props.preferenceType, }); }; render() { const { theme, timezone, weekStart, homeDashboardUID, language, isLoading, locale } = this.state; const { disabled } = this.props; const styles = getStyles(); const currentThemeOption = this.themeOptions.find((x) => x.value === theme) ?? this.themeOptions[0]; return (
Preferences} disabled={disabled}> Enjoying the experimental themes? Tell us what you'd like to see{' '} here. ) : undefined } > Home Dashboard } data-testid="User preferences home dashboard drop down" > this.onHomeDashboardChanged(v?.uid ?? '')} defaultOptions={true} isClearable={true} placeholder={t('shared-preferences.fields.home-dashboard-placeholder', 'Default dashboard')} inputId="home-dashboard-select" /> Language } data-testid="User preferences language drop down" > lang.value === language)?.value || ''} onChange={(lang: ComboboxOption | null) => this.onLanguageChanged(lang?.value ?? '')} options={this.languageOptions} placeholder={t('shared-preferences.fields.language-preference-placeholder', 'Choose language')} id="language-preference-select" /> {config.featureToggles.localeFormatPreference && ( Region format } description={t( 'shared-preferences.fields.locale-preference-description', 'Choose your region to see the corresponding date, time, and number format' )} data-testid="User preferences locale drop down" > loc.value === locale)?.value || ''} onChange={(locale: ComboboxOption | null) => this.onLocaleChanged(locale?.value ?? '')} options={this.localeOptions} placeholder={t('shared-preferences.fields.locale-preference-placeholder', 'Choose region')} id="locale-preference-select" /> )}
); } } export default SharedPreferences; const getStyles = stylesFactory(() => { return { labelText: css({ marginRight: '6px', }), form: css({ width: '100%', maxWidth: '600px', }), }; }); function getTranslatedThemeName(theme: ThemeRegistryItem) { switch (theme.id) { case 'dark': return t('shared.preferences.theme.dark-label', 'Dark'); case 'light': return t('shared.preferences.theme.light-label', 'Light'); case 'system': return t('shared.preferences.theme.system-label', 'System preference'); default: return theme.name; } }