savedSearchTab.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. import {Fragment, useCallback, useMemo} from 'react';
  2. import styled from '@emotion/styled';
  3. import Access from 'sentry/components/acl/access';
  4. import Badge from 'sentry/components/badge';
  5. import Button from 'sentry/components/button';
  6. import Confirm from 'sentry/components/confirm';
  7. import DropdownButton from 'sentry/components/dropdownButton';
  8. import CompactSelect from 'sentry/components/forms/compactSelect';
  9. import {ControlProps} from 'sentry/components/forms/selectControl';
  10. import QueryCount from 'sentry/components/queryCount';
  11. import {IconDelete} from 'sentry/icons';
  12. import {t} from 'sentry/locale';
  13. import space from 'sentry/styles/space';
  14. import {Organization, SavedSearch} from 'sentry/types';
  15. import {defined} from 'sentry/utils';
  16. import {getSortLabel} from './utils';
  17. type Props = {
  18. onSavedSearchDelete: (savedSearch: SavedSearch) => void;
  19. onSavedSearchSelect: (savedSearch: SavedSearch) => void;
  20. organization: Organization;
  21. savedSearchList: SavedSearch[];
  22. sort: string;
  23. isActive?: boolean;
  24. query?: string;
  25. queryCount?: number;
  26. };
  27. function SavedSearchTab({
  28. isActive,
  29. organization,
  30. savedSearchList,
  31. onSavedSearchSelect,
  32. onSavedSearchDelete,
  33. query,
  34. queryCount,
  35. sort,
  36. }: Props) {
  37. const savedSearch = savedSearchList.find(
  38. search => search.query === query && search.sort === sort
  39. );
  40. const savedSearchValue = savedSearch
  41. ? savedSearch.isGlobal
  42. ? `global-search-${savedSearch.id}`
  43. : `saved-search-${savedSearch.id}`
  44. : '';
  45. const options: ControlProps['options'] = useMemo(() => {
  46. const savedSearches = savedSearchList.filter(search => !search.isGlobal);
  47. const globalSearches = savedSearchList.filter(
  48. search => search.isGlobal && !search.isPinned && search.query !== 'is:unresolved'
  49. );
  50. function getSearchOption(search, keyPrefix) {
  51. return {
  52. value: `${keyPrefix}-${search.id}`,
  53. label: search.name,
  54. details: (
  55. <Details>
  56. {search.query}
  57. {` \u2022 ${t('Sort:')} ${getSortLabel(search.sort)}`}
  58. </Details>
  59. ),
  60. tooltip: (
  61. <Fragment>
  62. {`${search.name} \u2022 `}
  63. <TooltipSearchQuery>{search.query}</TooltipSearchQuery>
  64. {` \u2022 `}
  65. {t('Sort: ')}
  66. {getSortLabel(search.sort)}
  67. </Fragment>
  68. ),
  69. tooltipOptions: {delay: 1000},
  70. ...(!search.isPinned &&
  71. !search.isGlobal && {
  72. trailingItems: (
  73. <Access
  74. organization={organization}
  75. access={['org:write']}
  76. renderNoAccessMessage={false}
  77. >
  78. <Confirm
  79. onConfirm={() => onSavedSearchDelete(search)}
  80. message={t('Are you sure you want to delete this saved search?')}
  81. stopPropagation
  82. >
  83. <DeleteButton
  84. borderless
  85. icon={<IconDelete />}
  86. aria-label={t('delete')}
  87. size="zero"
  88. />
  89. </Confirm>
  90. </Access>
  91. ),
  92. }),
  93. trailingItemsSpanFullHeight: true,
  94. };
  95. }
  96. const searchOptions: ControlProps['options'] = [];
  97. if (savedSearches.length > 0) {
  98. searchOptions.push({
  99. value: 'saved-searches',
  100. label: t('Saved Searches'),
  101. options: savedSearches.map(search => getSearchOption(search, 'saved-search')),
  102. });
  103. }
  104. if (globalSearches.length > 0) {
  105. searchOptions.push({
  106. value: 'global-searches',
  107. label: t('Recommended Searches'),
  108. options: globalSearches.map(search => getSearchOption(search, 'global-search')),
  109. });
  110. }
  111. return searchOptions;
  112. }, []);
  113. const trigger = ({props, ref}) => (
  114. <StyledDropdownTrigger
  115. ref={ref}
  116. {...props}
  117. isActive={isActive}
  118. borderless
  119. priority="link"
  120. size="zero"
  121. >
  122. {isActive ? (
  123. <Fragment>
  124. <span>{savedSearch ? savedSearch.name : t('Custom Search')}&nbsp;</span>
  125. {defined(queryCount) && queryCount > 0 && (
  126. <Badge>
  127. <QueryCount hideParens count={queryCount} max={1000} />
  128. </Badge>
  129. )}
  130. </Fragment>
  131. ) : (
  132. t('Saved Searches')
  133. )}
  134. </StyledDropdownTrigger>
  135. );
  136. const onChange = useCallback(
  137. option => {
  138. const searchObj = savedSearchList.find(s =>
  139. s.isGlobal
  140. ? `global-search-${s.id}` === option.value
  141. : `saved-search-${s.id}` === option.value
  142. );
  143. searchObj && onSavedSearchSelect(searchObj);
  144. },
  145. [savedSearchList, onSavedSearchSelect]
  146. );
  147. return (
  148. <StyledCompactSelect
  149. renderWrapAs="li"
  150. trigger={trigger}
  151. options={options}
  152. value={savedSearchValue}
  153. isActive={isActive}
  154. onChange={onChange}
  155. offset={-4}
  156. maxMenuHeight={800}
  157. />
  158. );
  159. }
  160. export default SavedSearchTab;
  161. const StyledCompactSelect = styled(CompactSelect)<{isActive?: boolean}>`
  162. && {
  163. position: static;
  164. }
  165. border-bottom-width: 4px;
  166. border-bottom-style: solid;
  167. border-bottom-color: ${p => (p.isActive ? p.theme.active : 'transparent')};
  168. `;
  169. const StyledDropdownTrigger = styled(DropdownButton)<{isActive?: boolean}>`
  170. display: flex;
  171. height: calc(1.25rem - 2px);
  172. align-items: center;
  173. color: ${p => (p.isActive ? p.theme.textColor : p.theme.subText)};
  174. box-sizing: content-box;
  175. padding: ${space(1)} 0;
  176. &:hover {
  177. color: ${p => p.theme.textColor};
  178. }
  179. `;
  180. const Details = styled('span')`
  181. font-family: ${p => p.theme.text.familyMono};
  182. font-size: ${p => p.theme.fontSizeExtraSmall};
  183. max-width: 16rem;
  184. ${p => p.theme.overflowEllipsis}
  185. `;
  186. const TooltipSearchQuery = styled('span')`
  187. color: ${p => p.theme.subText};
  188. font-weight: normal;
  189. font-family: ${p => p.theme.text.familyMono};
  190. `;
  191. const DeleteButton = styled(Button)`
  192. color: ${p => p.theme.subText};
  193. flex-shrink: 0;
  194. padding: ${space(1)} 0;
  195. :hover {
  196. color: ${p => p.theme.error};
  197. }
  198. `;