import React, { PureComponent } from 'react'; import { css } from 'emotion'; import { Select, Field, Form, Tooltip, Icon, stylesFactory, Label, Button, RadioButtonGroup, FieldSet, TimeZonePicker, } from '@grafana/ui'; import { SelectableValue } from '@grafana/data'; import { selectors } from '@grafana/e2e-selectors'; import { DashboardSearchHit, DashboardSearchItemType } from 'app/features/search/types'; import { backendSrv } from 'app/core/services/backend_srv'; export interface Props { resourceUri: string; } export interface State { homeDashboardId: number; theme: string; timezone: string; dashboards: DashboardSearchHit[]; } const themes: SelectableValue[] = [ { value: '', label: 'Default' }, { value: 'dark', label: 'Dark' }, { value: 'light', label: 'Light' }, ]; export class SharedPreferences extends PureComponent { backendSrv = backendSrv; constructor(props: Props) { super(props); this.state = { homeDashboardId: 0, theme: '', timezone: '', dashboards: [], }; } async componentDidMount() { const prefs = await backendSrv.get(`/api/${this.props.resourceUri}/preferences`); 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, dashboards: [defaultDashboardHit, ...dashboards], }); } onSubmitForm = async () => { const { homeDashboardId, theme, timezone } = this.state; await backendSrv.put(`/api/${this.props.resourceUri}/preferences`, { homeDashboardId, theme, timezone, }); window.location.reload(); }; onThemeChanged = (value: string) => { this.setState({ theme: value }); }; onTimeZoneChanged = (timezone: string) => { if (!timezone) { return; } this.setState({ timezone: timezone }); }; onHomeDashboardChanged = (dashboardId: number) => { this.setState({ homeDashboardId: dashboardId }); }; getFullDashName = (dashboard: DashboardSearchHit) => { if (typeof dashboard.folderTitle === 'undefined' || dashboard.folderTitle === '') { return dashboard.title; } return dashboard.folderTitle + ' / ' + dashboard.title; }; render() { const { theme, timezone, homeDashboardId, dashboards } = this.state; const styles = getStyles(); return (
{() => { return (
item.value === theme)?.value} onChange={this.onThemeChanged} /> Home Dashboard } >