dashboardTable.tsx 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  1. import styled from '@emotion/styled';
  2. import type {Location} from 'history';
  3. import cloneDeep from 'lodash/cloneDeep';
  4. import {
  5. createDashboard,
  6. deleteDashboard,
  7. fetchDashboard,
  8. updateDashboardPermissions,
  9. } from 'sentry/actionCreators/dashboards';
  10. import {addErrorMessage, addSuccessMessage} from 'sentry/actionCreators/indicator';
  11. import type {Client} from 'sentry/api';
  12. import {ActivityAvatar} from 'sentry/components/activity/item/avatar';
  13. import {Button} from 'sentry/components/button';
  14. import {openConfirmModal} from 'sentry/components/confirm';
  15. import EmptyStateWarning from 'sentry/components/emptyStateWarning';
  16. import GridEditable, {
  17. COL_WIDTH_UNDEFINED,
  18. type GridColumnOrder,
  19. } from 'sentry/components/gridEditable';
  20. import Link from 'sentry/components/links/link';
  21. import TimeSince from 'sentry/components/timeSince';
  22. import {IconCopy, IconDelete} from 'sentry/icons';
  23. import {t} from 'sentry/locale';
  24. import {space} from 'sentry/styles/space';
  25. import type {Organization} from 'sentry/types/organization';
  26. import {trackAnalytics} from 'sentry/utils/analytics';
  27. import withApi from 'sentry/utils/withApi';
  28. import EditAccessSelector from 'sentry/views/dashboards/editAccessSelector';
  29. import type {
  30. DashboardDetails,
  31. DashboardListItem,
  32. DashboardPermissions,
  33. } from 'sentry/views/dashboards/types';
  34. import {cloneDashboard} from '../utils';
  35. type Props = {
  36. api: Client;
  37. dashboards: DashboardListItem[] | undefined;
  38. location: Location;
  39. onDashboardsChange: () => void;
  40. organization: Organization;
  41. isLoading?: boolean;
  42. };
  43. enum ResponseKeys {
  44. NAME = 'title',
  45. WIDGETS = 'widgetDisplay',
  46. OWNER = 'createdBy',
  47. ACCESS = 'permissions',
  48. CREATED = 'dateCreated',
  49. }
  50. function DashboardTable({
  51. api,
  52. organization,
  53. location,
  54. dashboards,
  55. onDashboardsChange,
  56. isLoading,
  57. }: Props) {
  58. const columnOrder = organization.features.includes('dashboards-edit-access')
  59. ? [
  60. {key: ResponseKeys.NAME, name: t('Name'), width: COL_WIDTH_UNDEFINED},
  61. {key: ResponseKeys.WIDGETS, name: t('Widgets'), width: COL_WIDTH_UNDEFINED},
  62. {key: ResponseKeys.OWNER, name: t('Owner'), width: COL_WIDTH_UNDEFINED},
  63. {key: ResponseKeys.ACCESS, name: t('Access'), width: COL_WIDTH_UNDEFINED},
  64. {key: ResponseKeys.CREATED, name: t('Created'), width: COL_WIDTH_UNDEFINED},
  65. ]
  66. : [
  67. {key: ResponseKeys.NAME, name: t('Name'), width: COL_WIDTH_UNDEFINED},
  68. {key: ResponseKeys.WIDGETS, name: t('Widgets'), width: COL_WIDTH_UNDEFINED},
  69. {key: ResponseKeys.OWNER, name: t('Owner'), width: COL_WIDTH_UNDEFINED},
  70. {key: ResponseKeys.CREATED, name: t('Created'), width: COL_WIDTH_UNDEFINED},
  71. ];
  72. function handleDelete(dashboard: DashboardListItem) {
  73. deleteDashboard(api, organization.slug, dashboard.id)
  74. .then(() => {
  75. trackAnalytics('dashboards_manage.delete', {
  76. organization,
  77. dashboard_id: parseInt(dashboard.id, 10),
  78. view_type: 'table',
  79. });
  80. onDashboardsChange();
  81. addSuccessMessage(t('Dashboard deleted'));
  82. })
  83. .catch(() => {
  84. addErrorMessage(t('Error deleting Dashboard'));
  85. });
  86. }
  87. async function handleDuplicate(dashboard: DashboardListItem) {
  88. try {
  89. const dashboardDetail = await fetchDashboard(api, organization.slug, dashboard.id);
  90. const newDashboard = cloneDashboard(dashboardDetail);
  91. newDashboard.widgets.map(widget => (widget.id = undefined));
  92. await createDashboard(api, organization.slug, newDashboard, true);
  93. trackAnalytics('dashboards_manage.duplicate', {
  94. organization,
  95. dashboard_id: parseInt(dashboard.id, 10),
  96. view_type: 'table',
  97. });
  98. onDashboardsChange();
  99. addSuccessMessage(t('Dashboard duplicated'));
  100. } catch (e) {
  101. addErrorMessage(t('Error duplicating Dashboard'));
  102. }
  103. }
  104. // TODO(__SENTRY_USING_REACT_ROUTER_SIX): We can remove this later, react
  105. // router 6 handles empty query objects without appending a trailing ?
  106. const queryLocation = {
  107. ...(location.query && Object.keys(location.query).length > 0
  108. ? {query: location.query}
  109. : {}),
  110. };
  111. const renderBodyCell = (
  112. column: GridColumnOrder<string>,
  113. dataRow: DashboardListItem
  114. ) => {
  115. if (column.key === ResponseKeys.NAME) {
  116. return (
  117. <Link
  118. to={{
  119. pathname: `/organizations/${organization.slug}/dashboard/${dataRow.id}/`,
  120. ...queryLocation,
  121. }}
  122. >
  123. {dataRow[ResponseKeys.NAME]}
  124. </Link>
  125. );
  126. }
  127. if (column.key === ResponseKeys.WIDGETS) {
  128. return dataRow[ResponseKeys.WIDGETS].length;
  129. }
  130. if (column.key === ResponseKeys.OWNER) {
  131. return dataRow[ResponseKeys.OWNER] ? (
  132. <ActivityAvatar type="user" user={dataRow[ResponseKeys.OWNER]} size={26} />
  133. ) : (
  134. <ActivityAvatar type="system" size={26} />
  135. );
  136. }
  137. if (
  138. column.key === ResponseKeys.ACCESS &&
  139. organization.features.includes('dashboards-edit-access')
  140. ) {
  141. /* Handles POST request for Edit Access Selector Changes */
  142. const onChangeEditAccess = (newDashboardPermissions: DashboardPermissions) => {
  143. const dashboardCopy = cloneDeep(dataRow);
  144. dashboardCopy.permissions = newDashboardPermissions;
  145. updateDashboardPermissions(api, organization.slug, dashboardCopy).then(
  146. (newDashboard: DashboardDetails) => {
  147. onDashboardsChange();
  148. addSuccessMessage(t('Dashboard Edit Access updated.'));
  149. return newDashboard;
  150. }
  151. );
  152. };
  153. return (
  154. <EditAccessSelector
  155. dashboard={dataRow}
  156. onChangeEditAccess={onChangeEditAccess}
  157. listOnly
  158. />
  159. );
  160. }
  161. if (column.key === ResponseKeys.CREATED) {
  162. return (
  163. <DateActionsContainer>
  164. <DateSelected>
  165. {dataRow[ResponseKeys.CREATED] ? (
  166. <DateStatus>
  167. <TimeSince date={dataRow[ResponseKeys.CREATED]} />
  168. </DateStatus>
  169. ) : (
  170. <DateStatus />
  171. )}
  172. </DateSelected>
  173. <ActionsIconWrapper>
  174. <StyledButton
  175. onClick={e => {
  176. e.stopPropagation();
  177. openConfirmModal({
  178. message: t('Are you sure you want to duplicate this dashboard?'),
  179. priority: 'primary',
  180. onConfirm: () => handleDuplicate(dataRow),
  181. });
  182. }}
  183. aria-label={t('Duplicate Dashboard')}
  184. data-test-id={'dashboard-duplicate'}
  185. icon={<IconCopy />}
  186. size="sm"
  187. />
  188. <StyledButton
  189. onClick={e => {
  190. e.stopPropagation();
  191. openConfirmModal({
  192. message: t('Are you sure you want to delete this dashboard?'),
  193. priority: 'danger',
  194. onConfirm: () => handleDelete(dataRow),
  195. });
  196. }}
  197. aria-label={t('Delete Dashboard')}
  198. data-test-id={'dashboard-delete'}
  199. icon={<IconDelete />}
  200. size="sm"
  201. disabled={dashboards && dashboards.length <= 1}
  202. />
  203. </ActionsIconWrapper>
  204. </DateActionsContainer>
  205. );
  206. }
  207. return <span>{dataRow[column.key]}</span>;
  208. };
  209. return (
  210. <GridEditable
  211. data={dashboards ?? []}
  212. // necessary for edit access dropdown
  213. bodyStyle={{overflow: 'visible'}}
  214. columnOrder={columnOrder}
  215. columnSortBy={[]}
  216. grid={{
  217. renderBodyCell,
  218. }}
  219. isLoading={isLoading}
  220. emptyMessage={
  221. <EmptyStateWarning>
  222. <p>{t('Sorry, no Dashboards match your filters.')}</p>
  223. </EmptyStateWarning>
  224. }
  225. />
  226. );
  227. }
  228. export default withApi(DashboardTable);
  229. const DateSelected = styled('div')`
  230. font-size: ${p => p.theme.fontSizeMedium};
  231. display: grid;
  232. grid-column-gap: ${space(1)};
  233. color: ${p => p.theme.textColor};
  234. ${p => p.theme.overflowEllipsis};
  235. `;
  236. const DateStatus = styled('span')`
  237. color: ${p => p.theme.textColor};
  238. padding-left: ${space(1)};
  239. `;
  240. const DateActionsContainer = styled('div')`
  241. display: flex;
  242. gap: ${space(4)};
  243. justify-content: space-between;
  244. align-items: center;
  245. `;
  246. const ActionsIconWrapper = styled('div')`
  247. display: flex;
  248. `;
  249. const StyledButton = styled(Button)`
  250. border: none;
  251. box-shadow: none;
  252. `;