Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

dnd folder (#72264)

Nick Fitzpatrick59246fc0936d
diff --git a/frontend/src/metabase-types/api/collection.ts b/frontend/src/metabase-types/api/collection.ts
index cd6858f96a3..d6cf03e7237 100644
--- a/frontend/src/metabase-types/api/collection.ts
+++ b/frontend/src/metabase-types/api/collection.ts
@@ -142,7 +142,7 @@ export interface CollectionItem {
isArchived: boolean,
opts?: Record<string, unknown>,
) => Promise<void>;
- setPinned?: (isPinned: boolean) => void;
+ setPinned?: (isPinned: number | boolean) => void;
setCollection?: (
collection: Pick<Collection, "id"> | Pick<Dashboard, "id">,
) => void;
diff --git a/frontend/src/metabase/collections/components/PinnedItemCard/PinnedItemCard.unit.spec.tsx b/frontend/src/metabase/collections/components/PinnedItemCard/PinnedItemCard.unit.spec.tsx
index f7f1591f2f7..3ecb098d906 100644
--- a/frontend/src/metabase/collections/components/PinnedItemCard/PinnedItemCard.unit.spec.tsx
+++ b/frontend/src/metabase/collections/components/PinnedItemCard/PinnedItemCard.unit.spec.tsx
@@ -58,7 +58,7 @@ const getCollectionItem = ({
description?: string;
collection_position?: number;
setArchived?: (isArchived: boolean) => Promise<void>;
- setPinned?: (isPinned: boolean) => void;
+ setPinned?: (isPinned: boolean | number) => void;
} = {}): CollectionItem & { description: string } => {
return createMockCollectionItem({
...rest,
diff --git a/frontend/src/metabase/common/components/dnd/CollectionDropTarget.jsx b/frontend/src/metabase/common/components/dnd/CollectionDropTarget.tsx
similarity index 68%
rename from frontend/src/metabase/common/components/dnd/CollectionDropTarget.jsx
rename to frontend/src/metabase/common/components/dnd/CollectionDropTarget.tsx
index 2af72a2d5fc..6977f838d5b 100644
--- a/frontend/src/metabase/common/components/dnd/CollectionDropTarget.jsx
+++ b/frontend/src/metabase/common/components/dnd/CollectionDropTarget.tsx
@@ -1,23 +1,29 @@
+import type { DropTargetMonitor } from "react-dnd";
import { DropTarget } from "react-dnd";
import {
canonicalCollectionId,
isRootTrashCollection,
} from "metabase/collections/utils";
+import type { Collection, CollectionItem } from "metabase-types/api";
import { DropArea } from "./DropArea";
import { MoveableDragTypes } from ".";
+interface CollectionDropTargetOwnProps {
+ collection: Collection;
+}
+
export const CollectionDropTarget = DropTarget(
MoveableDragTypes,
{
- drop(props) {
+ drop(props: CollectionDropTargetOwnProps) {
return { collection: props.collection };
},
- canDrop(props, monitor) {
+ canDrop(props: CollectionDropTargetOwnProps, monitor: DropTargetMonitor) {
const { collection } = props;
- const { item } = monitor.getItem();
+ const { item } = monitor.getItem() as { item: CollectionItem };
if (
!isRootTrashCollection(collection) &&
collection.can_write === false
@@ -41,4 +47,5 @@ export const CollectionDropTarget = DropTarget(
hovered: monitor.isOver() && monitor.canDrop(),
connectDropTarget: connect.dropTarget(),
}),
-)(DropArea);
+ // react-dnd v7 HOC types can't express the own/collected props split
+)(DropArea as any);
diff --git a/frontend/src/metabase/common/components/dnd/DropArea.jsx b/frontend/src/metabase/common/components/dnd/DropArea.tsx
similarity index 62%
rename from frontend/src/metabase/common/components/dnd/DropArea.jsx
rename to frontend/src/metabase/common/components/dnd/DropArea.tsx
index 637329b3826..046d1451bff 100644
--- a/frontend/src/metabase/common/components/dnd/DropArea.jsx
+++ b/frontend/src/metabase/common/components/dnd/DropArea.tsx
@@ -1,9 +1,18 @@
-/* eslint-disable react/prop-types */
import cx from "classnames";
import { Component } from "react";
+import type { ConnectDropTarget } from "react-dnd";
import CS from "metabase/css/core/index.css";
+interface DropTargetBackgroundAndBorderProps {
+ highlighted: boolean;
+ margin?: number;
+ marginLeft?: number;
+ marginRight?: number;
+ marginTop?: number;
+ marginBottom?: number;
+}
+
const DropTargetBackgroundAndBorder = ({
highlighted,
margin = 0,
@@ -11,7 +20,7 @@ const DropTargetBackgroundAndBorder = ({
marginRight = margin,
marginTop = margin,
marginBottom = margin,
-}) => (
+}: DropTargetBackgroundAndBorderProps) => (
<div
className={cx(CS.absolute, CS.rounded, {
[CS.pointerEventsNone]: !highlighted,
@@ -27,15 +36,37 @@ const DropTargetBackgroundAndBorder = ({
/>
);
-export class DropArea extends Component {
- constructor(props) {
+interface DropAreaProps {
+ connectDropTarget: ConnectDropTarget;
+ highlighted: boolean;
+ hovered: boolean;
+ hideUntilDrag?: boolean;
+ children:
+ | React.ReactNode
+ | ((props: Record<string, unknown>) => React.ReactNode);
+ className?: string;
+ style?: React.CSSProperties;
+ enableDropTargetBackground?: boolean;
+ margin: number;
+ marginLeft: number;
+ marginRight: number;
+ marginTop: number;
+ marginBottom: number;
+}
+
+interface DropAreaState {
+ show: boolean;
+}
+
+export class DropArea extends Component<DropAreaProps, DropAreaState> {
+ constructor(props: DropAreaProps) {
super(props);
this.state = {
show: this._shouldShow(props),
};
}
- UNSAFE_componentWillReceiveProps(nextProps) {
+ UNSAFE_componentWillReceiveProps(nextProps: DropAreaProps) {
// need to delay showing/hiding due to Chrome bug where "dragend" is triggered
// immediately if the content shifts during "dragstart"
// https://github.com/react-dnd/react-dnd/issues/477
@@ -44,7 +75,7 @@ export class DropArea extends Component {
}
}
- _shouldShow(props) {
+ _shouldShow(props: DropAreaProps) {
return !props.hideUntilDrag || props.highlighted;
}
diff --git a/frontend/src/metabase/common/components/dnd/ItemDragSource.jsx b/frontend/src/metabase/common/components/dnd/ItemDragSource.tsx
similarity index 59%
rename from frontend/src/metabase/common/components/dnd/ItemDragSource.jsx
rename to frontend/src/metabase/common/components/dnd/ItemDragSource.tsx
index 17f1476bb70..9d4e1db1c20 100644
--- a/frontend/src/metabase/common/components/dnd/ItemDragSource.jsx
+++ b/frontend/src/metabase/common/components/dnd/ItemDragSource.tsx
@@ -1,15 +1,29 @@
-/* eslint-disable react/prop-types */
+import type { ReactElement, ReactNode } from "react";
import { Component } from "react";
+import type {
+ ConnectDragPreview,
+ ConnectDragSource,
+ DragSourceMonitor,
+} from "react-dnd";
import { DragSource } from "react-dnd";
import { getEmptyImage } from "react-dnd-html5-backend";
import { getErrorMessage } from "metabase/api/utils";
import { isRootTrashCollection } from "metabase/collections/utils";
import { useToast } from "metabase/common/hooks";
+import type { Collection, CollectionItem } from "metabase-types/api";
import { dragTypeForItem } from ".";
-class ItemDragSourceInner extends Component {
+interface ItemDragSourceInnerProps {
+ connectDragSource: ConnectDragSource;
+ connectDragPreview: ConnectDragPreview;
+ isDragging: boolean;
+ children: ReactElement | ((props: Record<string, unknown>) => ReactElement);
+ [key: string]: unknown;
+}
+
+class ItemDragSourceInner extends Component<ItemDragSourceInnerProps> {
componentDidMount() {
// Use empty image as a drag preview so browsers don't draw it
// and we can draw whatever we want on the custom drag layer instead.
@@ -32,10 +46,20 @@ class ItemDragSourceInner extends Component {
}
}
+interface DragSourceOwnProps {
+ item: CollectionItem;
+ isSelected?: boolean;
+ selected?: CollectionItem[];
+ collection?: Collection;
+ onDrop?: () => void;
+ onMoveError?: (error: unknown) => void;
+ children?: ReactNode | ((props: Record<string, unknown>) => ReactNode);
+}
+
const DragSourceComponent = DragSource(
- (props) => dragTypeForItem(props.item),
+ (props: DragSourceOwnProps) => dragTypeForItem(props.item),
{
- canDrag({ isSelected, selected, collection, item }, monitor) {
+ canDrag({ isSelected, selected, collection }: DragSourceOwnProps) {
// can't drag if can't write the parent collection
if (
collection &&
@@ -49,15 +73,21 @@ const DragSourceComponent = DragSource(
return isSelected || numSelected === 0;
},
- beginDrag(props, monitor, component) {
+ beginDrag(props: DragSourceOwnProps) {
return { item: props.item };
},
- async endDrag({ selected, onDrop, onMoveError }, monitor, component) {
+ async endDrag(
+ { selected, onDrop, onMoveError }: DragSourceOwnProps,
+ monitor: DragSourceMonitor,
+ ) {
if (!monitor.didDrop()) {
return;
}
- const { item } = monitor.getItem();
- const { collection, pinIndex } = monitor.getDropResult();
+ const { item } = monitor.getItem() as { item: CollectionItem };
+ const { collection, pinIndex } = monitor.getDropResult() as {
+ collection?: Collection;
+ pinIndex?: number;
+ };
if (item) {
const items = selected && selected.length > 0 ? selected : [item];
try {
@@ -71,7 +101,7 @@ const DragSourceComponent = DragSource(
);
}
- onDrop && onDrop();
+ onDrop?.();
} catch (e) {
onMoveError?.(e);
}
@@ -83,11 +113,21 @@ const DragSourceComponent = DragSource(
connectDragPreview: connect.dragPreview(),
isDragging: monitor.isDragging(),
}),
-)(ItemDragSourceInner);
+ // react-dnd v7 HOC types can't express the own/collected props split
+)(ItemDragSourceInner as any);
+
+interface ItemDragSourceProps {
+ item: CollectionItem;
+ isSelected?: boolean;
+ selected?: CollectionItem[];
+ collection?: Collection;
+ onDrop?: () => void;
+ children?: ReactNode | ((props: Record<string, unknown>) => ReactNode);
+}
-export function ItemDragSource(props) {
+export function ItemDragSource(props: ItemDragSourceProps) {
const [sendToast] = useToast();
- const onMoveError = (error) =>
+ const onMoveError = (error: unknown) =>
sendToast({
message: getErrorMessage(error),
icon: "warning_triangle_filled",
diff --git a/frontend/src/metabase/common/components/dnd/ItemsDragLayer.jsx b/frontend/src/metabase/common/components/dnd/ItemsDragLayer.tsx
similarity index 69%
rename from frontend/src/metabase/common/components/dnd/ItemsDragLayer.jsx
rename to frontend/src/metabase/common/components/dnd/ItemsDragLayer.tsx
index 08a0dbd2c67..730feaa6497 100644
--- a/frontend/src/metabase/common/components/dnd/ItemsDragLayer.jsx
+++ b/frontend/src/metabase/common/components/dnd/ItemsDragLayer.tsx
@@ -1,13 +1,25 @@
-/* eslint-disable react/prop-types */
import { Component } from "react";
-import { DragLayer } from "react-dnd";
+import { DragLayer, type XYCoord } from "react-dnd";
import _ from "underscore";
+import type { CollectionContentTableColumnsMap } from "metabase/collections/components/CollectionContent/constants";
import PinnedItemCard from "metabase/collections/components/PinnedItemCard";
import { BaseItemsTable } from "metabase/common/components/ItemsTable/BaseItemsTable";
+import type { ItemRendererProps } from "metabase/common/components/ItemsTable/DefaultItemRenderer";
import { Box, Portal } from "metabase/ui";
+import type { Collection, CollectionItem } from "metabase-types/api";
-class ItemsDragLayerInner extends Component {
+interface ItemsDragLayerInnerProps {
+ isDragging: boolean;
+ currentOffset: XYCoord | null;
+ selectedItems: CollectionItem[];
+ pinnedItems: CollectionItem[];
+ item: { item: CollectionItem } | null;
+ collection: Collection;
+ visibleColumnsMap: CollectionContentTableColumnsMap;
+}
+
+class ItemsDragLayerInner extends Component<ItemsDragLayerInnerProps> {
render() {
const {
isDragging,
@@ -21,7 +33,7 @@ class ItemsDragLayerInner extends Component {
if (!isDragging || !currentOffset) {
return null;
}
- const items = selectedItems.length > 0 ? selectedItems : [item.item];
+ const items = selectedItems.length > 0 ? selectedItems : [item!.item];
const x = currentOffset.x + window.scrollX;
const y = currentOffset.y + window.scrollY;
return (
@@ -39,7 +51,7 @@ class ItemsDragLayerInner extends Component {
>
<DraggedItems
items={items}
- draggedItem={item.item}
+ draggedItem={item!.item}
pinnedItems={pinnedItems}
collection={collection}
visibleColumnsMap={visibleColumnsMap}
@@ -50,16 +62,25 @@ class ItemsDragLayerInner extends Component {
}
}
-export const ItemsDragLayer = DragLayer((monitor, props) => ({
+export const ItemsDragLayer = DragLayer((monitor) => ({
item: monitor.getItem(),
// itemType: monitor.getItemType(),
initialOffset: monitor.getInitialSourceClientOffset(),
currentOffset: monitor.getSourceClientOffset(),
isDragging: monitor.isDragging(),
-}))(ItemsDragLayerInner);
+ // react-dnd v7 HOC types can't express the own/collected props split
+}))(ItemsDragLayerInner as any);
+
+interface DraggedItemsProps {
+ items: CollectionItem[];
+ draggedItem: CollectionItem;
+ pinnedItems: CollectionItem[];
+ collection: Collection;
+ visibleColumnsMap: CollectionContentTableColumnsMap;
+}
-class DraggedItems extends Component {
- shouldComponentUpdate(nextProps) {
+class DraggedItems extends Component<DraggedItemsProps> {
+ shouldComponentUpdate(nextProps: DraggedItemsProps) {
// necessary for decent drag performance
return (
nextProps.items.length !== this.props.items.length ||
@@ -68,7 +89,7 @@ class DraggedItems extends Component {
);
}
- checkIsPinned = (item) => {
+ checkIsPinned = (item: CollectionItem) => {
const { pinnedItems } = this.props;
const index = pinnedItems.findIndex(
(i) => i.model === item.model && i.id === item.id,
@@ -76,7 +97,7 @@ class DraggedItems extends Component {
return index >= 0;
};
- renderItem = ({ item, ...itemProps }) => {
+ renderItem = ({ item, ...itemProps }: ItemRendererProps) => {
const isPinned = this.checkIsPinned(item);
const key = `${item.model}-${item.id}`;
@@ -100,7 +121,6 @@ class DraggedItems extends Component {
item={item}
isPinned={false}
draggable={false}
- hasBottomBorder={false}
/>
);
};
@@ -113,7 +133,7 @@ class DraggedItems extends Component {
<div
style={{
position: "absolute",
- transform: index > 0 ? `translate(0px, ${-index * 72}px)` : null,
+ transform: index > 0 ? `translate(0px, ${-index * 72}px)` : undefined,
}}
>
<BaseItemsTable
diff --git a/frontend/src/metabase/common/components/dnd/PinDropTarget.jsx b/frontend/src/metabase/common/components/dnd/PinDropTarget.tsx
similarity index 64%
rename from frontend/src/metabase/common/components/dnd/PinDropTarget.jsx
rename to frontend/src/metabase/common/components/dnd/PinDropTarget.tsx
index 296cdcd6047..761d548c176 100644
--- a/frontend/src/metabase/common/components/dnd/PinDropTarget.jsx
+++ b/frontend/src/metabase/common/components/dnd/PinDropTarget.tsx
@@ -1,22 +1,29 @@
-import PropTypes from "prop-types";
+import type { DropTargetMonitor } from "react-dnd";
import { DropTarget } from "react-dnd";
import { isItemPinned } from "metabase/collections/utils";
+import type { CollectionItem } from "metabase-types/api";
import { DropArea } from "./DropArea";
import { PinnableDragTypes } from ".";
+interface PinDropTargetOwnProps {
+ variant: "pin" | "unpin";
+ pinIndex: number;
+ noDrop: boolean;
+}
+
export const PinDropTarget = DropTarget(
PinnableDragTypes,
{
- drop(props, monitor, component) {
+ drop(props: PinDropTargetOwnProps) {
if (!props.noDrop) {
return { pinIndex: props.pinIndex };
}
},
- canDrop(props, monitor) {
- const { item } = monitor.getItem();
+ canDrop(props: PinDropTargetOwnProps, monitor: DropTargetMonitor) {
+ const { item } = monitor.getItem() as { item: CollectionItem };
const { variant } = props;
// NOTE: not necessary to check collection permission here since we
// enforce it when beginning to drag and item within the same collection
@@ -34,8 +41,5 @@ export const PinDropTarget = DropTarget(
hovered: monitor.isOver() && monitor.canDrop(),
connectDropTarget: connect.dropTarget(),
}),
-)(DropArea);
-
-PinDropTarget.propTypes = {
- variant: PropTypes.oneOf(["pin", "unpin"]).isRequired,
-};
+ // react-dnd v7 HOC types can't express the own/collected props split
+)(DropArea as any);
diff --git a/frontend/src/metabase/common/components/dnd/PinnedItemSortDropTarget.jsx b/frontend/src/metabase/common/components/dnd/PinnedItemSortDropTarget.tsx
similarity index 54%
rename from frontend/src/metabase/common/components/dnd/PinnedItemSortDropTarget.jsx
rename to frontend/src/metabase/common/components/dnd/PinnedItemSortDropTarget.tsx
index 36bc1af8db1..c996001042e 100644
--- a/frontend/src/metabase/common/components/dnd/PinnedItemSortDropTarget.jsx
+++ b/frontend/src/metabase/common/components/dnd/PinnedItemSortDropTarget.tsx
@@ -1,22 +1,34 @@
-import PropTypes from "prop-types";
+import type { DropTargetMonitor } from "react-dnd";
import { DropTarget } from "react-dnd";
import { isItemPinned } from "metabase/collections/utils";
+import type { CollectionItem } from "metabase-types/api";
import { DropArea } from "./DropArea";
import { PinnableDragTypes } from ".";
+interface PinnedItemSortDropTargetOwnProps {
+ isFrontTarget: boolean;
+ isBackTarget: boolean;
+ itemModel: string;
+ pinIndex: number;
+ noDrop: boolean;
+}
+
export const PinnedItemSortDropTarget = DropTarget(
PinnableDragTypes,
{
- drop(props, monitor, component) {
+ drop(props: PinnedItemSortDropTargetOwnProps) {
if (!props.noDrop) {
return { pinIndex: props.pinIndex };
}
},
- canDrop(props, monitor) {
- const { item } = monitor.getItem();
+ canDrop(
+ props: PinnedItemSortDropTargetOwnProps,
+ monitor: DropTargetMonitor,
+ ) {
+ const { item } = monitor.getItem() as { item: CollectionItem };
const { isFrontTarget, isBackTarget, itemModel, pinIndex } = props;
// NOTE: not necessary to check collection permission here since we
@@ -30,10 +42,16 @@ export const PinnedItemSortDropTarget = DropTarget(
}
if (isFrontTarget) {
- const isInFrontOfItem = pinIndex < item.collection_position;
+ const isInFrontOfItem =
+ pinIndex != null &&
+ item.collection_position != null &&
+ pinIndex < item.collection_position;
return isInFrontOfItem;
} else if (isBackTarget) {
- const isBehindItem = pinIndex > item.collection_position;
+ const isBehindItem =
+ pinIndex != null &&
+ item.collection_position != null &&
+ pinIndex > item.collection_position;
return isBehindItem;
}
@@ -45,11 +63,5 @@ export const PinnedItemSortDropTarget = DropTarget(
hovered: monitor.isOver() && monitor.canDrop(),
connectDropTarget: connect.dropTarget(),
}),
-)(DropArea);
-
-PinnedItemSortDropTarget.propTypes = {
- isFrontTarget: PropTypes.bool,
- isBackTarget: PropTypes.bool,
- itemModel: PropTypes.string,
- pinIndex: PropTypes.number,
-};
+ // react-dnd v7 HOC types can't express the own/collected props split
+)(DropArea as any);