savedIssueSearches.tsx 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. import {Fragment, useState} from 'react';
  2. import {css, useTheme} from '@emotion/react';
  3. import styled from '@emotion/styled';
  4. import orderBy from 'lodash/orderBy';
  5. import {openModal} from 'sentry/actionCreators/modal';
  6. import {Button, ButtonLabel} from 'sentry/components/button';
  7. import {openConfirmModal} from 'sentry/components/confirm';
  8. import {DropdownMenu, MenuItemProps} from 'sentry/components/dropdownMenu';
  9. import LoadingError from 'sentry/components/loadingError';
  10. import LoadingIndicator from 'sentry/components/loadingIndicator';
  11. import {CreateSavedSearchModal} from 'sentry/components/modals/savedSearchModal/createSavedSearchModal';
  12. import {EditSavedSearchModal} from 'sentry/components/modals/savedSearchModal/editSavedSearchModal';
  13. import {IconAdd, IconEllipsis} from 'sentry/icons';
  14. import {t} from 'sentry/locale';
  15. import {space} from 'sentry/styles/space';
  16. import {Organization, SavedSearch, SavedSearchVisibility} from 'sentry/types';
  17. import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
  18. import useMedia from 'sentry/utils/useMedia';
  19. import {useSyncedLocalStorageState} from 'sentry/utils/useSyncedLocalStorageState';
  20. import {useDeleteSavedSearchOptimistic} from 'sentry/views/issueList/mutations/useDeleteSavedSearch';
  21. import {useFetchSavedSearchesForOrg} from 'sentry/views/issueList/queries/useFetchSavedSearchesForOrg';
  22. import {SAVED_SEARCHES_SIDEBAR_OPEN_LOCALSTORAGE_KEY} from 'sentry/views/issueList/utils';
  23. interface SavedIssueSearchesProps {
  24. onSavedSearchSelect: (savedSearch: SavedSearch) => void;
  25. organization: Organization;
  26. query: string;
  27. sort: string;
  28. }
  29. interface SavedSearchItemProps
  30. extends Pick<SavedIssueSearchesProps, 'organization' | 'onSavedSearchSelect'> {
  31. savedSearch: SavedSearch;
  32. }
  33. type CreateNewSavedSearchButtonProps = Pick<
  34. SavedIssueSearchesProps,
  35. 'query' | 'sort' | 'organization'
  36. >;
  37. const MAX_SHOWN_SEARCHES = 4;
  38. const SavedSearchItemDescription = ({
  39. savedSearch,
  40. }: Pick<SavedSearchItemProps, 'savedSearch'>) => {
  41. if (savedSearch.isGlobal) {
  42. return <SavedSearchItemQuery>{savedSearch.query}</SavedSearchItemQuery>;
  43. }
  44. return (
  45. <SavedSearchItemVisbility>
  46. {savedSearch.visibility === SavedSearchVisibility.Organization
  47. ? t('Anyone in organization can see but not edit')
  48. : t('Only you can see and edit')}
  49. </SavedSearchItemVisbility>
  50. );
  51. };
  52. const SavedSearchItem = ({
  53. organization,
  54. onSavedSearchSelect,
  55. savedSearch,
  56. }: SavedSearchItemProps) => {
  57. const {mutate: deleteSavedSearch} = useDeleteSavedSearchOptimistic();
  58. const hasOrgWriteAccess = organization.access?.includes('org:write');
  59. const canEdit =
  60. savedSearch.visibility === SavedSearchVisibility.Owner || hasOrgWriteAccess;
  61. const actions: MenuItemProps[] = [
  62. {
  63. key: 'edit',
  64. label: 'Edit',
  65. disabled: !canEdit,
  66. details: !canEdit
  67. ? t('You do not have permission to edit this search.')
  68. : undefined,
  69. onAction: () => {
  70. openModal(deps => (
  71. <EditSavedSearchModal {...deps} {...{organization, savedSearch}} />
  72. ));
  73. },
  74. },
  75. {
  76. disabled: !canEdit,
  77. details: !canEdit
  78. ? t('You do not have permission to delete this search.')
  79. : undefined,
  80. key: 'delete',
  81. label: t('Delete'),
  82. onAction: () => {
  83. openConfirmModal({
  84. message: t('Are you sure you want to delete this saved search?'),
  85. onConfirm: () =>
  86. deleteSavedSearch({orgSlug: organization.slug, id: savedSearch.id}),
  87. });
  88. },
  89. priority: 'danger',
  90. },
  91. ];
  92. return (
  93. <SearchListItem hasMenu={!savedSearch.isGlobal}>
  94. <StyledItemButton
  95. aria-label={savedSearch.name}
  96. onClick={() => onSavedSearchSelect(savedSearch)}
  97. borderless
  98. >
  99. <TitleDescriptionWrapper>
  100. <SavedSearchItemTitle>{savedSearch.name}</SavedSearchItemTitle>
  101. <SavedSearchItemDescription savedSearch={savedSearch} />
  102. </TitleDescriptionWrapper>
  103. </StyledItemButton>
  104. {!savedSearch.isGlobal && (
  105. <OverflowMenu
  106. position="bottom-end"
  107. items={actions}
  108. size="sm"
  109. minMenuWidth={200}
  110. trigger={props => (
  111. <Button
  112. {...props}
  113. aria-label={t('Saved search options')}
  114. borderless
  115. icon={<IconEllipsis size="sm" />}
  116. size="sm"
  117. />
  118. )}
  119. />
  120. )}
  121. </SearchListItem>
  122. );
  123. };
  124. function CreateNewSavedSearchButton({
  125. organization,
  126. query,
  127. sort,
  128. }: CreateNewSavedSearchButtonProps) {
  129. const onClick = () => {
  130. trackAdvancedAnalyticsEvent('search.saved_search_open_create_modal', {
  131. organization,
  132. });
  133. openModal(deps => (
  134. <CreateSavedSearchModal {...deps} {...{organization, query, sort}} />
  135. ));
  136. };
  137. return (
  138. <Button
  139. aria-label={t('Create a new saved search')}
  140. onClick={onClick}
  141. icon={<IconAdd size="sm" />}
  142. borderless
  143. size="sm"
  144. />
  145. );
  146. }
  147. const SavedIssueSearches = ({
  148. organization,
  149. onSavedSearchSelect,
  150. query,
  151. sort,
  152. }: SavedIssueSearchesProps) => {
  153. const theme = useTheme();
  154. const [isOpen] = useSyncedLocalStorageState(
  155. SAVED_SEARCHES_SIDEBAR_OPEN_LOCALSTORAGE_KEY,
  156. false
  157. );
  158. const [showAll, setShowAll] = useState(false);
  159. const {
  160. data: savedSearches,
  161. isLoading,
  162. isError,
  163. refetch,
  164. } = useFetchSavedSearchesForOrg({orgSlug: organization.slug});
  165. const isMobile = useMedia(`(max-width: ${theme.breakpoints.small})`);
  166. if (!isOpen || isMobile) {
  167. return null;
  168. }
  169. if (isLoading) {
  170. return (
  171. <StyledSidebar>
  172. <LoadingIndicator />
  173. </StyledSidebar>
  174. );
  175. }
  176. if (isError) {
  177. return (
  178. <StyledSidebar>
  179. <LoadingError onRetry={refetch} />
  180. </StyledSidebar>
  181. );
  182. }
  183. const orgSavedSearches = orderBy(
  184. savedSearches.filter(search => !search.isGlobal && !search.isPinned),
  185. 'dateCreated',
  186. 'desc'
  187. );
  188. const recommendedSavedSearches = savedSearches.filter(search => search.isGlobal);
  189. const shownOrgSavedSearches = showAll
  190. ? orgSavedSearches
  191. : orgSavedSearches.slice(0, MAX_SHOWN_SEARCHES);
  192. return (
  193. <StyledSidebar>
  194. <Fragment>
  195. <HeadingContainer>
  196. <Heading>{t('Saved Searches')}</Heading>
  197. <CreateNewSavedSearchButton {...{organization, query, sort}} />
  198. </HeadingContainer>
  199. <SearchesContainer>
  200. {shownOrgSavedSearches.map(item => (
  201. <SavedSearchItem
  202. key={item.id}
  203. organization={organization}
  204. onSavedSearchSelect={onSavedSearchSelect}
  205. savedSearch={item}
  206. />
  207. ))}
  208. {shownOrgSavedSearches.length === 0 && (
  209. <NoSavedSearchesText>
  210. {t("You don't have any saved searches")}
  211. </NoSavedSearchesText>
  212. )}
  213. </SearchesContainer>
  214. {orgSavedSearches.length > shownOrgSavedSearches.length && (
  215. <ShowAllButton size="zero" borderless onClick={() => setShowAll(true)}>
  216. {t(
  217. 'Show %s more',
  218. (orgSavedSearches.length - shownOrgSavedSearches.length).toLocaleString()
  219. )}
  220. </ShowAllButton>
  221. )}
  222. </Fragment>
  223. {recommendedSavedSearches.length > 0 && (
  224. <Fragment>
  225. <HeadingContainer>
  226. <Heading>{t('Recommended Searches')}</Heading>
  227. </HeadingContainer>
  228. <SearchesContainer>
  229. {recommendedSavedSearches.map(item => (
  230. <SavedSearchItem
  231. key={item.id}
  232. organization={organization}
  233. onSavedSearchSelect={onSavedSearchSelect}
  234. savedSearch={item}
  235. />
  236. ))}
  237. </SearchesContainer>
  238. </Fragment>
  239. )}
  240. </StyledSidebar>
  241. );
  242. };
  243. const StyledSidebar = styled('aside')`
  244. grid-area: saved-searches;
  245. width: 100%;
  246. padding: ${space(2)};
  247. @media (max-width: ${p => p.theme.breakpoints.small}) {
  248. border-bottom: 1px solid ${p => p.theme.gray200};
  249. padding: ${space(2)} 0;
  250. }
  251. @media (min-width: ${p => p.theme.breakpoints.small}) {
  252. border-left: 1px solid ${p => p.theme.gray200};
  253. max-width: 340px;
  254. }
  255. `;
  256. const HeadingContainer = styled('div')`
  257. display: flex;
  258. justify-content: space-between;
  259. align-items: center;
  260. height: 38px;
  261. padding-left: ${space(2)};
  262. margin-top: ${space(3)};
  263. &:first-of-type {
  264. margin-top: 0;
  265. }
  266. `;
  267. const Heading = styled('h2')`
  268. font-size: ${p => p.theme.fontSizeExtraLarge};
  269. margin: 0;
  270. `;
  271. const SearchesContainer = styled('ul')`
  272. padding: 0;
  273. margin-bottom: ${space(1)};
  274. `;
  275. const StyledItemButton = styled(Button)`
  276. display: block;
  277. width: 100%;
  278. text-align: left;
  279. height: auto;
  280. font-weight: normal;
  281. line-height: ${p => p.theme.text.lineHeightBody};
  282. padding: ${space(1)} ${space(2)};
  283. ${ButtonLabel} {
  284. justify-content: start;
  285. }
  286. `;
  287. const OverflowMenu = styled(DropdownMenu)`
  288. position: absolute;
  289. top: 12px;
  290. right: ${space(1)};
  291. `;
  292. const SearchListItem = styled('li')<{hasMenu?: boolean}>`
  293. position: relative;
  294. list-style: none;
  295. padding: 0;
  296. margin: 0;
  297. ${p =>
  298. p.hasMenu &&
  299. css`
  300. @media (max-width: ${p.theme.breakpoints.small}) {
  301. ${StyledItemButton} {
  302. padding-right: 60px;
  303. }
  304. }
  305. @media (min-width: ${p.theme.breakpoints.small}) {
  306. ${OverflowMenu} {
  307. display: none;
  308. }
  309. &:hover,
  310. &:focus-within {
  311. ${OverflowMenu} {
  312. display: block;
  313. }
  314. ${StyledItemButton} {
  315. padding-right: 60px;
  316. }
  317. }
  318. }
  319. `}
  320. `;
  321. const TitleDescriptionWrapper = styled('div')`
  322. overflow: hidden;
  323. `;
  324. const SavedSearchItemTitle = styled('div')`
  325. font-size: ${p => p.theme.fontSizeLarge};
  326. ${p => p.theme.overflowEllipsis}
  327. `;
  328. const SavedSearchItemVisbility = styled('div')`
  329. color: ${p => p.theme.subText};
  330. font-size: ${p => p.theme.fontSizeSmall};
  331. ${p => p.theme.overflowEllipsis}
  332. `;
  333. const SavedSearchItemQuery = styled('div')`
  334. font-family: ${p => p.theme.text.familyMono};
  335. font-size: ${p => p.theme.fontSizeSmall};
  336. color: ${p => p.theme.subText};
  337. ${p => p.theme.overflowEllipsis}
  338. `;
  339. const ShowAllButton = styled(Button)`
  340. color: ${p => p.theme.linkColor};
  341. font-weight: normal;
  342. padding: ${space(0.5)} ${space(2)};
  343. &:hover {
  344. color: ${p => p.theme.linkHoverColor};
  345. }
  346. `;
  347. const NoSavedSearchesText = styled('p')`
  348. padding: 0 ${space(2)};
  349. margin: ${space(0.5)} 0;
  350. color: ${p => p.theme.subText};
  351. `;
  352. export default SavedIssueSearches;