123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300 |
- import {Fragment} from 'react';
- import {browserHistory} from 'react-router';
- import styled from '@emotion/styled';
- import {Location, Query} from 'history';
- import {
- createDashboard,
- deleteDashboard,
- fetchDashboard,
- } from 'sentry/actionCreators/dashboards';
- import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
- import {Client} from 'sentry/api';
- import Button from 'sentry/components/button';
- import {openConfirmModal} from 'sentry/components/confirm';
- import DropdownMenuControl from 'sentry/components/dropdownMenuControl';
- import {MenuItemProps} from 'sentry/components/dropdownMenuItem';
- import EmptyStateWarning from 'sentry/components/emptyStateWarning';
- import Pagination from 'sentry/components/pagination';
- import TimeSince from 'sentry/components/timeSince';
- import {IconEllipsis} from 'sentry/icons';
- import {t, tn} from 'sentry/locale';
- import space from 'sentry/styles/space';
- import {Organization} from 'sentry/types';
- import {trackAnalyticsEvent} from 'sentry/utils/analytics';
- import withApi from 'sentry/utils/withApi';
- import {DashboardListItem, DisplayType} from 'sentry/views/dashboardsV2/types';
- import {cloneDashboard, miniWidget} from '../utils';
- import DashboardCard from './dashboardCard';
- import GridPreview from './gridPreview';
- type Props = {
- api: Client;
- dashboards: DashboardListItem[] | null;
- location: Location;
- onDashboardsChange: () => void;
- organization: Organization;
- pageLinks: string;
- };
- function DashboardList({
- api,
- organization,
- location,
- dashboards,
- pageLinks,
- onDashboardsChange,
- }: Props) {
- function handleDelete(dashboard: DashboardListItem) {
- deleteDashboard(api, organization.slug, dashboard.id)
- .then(() => {
- trackAnalyticsEvent({
- eventKey: 'dashboards_manage.delete',
- eventName: 'Dashboards Manager: Dashboard Deleted',
- organization_id: parseInt(organization.id, 10),
- dashboard_id: parseInt(dashboard.id, 10),
- });
- onDashboardsChange();
- addSuccessMessage(t('Dashboard deleted'));
- })
- .catch(() => {
- addErrorMessage(t('Error deleting Dashboard'));
- });
- }
- async function handleDuplicate(dashboard: DashboardListItem) {
- try {
- const dashboardDetail = await fetchDashboard(api, organization.slug, dashboard.id);
- const newDashboard = cloneDashboard(dashboardDetail);
- newDashboard.widgets.map(widget => (widget.id = undefined));
- await createDashboard(api, organization.slug, newDashboard, true);
- trackAnalyticsEvent({
- eventKey: 'dashboards_manage.duplicate',
- eventName: 'Dashboards Manager: Dashboard Duplicated',
- organization_id: parseInt(organization.id, 10),
- dashboard_id: parseInt(dashboard.id, 10),
- });
- onDashboardsChange();
- addSuccessMessage(t('Dashboard duplicated'));
- } catch (e) {
- addErrorMessage(t('Error duplicating Dashboard'));
- }
- }
- function renderDropdownMenu(dashboard: DashboardListItem) {
- const menuItems: MenuItemProps[] = [
- {
- key: 'dashboard-duplicate',
- label: t('Duplicate'),
- onAction: () => handleDuplicate(dashboard),
- },
- {
- key: 'dashboard-delete',
- label: t('Delete'),
- priority: 'danger',
- onAction: () => {
- openConfirmModal({
- message: t('Are you sure you want to delete this dashboard?'),
- priority: 'danger',
- onConfirm: () => handleDelete(dashboard),
- });
- },
- },
- ];
- return (
- <DropdownMenuControl
- items={menuItems}
- trigger={({props: triggerProps, ref: triggerRef}) => (
- <DropdownTrigger
- ref={triggerRef}
- {...triggerProps}
- aria-label={t('Dashboard actions')}
- size="xs"
- borderless
- onClick={e => {
- e.stopPropagation();
- e.preventDefault();
- triggerProps.onClick?.(e);
- }}
- icon={<IconEllipsis direction="down" size="sm" />}
- />
- )}
- placement="bottom right"
- disabledKeys={dashboards && dashboards.length <= 1 ? ['dashboard-delete'] : []}
- offset={4}
- />
- );
- }
- function renderDndPreview(dashboard) {
- return (
- <WidgetGrid>
- {dashboard.widgetDisplay.map((displayType, i) => {
- return displayType === DisplayType.BIG_NUMBER ? (
- <BigNumberWidgetWrapper key={`${i}-${displayType}`}>
- <WidgetImage src={miniWidget(displayType)} />
- </BigNumberWidgetWrapper>
- ) : (
- <MiniWidgetWrapper key={`${i}-${displayType}`}>
- <WidgetImage src={miniWidget(displayType)} />
- </MiniWidgetWrapper>
- );
- })}
- </WidgetGrid>
- );
- }
- function renderGridPreview(dashboard) {
- return <GridPreview widgetPreview={dashboard.widgetPreview} />;
- }
- function renderMiniDashboards() {
- const isUsingGrid = organization.features.includes('dashboard-grid-layout');
- return dashboards?.map((dashboard, index) => {
- const widgetRenderer = isUsingGrid ? renderGridPreview : renderDndPreview;
- const widgetCount = isUsingGrid
- ? dashboard.widgetPreview.length
- : dashboard.widgetDisplay.length;
- return (
- <DashboardCard
- key={`${index}-${dashboard.id}`}
- title={dashboard.title}
- to={{
- pathname: `/organizations/${organization.slug}/dashboard/${dashboard.id}/`,
- query: {...location.query},
- }}
- detail={tn('%s widget', '%s widgets', widgetCount)}
- dateStatus={
- dashboard.dateCreated ? <TimeSince date={dashboard.dateCreated} /> : undefined
- }
- createdBy={dashboard.createdBy}
- renderWidgets={() => widgetRenderer(dashboard)}
- renderContextMenu={() => renderDropdownMenu(dashboard)}
- />
- );
- });
- }
- function renderDashboardGrid() {
- if (!dashboards?.length) {
- return (
- <EmptyStateWarning>
- <p>{t('Sorry, no Dashboards match your filters.')}</p>
- </EmptyStateWarning>
- );
- }
- return <DashboardGrid>{renderMiniDashboards()}</DashboardGrid>;
- }
- return (
- <Fragment>
- {renderDashboardGrid()}
- <PaginationRow
- pageLinks={pageLinks}
- onCursor={(cursor, path, query, direction) => {
- const offset = Number(cursor?.split?.(':')?.[1] ?? 0);
- const newQuery: Query & {cursor?: string} = {...query, cursor};
- const isPrevious = direction === -1;
- if (offset <= 0 && isPrevious) {
- delete newQuery.cursor;
- }
- trackAnalyticsEvent({
- eventKey: 'dashboards_manage.paginate',
- eventName: 'Dashboards Manager: Paginate',
- organization_id: parseInt(organization.id, 10),
- });
- browserHistory.push({
- pathname: path,
- query: newQuery,
- });
- }}
- />
- </Fragment>
- );
- }
- const DashboardGrid = styled('div')`
- display: grid;
- grid-template-columns: minmax(100px, 1fr);
- grid-template-rows: repeat(3, max-content);
- gap: ${space(2)};
- @media (min-width: ${p => p.theme.breakpoints.small}) {
- grid-template-columns: repeat(2, minmax(100px, 1fr));
- }
- @media (min-width: ${p => p.theme.breakpoints.large}) {
- grid-template-columns: repeat(3, minmax(100px, 1fr));
- }
- `;
- const WidgetGrid = styled('div')`
- display: grid;
- grid-template-columns: repeat(2, minmax(0, 1fr));
- grid-auto-flow: row dense;
- gap: ${space(0.25)};
- @media (min-width: ${p => p.theme.breakpoints.medium}) {
- grid-template-columns: repeat(4, minmax(0, 1fr));
- }
- @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
- grid-template-columns: repeat(6, minmax(0, 1fr));
- }
- @media (min-width: ${p => p.theme.breakpoints.xxlarge}) {
- grid-template-columns: repeat(8, minmax(0, 1fr));
- }
- `;
- const BigNumberWidgetWrapper = styled('div')`
- display: flex;
- align-items: flex-start;
- width: 100%;
- height: 100%;
- /* 2 cols */
- grid-area: span 1 / span 2;
- @media (min-width: ${p => p.theme.breakpoints.small}) {
- /* 4 cols */
- grid-area: span 1 / span 1;
- }
- @media (min-width: ${p => p.theme.breakpoints.xlarge}) {
- /* 6 and 8 cols */
- grid-area: span 1 / span 2;
- }
- `;
- const MiniWidgetWrapper = styled('div')`
- display: flex;
- align-items: flex-start;
- width: 100%;
- height: 100%;
- grid-area: span 2 / span 2;
- `;
- const WidgetImage = styled('img')`
- width: 100%;
- height: 100%;
- `;
- const PaginationRow = styled(Pagination)`
- margin-bottom: ${space(3)};
- `;
- const DropdownTrigger = styled(Button)`
- transform: translateX(${space(1)});
- `;
- export default withApi(DashboardList);
|