SharedPreferences.tsx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. import React, { PureComponent } from 'react';
  2. import { Label } from 'app/core/components/Label/Label';
  3. import Select from 'app/core/components/Picker/Select';
  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. 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. <Label
  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. </Label>
  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. value={timezones.find(item => item.value === timezone)}
  102. onChange={timezone => this.onTimeZoneChanged(timezone.value)}
  103. options={timezones}
  104. width={20}
  105. />
  106. </div>
  107. <div className="gf-form-button-row">
  108. <button type="submit" className="btn btn-success">
  109. Save
  110. </button>
  111. </div>
  112. </form>
  113. );
  114. }
  115. }
  116. export default SharedPreferences;