mirror of
https://github.com/grafana/grafana.git
synced 2025-02-15 01:53:33 -06:00
* FormField to LegacyForms * FormLabel to InlineFormLabel * Move SecretFormField to LeagcyForms
166 lines
4.6 KiB
TypeScript
166 lines
4.6 KiB
TypeScript
import React, { PureComponent } from 'react';
|
|
|
|
import { InlineFormLabel, LegacyForms } from '@grafana/ui';
|
|
const { Select } = LegacyForms;
|
|
|
|
import { DashboardSearchHit, DashboardSearchHitType } from 'app/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 = [
|
|
{ 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<Props, State> {
|
|
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 DashboardSearchHitType,
|
|
uid: '',
|
|
uri: '',
|
|
url: '',
|
|
folderId: 0,
|
|
folderTitle: '',
|
|
folderUid: '',
|
|
folderUrl: '',
|
|
isStarred: false,
|
|
slug: '',
|
|
};
|
|
|
|
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 (event: React.SyntheticEvent) => {
|
|
event.preventDefault();
|
|
|
|
const { homeDashboardId, theme, timezone } = this.state;
|
|
|
|
await 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 });
|
|
};
|
|
|
|
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;
|
|
|
|
return (
|
|
<form className="section gf-form-group" onSubmit={this.onSubmitForm}>
|
|
<h3 className="page-heading">Preferences</h3>
|
|
<div className="gf-form">
|
|
<span className="gf-form-label width-11">UI Theme</span>
|
|
<Select
|
|
isSearchable={false}
|
|
value={themes.find(item => item.value === theme)}
|
|
options={themes}
|
|
onChange={theme => this.onThemeChanged(theme.value)}
|
|
width={20}
|
|
/>
|
|
</div>
|
|
<div className="gf-form">
|
|
<InlineFormLabel
|
|
width={11}
|
|
tooltip="Not finding dashboard you want? Star it first, then it should appear in this select box."
|
|
>
|
|
Home Dashboard
|
|
</InlineFormLabel>
|
|
<Select
|
|
value={dashboards.find(dashboard => dashboard.id === homeDashboardId)}
|
|
getOptionValue={i => i.id}
|
|
getOptionLabel={this.getFullDashName}
|
|
onChange={(dashboard: DashboardSearchHit) => this.onHomeDashboardChanged(dashboard.id)}
|
|
options={dashboards}
|
|
placeholder="Choose default dashboard"
|
|
width={20}
|
|
/>
|
|
</div>
|
|
<div className="gf-form">
|
|
<label className="gf-form-label width-11">Timezone</label>
|
|
<Select
|
|
isSearchable={false}
|
|
value={timezones.find(item => item.value === timezone)}
|
|
onChange={timezone => this.onTimeZoneChanged(timezone.value)}
|
|
options={timezones}
|
|
width={20}
|
|
/>
|
|
</div>
|
|
<div className="gf-form-button-row">
|
|
<button type="submit" className="btn btn-primary">
|
|
Save
|
|
</button>
|
|
</div>
|
|
</form>
|
|
);
|
|
}
|
|
}
|
|
|
|
export default SharedPreferences;
|