import React, { PureComponent } from 'react'; import { Select, Label } from '@grafana/ui'; import { getBackendSrv, BackendSrv } from 'app/core/services/backend_srv'; import { DashboardSearchHit } from 'app/types'; export interface Props { resourceUri: string; } export interface State { homeDashboardId: number; theme: string; timezone: string; dashboards: DashboardSearchHit[]; } const themes = [{ value: '', label: 'Default' }, { value: 'dark', label: 'Dark' }, { value: 'light', label: 'Light' }]; const timezones = [ { value: '', label: 'Default' }, { value: 'browser', label: 'Local browser time' }, { value: 'utc', label: 'UTC' }, ]; export class SharedPreferences extends PureComponent { backendSrv: BackendSrv = getBackendSrv(); constructor(props) { super(props); this.state = { homeDashboardId: 0, theme: '', timezone: '', dashboards: [], }; } async componentDidMount() { const prefs = await this.backendSrv.get(`/api/${this.props.resourceUri}/preferences`); const dashboards = await this.backendSrv.search({ starred: true }); if (prefs.homeDashboardId > 0 && !dashboards.find(d => d.id === prefs.homeDashboardId)) { const missing = await this.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, dashboards: [{ id: 0, title: 'Default', tags: [], type: '', uid: '', uri: '', url: '' }, ...dashboards], }); } onSubmitForm = async event => { event.preventDefault(); const { homeDashboardId, theme, timezone } = this.state; await this.backendSrv.put(`/api/${this.props.resourceUri}/preferences`, { homeDashboardId, theme, timezone, }); window.location.reload(); }; onThemeChanged = (theme: string) => { this.setState({ theme }); }; onTimeZoneChanged = (timezone: string) => { this.setState({ timezone }); }; onHomeDashboardChanged = (dashboardId: number) => { this.setState({ homeDashboardId: dashboardId }); }; render() { const { theme, timezone, homeDashboardId, dashboards } = this.state; return (

Preferences

UI Theme dashboard.id === homeDashboardId)} getOptionValue={i => i.id} getOptionLabel={i => i.title} onChange={(dashboard: DashboardSearchHit) => this.onHomeDashboardChanged(dashboard.id)} options={dashboards} placeholder="Chose default dashboard" width={20} />