sortableWidget.tsx 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. import {ComponentProps, useEffect} from 'react';
  2. import {useSortable} from '@dnd-kit/sortable';
  3. import styled from '@emotion/styled';
  4. import {Organization} from 'sentry/types';
  5. import theme from 'sentry/utils/theme';
  6. import withOrganization from 'sentry/utils/withOrganization';
  7. import WidgetCard from 'sentry/views/dashboardsV2/widgetCard';
  8. import {DashboardFilters, Widget} from './types';
  9. import DnDKitWidgetWrapper from './widgetWrapper';
  10. const TABLE_ITEM_LIMIT = 20;
  11. type Props = {
  12. dragId: string;
  13. index: string;
  14. isEditing: boolean;
  15. onDelete: () => void;
  16. onDuplicate: () => void;
  17. onEdit: () => void;
  18. organization: Organization;
  19. widget: Widget;
  20. widgetLimitReached: boolean;
  21. dashboardFilters?: DashboardFilters;
  22. isMobile?: boolean;
  23. isPreview?: boolean;
  24. windowWidth?: number;
  25. };
  26. function SortableWidget(props: Props) {
  27. const {
  28. organization,
  29. widget,
  30. dragId,
  31. isEditing,
  32. widgetLimitReached,
  33. onDelete,
  34. onEdit,
  35. onDuplicate,
  36. isPreview,
  37. isMobile,
  38. windowWidth,
  39. index,
  40. dashboardFilters,
  41. } = props;
  42. const {
  43. attributes,
  44. listeners,
  45. setNodeRef,
  46. transform,
  47. isDragging: currentWidgetDragging,
  48. isSorting,
  49. } = useSortable({
  50. id: dragId,
  51. transition: null,
  52. });
  53. useEffect(() => {
  54. if (!currentWidgetDragging) {
  55. return undefined;
  56. }
  57. document.body.style.cursor = 'grabbing';
  58. return function cleanup() {
  59. document.body.style.cursor = '';
  60. };
  61. }, [currentWidgetDragging]);
  62. let widgetProps: ComponentProps<typeof WidgetCard> = {
  63. widget,
  64. isEditing,
  65. widgetLimitReached,
  66. onDelete,
  67. onEdit,
  68. onDuplicate,
  69. isSorting,
  70. hideToolbar: isSorting,
  71. currentWidgetDragging,
  72. showContextMenu: true,
  73. isPreview,
  74. showWidgetViewerButton: organization.features.includes('widget-viewer-modal'),
  75. index,
  76. dashboardFilters,
  77. };
  78. if (organization.features.includes('dashboard-grid-layout')) {
  79. widgetProps = {
  80. ...widgetProps,
  81. isMobile,
  82. windowWidth,
  83. // TODO(nar): These aren't necessary for supporting RGL
  84. isSorting: false,
  85. currentWidgetDragging: false,
  86. tableItemLimit: TABLE_ITEM_LIMIT,
  87. };
  88. return (
  89. <GridWidgetWrapper>
  90. <WidgetCard {...widgetProps} />
  91. </GridWidgetWrapper>
  92. );
  93. }
  94. const initialStyles: ComponentProps<typeof DnDKitWidgetWrapper>['animate'] = {
  95. zIndex: 'auto',
  96. };
  97. widgetProps = {...widgetProps, draggableProps: {attributes, listeners}};
  98. return (
  99. <DnDKitWidgetWrapper
  100. ref={setNodeRef}
  101. displayType={widget.displayType}
  102. layoutId={dragId}
  103. style={{
  104. // Origin is set to top right-hand corner where the drag handle is placed.
  105. // Otherwise, set the origin to be the top left-hand corner when swapping widgets.
  106. originX: currentWidgetDragging ? 1 : 0,
  107. originY: 0,
  108. boxShadow: currentWidgetDragging ? theme.dropShadowHeavy : 'none',
  109. borderRadius: currentWidgetDragging ? theme.borderRadius : undefined,
  110. }}
  111. animate={
  112. transform
  113. ? {
  114. x: transform.x,
  115. y: transform.y,
  116. scaleX: transform?.scaleX && transform.scaleX <= 1 ? transform.scaleX : 1,
  117. scaleY: transform?.scaleY && transform.scaleY <= 1 ? transform.scaleY : 1,
  118. zIndex: currentWidgetDragging ? theme.zIndex.modal : 'auto',
  119. }
  120. : initialStyles
  121. }
  122. transformTemplate={(___transform, generatedTransform) => {
  123. if (isEditing && !!transform) {
  124. return generatedTransform;
  125. }
  126. return 'none';
  127. }}
  128. transition={{
  129. duration: !currentWidgetDragging ? 0.25 : 0,
  130. easings: {
  131. type: 'spring',
  132. },
  133. }}
  134. >
  135. <WidgetCard {...widgetProps} />
  136. </DnDKitWidgetWrapper>
  137. );
  138. }
  139. export default withOrganization(SortableWidget);
  140. const GridWidgetWrapper = styled('div')`
  141. height: 100%;
  142. `;