savedIssueSearches.tsx 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 {IconClose, 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 {trackAnalytics} from 'sentry/utils/analytics';
  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. function 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. function 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. trackAnalytics('search.saved_search_open_create_modal', {
  131. organization,
  132. });
  133. openModal(deps => (
  134. <CreateSavedSearchModal {...deps} {...{organization, query, sort}} />
  135. ));
  136. };
  137. return (
  138. <Button onClick={onClick} priority="link" size="sm">
  139. {t('Add saved search')}
  140. </Button>
  141. );
  142. }
  143. function SavedIssueSearches({
  144. organization,
  145. onSavedSearchSelect,
  146. query,
  147. sort,
  148. }: SavedIssueSearchesProps) {
  149. const theme = useTheme();
  150. const [isOpen, setIsOpen] = useSyncedLocalStorageState(
  151. SAVED_SEARCHES_SIDEBAR_OPEN_LOCALSTORAGE_KEY,
  152. false
  153. );
  154. const [showAll, setShowAll] = useState(false);
  155. const {
  156. data: savedSearches,
  157. isLoading,
  158. isError,
  159. refetch,
  160. } = useFetchSavedSearchesForOrg({orgSlug: organization.slug});
  161. const isMobile = useMedia(`(max-width: ${theme.breakpoints.small})`);
  162. if (!isOpen || isMobile) {
  163. return null;
  164. }
  165. if (isLoading) {
  166. return (
  167. <StyledSidebar>
  168. <LoadingIndicator />
  169. </StyledSidebar>
  170. );
  171. }
  172. if (isError) {
  173. return (
  174. <StyledSidebar>
  175. <LoadingError onRetry={refetch} />
  176. </StyledSidebar>
  177. );
  178. }
  179. const orgSavedSearches = orderBy(
  180. savedSearches.filter(search => !search.isGlobal && !search.isPinned),
  181. 'dateCreated',
  182. 'desc'
  183. );
  184. const recommendedSavedSearches = savedSearches.filter(search => search.isGlobal);
  185. const shownOrgSavedSearches = showAll
  186. ? orgSavedSearches
  187. : orgSavedSearches.slice(0, MAX_SHOWN_SEARCHES);
  188. return (
  189. <StyledSidebar>
  190. <Fragment>
  191. <HeadingContainer>
  192. <Heading>{t('Saved Searches')}</Heading>
  193. <Button
  194. aria-label={t('Collapse sidebar')}
  195. borderless
  196. onClick={() => setIsOpen(false)}
  197. icon={<IconClose size="sm" />}
  198. />
  199. </HeadingContainer>
  200. <CreateSavedSearchWrapper>
  201. <CreateNewSavedSearchButton {...{organization, query, sort}} />
  202. </CreateSavedSearchWrapper>
  203. <SearchesContainer>
  204. {shownOrgSavedSearches.map(item => (
  205. <SavedSearchItem
  206. key={item.id}
  207. organization={organization}
  208. onSavedSearchSelect={onSavedSearchSelect}
  209. savedSearch={item}
  210. />
  211. ))}
  212. {shownOrgSavedSearches.length === 0 && (
  213. <NoSavedSearchesText>
  214. {t("You don't have any saved searches")}
  215. </NoSavedSearchesText>
  216. )}
  217. </SearchesContainer>
  218. {orgSavedSearches.length > shownOrgSavedSearches.length && (
  219. <ShowAllButton size="zero" borderless onClick={() => setShowAll(true)}>
  220. {t(
  221. 'Show %s more',
  222. (orgSavedSearches.length - shownOrgSavedSearches.length).toLocaleString()
  223. )}
  224. </ShowAllButton>
  225. )}
  226. </Fragment>
  227. {recommendedSavedSearches.length > 0 && (
  228. <Fragment>
  229. <HeadingContainer>
  230. <Heading>{t('Recommended Searches')}</Heading>
  231. </HeadingContainer>
  232. <SearchesContainer>
  233. {recommendedSavedSearches.map(item => (
  234. <SavedSearchItem
  235. key={item.id}
  236. organization={organization}
  237. onSavedSearchSelect={onSavedSearchSelect}
  238. savedSearch={item}
  239. />
  240. ))}
  241. </SearchesContainer>
  242. </Fragment>
  243. )}
  244. </StyledSidebar>
  245. );
  246. }
  247. const StyledSidebar = styled('aside')`
  248. grid-area: saved-searches;
  249. width: 100%;
  250. padding: ${space(2)};
  251. @media (max-width: ${p => p.theme.breakpoints.small}) {
  252. border-bottom: 1px solid ${p => p.theme.gray200};
  253. padding: ${space(2)} 0;
  254. }
  255. @media (min-width: ${p => p.theme.breakpoints.small}) {
  256. border-left: 1px solid ${p => p.theme.gray200};
  257. max-width: 340px;
  258. }
  259. `;
  260. const HeadingContainer = styled('div')`
  261. display: flex;
  262. justify-content: space-between;
  263. align-items: center;
  264. height: 38px;
  265. padding-left: ${space(2)};
  266. margin-top: ${space(3)};
  267. &:first-of-type {
  268. margin-top: 0;
  269. }
  270. `;
  271. const Heading = styled('h2')`
  272. font-size: ${p => p.theme.fontSizeExtraLarge};
  273. margin: 0;
  274. `;
  275. const CreateSavedSearchWrapper = styled('div')`
  276. padding: 0 ${space(2)};
  277. margin-bottom: ${space(1)};
  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;