toolbarGroupBy.tsx 6.3 KB

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