sortableWidget.tsx 3.7 KB

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