import {Fragment} from 'react';
import {Location} from 'history';
import Feature from 'sentry/components/acl/feature';
import FeatureDisabled from 'sentry/components/acl/featureDisabled';
import GuideAnchor from 'sentry/components/assistant/guideAnchor';
import {Button} from 'sentry/components/button';
import DataExport, {ExportQueryType} from 'sentry/components/dataExport';
import {Hovercard} from 'sentry/components/hovercard';
import {IconDownload, IconStack, IconTag} from 'sentry/icons';
import {t} from 'sentry/locale';
import {OrganizationSummary} from 'sentry/types';
import {trackAnalytics} from 'sentry/utils/analytics';
import {TableData} from 'sentry/utils/discover/discoverQuery';
import EventView from 'sentry/utils/discover/eventView';
import {downloadAsCsv} from '../utils';
type Props = {
error: string | null;
eventView: EventView;
isLoading: boolean;
location: Location;
onChangeShowTags: () => void;
onEdit: () => void;
organization: OrganizationSummary;
showTags: boolean;
tableData: TableData | null | undefined;
title: string;
};
function handleDownloadAsCsv(title: string, {organization, eventView, tableData}: Props) {
trackAnalytics('discover_v2.results.download_csv', {
organization: organization.id, // org summary
});
downloadAsCsv(tableData, eventView.getColumns(), title);
}
function renderDownloadButton(canEdit: boolean, props: Props) {
const {tableData} = props;
return (
renderBrowserExportButton(canEdit, props)}
>
{tableData?.data && tableData.data.length < 50
? renderBrowserExportButton(canEdit, props)
: renderAsyncExportButton(canEdit, props)}
);
}
function renderBrowserExportButton(canEdit: boolean, props: Props) {
const {isLoading, error} = props;
const disabled = isLoading || error !== null || canEdit === false;
const onClick = disabled ? undefined : () => handleDownloadAsCsv(props.title, props);
return (
}
title={t(
"There aren't that many results, start your export and it'll download immediately."
)}
>
{t('Export All')}
);
}
function renderAsyncExportButton(canEdit: boolean, props: Props) {
const {isLoading, error, location, eventView} = props;
const disabled = isLoading || error !== null || canEdit === false;
return (
}
>
{t('Export All')}
);
}
// Placate eslint proptype checking
function renderEditButton(canEdit: boolean, props: Props) {
const onClick = canEdit ? props.onEdit : undefined;
return (
}
>
{t('Columns')}
);
}
// Placate eslint proptype checking
function renderSummaryButton({onChangeShowTags, showTags}: Props) {
return (
}>
{showTags ? t('Hide Tags') : t('Show Tags')}
);
}
type FeatureWrapperProps = Props & {
children: (hasFeature: boolean, props: Props) => React.ReactNode;
};
function FeatureWrapper(props: FeatureWrapperProps) {
const noEditMessage = t('Requires discover query feature.');
const editFeatures = ['organizations:discover-query'];
const renderDisabled = p => (
}
>
{p.children(p)}
);
return (
{({hasFeature}) => props.children(hasFeature, props)}
);
}
function HeaderActions(props: Props) {
return (
{renderEditButton}
{renderDownloadButton}
{renderSummaryButton(props)}
);
}
export default HeaderActions;