savedSearchTab.tsx 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 CompactSelect from 'sentry/components/compactSelect';
  7. import Confirm from 'sentry/components/confirm';
  8. import DropdownButton from 'sentry/components/dropdownButton';
  9. import {ControlProps} from 'sentry/components/forms/controls/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 => (
  114. <StyledDropdownTrigger
  115. {...props}
  116. isActive={isActive}
  117. borderless
  118. priority="link"
  119. size="zero"
  120. >
  121. {isActive ? (
  122. <Fragment>
  123. <span>{savedSearch ? savedSearch.name : t('Custom Search')}&nbsp;</span>
  124. {defined(queryCount) && queryCount > 0 && (
  125. <Badge>
  126. <QueryCount hideParens count={queryCount} max={1000} />
  127. </Badge>
  128. )}
  129. </Fragment>
  130. ) : (
  131. t('Saved Searches')
  132. )}
  133. </StyledDropdownTrigger>
  134. );
  135. const onChange = useCallback(
  136. option => {
  137. const searchObj = savedSearchList.find(s =>
  138. s.isGlobal
  139. ? `global-search-${s.id}` === option.value
  140. : `saved-search-${s.id}` === option.value
  141. );
  142. searchObj && onSavedSearchSelect(searchObj);
  143. },
  144. [savedSearchList, onSavedSearchSelect]
  145. );
  146. return (
  147. <StyledCompactSelect
  148. renderWrapAs="li"
  149. trigger={trigger}
  150. options={options}
  151. value={savedSearchValue}
  152. isActive={isActive}
  153. onChange={onChange}
  154. offset={-4}
  155. maxMenuHeight={800}
  156. />
  157. );
  158. }
  159. export default SavedSearchTab;
  160. const StyledCompactSelect = styled(CompactSelect)<{isActive?: boolean}>`
  161. && {
  162. position: static;
  163. }
  164. border-bottom-width: 4px;
  165. border-bottom-style: solid;
  166. border-bottom-color: ${p => (p.isActive ? p.theme.active : 'transparent')};
  167. `;
  168. const StyledDropdownTrigger = styled(DropdownButton)<{isActive?: boolean}>`
  169. display: flex;
  170. height: calc(1.25rem - 2px);
  171. align-items: center;
  172. color: ${p => (p.isActive ? p.theme.textColor : p.theme.subText)};
  173. box-sizing: content-box;
  174. padding: ${space(1)} 0;
  175. &:hover {
  176. color: ${p => p.theme.textColor};
  177. }
  178. `;
  179. const Details = styled('span')`
  180. font-family: ${p => p.theme.text.familyMono};
  181. font-size: ${p => p.theme.fontSizeExtraSmall};
  182. max-width: 16rem;
  183. ${p => p.theme.overflowEllipsis}
  184. `;
  185. const TooltipSearchQuery = styled('span')`
  186. color: ${p => p.theme.subText};
  187. font-weight: normal;
  188. font-family: ${p => p.theme.text.familyMono};
  189. `;
  190. const DeleteButton = styled(Button)`
  191. color: ${p => p.theme.subText};
  192. flex-shrink: 0;
  193. padding: ${space(1)} 0;
  194. :hover {
  195. color: ${p => p.theme.error};
  196. }
  197. `;