import { css } from '@emotion/css'; import { t, Trans } from '@lingui/macro'; 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, Icon, Label, RadioButtonGroup, Select, stylesFactory, TimeZonePicker, Tooltip, WeekStartPicker, FeatureBadge, } from '@grafana/ui'; import { PreferencesService } from 'app/core/services/PreferencesService'; import { backendSrv } from 'app/core/services/backend_srv'; import { DashboardSearchHit, DashboardSearchItemType } from 'app/features/search/types'; import { UserPreferencesDTO } from '../../../types'; export interface Props { resourceUri: string; disabled?: boolean; } export type State = UserPreferencesDTO & { dashboards: DashboardSearchHit[]; }; const themes: SelectableValue[] = [ { value: '', label: t({ id: 'shared-preferences.theme.default-label', message: 'Default' }) }, { value: 'dark', label: t({ id: 'shared-preferences.theme.dark-label', message: 'Dark' }) }, { value: 'light', label: t({ id: 'shared-preferences.theme.light-label', message: 'Light' }) }, ]; const languages: Array> = [ { value: '', label: t({ id: 'common.locale.default', message: 'Default', }), }, { value: 'en', label: t({ id: 'common.locale.en', message: 'English', }), }, { value: 'es', label: t({ id: 'common.locale.es', message: 'Spanish', }), }, { value: 'fr', label: t({ id: 'common.locale.fr', message: 'French', }), }, ]; 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 = { homeDashboardId: 0, theme: '', timezone: '', weekStart: '', locale: '', dashboards: [], queryHistory: { homeTab: '' }, }; } async componentDidMount() { const prefs = await this.service.load(); const dashboards = await backendSrv.search({ starred: true }); const defaultDashboardHit: DashboardSearchHit = { id: 0, title: 'Default', tags: [], type: '' as DashboardSearchItemType, uid: '', uri: '', url: '', folderId: 0, folderTitle: '', folderUid: '', folderUrl: '', isStarred: false, slug: '', items: [], }; if (prefs.homeDashboardId > 0 && !dashboards.find((d) => d.id === prefs.homeDashboardId)) { const missing = await backendSrv.search({ dashboardIds: [prefs.homeDashboardId] }); if (missing && missing.length > 0) { dashboards.push(missing[0]); } } this.setState({ homeDashboardId: prefs.homeDashboardId, theme: prefs.theme, timezone: prefs.timezone, weekStart: prefs.weekStart, locale: prefs.locale, dashboards: [defaultDashboardHit, ...dashboards], queryHistory: prefs.queryHistory, }); } onSubmitForm = async () => { const { homeDashboardId, theme, timezone, weekStart, locale, queryHistory } = this.state; await this.service.update({ homeDashboardId, theme, timezone, weekStart, locale, 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 = (dashboardId: number) => { this.setState({ homeDashboardId: dashboardId }); }; onLocaleChanged = (locale: string) => { this.setState({ locale }); }; getFullDashName = (dashboard: SelectableValue) => { if (typeof dashboard.folderTitle === 'undefined' || dashboard.folderTitle === '') { return dashboard.title; } return dashboard.folderTitle + ' / ' + dashboard.title; }; render() { const { theme, timezone, weekStart, homeDashboardId, locale, dashboards } = this.state; const { disabled } = this.props; const styles = getStyles(); const homeDashboardTooltip = ( Not finding the dashboard you want? Star it first, then it should appear in this select box. } > ); return (
{() => { return (
Preferences} disabled={disabled}> item.value === theme)?.value} onChange={this.onThemeChanged} /> Home Dashboard {homeDashboardTooltip} } data-testid="User preferences home dashboard drop down" > lang.value === locale)} onChange={(locale: SelectableValue) => this.onLocaleChanged(locale.value ?? '')} options={languages} placeholder={t({ id: 'shared-preferences.fields.locale-placeholder', message: 'Choose language', })} inputId="locale-select" /> ) : null}
); }}
); } } export default SharedPreferences; const getStyles = stylesFactory(() => { return { labelText: css` margin-right: 6px; `, }; });