dashboardList.tsx 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  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. // TODO(__SENTRY_USING_REACT_ROUTER_SIX): We can remove this later, react
  122. // router 6 handles empty query objects without appending a trailing ?
  123. const queryLocation = {
  124. ...(location.query && Object.keys(location.query).length > 0
  125. ? {query: location.query}
  126. : {}),
  127. };
  128. function renderMiniDashboards() {
  129. return dashboards?.map((dashboard, index) => {
  130. return (
  131. <DashboardCard
  132. key={`${index}-${dashboard.id}`}
  133. title={dashboard.title}
  134. to={{
  135. pathname: `/organizations/${organization.slug}/dashboard/${dashboard.id}/`,
  136. ...queryLocation,
  137. }}
  138. detail={tn('%s widget', '%s widgets', dashboard.widgetPreview.length)}
  139. dateStatus={
  140. dashboard.dateCreated ? <TimeSince date={dashboard.dateCreated} /> : undefined
  141. }
  142. createdBy={dashboard.createdBy}
  143. renderWidgets={() => renderGridPreview(dashboard)}
  144. renderContextMenu={() => renderDropdownMenu(dashboard)}
  145. />
  146. );
  147. });
  148. }
  149. function renderDashboardGrid() {
  150. if (!dashboards?.length) {
  151. return (
  152. <EmptyStateWarning>
  153. <p>{t('Sorry, no Dashboards match your filters.')}</p>
  154. </EmptyStateWarning>
  155. );
  156. }
  157. return <DashboardGrid>{renderMiniDashboards()}</DashboardGrid>;
  158. }
  159. return (
  160. <Fragment>
  161. {renderDashboardGrid()}
  162. <PaginationRow
  163. pageLinks={pageLinks}
  164. onCursor={(cursor, path, query, direction) => {
  165. const offset = Number(cursor?.split?.(':')?.[1] ?? 0);
  166. const newQuery: Query & {cursor?: string} = {...query, cursor};
  167. const isPrevious = direction === -1;
  168. if (offset <= 0 && isPrevious) {
  169. delete newQuery.cursor;
  170. }
  171. trackAnalytics('dashboards_manage.paginate', {organization});
  172. browserHistory.push({
  173. pathname: path,
  174. query: newQuery,
  175. });
  176. }}
  177. />
  178. </Fragment>
  179. );
  180. }
  181. const DashboardGrid = styled('div')`
  182. display: grid;
  183. grid-template-columns: minmax(100px, 1fr);
  184. grid-template-rows: repeat(3, max-content);
  185. gap: ${space(2)};
  186. @media (min-width: ${p => p.theme.breakpoints.small}) {
  187. grid-template-columns: repeat(2, minmax(100px, 1fr));
  188. }
  189. @media (min-width: ${p => p.theme.breakpoints.large}) {
  190. grid-template-columns: repeat(3, minmax(100px, 1fr));
  191. }
  192. `;
  193. const PaginationRow = styled(Pagination)`
  194. margin-bottom: ${space(3)};
  195. `;
  196. const DropdownTrigger = styled(Button)`
  197. transform: translateX(${space(1)});
  198. `;
  199. export default withApi(DashboardList);