dashboardList.tsx 6.3 KB

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