toolbarGroupBy.tsx 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. import {useMemo} from 'react';
  2. import {useSortable} from '@dnd-kit/sortable';
  3. import {CSS} from '@dnd-kit/utilities';
  4. import styled from '@emotion/styled';
  5. import type {SelectKey, SelectOption} from 'sentry/components/compactSelect';
  6. import {CompactSelect} from 'sentry/components/compactSelect';
  7. import {Button} from 'sentry/components/core/button';
  8. import {Tooltip} from 'sentry/components/tooltip';
  9. import {IconAdd} from 'sentry/icons/iconAdd';
  10. import {IconDelete} from 'sentry/icons/iconDelete';
  11. import {IconGrabbable} from 'sentry/icons/iconGrabbable';
  12. import {t} from 'sentry/locale';
  13. import {defined} from 'sentry/utils';
  14. import {
  15. useExploreGroupBys,
  16. useExploreMode,
  17. useSetExploreGroupBys,
  18. } from 'sentry/views/explore/contexts/pageParamsContext';
  19. import {UNGROUPED} from 'sentry/views/explore/contexts/pageParamsContext/groupBys';
  20. import {Mode} from 'sentry/views/explore/contexts/pageParamsContext/mode';
  21. import {DragNDropContext} from '../contexts/dragNDropContext';
  22. import {useSpanTags} from '../contexts/spanTagsContext';
  23. import type {Column} from '../hooks/useDragNDropColumns';
  24. import {
  25. ToolbarHeader,
  26. ToolbarHeaderButton,
  27. ToolbarLabel,
  28. ToolbarRow,
  29. ToolbarSection,
  30. } from './styles';
  31. interface ToolbarGroupByProps {
  32. disabled?: boolean;
  33. }
  34. export function ToolbarGroupBy({disabled}: ToolbarGroupByProps) {
  35. const {tags} = useSpanTags();
  36. const mode = useExploreMode();
  37. const groupBys = useExploreGroupBys();
  38. const setGroupBys = useSetExploreGroupBys();
  39. const disabledOptions: Array<SelectOption<string>> = useMemo(() => {
  40. return [
  41. {
  42. label: <Disabled>{t('Samples not grouped')}</Disabled>,
  43. value: UNGROUPED,
  44. textValue: t('none'),
  45. },
  46. ];
  47. }, []);
  48. const enabledOptions: Array<SelectOption<string>> = useMemo(() => {
  49. const potentialOptions = [
  50. // We do not support grouping by span id, we have a dedicated sample mode for that
  51. ...Object.keys(tags).filter(key => key !== 'id'),
  52. // These options aren't known to exist on this project but it was inserted into
  53. // the group bys somehow so it should be a valid options in the group bys.
  54. //
  55. // One place this may come from is when switching projects/environment/date range,
  56. // a tag may disappear based on the selection.
  57. ...groupBys.filter(groupBy => groupBy && !tags.hasOwnProperty(groupBy)),
  58. ];
  59. potentialOptions.sort();
  60. return [
  61. // hard code in an empty option
  62. {label: <Disabled>{t('None')}</Disabled>, value: UNGROUPED, textValue: t('none')},
  63. ...potentialOptions.map(key => ({label: key, value: key, textValue: key})),
  64. ];
  65. }, [groupBys, tags]);
  66. return (
  67. <DragNDropContext columns={groupBys} setColumns={setGroupBys}>
  68. {({editableColumns, insertColumn, updateColumnAtIndex, deleteColumnAtIndex}) => {
  69. return (
  70. <ToolbarSection data-test-id="section-group-by">
  71. <ToolbarHeader>
  72. <Tooltip
  73. position="right"
  74. title={t(
  75. 'Aggregated data by a key attribute to calculate averages, percentiles, count and more'
  76. )}
  77. >
  78. <ToolbarLabel disabled={disabled}>{t('Group By')}</ToolbarLabel>
  79. </Tooltip>
  80. <Tooltip title={t('Add a new group')}>
  81. <ToolbarHeaderButton
  82. disabled={disabled}
  83. size="zero"
  84. onClick={insertColumn}
  85. borderless
  86. aria-label={t('Add Group')}
  87. icon={<IconAdd />}
  88. />
  89. </Tooltip>
  90. </ToolbarHeader>
  91. {disabled ? (
  92. <FullWidthTooltip
  93. title={t('Switch to aggregate results to apply grouping')}
  94. >
  95. <ColumnEditorRow
  96. disabled={disabled}
  97. canDelete={false}
  98. column={{id: 1, column: ''}}
  99. options={disabledOptions}
  100. onColumnChange={() => {}}
  101. onColumnDelete={() => {}}
  102. />
  103. </FullWidthTooltip>
  104. ) : (
  105. editableColumns.map((column, i) => (
  106. <ColumnEditorRow
  107. disabled={mode === Mode.SAMPLES}
  108. key={column.id}
  109. canDelete={
  110. editableColumns.length > 1 || !['', undefined].includes(column.column)
  111. }
  112. column={column}
  113. options={enabledOptions}
  114. onColumnChange={c => updateColumnAtIndex(i, c)}
  115. onColumnDelete={() => deleteColumnAtIndex(i)}
  116. />
  117. ))
  118. )}
  119. </ToolbarSection>
  120. );
  121. }}
  122. </DragNDropContext>
  123. );
  124. }
  125. interface ColumnEditorRowProps {
  126. canDelete: boolean;
  127. column: Column;
  128. onColumnChange: (column: string) => void;
  129. onColumnDelete: () => void;
  130. options: Array<SelectOption<string>>;
  131. disabled?: boolean;
  132. }
  133. function ColumnEditorRow({
  134. canDelete,
  135. column,
  136. options,
  137. onColumnChange,
  138. onColumnDelete,
  139. disabled = false,
  140. }: ColumnEditorRowProps) {
  141. const {attributes, listeners, setNodeRef, transform, transition} = useSortable({
  142. id: column.id,
  143. });
  144. function handleColumnChange(option: SelectOption<SelectKey>) {
  145. if (defined(option) && typeof option.value === 'string') {
  146. onColumnChange(option.value);
  147. }
  148. }
  149. const label = useMemo(() => {
  150. const tag = options.find(option => option.value === column.column);
  151. return <TriggerLabel>{tag?.label ?? t('None')}</TriggerLabel>;
  152. }, [column.column, options]);
  153. return (
  154. <ToolbarRow
  155. key={column.id}
  156. ref={setNodeRef}
  157. style={{transform: CSS.Transform.toString(transform), transition}}
  158. {...attributes}
  159. >
  160. <Button
  161. aria-label={t('Drag to reorder')}
  162. borderless
  163. size="zero"
  164. disabled={disabled}
  165. icon={<IconGrabbable size="sm" />}
  166. {...listeners}
  167. />
  168. <StyledCompactSelect
  169. data-test-id="editor-column"
  170. options={options}
  171. triggerLabel={label}
  172. disabled={disabled}
  173. value={column.column ?? ''}
  174. onChange={handleColumnChange}
  175. searchable
  176. triggerProps={{style: {width: '100%'}}}
  177. />
  178. <Button
  179. aria-label={t('Remove Column')}
  180. borderless
  181. disabled={!canDelete || disabled}
  182. size="zero"
  183. icon={<IconDelete size="sm" />}
  184. onClick={() => onColumnDelete()}
  185. />
  186. </ToolbarRow>
  187. );
  188. }
  189. const StyledCompactSelect = styled(CompactSelect)`
  190. flex-grow: 1;
  191. min-width: 0;
  192. `;
  193. const TriggerLabel = styled('span')`
  194. ${p => p.theme.overflowEllipsis}
  195. text-align: left;
  196. line-height: normal;
  197. position: relative;
  198. font-weight: normal;
  199. `;
  200. const Disabled = styled('span')`
  201. color: ${p => p.theme.gray300};
  202. `;
  203. const FullWidthTooltip = styled(Tooltip)`
  204. width: 100%;
  205. `;