SharedPreferences.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import React, { PureComponent } from 'react';
  2. import { FormLabel, Select } from '@grafana/ui';
  3. import { getBackendSrv, BackendSrv } from 'app/core/services/backend_srv';
  4. import { DashboardSearchHit, DashboardSearchHitType } from 'app/types';
  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: BackendSrv = getBackendSrv();
  22. constructor(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 => {
  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. render() {
  82. const { theme, timezone, homeDashboardId, dashboards } = this.state;
  83. return (
  84. <form className="section gf-form-group" onSubmit={this.onSubmitForm}>
  85. <h3 className="page-heading">Preferences</h3>
  86. <div className="gf-form">
  87. <span className="gf-form-label width-11">UI Theme</span>
  88. <Select
  89. isSearchable={false}
  90. value={themes.find(item => item.value === theme)}
  91. options={themes}
  92. onChange={theme => this.onThemeChanged(theme.value)}
  93. width={20}
  94. />
  95. </div>
  96. <div className="gf-form">
  97. <FormLabel
  98. width={11}
  99. tooltip="Not finding dashboard you want? Star it first, then it should appear in this select box."
  100. >
  101. Home Dashboard
  102. </FormLabel>
  103. <Select
  104. value={dashboards.find(dashboard => dashboard.id === homeDashboardId)}
  105. getOptionValue={i => i.id}
  106. getOptionLabel={i => i.title}
  107. onChange={(dashboard: DashboardSearchHit) => this.onHomeDashboardChanged(dashboard.id)}
  108. options={dashboards}
  109. placeholder="Chose default dashboard"
  110. width={20}
  111. />
  112. </div>
  113. <div className="gf-form">
  114. <label className="gf-form-label width-11">Timezone</label>
  115. <Select
  116. isSearchable={false}
  117. value={timezones.find(item => item.value === timezone)}
  118. onChange={timezone => this.onTimeZoneChanged(timezone.value)}
  119. options={timezones}
  120. width={20}
  121. />
  122. </div>
  123. <div className="gf-form-button-row">
  124. <button type="submit" className="btn btn-primary">
  125. Save
  126. </button>
  127. </div>
  128. </form>
  129. );
  130. }
  131. }
  132. export default SharedPreferences;