LinkSettingsEdit.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. import React, { useState } from 'react';
  2. import { SelectableValue } from '@grafana/data';
  3. import { CollapsableSection, TagsInput, Select, Field, Input, Checkbox, Button } from '@grafana/ui';
  4. import { DashboardLink, DashboardModel } from '../../state/DashboardModel';
  5. export const newLink = {
  6. icon: 'external link',
  7. title: 'New link',
  8. tooltip: '',
  9. type: 'dashboards',
  10. url: '',
  11. asDropdown: false,
  12. tags: [],
  13. targetBlank: false,
  14. keepTime: false,
  15. includeVars: false,
  16. } as DashboardLink;
  17. const linkTypeOptions = [
  18. { value: 'dashboards', label: 'Dashboards' },
  19. { value: 'link', label: 'Link' },
  20. ];
  21. export const linkIconMap: { [key: string]: string } = {
  22. 'external link': 'external-link-alt',
  23. dashboard: 'apps',
  24. question: 'question-circle',
  25. info: 'info-circle',
  26. bolt: 'bolt',
  27. doc: 'file-alt',
  28. cloud: 'cloud',
  29. };
  30. const linkIconOptions = Object.keys(linkIconMap).map((key) => ({ label: key, value: key }));
  31. type LinkSettingsEditProps = {
  32. editLinkIdx: number;
  33. dashboard: DashboardModel;
  34. onGoBack: () => void;
  35. };
  36. export const LinkSettingsEdit: React.FC<LinkSettingsEditProps> = ({ editLinkIdx, dashboard, onGoBack }) => {
  37. const [linkSettings, setLinkSettings] = useState(editLinkIdx !== null ? dashboard.links[editLinkIdx] : newLink);
  38. const onUpdate = (link: DashboardLink) => {
  39. const links = [...dashboard.links];
  40. links.splice(editLinkIdx, 1, link);
  41. dashboard.links = links;
  42. setLinkSettings(link);
  43. };
  44. const onTagsChange = (tags: any[]) => {
  45. onUpdate({ ...linkSettings, tags: tags });
  46. };
  47. const onTypeChange = (selectedItem: SelectableValue) => {
  48. const update = { ...linkSettings, type: selectedItem.value };
  49. // clear props that are no longe revant for this type
  50. if (update.type === 'dashboards') {
  51. update.url = '';
  52. update.tooltip = '';
  53. } else {
  54. update.tags = [];
  55. }
  56. onUpdate(update);
  57. };
  58. const onIconChange = (selectedItem: SelectableValue) => {
  59. onUpdate({ ...linkSettings, icon: selectedItem.value });
  60. };
  61. const onChange = (ev: React.FocusEvent<HTMLInputElement>) => {
  62. const target = ev.currentTarget;
  63. onUpdate({
  64. ...linkSettings,
  65. [target.name]: target.type === 'checkbox' ? target.checked : target.value,
  66. });
  67. };
  68. const isNew = linkSettings.title === newLink.title;
  69. return (
  70. <div style={{ maxWidth: '600px' }}>
  71. <Field label="Title">
  72. <Input name="title" id="title" value={linkSettings.title} onChange={onChange} autoFocus={isNew} />
  73. </Field>
  74. <Field label="Type">
  75. <Select inputId="link-type-input" value={linkSettings.type} options={linkTypeOptions} onChange={onTypeChange} />
  76. </Field>
  77. {linkSettings.type === 'dashboards' && (
  78. <>
  79. <Field label="With tags">
  80. <TagsInput tags={linkSettings.tags} placeholder="add tags" onChange={onTagsChange} />
  81. </Field>
  82. </>
  83. )}
  84. {linkSettings.type === 'link' && (
  85. <>
  86. <Field label="URL">
  87. <Input name="url" value={linkSettings.url} onChange={onChange} />
  88. </Field>
  89. <Field label="Tooltip">
  90. <Input name="tooltip" value={linkSettings.tooltip} onChange={onChange} placeholder="Open dashboard" />
  91. </Field>
  92. <Field label="Icon">
  93. <Select value={linkSettings.icon} options={linkIconOptions} onChange={onIconChange} />
  94. </Field>
  95. </>
  96. )}
  97. <CollapsableSection label="Options" isOpen={true}>
  98. {linkSettings.type === 'dashboards' && (
  99. <Field>
  100. <Checkbox label="Show as dropdown" name="asDropdown" value={linkSettings.asDropdown} onChange={onChange} />
  101. </Field>
  102. )}
  103. <Field>
  104. <Checkbox
  105. label="Include current time range"
  106. name="keepTime"
  107. value={linkSettings.keepTime}
  108. onChange={onChange}
  109. />
  110. </Field>
  111. <Field>
  112. <Checkbox
  113. label="Include current template variable values"
  114. name="includeVars"
  115. value={linkSettings.includeVars}
  116. onChange={onChange}
  117. />
  118. </Field>
  119. <Field>
  120. <Checkbox
  121. label="Open link in new tab"
  122. name="targetBlank"
  123. value={linkSettings.targetBlank}
  124. onChange={onChange}
  125. />
  126. </Field>
  127. </CollapsableSection>
  128. <Button onClick={onGoBack}>Apply</Button>
  129. </div>
  130. );
  131. };