123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406 |
- import {Fragment, useState} from 'react';
- import {css, useTheme} from '@emotion/react';
- import styled from '@emotion/styled';
- import orderBy from 'lodash/orderBy';
- import {openModal} from 'sentry/actionCreators/modal';
- import Button, {ButtonLabel} from 'sentry/components/button';
- import {openConfirmModal} from 'sentry/components/confirm';
- import DropdownMenuControl from 'sentry/components/dropdownMenuControl';
- import {MenuItemProps} from 'sentry/components/dropdownMenuItem';
- import LoadingError from 'sentry/components/loadingError';
- import LoadingIndicator from 'sentry/components/loadingIndicator';
- import {CreateSavedSearchModal} from 'sentry/components/modals/savedSearchModal/createSavedSearchModal';
- import {EditSavedSearchModal} from 'sentry/components/modals/savedSearchModal/editSavedSearchModal';
- import {IconAdd, IconEllipsis} from 'sentry/icons';
- import {t} from 'sentry/locale';
- import space from 'sentry/styles/space';
- import {Organization, SavedSearch, SavedSearchVisibility} from 'sentry/types';
- import trackAdvancedAnalyticsEvent from 'sentry/utils/analytics/trackAdvancedAnalyticsEvent';
- import useMedia from 'sentry/utils/useMedia';
- import {useSyncedLocalStorageState} from 'sentry/utils/useSyncedLocalStorageState';
- import {useDeleteSavedSearchOptimistic} from 'sentry/views/issueList/mutations/useDeleteSavedSearch';
- import {useFetchSavedSearchesForOrg} from 'sentry/views/issueList/queries/useFetchSavedSearchesForOrg';
- import {SAVED_SEARCHES_SIDEBAR_OPEN_LOCALSTORAGE_KEY} from 'sentry/views/issueList/utils';
- interface SavedIssueSearchesProps {
- onSavedSearchSelect: (savedSearch: SavedSearch) => void;
- organization: Organization;
- query: string;
- sort: string;
- }
- interface SavedSearchItemProps
- extends Pick<SavedIssueSearchesProps, 'organization' | 'onSavedSearchSelect'> {
- savedSearch: SavedSearch;
- }
- type CreateNewSavedSearchButtonProps = Pick<
- SavedIssueSearchesProps,
- 'query' | 'sort' | 'organization'
- >;
- const MAX_SHOWN_SEARCHES = 4;
- const SavedSearchItemDescription = ({
- savedSearch,
- }: Pick<SavedSearchItemProps, 'savedSearch'>) => {
- if (savedSearch.isGlobal) {
- return <SavedSearchItemQuery>{savedSearch.query}</SavedSearchItemQuery>;
- }
- return (
- <SavedSearchItemVisbility>
- {savedSearch.visibility === SavedSearchVisibility.Organization
- ? t('Anyone in organization can see but not edit')
- : t('Only you can see and edit')}
- </SavedSearchItemVisbility>
- );
- };
- const SavedSearchItem = ({
- organization,
- onSavedSearchSelect,
- savedSearch,
- }: SavedSearchItemProps) => {
- const {mutate: deleteSavedSearch} = useDeleteSavedSearchOptimistic();
- const hasOrgWriteAccess = organization.access?.includes('org:write');
- const canEdit =
- savedSearch.visibility === SavedSearchVisibility.Owner || hasOrgWriteAccess;
- const actions: MenuItemProps[] = [
- {
- key: 'edit',
- label: 'Edit',
- disabled: !canEdit,
- details: !canEdit
- ? t('You do not have permission to edit this search.')
- : undefined,
- onAction: () => {
- openModal(deps => (
- <EditSavedSearchModal {...deps} {...{organization, savedSearch}} />
- ));
- },
- },
- {
- disabled: !canEdit,
- details: !canEdit
- ? t('You do not have permission to delete this search.')
- : undefined,
- key: 'delete',
- label: t('Delete'),
- onAction: () => {
- openConfirmModal({
- message: t('Are you sure you want to delete this saved search?'),
- onConfirm: () =>
- deleteSavedSearch({orgSlug: organization.slug, id: savedSearch.id}),
- });
- },
- priority: 'danger',
- },
- ];
- return (
- <SearchListItem hasMenu={!savedSearch.isGlobal}>
- <StyledItemButton
- aria-label={savedSearch.name}
- onClick={() => onSavedSearchSelect(savedSearch)}
- borderless
- >
- <TitleDescriptionWrapper>
- <SavedSearchItemTitle>{savedSearch.name}</SavedSearchItemTitle>
- <SavedSearchItemDescription savedSearch={savedSearch} />
- </TitleDescriptionWrapper>
- </StyledItemButton>
- {!savedSearch.isGlobal && (
- <OverflowMenu
- position="bottom-end"
- items={actions}
- size="sm"
- minMenuWidth={200}
- trigger={props => (
- <Button
- {...props}
- aria-label={t('Saved search options')}
- borderless
- icon={<IconEllipsis size="sm" />}
- size="sm"
- />
- )}
- />
- )}
- </SearchListItem>
- );
- };
- function CreateNewSavedSearchButton({
- organization,
- query,
- sort,
- }: CreateNewSavedSearchButtonProps) {
- const onClick = () => {
- trackAdvancedAnalyticsEvent('search.saved_search_open_create_modal', {
- organization,
- });
- openModal(deps => (
- <CreateSavedSearchModal {...deps} {...{organization, query, sort}} />
- ));
- };
- return (
- <Button
- aria-label={t('Create a new saved search')}
- onClick={onClick}
- icon={<IconAdd size="sm" />}
- borderless
- size="sm"
- />
- );
- }
- const SavedIssueSearches = ({
- organization,
- onSavedSearchSelect,
- query,
- sort,
- }: SavedIssueSearchesProps) => {
- const theme = useTheme();
- const [isOpen, setIsOpen] = useSyncedLocalStorageState(
- SAVED_SEARCHES_SIDEBAR_OPEN_LOCALSTORAGE_KEY,
- false
- );
- const [showAll, setShowAll] = useState(false);
- const {
- data: savedSearches,
- isLoading,
- isError,
- refetch,
- } = useFetchSavedSearchesForOrg({orgSlug: organization.slug});
- const isAboveContent = useMedia(`(max-width: ${theme.breakpoints.small})`);
- const onClickSavedSearch = (savedSearch: SavedSearch) => {
- // On small screens, the sidebar appears above the issue list, so we
- // will close it automatically for convenience.
- if (isAboveContent) {
- setIsOpen(false);
- }
- onSavedSearchSelect(savedSearch);
- };
- if (!isOpen) {
- return null;
- }
- if (isLoading) {
- return (
- <StyledSidebar>
- <LoadingIndicator />
- </StyledSidebar>
- );
- }
- if (isError) {
- return (
- <StyledSidebar>
- <LoadingError onRetry={refetch} />
- </StyledSidebar>
- );
- }
- const orgSavedSearches = orderBy(
- savedSearches.filter(search => !search.isGlobal && !search.isPinned),
- 'dateCreated',
- 'desc'
- );
- const recommendedSavedSearches = savedSearches.filter(search => search.isGlobal);
- const shownOrgSavedSearches = showAll
- ? orgSavedSearches
- : orgSavedSearches.slice(0, MAX_SHOWN_SEARCHES);
- return (
- <StyledSidebar>
- <Fragment>
- <HeadingContainer>
- <Heading>{t('Saved Searches')}</Heading>
- <CreateNewSavedSearchButton {...{organization, query, sort}} />
- </HeadingContainer>
- <SearchesContainer>
- {shownOrgSavedSearches.map(item => (
- <SavedSearchItem
- key={item.id}
- organization={organization}
- onSavedSearchSelect={onClickSavedSearch}
- savedSearch={item}
- />
- ))}
- {shownOrgSavedSearches.length === 0 && (
- <NoSavedSearchesText>
- {t("You don't have any saved searches")}
- </NoSavedSearchesText>
- )}
- </SearchesContainer>
- {orgSavedSearches.length > shownOrgSavedSearches.length && (
- <ShowAllButton size="zero" borderless onClick={() => setShowAll(true)}>
- {t(
- 'Show %s more',
- (orgSavedSearches.length - shownOrgSavedSearches.length).toLocaleString()
- )}
- </ShowAllButton>
- )}
- </Fragment>
- {recommendedSavedSearches.length > 0 && (
- <Fragment>
- <HeadingContainer>
- <Heading>{t('Recommended Searches')}</Heading>
- </HeadingContainer>
- <SearchesContainer>
- {recommendedSavedSearches.map(item => (
- <SavedSearchItem
- key={item.id}
- organization={organization}
- onSavedSearchSelect={onClickSavedSearch}
- savedSearch={item}
- />
- ))}
- </SearchesContainer>
- </Fragment>
- )}
- </StyledSidebar>
- );
- };
- const StyledSidebar = styled('aside')`
- grid-area: saved-searches;
- width: 100%;
- padding: ${space(2)};
- @media (max-width: ${p => p.theme.breakpoints.small}) {
- border-bottom: 1px solid ${p => p.theme.gray200};
- padding: ${space(2)} 0;
- }
- @media (min-width: ${p => p.theme.breakpoints.small}) {
- border-left: 1px solid ${p => p.theme.gray200};
- max-width: 340px;
- }
- `;
- const HeadingContainer = styled('div')`
- display: flex;
- justify-content: space-between;
- align-items: center;
- height: 38px;
- padding-left: ${space(2)};
- margin-top: ${space(3)};
- &:first-of-type {
- margin-top: 0;
- }
- `;
- const Heading = styled('h2')`
- font-size: ${p => p.theme.fontSizeExtraLarge};
- margin: 0;
- `;
- const SearchesContainer = styled('ul')`
- padding: 0;
- margin-bottom: ${space(1)};
- `;
- const StyledItemButton = styled(Button)`
- display: block;
- width: 100%;
- text-align: left;
- height: auto;
- font-weight: normal;
- line-height: ${p => p.theme.text.lineHeightBody};
- padding: ${space(1)} ${space(2)};
- ${ButtonLabel} {
- justify-content: start;
- }
- `;
- const OverflowMenu = styled(DropdownMenuControl)`
- position: absolute;
- top: 12px;
- right: ${space(1)};
- `;
- const SearchListItem = styled('li')<{hasMenu?: boolean}>`
- position: relative;
- list-style: none;
- padding: 0;
- margin: 0;
- ${p =>
- p.hasMenu &&
- css`
- @media (max-width: ${p.theme.breakpoints.small}) {
- ${StyledItemButton} {
- padding-right: 60px;
- }
- }
- @media (min-width: ${p.theme.breakpoints.small}) {
- ${OverflowMenu} {
- display: none;
- }
- &:hover,
- &:focus-within {
- ${OverflowMenu} {
- display: block;
- }
- ${StyledItemButton} {
- padding-right: 60px;
- }
- }
- }
- `}
- `;
- const TitleDescriptionWrapper = styled('div')`
- overflow: hidden;
- `;
- const SavedSearchItemTitle = styled('div')`
- font-size: ${p => p.theme.fontSizeLarge};
- ${p => p.theme.overflowEllipsis}
- `;
- const SavedSearchItemVisbility = styled('div')`
- color: ${p => p.theme.subText};
- font-size: ${p => p.theme.fontSizeSmall};
- ${p => p.theme.overflowEllipsis}
- `;
- const SavedSearchItemQuery = styled('div')`
- font-family: ${p => p.theme.text.familyMono};
- font-size: ${p => p.theme.fontSizeSmall};
- color: ${p => p.theme.subText};
- ${p => p.theme.overflowEllipsis}
- `;
- const ShowAllButton = styled(Button)`
- color: ${p => p.theme.linkColor};
- font-weight: normal;
- padding: ${space(0.5)} ${space(2)};
- &:hover {
- color: ${p => p.theme.linkHoverColor};
- }
- `;
- const NoSavedSearchesText = styled('p')`
- padding: 0 ${space(2)};
- margin: ${space(0.5)} 0;
- color: ${p => p.theme.subText};
- `;
- export default SavedIssueSearches;
|