SharedPreferences.tsx 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. import React, { PureComponent } from 'react';
  2. import { Label } from 'app/core/components/Label/Label';
  3. import SimplePicker from 'app/core/components/Picker/SimplePicker';
  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: '', text: 'Default' }, { value: 'dark', text: 'Dark' }, { value: 'light', text: 'Light' }];
  16. const timezones = [
  17. { value: '', text: 'Default' },
  18. { value: 'browser', text: 'Local browser time' },
  19. { value: 'utc', text: '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. <SimplePicker
  75. value={themes.find(item => item.value === theme)}
  76. options={themes}
  77. getOptionValue={i => i.value}
  78. getOptionLabel={i => i.text}
  79. onSelected={theme => this.onThemeChanged(theme.value)}
  80. width={20}
  81. />
  82. </div>
  83. <div className="gf-form">
  84. <Label
  85. width={11}
  86. tooltip="Not finding dashboard you want? Star it first, then it should appear in this select box."
  87. >
  88. Home Dashboard
  89. </Label>
  90. <SimplePicker
  91. value={dashboards.find(dashboard => dashboard.id === homeDashboardId)}
  92. getOptionValue={i => i.id}
  93. getOptionLabel={i => i.title}
  94. onSelected={(dashboard: DashboardSearchHit) => this.onHomeDashboardChanged(dashboard.id)}
  95. options={dashboards}
  96. placeholder="Chose default dashboard"
  97. width={20}
  98. />
  99. </div>
  100. <div className="gf-form">
  101. <label className="gf-form-label width-11">Timezone</label>
  102. <SimplePicker
  103. value={timezones.find(item => item.value === timezone)}
  104. getOptionValue={i => i.value}
  105. getOptionLabel={i => i.text}
  106. onSelected={timezone => this.onTimeZoneChanged(timezone.value)}
  107. options={timezones}
  108. width={20}
  109. />
  110. </div>
  111. <div className="gf-form-button-row">
  112. <button type="submit" className="btn btn-success">
  113. Save
  114. </button>
  115. </div>
  116. </form>
  117. );
  118. }
  119. }
  120. export default SharedPreferences;