Preferences: Support setting any dashboard as home, not just the starred ones (#54258)

This commit is contained in:
Ryan McKinley 2022-08-26 09:40:40 -07:00 committed by GitHub
parent 93c5c175fe
commit 2db6a199b8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 51 additions and 113 deletions

View File

@ -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"]

View File

@ -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

View File

@ -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

View File

@ -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: '',
},

View File

@ -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<DashboardSearchItem, 'uid'> & { 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<SelectableValue<string>> = [
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<Props, State> {
service: PreferencesService;
@ -100,27 +77,16 @@ export class SharedPreferences extends PureComponent<Props, State> {
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<Props, State> {
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<Props, State> {
this.setState({ locale });
};
getFullDashName = (dashboard: SelectableValue<DashboardSearchItem>) => {
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 = (
<Tooltip
content={
<Trans id="shared-preferences.fields.home-dashboard-tooltip">
Not finding the dashboard you want? Star it first, then it should appear in this select box.
</Trans>
}
>
<Icon name="info-circle" />
</Tooltip>
);
return (
<Form onSubmit={this.onSubmitForm}>
{() => {
@ -205,23 +151,18 @@ export class SharedPreferences extends PureComponent<Props, State> {
<span className={styles.labelText}>
<Trans id="shared-preferences.fields.home-dashboard-label">Home Dashboard</Trans>
</span>
{homeDashboardTooltip}
</Label>
}
data-testid="User preferences home dashboard drop down"
>
<Select
value={dashboards.find((dashboard) => dashboard.uid === homeDashboardUID)}
getOptionValue={(i) => i.uid}
getOptionLabel={this.getFullDashName}
onChange={(dashboard: SelectableValue<DashboardSearchItem>) =>
this.onHomeDashboardChanged(dashboard.uid)
}
options={dashboards}
<DashboardPicker
value={homeDashboardUID}
onChange={(v) => this.onHomeDashboardChanged(v?.uid ?? '')}
defaultOptions={true}
isClearable={true}
placeholder={t({
id: 'shared-preferences.fields.home-dashboard-placeholder',
message: 'Choose default dashboard',
message: 'Default dashboard',
})}
inputId="home-dashboard-select"
/>

View File

@ -719,11 +719,7 @@ msgstr "Home Dashboard"
#: public/app/core/components/SharedPreferences/SharedPreferences.tsx
msgid "shared-preferences.fields.home-dashboard-placeholder"
msgstr "Choose default dashboard"
#: public/app/core/components/SharedPreferences/SharedPreferences.tsx
msgid "shared-preferences.fields.home-dashboard-tooltip"
msgstr "Not finding the dashboard you want? Star it first, then it should appear in this select box."
msgstr "Default dashboard"
#: public/app/core/components/SharedPreferences/SharedPreferences.tsx
msgid "shared-preferences.fields.locale-label"

View File

@ -724,10 +724,6 @@ msgstr ""
msgid "shared-preferences.fields.home-dashboard-placeholder"
msgstr ""
#: public/app/core/components/SharedPreferences/SharedPreferences.tsx
msgid "shared-preferences.fields.home-dashboard-tooltip"
msgstr ""
#: public/app/core/components/SharedPreferences/SharedPreferences.tsx
msgid "shared-preferences.fields.locale-label"
msgstr ""

View File

@ -724,10 +724,6 @@ msgstr ""
msgid "shared-preferences.fields.home-dashboard-placeholder"
msgstr ""
#: public/app/core/components/SharedPreferences/SharedPreferences.tsx
msgid "shared-preferences.fields.home-dashboard-tooltip"
msgstr ""
#: public/app/core/components/SharedPreferences/SharedPreferences.tsx
msgid "shared-preferences.fields.locale-label"
msgstr ""

View File

@ -718,10 +718,6 @@ msgstr ""
msgid "shared-preferences.fields.home-dashboard-placeholder"
msgstr ""
#: public/app/core/components/SharedPreferences/SharedPreferences.tsx
msgid "shared-preferences.fields.home-dashboard-tooltip"
msgstr ""
#: public/app/core/components/SharedPreferences/SharedPreferences.tsx
msgid "shared-preferences.fields.locale-label"
msgstr ""