|
|
|
|
@ -22,7 +22,7 @@ import { |
|
|
|
|
} 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 { DashboardSearchItem, DashboardSearchItemType } from 'app/features/search/types'; |
|
|
|
|
|
|
|
|
|
import { UserPreferencesDTO } from '../../../types'; |
|
|
|
|
|
|
|
|
|
@ -32,7 +32,7 @@ export interface Props { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
export type State = UserPreferencesDTO & { |
|
|
|
|
dashboards: DashboardSearchHit[]; |
|
|
|
|
dashboards: DashboardSearchItem[]; |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
const themes: SelectableValue[] = [ |
|
|
|
|
@ -74,6 +74,22 @@ const languages: Array<SelectableValue<string>> = [ |
|
|
|
|
|
|
|
|
|
const i18nFlag = Boolean(config.featureToggles.internationalization); |
|
|
|
|
|
|
|
|
|
const DEFAULT_DASHBOARD_HOME: DashboardSearchItem = { |
|
|
|
|
title: 'Default', |
|
|
|
|
tags: [], |
|
|
|
|
type: '' as DashboardSearchItemType, |
|
|
|
|
uid: undefined, |
|
|
|
|
uri: '', |
|
|
|
|
url: '', |
|
|
|
|
folderId: 0, |
|
|
|
|
folderTitle: '', |
|
|
|
|
folderUid: '', |
|
|
|
|
folderUrl: '', |
|
|
|
|
isStarred: false, |
|
|
|
|
slug: '', |
|
|
|
|
items: [], |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
export class SharedPreferences extends PureComponent<Props, State> { |
|
|
|
|
service: PreferencesService; |
|
|
|
|
|
|
|
|
|
@ -82,7 +98,7 @@ export class SharedPreferences extends PureComponent<Props, State> { |
|
|
|
|
|
|
|
|
|
this.service = new PreferencesService(props.resourceUri); |
|
|
|
|
this.state = { |
|
|
|
|
homeDashboardId: 0, |
|
|
|
|
homeDashboardUID: DEFAULT_DASHBOARD_HOME.uid, |
|
|
|
|
theme: '', |
|
|
|
|
timezone: '', |
|
|
|
|
weekStart: '', |
|
|
|
|
@ -94,45 +110,44 @@ export class SharedPreferences extends PureComponent<Props, State> { |
|
|
|
|
|
|
|
|
|
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: [], |
|
|
|
|
}; |
|
|
|
|
const dashboards = (await backendSrv.search({ starred: true })) as DashboardSearchItem[]; |
|
|
|
|
|
|
|
|
|
if (prefs.homeDashboardUID && !dashboards.find((d) => d.uid === prefs.homeDashboardUID)) { |
|
|
|
|
const missingDash = await backendSrv.getDashboardByUid(prefs.homeDashboardUID); |
|
|
|
|
|
|
|
|
|
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]); |
|
|
|
|
if (missingDash?.dashboard) { |
|
|
|
|
dashboards.push({ |
|
|
|
|
title: missingDash.dashboard.title, |
|
|
|
|
tags: [], |
|
|
|
|
type: DashboardSearchItemType.DashDB, |
|
|
|
|
uid: missingDash.dashboard.uid, |
|
|
|
|
uri: '', // uri is not part of dashboard metadata
|
|
|
|
|
url: missingDash.meta.url || '', |
|
|
|
|
folderId: missingDash.meta.folderId, |
|
|
|
|
folderTitle: missingDash.meta.folderTitle, |
|
|
|
|
folderUid: missingDash.meta.folderUid, |
|
|
|
|
folderUrl: missingDash.meta.folderUrl, |
|
|
|
|
isStarred: missingDash.meta.isStarred || false, |
|
|
|
|
slug: missingDash.meta.slug, |
|
|
|
|
items: [], |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
this.setState({ |
|
|
|
|
homeDashboardId: prefs.homeDashboardId, |
|
|
|
|
homeDashboardUID: prefs.homeDashboardUID, |
|
|
|
|
theme: prefs.theme, |
|
|
|
|
timezone: prefs.timezone, |
|
|
|
|
weekStart: prefs.weekStart, |
|
|
|
|
locale: prefs.locale, |
|
|
|
|
dashboards: [defaultDashboardHit, ...dashboards], |
|
|
|
|
dashboards: [DEFAULT_DASHBOARD_HOME, ...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 }); |
|
|
|
|
const { homeDashboardUID, theme, timezone, weekStart, locale, queryHistory } = this.state; |
|
|
|
|
await this.service.update({ homeDashboardUID, theme, timezone, weekStart, locale, queryHistory }); |
|
|
|
|
window.location.reload(); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
@ -151,15 +166,15 @@ export class SharedPreferences extends PureComponent<Props, State> { |
|
|
|
|
this.setState({ weekStart: weekStart }); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
onHomeDashboardChanged = (dashboardId: number) => { |
|
|
|
|
this.setState({ homeDashboardId: dashboardId }); |
|
|
|
|
onHomeDashboardChanged = (dashboardUID: string) => { |
|
|
|
|
this.setState({ homeDashboardUID: dashboardUID }); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
onLocaleChanged = (locale: string) => { |
|
|
|
|
this.setState({ locale }); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
getFullDashName = (dashboard: SelectableValue<DashboardSearchHit>) => { |
|
|
|
|
getFullDashName = (dashboard: SelectableValue<DashboardSearchItem>) => { |
|
|
|
|
if (typeof dashboard.folderTitle === 'undefined' || dashboard.folderTitle === '') { |
|
|
|
|
return dashboard.title; |
|
|
|
|
} |
|
|
|
|
@ -167,7 +182,7 @@ export class SharedPreferences extends PureComponent<Props, State> { |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
render() { |
|
|
|
|
const { theme, timezone, weekStart, homeDashboardId, locale, dashboards } = this.state; |
|
|
|
|
const { theme, timezone, weekStart, homeDashboardUID, locale, dashboards } = this.state; |
|
|
|
|
const { disabled } = this.props; |
|
|
|
|
const styles = getStyles(); |
|
|
|
|
|
|
|
|
|
@ -209,11 +224,11 @@ export class SharedPreferences extends PureComponent<Props, State> { |
|
|
|
|
data-testid="User preferences home dashboard drop down" |
|
|
|
|
> |
|
|
|
|
<Select |
|
|
|
|
value={dashboards.find((dashboard) => dashboard.id === homeDashboardId)} |
|
|
|
|
getOptionValue={(i) => i.id} |
|
|
|
|
value={dashboards.find((dashboard) => dashboard.uid === homeDashboardUID)} |
|
|
|
|
getOptionValue={(i) => i.uid} |
|
|
|
|
getOptionLabel={this.getFullDashName} |
|
|
|
|
onChange={(dashboard: SelectableValue<DashboardSearchHit>) => |
|
|
|
|
this.onHomeDashboardChanged(dashboard.id) |
|
|
|
|
onChange={(dashboard: SelectableValue<DashboardSearchItem>) => |
|
|
|
|
this.onHomeDashboardChanged(dashboard.uid) |
|
|
|
|
} |
|
|
|
|
options={dashboards} |
|
|
|
|
placeholder={t({ |
|
|
|
|
|