SharedPreferences.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 } 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. if (prefs.homeDashboardId > 0 && !dashboards.find(d => d.id === prefs.homeDashboardId)) {
  35. const missing = await this.backendSrv.search({ dashboardIds: [prefs.homeDashboardId] });
  36. if (missing && missing.length > 0) {
  37. dashboards.push(missing[0]);
  38. }
  39. }
  40. this.setState({
  41. homeDashboardId: prefs.homeDashboardId,
  42. theme: prefs.theme,
  43. timezone: prefs.timezone,
  44. dashboards: [{ id: 0, title: 'Default', tags: [], type: '', uid: '', uri: '', url: '' }, ...dashboards],
  45. });
  46. }
  47. onSubmitForm = async event => {
  48. event.preventDefault();
  49. const { homeDashboardId, theme, timezone } = this.state;
  50. await this.backendSrv.put(`/api/${this.props.resourceUri}/preferences`, {
  51. homeDashboardId,
  52. theme,
  53. timezone,
  54. });
  55. window.location.reload();
  56. };
  57. onThemeChanged = (theme: string) => {
  58. this.setState({ theme });
  59. };
  60. onTimeZoneChanged = (timezone: string) => {
  61. this.setState({ timezone });
  62. };
  63. onHomeDashboardChanged = (dashboardId: number) => {
  64. this.setState({ homeDashboardId: dashboardId });
  65. };
  66. render() {
  67. const { theme, timezone, homeDashboardId, dashboards } = this.state;
  68. return (
  69. <form className="section gf-form-group" onSubmit={this.onSubmitForm}>
  70. <h3 className="page-heading">Preferences</h3>
  71. <div className="gf-form">
  72. <span className="gf-form-label width-11">UI Theme</span>
  73. <Select
  74. isSearchable={false}
  75. value={themes.find(item => item.value === theme)}
  76. options={themes}
  77. onChange={theme => this.onThemeChanged(theme.value)}
  78. width={20}
  79. />
  80. </div>
  81. <div className="gf-form">
  82. <FormLabel
  83. width={11}
  84. tooltip="Not finding dashboard you want? Star it first, then it should appear in this select box."
  85. >
  86. Home Dashboard
  87. </FormLabel>
  88. <Select
  89. value={dashboards.find(dashboard => dashboard.id === homeDashboardId)}
  90. getOptionValue={i => i.id}
  91. getOptionLabel={i => i.title}
  92. onChange={(dashboard: DashboardSearchHit) => this.onHomeDashboardChanged(dashboard.id)}
  93. options={dashboards}
  94. placeholder="Chose default dashboard"
  95. width={20}
  96. />
  97. </div>
  98. <div className="gf-form">
  99. <label className="gf-form-label width-11">Timezone</label>
  100. <Select
  101. isSearchable={false}
  102. value={timezones.find(item => item.value === timezone)}
  103. onChange={timezone => this.onTimeZoneChanged(timezone.value)}
  104. options={timezones}
  105. width={20}
  106. />
  107. </div>
  108. <div className="gf-form-button-row">
  109. <button type="submit" className="btn btn-success">
  110. Save
  111. </button>
  112. </div>
  113. </form>
  114. );
  115. }
  116. }
  117. export default SharedPreferences;