import { css } from '@emotion/css'; import React, { PureComponent } from 'react'; import { FeatureState, SelectableValue } from '@grafana/data'; import { selectors } from '@grafana/e2e-selectors'; import { config } from '@grafana/runtime'; import { Button, Field, FieldSet, Form, Label, RadioButtonGroup, Select, stylesFactory, TimeZonePicker, WeekStartPicker, FeatureBadge, } 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 { PreferencesService } from 'app/core/services/PreferencesService'; import { UserPreferencesDTO } from 'app/types'; export interface Props { resourceUri: string; disabled?: boolean; onConfirm?: () => Promise; } export type State = UserPreferencesDTO; const themes: SelectableValue[] = [ { value: '', label: t('shared-preferences.theme.default-label', 'Default') }, { value: 'dark', label: t('shared-preferences.theme.dark-label', 'Dark') }, { value: 'light', label: t('shared-preferences.theme.light-label', 'Light') }, ]; function getLanguageOptions(): Array> { const languageOptions = LANGUAGES.map((v) => ({ value: v.code, label: v.name, })); const options = [ { value: '', label: t('common.locale.default', 'Default'), }, ...languageOptions, ]; return options; } const i18nFlag = Boolean(config.featureToggles.internationalization); export class SharedPreferences extends PureComponent { service: PreferencesService; constructor(props: Props) { super(props); this.service = new PreferencesService(props.resourceUri); this.state = { theme: '', timezone: '', weekStart: '', language: '', queryHistory: { homeTab: '' }, }; } async componentDidMount() { const prefs = await this.service.load(); this.setState({ homeDashboardUID: prefs.homeDashboardUID, theme: prefs.theme, timezone: prefs.timezone, weekStart: prefs.weekStart, language: prefs.language, queryHistory: prefs.queryHistory, }); } onSubmitForm = async () => { const confirmationResult = this.props.onConfirm ? await this.props.onConfirm() : true; if (confirmationResult) { const { homeDashboardUID, theme, timezone, weekStart, language, queryHistory } = this.state; await this.service.update({ homeDashboardUID, theme, timezone, weekStart, language, queryHistory }); window.location.reload(); } }; onThemeChanged = (value: string) => { this.setState({ theme: value }); }; onTimeZoneChanged = (timezone?: string) => { if (!timezone) { return; } this.setState({ timezone: timezone }); }; onWeekStartChanged = (weekStart: string) => { this.setState({ weekStart: weekStart }); }; onHomeDashboardChanged = (dashboardUID: string) => { this.setState({ homeDashboardUID: dashboardUID }); }; onLanguageChanged = (language: string) => { this.setState({ language }); }; render() { const { theme, timezone, weekStart, homeDashboardUID, language } = this.state; const { disabled } = this.props; const styles = getStyles(); const languages = getLanguageOptions(); return (
{() => { return (
Preferences} disabled={disabled}> item.value === theme)?.value} onChange={this.onThemeChanged} /> 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" /> {i18nFlag ? ( Language } data-testid="User preferences language drop down" >