toolbarGroupBy.tsx 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 {Button} from 'sentry/components/button';
  6. import type {SelectKey, SelectOption} from 'sentry/components/compactSelect';
  7. import {CompactSelect} from 'sentry/components/compactSelect';
  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. {
  63. label: <Disabled>{t('None')}</Disabled>,
  64. value: UNGROUPED,
  65. textValue: t('none'),
  66. },
  67. ...potentialOptions.map(key => ({
  68. label: key,
  69. value: key,
  70. textValue: key,
  71. })),
  72. ];
  73. }, [groupBys, tags]);
  74. return (
  75. <DragNDropContext columns={groupBys} setColumns={setGroupBys}>
  76. {({editableColumns, insertColumn, updateColumnAtIndex, deleteColumnAtIndex}) => {
  77. return (
  78. <ToolbarSection data-test-id="section-group-by">
  79. <ToolbarHeader>
  80. <Tooltip
  81. position="right"
  82. title={t(
  83. 'Aggregated data by a key attribute to calculate averages, percentiles, count and more'
  84. )}
  85. >
  86. <ToolbarLabel disabled={disabled}>{t('Group By')}</ToolbarLabel>
  87. </Tooltip>
  88. <Tooltip title={t('Add a new group')}>
  89. <ToolbarHeaderButton
  90. disabled={disabled}
  91. size="zero"
  92. onClick={insertColumn}
  93. borderless
  94. aria-label={t('Add Group')}
  95. icon={<IconAdd />}
  96. />
  97. </Tooltip>
  98. </ToolbarHeader>
  99. {disabled ? (
  100. <FullWidthTooltip
  101. title={t('Switch to aggregate results to apply grouping')}
  102. >
  103. <ColumnEditorRow
  104. disabled={disabled}
  105. canDelete={false}
  106. column={{id: 1, column: ''}}
  107. options={disabledOptions}
  108. onColumnChange={() => {}}
  109. onColumnDelete={() => {}}
  110. />
  111. </FullWidthTooltip>
  112. ) : (
  113. editableColumns.map((column, i) => (
  114. <ColumnEditorRow
  115. disabled={mode === Mode.SAMPLES}
  116. key={column.id}
  117. canDelete={
  118. editableColumns.length > 1 || !['', undefined].includes(column.column)
  119. }
  120. column={column}
  121. options={enabledOptions}
  122. onColumnChange={c => updateColumnAtIndex(i, c)}
  123. onColumnDelete={() => deleteColumnAtIndex(i)}
  124. />
  125. ))
  126. )}
  127. </ToolbarSection>
  128. );
  129. }}
  130. </DragNDropContext>
  131. );
  132. }
  133. interface ColumnEditorRowProps {
  134. canDelete: boolean;
  135. column: Column;
  136. onColumnChange: (column: string) => void;
  137. onColumnDelete: () => void;
  138. options: Array<SelectOption<string>>;
  139. disabled?: boolean;
  140. }
  141. function ColumnEditorRow({
  142. canDelete,
  143. column,
  144. options,
  145. onColumnChange,
  146. onColumnDelete,
  147. disabled = false,
  148. }: ColumnEditorRowProps) {
  149. const {attributes, listeners, setNodeRef, transform, transition} = useSortable({
  150. id: column.id,
  151. });
  152. function handleColumnChange(option: SelectOption<SelectKey>) {
  153. if (defined(option) && typeof option.value === 'string') {
  154. onColumnChange(option.value);
  155. }
  156. }
  157. const label = useMemo(() => {
  158. const tag = options.find(option => option.value === column.column);
  159. return <TriggerLabel>{tag?.label ?? t('None')}</TriggerLabel>;
  160. }, [column.column, options]);
  161. return (
  162. <ToolbarRow
  163. key={column.id}
  164. ref={setNodeRef}
  165. style={{
  166. transform: CSS.Transform.toString(transform),
  167. transition,
  168. }}
  169. {...attributes}
  170. >
  171. <Button
  172. aria-label={t('Drag to reorder')}
  173. borderless
  174. size="zero"
  175. disabled={disabled}
  176. icon={<IconGrabbable size="sm" />}
  177. {...listeners}
  178. />
  179. <StyledCompactSelect
  180. data-test-id="editor-column"
  181. options={options}
  182. triggerLabel={label}
  183. disabled={disabled}
  184. value={column.column ?? ''}
  185. onChange={handleColumnChange}
  186. searchable
  187. triggerProps={{
  188. style: {
  189. width: '100%',
  190. },
  191. }}
  192. />
  193. <Button
  194. aria-label={t('Remove Column')}
  195. borderless
  196. disabled={!canDelete || disabled}
  197. size="zero"
  198. icon={<IconDelete size="sm" />}
  199. onClick={() => onColumnDelete()}
  200. />
  201. </ToolbarRow>
  202. );
  203. }
  204. const StyledCompactSelect = styled(CompactSelect)`
  205. flex-grow: 1;
  206. min-width: 0;
  207. `;
  208. const TriggerLabel = styled('span')`
  209. ${p => p.theme.overflowEllipsis}
  210. text-align: left;
  211. line-height: normal;
  212. position: relative;
  213. font-weight: normal;
  214. `;
  215. const Disabled = styled('span')`
  216. color: ${p => p.theme.gray300};
  217. `;
  218. const FullWidthTooltip = styled(Tooltip)`
  219. width: 100%;
  220. `;