SharedPreferences.tsx 4.0 KB

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