import {Fragment} from 'react';
import styled from '@emotion/styled';
import Feature from 'sentry/components/acl/feature';
import FeatureDisabled from 'sentry/components/acl/featureDisabled';
import {Button} from 'sentry/components/button';
import ButtonBar from 'sentry/components/buttonBar';
import Confirm from 'sentry/components/confirm';
import FeedbackWidgetButton from 'sentry/components/feedback/widget/feedbackWidgetButton';
import {Hovercard} from 'sentry/components/hovercard';
import {Tooltip} from 'sentry/components/tooltip';
import {IconAdd, IconDownload, IconEdit} from 'sentry/icons';
import {t, tct} from 'sentry/locale';
import {space} from 'sentry/styles/space';
import type {Organization} from 'sentry/types/organization';
import {trackAnalytics} from 'sentry/utils/analytics';
import {hasCustomMetrics} from 'sentry/utils/metrics/features';
import useOrganization from 'sentry/utils/useOrganization';
import {useUser} from 'sentry/utils/useUser';
import {useUserTeams} from 'sentry/utils/useUserTeams';
import {AddWidgetButton} from 'sentry/views/dashboards/addWidget';
import EditAccessSelector from 'sentry/views/dashboards/editAccessSelector';
import {DataSet} from 'sentry/views/dashboards/widgetBuilder/utils';
import {checkUserHasEditAccess, UNSAVED_FILTERS_MESSAGE} from './detail';
import exportDashboard from './exportDashboard';
import type {DashboardDetails, DashboardListItem, DashboardPermissions} from './types';
import {DashboardState, MAX_WIDGETS} from './types';
type Props = {
dashboard: DashboardDetails;
dashboardState: DashboardState;
dashboards: DashboardListItem[];
onAddWidget: (dataset: DataSet) => void;
onCancel: () => void;
onCommit: () => void;
onDelete: () => void;
onEdit: () => void;
organization: Organization;
widgetLimitReached: boolean;
hasUnsavedFilters?: boolean;
onChangeEditAccess?: (newDashboardPermissions: DashboardPermissions) => void;
};
function Controls({
dashboardState,
dashboard,
dashboards,
hasUnsavedFilters,
widgetLimitReached,
onChangeEditAccess,
onEdit,
onCommit,
onDelete,
onCancel,
onAddWidget,
}: Props) {
function renderCancelButton(label = t('Cancel')) {
return (
);
}
const organization = useOrganization();
const currentUser = useUser();
const {teams: userTeams} = useUserTeams();
if ([DashboardState.EDIT, DashboardState.PENDING_DELETE].includes(dashboardState)) {
return (
{renderCancelButton()}
);
}
if (dashboardState === DashboardState.CREATE) {
return (
{renderCancelButton()}
);
}
if (dashboardState === DashboardState.PREVIEW) {
return (
{renderCancelButton(t('Go Back'))}
);
}
const defaultDataset = organization.features.includes(
'performance-discover-dataset-selector'
)
? DataSet.ERRORS
: DataSet.EVENTS;
let hasEditAccess = true;
if (organization.features.includes('dashboards-edit-access')) {
hasEditAccess = checkUserHasEditAccess(
currentUser,
userTeams,
organization,
dashboard.permissions,
dashboard.createdBy
);
}
return (
{hasFeature => (
{hasFeature ? (
{hasCustomMetrics(organization) ? (
) : (
}
onClick={() => {
trackAnalytics('dashboards_views.widget_library.opened', {
organization,
});
onAddWidget(defaultDataset);
}}
title={
!hasEditAccess &&
t('You do not have permission to edit this dashboard')
}
>
{t('Add Widget')}
)}
) : null}
)}
);
}
function DashboardEditFeature({
children,
}: {
children: (hasFeature: boolean) => React.ReactNode;
}) {
const renderDisabled = p => (
}
>
{p.children(p)}
);
return (
{({hasFeature}) => children(hasFeature)}
);
}
const StyledButtonBar = styled(ButtonBar)`
@media (max-width: ${p => p.theme.breakpoints.small}) {
grid-auto-flow: row;
grid-row-gap: ${space(1)};
width: 100%;
}
`;
export default Controls;