dashboardList.tsx 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. import {Fragment} from 'react';
  2. import {browserHistory} from 'react-router';
  3. import styled from '@emotion/styled';
  4. import {Location, Query} from 'history';
  5. import {
  6. createDashboard,
  7. deleteDashboard,
  8. fetchDashboard,
  9. } from 'sentry/actionCreators/dashboards';
  10. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  11. import {Client} from 'sentry/api';
  12. import {Button} from 'sentry/components/button';
  13. import {openConfirmModal} from 'sentry/components/confirm';
  14. import {DropdownMenu, MenuItemProps} from 'sentry/components/dropdownMenu';
  15. import EmptyStateWarning from 'sentry/components/emptyStateWarning';
  16. import Pagination from 'sentry/components/pagination';
  17. import TimeSince from 'sentry/components/timeSince';
  18. import {IconEllipsis} from 'sentry/icons';
  19. import {t, tn} from 'sentry/locale';
  20. import {space} from 'sentry/styles/space';
  21. import {Organization} from 'sentry/types';
  22. import {trackAnalytics} from 'sentry/utils/analytics';
  23. import withApi from 'sentry/utils/withApi';
  24. import {DashboardListItem} from 'sentry/views/dashboards/types';
  25. import {cloneDashboard} from '../utils';
  26. import DashboardCard from './dashboardCard';
  27. import GridPreview from './gridPreview';
  28. type Props = {
  29. api: Client;
  30. dashboards: DashboardListItem[] | null;
  31. location: Location;
  32. onDashboardsChange: () => void;
  33. organization: Organization;
  34. pageLinks: string;
  35. };
  36. function DashboardList({
  37. api,
  38. organization,
  39. location,
  40. dashboards,
  41. pageLinks,
  42. onDashboardsChange,
  43. }: Props) {
  44. function handleDelete(dashboard: DashboardListItem) {
  45. deleteDashboard(api, organization.slug, dashboard.id)
  46. .then(() => {
  47. trackAnalytics('dashboards_manage.delete', {
  48. organization,
  49. dashboard_id: parseInt(dashboard.id, 10),
  50. });
  51. onDashboardsChange();
  52. addSuccessMessage(t('Dashboard deleted'));
  53. })
  54. .catch(() => {
  55. addErrorMessage(t('Error deleting Dashboard'));
  56. });
  57. }
  58. async function handleDuplicate(dashboard: DashboardListItem) {
  59. try {
  60. const dashboardDetail = await fetchDashboard(api, organization.slug, dashboard.id);
  61. const newDashboard = cloneDashboard(dashboardDetail);
  62. newDashboard.widgets.map(widget => (widget.id = undefined));
  63. await createDashboard(api, organization.slug, newDashboard, true);
  64. trackAnalytics('dashboards_manage.duplicate', {
  65. organization,
  66. dashboard_id: parseInt(dashboard.id, 10),
  67. });
  68. onDashboardsChange();
  69. addSuccessMessage(t('Dashboard duplicated'));
  70. } catch (e) {
  71. addErrorMessage(t('Error duplicating Dashboard'));
  72. }
  73. }
  74. function renderDropdownMenu(dashboard: DashboardListItem) {
  75. const menuItems: MenuItemProps[] = [
  76. {
  77. key: 'dashboard-duplicate',
  78. label: t('Duplicate'),
  79. onAction: () => handleDuplicate(dashboard),
  80. },
  81. {
  82. key: 'dashboard-delete',
  83. label: t('Delete'),
  84. priority: 'danger',
  85. onAction: () => {
  86. openConfirmModal({
  87. message: t('Are you sure you want to delete this dashboard?'),
  88. priority: 'danger',
  89. onConfirm: () => handleDelete(dashboard),
  90. });
  91. },
  92. },
  93. ];
  94. return (
  95. <DropdownMenu
  96. items={menuItems}
  97. trigger={triggerProps => (
  98. <DropdownTrigger
  99. {...triggerProps}
  100. aria-label={t('Dashboard actions')}
  101. size="xs"
  102. borderless
  103. onClick={e => {
  104. e.stopPropagation();
  105. e.preventDefault();
  106. triggerProps.onClick?.(e);
  107. }}
  108. icon={<IconEllipsis direction="down" size="sm" />}
  109. />
  110. )}
  111. position="bottom-end"
  112. disabledKeys={dashboards && dashboards.length <= 1 ? ['dashboard-delete'] : []}
  113. offset={4}
  114. />
  115. );
  116. }
  117. function renderGridPreview(dashboard) {
  118. return <GridPreview widgetPreview={dashboard.widgetPreview} />;
  119. }
  120. function renderMiniDashboards() {
  121. return dashboards?.map((dashboard, index) => {
  122. return (
  123. <DashboardCard
  124. key={`${index}-${dashboard.id}`}
  125. title={dashboard.title}
  126. to={{
  127. pathname: `/organizations/${organization.slug}/dashboard/${dashboard.id}/`,
  128. query: {...location.query},
  129. }}
  130. detail={tn('%s widget', '%s widgets', dashboard.widgetPreview.length)}
  131. dateStatus={
  132. dashboard.dateCreated ? <TimeSince date={dashboard.dateCreated} /> : undefined
  133. }
  134. createdBy={dashboard.createdBy}
  135. renderWidgets={() => renderGridPreview(dashboard)}
  136. renderContextMenu={() => renderDropdownMenu(dashboard)}
  137. />
  138. );
  139. });
  140. }
  141. function renderDashboardGrid() {
  142. if (!dashboards?.length) {
  143. return (
  144. <EmptyStateWarning>
  145. <p>{t('Sorry, no Dashboards match your filters.')}</p>
  146. </EmptyStateWarning>
  147. );
  148. }
  149. return <DashboardGrid>{renderMiniDashboards()}</DashboardGrid>;
  150. }
  151. return (
  152. <Fragment>
  153. {renderDashboardGrid()}
  154. <PaginationRow
  155. pageLinks={pageLinks}
  156. onCursor={(cursor, path, query, direction) => {
  157. const offset = Number(cursor?.split?.(':')?.[1] ?? 0);
  158. const newQuery: Query & {cursor?: string} = {...query, cursor};
  159. const isPrevious = direction === -1;
  160. if (offset <= 0 && isPrevious) {
  161. delete newQuery.cursor;
  162. }
  163. trackAnalytics('dashboards_manage.paginate', {organization});
  164. browserHistory.push({
  165. pathname: path,
  166. query: newQuery,
  167. });
  168. }}
  169. />
  170. </Fragment>
  171. );
  172. }
  173. const DashboardGrid = styled('div')`
  174. display: grid;
  175. grid-template-columns: minmax(100px, 1fr);
  176. grid-template-rows: repeat(3, max-content);
  177. gap: ${space(2)};
  178. @media (min-width: ${p => p.theme.breakpoints.small}) {
  179. grid-template-columns: repeat(2, minmax(100px, 1fr));
  180. }
  181. @media (min-width: ${p => p.theme.breakpoints.large}) {
  182. grid-template-columns: repeat(3, minmax(100px, 1fr));
  183. }
  184. `;
  185. const PaginationRow = styled(Pagination)`
  186. margin-bottom: ${space(3)};
  187. `;
  188. const DropdownTrigger = styled(Button)`
  189. transform: translateX(${space(1)});
  190. `;
  191. export default withApi(DashboardList);