SharedPreferences.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. import React, { PureComponent } from 'react';
  2. import { FormLabel, Select } from '@grafana/ui';
  3. import { DashboardSearchHit, DashboardSearchHitType } from 'app/types';
  4. import { getBackendSrv } from 'app/core/services/backend_srv';
  5. export interface Props {
  6. resourceUri: string;
  7. }
  8. export interface State {
  9. homeDashboardId: number;
  10. theme: string;
  11. timezone: string;
  12. dashboards: DashboardSearchHit[];
  13. }
  14. const themes = [{ value: '', label: 'Default' }, { value: 'dark', label: 'Dark' }, { value: 'light', label: 'Light' }];
  15. const timezones = [
  16. { value: '', label: 'Default' },
  17. { value: 'browser', label: 'Local browser time' },
  18. { value: 'utc', label: 'UTC' },
  19. ];
  20. export class SharedPreferences extends PureComponent<Props, State> {
  21. backendSrv = getBackendSrv();
  22. constructor(props: Props) {
  23. super(props);
  24. this.state = {
  25. homeDashboardId: 0,
  26. theme: '',
  27. timezone: '',
  28. dashboards: [],
  29. };
  30. }
  31. async componentDidMount() {
  32. const prefs = await this.backendSrv.get(`/api/${this.props.resourceUri}/preferences`);
  33. const dashboards = await this.backendSrv.search({ starred: true });
  34. const defaultDashboardHit: DashboardSearchHit = {
  35. id: 0,
  36. title: 'Default',
  37. tags: [],
  38. type: '' as DashboardSearchHitType,
  39. uid: '',
  40. uri: '',
  41. url: '',
  42. folderId: 0,
  43. folderTitle: '',
  44. folderUid: '',
  45. folderUrl: '',
  46. isStarred: false,
  47. slug: '',
  48. };
  49. if (prefs.homeDashboardId > 0 && !dashboards.find(d => d.id === prefs.homeDashboardId)) {
  50. const missing = await this.backendSrv.search({ dashboardIds: [prefs.homeDashboardId] });
  51. if (missing && missing.length > 0) {
  52. dashboards.push(missing[0]);
  53. }
  54. }
  55. this.setState({
  56. homeDashboardId: prefs.homeDashboardId,
  57. theme: prefs.theme,
  58. timezone: prefs.timezone,
  59. dashboards: [defaultDashboardHit, ...dashboards],
  60. });
  61. }
  62. onSubmitForm = async (event: React.SyntheticEvent) => {
  63. event.preventDefault();
  64. const { homeDashboardId, theme, timezone } = this.state;
  65. await this.backendSrv.put(`/api/${this.props.resourceUri}/preferences`, {
  66. homeDashboardId,
  67. theme,
  68. timezone,
  69. });
  70. window.location.reload();
  71. };
  72. onThemeChanged = (theme: string) => {
  73. this.setState({ theme });
  74. };
  75. onTimeZoneChanged = (timezone: string) => {
  76. this.setState({ timezone });
  77. };
  78. onHomeDashboardChanged = (dashboardId: number) => {
  79. this.setState({ homeDashboardId: dashboardId });
  80. };
  81. getFullDashName = (dashboard: DashboardSearchHit) => {
  82. if (typeof dashboard.folderTitle === 'undefined' || dashboard.folderTitle === '') {
  83. return dashboard.title;
  84. }
  85. return dashboard.folderTitle + ' / ' + dashboard.title;
  86. };
  87. render() {
  88. const { theme, timezone, homeDashboardId, dashboards } = this.state;
  89. return (
  90. <form className="section gf-form-group" onSubmit={this.onSubmitForm}>
  91. <h3 className="page-heading">Preferences</h3>
  92. <div className="gf-form">
  93. <span className="gf-form-label width-11">UI Theme</span>
  94. <Select
  95. isSearchable={false}
  96. value={themes.find(item => item.value === theme)}
  97. options={themes}
  98. onChange={theme => this.onThemeChanged(theme.value)}
  99. width={20}
  100. />
  101. </div>
  102. <div className="gf-form">
  103. <FormLabel
  104. width={11}
  105. tooltip="Not finding dashboard you want? Star it first, then it should appear in this select box."
  106. >
  107. Home Dashboard
  108. </FormLabel>
  109. <Select
  110. value={dashboards.find(dashboard => dashboard.id === homeDashboardId)}
  111. getOptionValue={i => i.id}
  112. getOptionLabel={this.getFullDashName}
  113. onChange={(dashboard: DashboardSearchHit) => this.onHomeDashboardChanged(dashboard.id)}
  114. options={dashboards}
  115. placeholder="Choose default dashboard"
  116. width={20}
  117. />
  118. </div>
  119. <div className="gf-form">
  120. <label className="gf-form-label width-11">Timezone</label>
  121. <Select
  122. isSearchable={false}
  123. value={timezones.find(item => item.value === timezone)}
  124. onChange={timezone => this.onTimeZoneChanged(timezone.value)}
  125. options={timezones}
  126. width={20}
  127. />
  128. </div>
  129. <div className="gf-form-button-row">
  130. <button type="submit" className="btn btn-primary">
  131. Save
  132. </button>
  133. </div>
  134. </form>
  135. );
  136. }
  137. }
  138. export default SharedPreferences;