From 2db6a199b8379464961356e575f7d2cabbf6c719 Mon Sep 17 00:00:00 2001 From: Ryan McKinley Date: Fri, 26 Aug 2022 09:40:40 -0700 Subject: [PATCH] Preferences: Support setting any dashboard as home, not just the starred ones (#54258) --- .betterer.results | 3 - pkg/api/preferences.go | 39 ++++++--- .../components/Select/DashboardPicker.tsx | 20 ++--- .../SharedPreferences.test.tsx | 5 +- .../SharedPreferences/SharedPreferences.tsx | 79 +++---------------- public/locales/en-US/messages.po | 6 +- public/locales/es-ES/messages.po | 4 - public/locales/fr-FR/messages.po | 4 - public/locales/pseudo-LOCALE/messages.po | 4 - 9 files changed, 51 insertions(+), 113 deletions(-) diff --git a/.betterer.results b/.betterer.results index f5925e1daa7..1c962564432 100644 --- a/.betterer.results +++ b/.betterer.results @@ -2900,9 +2900,6 @@ exports[`better eslint`] = { "public/app/core/components/Select/SortPicker.tsx:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"] ], - "public/app/core/components/SharedPreferences/SharedPreferences.tsx:5381": [ - [0, 0, 0, "Do not use any type assertions.", "0"] - ], "public/app/core/components/TagFilter/TagBadge.tsx:5381": [ [0, 0, 0, "Unexpected any. Specify a different type.", "0"], [0, 0, 0, "Unexpected any. Specify a different type.", "1"] diff --git a/pkg/api/preferences.go b/pkg/api/preferences.go index d7e050fb696..d505e05331e 100644 --- a/pkg/api/preferences.go +++ b/pkg/api/preferences.go @@ -31,11 +31,15 @@ func (hs *HTTPServer) SetHomeDashboard(c *models.ReqContext) response.Response { dashboardID := cmd.HomeDashboardID if cmd.HomeDashboardUID != nil { query := models.GetDashboardQuery{Uid: *cmd.HomeDashboardUID} - err := hs.DashboardService.GetDashboard(c.Req.Context(), &query) - if err != nil { - return response.Error(404, "Dashboard not found", err) + if query.Uid == "" { + dashboardID = 0 // clear the value + } else { + err := hs.DashboardService.GetDashboard(c.Req.Context(), &query) + if err != nil { + return response.Error(404, "Dashboard not found", err) + } + dashboardID = query.Result.Id } - dashboardID = query.Result.Id } cmd.HomeDashboardID = dashboardID @@ -122,11 +126,16 @@ func (hs *HTTPServer) updatePreferencesFor(ctx context.Context, orgID, userID, t dashboardID := dtoCmd.HomeDashboardID if dtoCmd.HomeDashboardUID != nil { query := models.GetDashboardQuery{Uid: *dtoCmd.HomeDashboardUID, OrgId: orgID} - err := hs.DashboardService.GetDashboard(ctx, &query) - if err != nil { - return response.Error(404, "Dashboard not found", err) + if query.Uid == "" { + // clear the value + dashboardID = 0 + } else { + err := hs.DashboardService.GetDashboard(ctx, &query) + if err != nil { + return response.Error(404, "Dashboard not found", err) + } + dashboardID = query.Result.Id } - dashboardID = query.Result.Id } dtoCmd.HomeDashboardID = dashboardID @@ -176,11 +185,17 @@ func (hs *HTTPServer) patchPreferencesFor(ctx context.Context, orgID, userID, te dashboardID := dtoCmd.HomeDashboardID if dtoCmd.HomeDashboardUID != nil { query := models.GetDashboardQuery{Uid: *dtoCmd.HomeDashboardUID, OrgId: orgID} - err := hs.DashboardService.GetDashboard(ctx, &query) - if err != nil { - return response.Error(404, "Dashboard not found", err) + if query.Uid == "" { + // clear the value + defaultDash := int64(0) + dashboardID = &defaultDash + } else { + err := hs.DashboardService.GetDashboard(ctx, &query) + if err != nil { + return response.Error(404, "Dashboard not found", err) + } + dashboardID = &query.Result.Id } - dashboardID = &query.Result.Id } dtoCmd.HomeDashboardID = dashboardID diff --git a/public/app/core/components/Select/DashboardPicker.tsx b/public/app/core/components/Select/DashboardPicker.tsx index 4426cc4a99e..279dab1cb23 100644 --- a/public/app/core/components/Select/DashboardPicker.tsx +++ b/public/app/core/components/Select/DashboardPicker.tsx @@ -53,15 +53,17 @@ export const DashboardPicker = ({ // value was manually changed from outside or we are rendering for the first time. // We need to fetch dashboard information. const res = await backendSrv.getDashboardByUid(value); - setCurrent({ - value: { - uid: res.dashboard.uid, - title: res.dashboard.title, - folderTitle: res.meta.folderTitle, - folderUid: res.meta.folderUid, - }, - label: formatLabel(res.meta?.folderTitle, res.dashboard.title), - }); + if (res.dashboard) { + setCurrent({ + value: { + uid: res.dashboard.uid, + title: res.dashboard.title, + folderTitle: res.meta.folderTitle, + folderUid: res.meta.folderUid, + }, + label: formatLabel(res.meta?.folderTitle, res.dashboard.title), + }); + } })(); // we don't need to rerun this effect every time `current` changes // eslint-disable-next-line react-hooks/exhaustive-deps diff --git a/public/app/core/components/SharedPreferences/SharedPreferences.test.tsx b/public/app/core/components/SharedPreferences/SharedPreferences.test.tsx index 29351822ab1..88b4df6bb19 100644 --- a/public/app/core/components/SharedPreferences/SharedPreferences.test.tsx +++ b/public/app/core/components/SharedPreferences/SharedPreferences.test.tsx @@ -161,7 +161,6 @@ describe('SharedPreferences', () => { const darkThemeRadio = assertInstanceOf(screen.getByLabelText('Dark'), HTMLInputElement); await userEvent.click(darkThemeRadio); - await selectOptionInTest(screen.getByLabelText('Home Dashboard'), 'Another Dashboard'); await selectOptionInTest(screen.getByLabelText('Timezone'), 'Australia/Sydney'); await selectOptionInTest(screen.getByLabelText('Week start'), 'Saturday'); await selectOptionInTest(screen.getByLabelText(/language/i), 'French'); @@ -171,7 +170,7 @@ describe('SharedPreferences', () => { timezone: 'Australia/Sydney', weekStart: 'saturday', theme: 'dark', - homeDashboardUID: 'anotherDash', + homeDashboardUID: 'myDash', queryHistory: { homeTab: '', }, @@ -193,7 +192,7 @@ describe('SharedPreferences', () => { timezone: 'browser', weekStart: '', theme: '', - homeDashboardUID: undefined, + homeDashboardUID: 'myDash', queryHistory: { homeTab: '', }, diff --git a/public/app/core/components/SharedPreferences/SharedPreferences.tsx b/public/app/core/components/SharedPreferences/SharedPreferences.tsx index d3ed914bbd1..53fd311273a 100644 --- a/public/app/core/components/SharedPreferences/SharedPreferences.tsx +++ b/public/app/core/components/SharedPreferences/SharedPreferences.tsx @@ -10,33 +10,25 @@ import { Field, FieldSet, Form, - Icon, Label, RadioButtonGroup, Select, stylesFactory, TimeZonePicker, - Tooltip, WeekStartPicker, FeatureBadge, } from '@grafana/ui'; +import { DashboardPicker } from 'app/core/components/Select/DashboardPicker'; import { ENGLISH_US, FRENCH_FRANCE, SPANISH_SPAIN } from 'app/core/internationalization/constants'; import { PreferencesService } from 'app/core/services/PreferencesService'; -import { backendSrv } from 'app/core/services/backend_srv'; -import { DashboardSearchItem, DashboardSearchItemType } from 'app/features/search/types'; - -import { UserPreferencesDTO } from '../../../types'; +import { UserPreferencesDTO } from 'app/types'; export interface Props { resourceUri: string; disabled?: boolean; } -type DefaultDashboardSearchItem = Omit & { uid?: string }; - -export type State = UserPreferencesDTO & { - dashboards: DashboardSearchItem[] | DefaultDashboardSearchItem[]; -}; +export type State = UserPreferencesDTO; const themes: SelectableValue[] = [ { value: '', label: t({ id: 'shared-preferences.theme.default-label', message: 'Default' }) }, @@ -77,21 +69,6 @@ const languages: Array> = [ const i18nFlag = Boolean(config.featureToggles.internationalization); -const DEFAULT_DASHBOARD_HOME: DefaultDashboardSearchItem = { - title: 'Default', - tags: [], - type: '' as DashboardSearchItemType, - uid: undefined, - uri: '', - url: '', - folderTitle: '', - folderUid: '', - folderUrl: '', - isStarred: false, - slug: '', - items: [], -}; - export class SharedPreferences extends PureComponent { service: PreferencesService; @@ -100,27 +77,16 @@ export class SharedPreferences extends PureComponent { this.service = new PreferencesService(props.resourceUri); this.state = { - homeDashboardUID: DEFAULT_DASHBOARD_HOME.uid, theme: '', timezone: '', weekStart: '', locale: '', - dashboards: [], queryHistory: { homeTab: '' }, }; } async componentDidMount() { const prefs = await this.service.load(); - const dashboards = await backendSrv.search({ starred: true }); - - if (prefs.homeDashboardUID && !dashboards.find((d) => d.uid === prefs.homeDashboardUID)) { - const missingDash = await backendSrv.search({ dashboardUIDs: prefs.homeDashboardUID }); - - if (missingDash.length > 0) { - dashboards.push(missingDash[0]); - } - } this.setState({ homeDashboardUID: prefs.homeDashboardUID, @@ -128,7 +94,6 @@ export class SharedPreferences extends PureComponent { timezone: prefs.timezone, weekStart: prefs.weekStart, locale: prefs.locale, - dashboards: [DEFAULT_DASHBOARD_HOME, ...dashboards], queryHistory: prefs.queryHistory, }); } @@ -162,30 +127,11 @@ export class SharedPreferences extends PureComponent { 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, homeDashboardUID, locale, dashboards } = this.state; + const { theme, timezone, weekStart, homeDashboardUID, locale } = 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 (
{() => { @@ -205,23 +151,18 @@ export class SharedPreferences extends PureComponent { Home Dashboard - - {homeDashboardTooltip} } data-testid="User preferences home dashboard drop down" > -