123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- import React, {MouseEvent} from 'react';
- import {browserHistory} from 'react-router';
- import styled from '@emotion/styled';
- import classNames from 'classnames';
- import {Location, Query} from 'history';
- import moment from 'moment';
- import {resetGlobalSelection} from 'app/actionCreators/globalSelection';
- import {Client} from 'app/api';
- import DropdownMenu from 'app/components/dropdownMenu';
- import EmptyStateWarning from 'app/components/emptyStateWarning';
- import MenuItem from 'app/components/menuItem';
- import Pagination from 'app/components/pagination';
- import TimeSince from 'app/components/timeSince';
- import {IconEllipsis} from 'app/icons';
- import {t} from 'app/locale';
- import space from 'app/styles/space';
- import {Organization, SavedQuery} from 'app/types';
- import {trackAnalyticsEvent} from 'app/utils/analytics';
- import EventView from 'app/utils/discover/eventView';
- import parseLinkHeader from 'app/utils/parseLinkHeader';
- import withApi from 'app/utils/withApi';
- import {handleCreateQuery, handleDeleteQuery} from './savedQuery/utils';
- import MiniGraph from './miniGraph';
- import QueryCard from './querycard';
- import {getPrebuiltQueries} from './utils';
- type Props = {
- api: Client;
- organization: Organization;
- location: Location;
- savedQueries: SavedQuery[];
- renderPrebuilt: boolean;
- pageLinks: string;
- onQueryChange: () => void;
- savedQuerySearchQuery: string;
- };
- class QueryList extends React.Component<Props> {
- componentDidMount() {
- /**
- * We need to reset global selection here because the saved queries can define their own projects
- * in the query. This can lead to mismatched queries for the project
- */
- resetGlobalSelection();
- }
- handleDeleteQuery = (eventView: EventView) => (event: React.MouseEvent<Element>) => {
- event.preventDefault();
- event.stopPropagation();
- const {api, organization, onQueryChange, location, savedQueries} = this.props;
- handleDeleteQuery(api, organization, eventView).then(() => {
- if (savedQueries.length === 1 && location.query.cursor) {
- browserHistory.push({
- pathname: location.pathname,
- query: {...location.query, cursor: undefined},
- });
- } else {
- onQueryChange();
- }
- });
- };
- handleDuplicateQuery = (eventView: EventView) => (event: React.MouseEvent<Element>) => {
- event.preventDefault();
- event.stopPropagation();
- const {api, location, organization, onQueryChange} = this.props;
- eventView = eventView.clone();
- eventView.name = `${eventView.name} copy`;
- handleCreateQuery(api, organization, eventView).then(() => {
- onQueryChange();
- browserHistory.push({
- pathname: location.pathname,
- query: {},
- });
- });
- };
- renderQueries() {
- const {pageLinks, renderPrebuilt} = this.props;
- const links = parseLinkHeader(pageLinks || '');
- let cards: React.ReactNode[] = [];
- // If we're on the first page (no-previous page exists)
- // include the pre-built queries.
- if (renderPrebuilt && (!links.previous || links.previous.results === false)) {
- cards = cards.concat(this.renderPrebuiltQueries());
- }
- cards = cards.concat(this.renderSavedQueries());
- if (cards.filter(x => x).length === 0) {
- return (
- <StyledEmptyStateWarning>
- <p>{t('No saved queries match that filter')}</p>
- </StyledEmptyStateWarning>
- );
- }
- return cards;
- }
- renderPrebuiltQueries() {
- const {location, organization, savedQuerySearchQuery} = this.props;
- const views = getPrebuiltQueries(organization);
- const hasSearchQuery =
- typeof savedQuerySearchQuery === 'string' && savedQuerySearchQuery.length > 0;
- const needleSearch = hasSearchQuery ? savedQuerySearchQuery.toLowerCase() : '';
- const list = views.map((view, index) => {
- const eventView = EventView.fromNewQueryWithLocation(view, location);
- // if a search is performed on the list of queries, we filter
- // on the pre-built queries
- if (
- hasSearchQuery &&
- eventView.name &&
- !eventView.name.toLowerCase().includes(needleSearch)
- ) {
- return null;
- }
- const recentTimeline = t('Last ') + eventView.statsPeriod;
- const customTimeline =
- moment(eventView.start).format('MMM D, YYYY h:mm A') +
- ' - ' +
- moment(eventView.end).format('MMM D, YYYY h:mm A');
- const to = eventView.getResultsViewUrlTarget(organization.slug);
- return (
- <QueryCard
- key={`${index}-${eventView.name}`}
- to={to}
- title={eventView.name}
- subtitle={eventView.statsPeriod ? recentTimeline : customTimeline}
- queryDetail={eventView.query}
- createdBy={eventView.createdBy}
- renderGraph={() => (
- <MiniGraph
- location={location}
- eventView={eventView}
- organization={organization}
- />
- )}
- onEventClick={() => {
- trackAnalyticsEvent({
- eventKey: 'discover_v2.prebuilt_query_click',
- eventName: 'Discoverv2: Click a pre-built query',
- organization_id: parseInt(this.props.organization.id, 10),
- query_name: eventView.name,
- });
- }}
- />
- );
- });
- return list;
- }
- renderSavedQueries() {
- const {savedQueries, location, organization} = this.props;
- if (!savedQueries || !Array.isArray(savedQueries) || savedQueries.length === 0) {
- return [];
- }
- return savedQueries.map((savedQuery, index) => {
- const eventView = EventView.fromSavedQuery(savedQuery);
- const recentTimeline = t('Last ') + eventView.statsPeriod;
- const customTimeline =
- moment(eventView.start).format('MMM D, YYYY h:mm A') +
- ' - ' +
- moment(eventView.end).format('MMM D, YYYY h:mm A');
- const to = eventView.getResultsViewShortUrlTarget(organization.slug);
- const dateStatus = <TimeSince date={savedQuery.dateUpdated} />;
- return (
- <QueryCard
- key={`${index}-${eventView.id}`}
- to={to}
- title={eventView.name}
- subtitle={eventView.statsPeriod ? recentTimeline : customTimeline}
- queryDetail={eventView.query}
- createdBy={eventView.createdBy}
- dateStatus={dateStatus}
- onEventClick={() => {
- trackAnalyticsEvent({
- eventKey: 'discover_v2.saved_query_click',
- eventName: 'Discoverv2: Click a saved query',
- organization_id: parseInt(this.props.organization.id, 10),
- });
- }}
- renderGraph={() => (
- <MiniGraph
- location={location}
- eventView={eventView}
- organization={organization}
- />
- )}
- renderContextMenu={() => (
- <ContextMenu>
- <MenuItem
- data-test-id="delete-query"
- onClick={this.handleDeleteQuery(eventView)}
- >
- {t('Delete Query')}
- </MenuItem>
- <MenuItem
- data-test-id="duplicate-query"
- onClick={this.handleDuplicateQuery(eventView)}
- >
- {t('Duplicate Query')}
- </MenuItem>
- </ContextMenu>
- )}
- />
- );
- });
- }
- render() {
- const {pageLinks} = this.props;
- return (
- <React.Fragment>
- <QueryGrid>{this.renderQueries()}</QueryGrid>
- <PaginationRow
- pageLinks={pageLinks}
- onCursor={(cursor: string, path: string, query: Query, direction: number) => {
- const offset = Number(cursor.split(':')[1]);
- const newQuery: Query & {cursor?: string} = {...query, cursor};
- const isPrevious = direction === -1;
- if (offset <= 0 && isPrevious) {
- delete newQuery.cursor;
- }
- browserHistory.push({
- pathname: path,
- query: newQuery,
- });
- }}
- />
- </React.Fragment>
- );
- }
- }
- const PaginationRow = styled(Pagination)`
- margin-bottom: 20px;
- `;
- const QueryGrid = styled('div')`
- display: grid;
- grid-template-columns: minmax(100px, 1fr);
- grid-gap: ${space(3)};
- @media (min-width: ${p => p.theme.breakpoints[1]}) {
- grid-template-columns: repeat(2, minmax(100px, 1fr));
- }
- @media (min-width: ${p => p.theme.breakpoints[2]}) {
- grid-template-columns: repeat(3, minmax(100px, 1fr));
- }
- `;
- const ContextMenu = ({children}) => (
- <DropdownMenu>
- {({isOpen, getRootProps, getActorProps, getMenuProps}) => {
- const topLevelCx = classNames('dropdown', {
- 'anchor-right': true,
- open: isOpen,
- });
- return (
- <MoreOptions
- {...getRootProps({
- className: topLevelCx,
- })}
- >
- <DropdownTarget
- {...getActorProps<HTMLDivElement>({
- onClick: (event: MouseEvent) => {
- event.stopPropagation();
- event.preventDefault();
- },
- })}
- >
- <IconEllipsis data-test-id="context-menu" size="md" />
- </DropdownTarget>
- {isOpen && (
- <ul {...getMenuProps({})} className={classNames('dropdown-menu')}>
- {children}
- </ul>
- )}
- </MoreOptions>
- );
- }}
- </DropdownMenu>
- );
- const MoreOptions = styled('span')`
- display: flex;
- color: ${p => p.theme.textColor};
- `;
- const DropdownTarget = styled('div')`
- display: flex;
- `;
- const StyledEmptyStateWarning = styled(EmptyStateWarning)`
- grid-column: 1 / 4;
- `;
- export default withApi(QueryList);
|