savedIssueSearches.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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, setIsOpen] = 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 isAboveContent = useMedia(`(max-width: ${theme.breakpoints.small})`);
  166. const onClickSavedSearch = (savedSearch: SavedSearch) => {
  167. // On small screens, the sidebar appears above the issue list, so we
  168. // will close it automatically for convenience.
  169. if (isAboveContent) {
  170. setIsOpen(false);
  171. }
  172. onSavedSearchSelect(savedSearch);
  173. };
  174. if (!isOpen) {
  175. return null;
  176. }
  177. if (isLoading) {
  178. return (
  179. <StyledSidebar>
  180. <LoadingIndicator />
  181. </StyledSidebar>
  182. );
  183. }
  184. if (isError) {
  185. return (
  186. <StyledSidebar>
  187. <LoadingError onRetry={refetch} />
  188. </StyledSidebar>
  189. );
  190. }
  191. const orgSavedSearches = orderBy(
  192. savedSearches.filter(search => !search.isGlobal && !search.isPinned),
  193. 'dateCreated',
  194. 'desc'
  195. );
  196. const recommendedSavedSearches = savedSearches.filter(search => search.isGlobal);
  197. const shownOrgSavedSearches = showAll
  198. ? orgSavedSearches
  199. : orgSavedSearches.slice(0, MAX_SHOWN_SEARCHES);
  200. return (
  201. <StyledSidebar>
  202. <Fragment>
  203. <HeadingContainer>
  204. <Heading>{t('Saved Searches')}</Heading>
  205. <CreateNewSavedSearchButton {...{organization, query, sort}} />
  206. </HeadingContainer>
  207. <SearchesContainer>
  208. {shownOrgSavedSearches.map(item => (
  209. <SavedSearchItem
  210. key={item.id}
  211. organization={organization}
  212. onSavedSearchSelect={onClickSavedSearch}
  213. savedSearch={item}
  214. />
  215. ))}
  216. {shownOrgSavedSearches.length === 0 && (
  217. <NoSavedSearchesText>
  218. {t("You don't have any saved searches")}
  219. </NoSavedSearchesText>
  220. )}
  221. </SearchesContainer>
  222. {orgSavedSearches.length > shownOrgSavedSearches.length && (
  223. <ShowAllButton size="zero" borderless onClick={() => setShowAll(true)}>
  224. {t(
  225. 'Show %s more',
  226. (orgSavedSearches.length - shownOrgSavedSearches.length).toLocaleString()
  227. )}
  228. </ShowAllButton>
  229. )}
  230. </Fragment>
  231. {recommendedSavedSearches.length > 0 && (
  232. <Fragment>
  233. <HeadingContainer>
  234. <Heading>{t('Recommended Searches')}</Heading>
  235. </HeadingContainer>
  236. <SearchesContainer>
  237. {recommendedSavedSearches.map(item => (
  238. <SavedSearchItem
  239. key={item.id}
  240. organization={organization}
  241. onSavedSearchSelect={onClickSavedSearch}
  242. savedSearch={item}
  243. />
  244. ))}
  245. </SearchesContainer>
  246. </Fragment>
  247. )}
  248. </StyledSidebar>
  249. );
  250. };
  251. const StyledSidebar = styled('aside')`
  252. grid-area: saved-searches;
  253. width: 100%;
  254. padding: ${space(2)};
  255. @media (max-width: ${p => p.theme.breakpoints.small}) {
  256. border-bottom: 1px solid ${p => p.theme.gray200};
  257. padding: ${space(2)} 0;
  258. }
  259. @media (min-width: ${p => p.theme.breakpoints.small}) {
  260. border-left: 1px solid ${p => p.theme.gray200};
  261. max-width: 340px;
  262. }
  263. `;
  264. const HeadingContainer = styled('div')`
  265. display: flex;
  266. justify-content: space-between;
  267. align-items: center;
  268. height: 38px;
  269. padding-left: ${space(2)};
  270. margin-top: ${space(3)};
  271. &:first-of-type {
  272. margin-top: 0;
  273. }
  274. `;
  275. const Heading = styled('h2')`
  276. font-size: ${p => p.theme.fontSizeExtraLarge};
  277. margin: 0;
  278. `;
  279. const SearchesContainer = styled('ul')`
  280. padding: 0;
  281. margin-bottom: ${space(1)};
  282. `;
  283. const StyledItemButton = styled(Button)`
  284. display: block;
  285. width: 100%;
  286. text-align: left;
  287. height: auto;
  288. font-weight: normal;
  289. line-height: ${p => p.theme.text.lineHeightBody};
  290. padding: ${space(1)} ${space(2)};
  291. ${ButtonLabel} {
  292. justify-content: start;
  293. }
  294. `;
  295. const OverflowMenu = styled(DropdownMenu)`
  296. position: absolute;
  297. top: 12px;
  298. right: ${space(1)};
  299. `;
  300. const SearchListItem = styled('li')<{hasMenu?: boolean}>`
  301. position: relative;
  302. list-style: none;
  303. padding: 0;
  304. margin: 0;
  305. ${p =>
  306. p.hasMenu &&
  307. css`
  308. @media (max-width: ${p.theme.breakpoints.small}) {
  309. ${StyledItemButton} {
  310. padding-right: 60px;
  311. }
  312. }
  313. @media (min-width: ${p.theme.breakpoints.small}) {
  314. ${OverflowMenu} {
  315. display: none;
  316. }
  317. &:hover,
  318. &:focus-within {
  319. ${OverflowMenu} {
  320. display: block;
  321. }
  322. ${StyledItemButton} {
  323. padding-right: 60px;
  324. }
  325. }
  326. }
  327. `}
  328. `;
  329. const TitleDescriptionWrapper = styled('div')`
  330. overflow: hidden;
  331. `;
  332. const SavedSearchItemTitle = styled('div')`
  333. font-size: ${p => p.theme.fontSizeLarge};
  334. ${p => p.theme.overflowEllipsis}
  335. `;
  336. const SavedSearchItemVisbility = styled('div')`
  337. color: ${p => p.theme.subText};
  338. font-size: ${p => p.theme.fontSizeSmall};
  339. ${p => p.theme.overflowEllipsis}
  340. `;
  341. const SavedSearchItemQuery = styled('div')`
  342. font-family: ${p => p.theme.text.familyMono};
  343. font-size: ${p => p.theme.fontSizeSmall};
  344. color: ${p => p.theme.subText};
  345. ${p => p.theme.overflowEllipsis}
  346. `;
  347. const ShowAllButton = styled(Button)`
  348. color: ${p => p.theme.linkColor};
  349. font-weight: normal;
  350. padding: ${space(0.5)} ${space(2)};
  351. &:hover {
  352. color: ${p => p.theme.linkHoverColor};
  353. }
  354. `;
  355. const NoSavedSearchesText = styled('p')`
  356. padding: 0 ${space(2)};
  357. margin: ${space(0.5)} 0;
  358. color: ${p => p.theme.subText};
  359. `;
  360. export default SavedIssueSearches;