Skip to content

Commit 81e5280

Browse files
committed
🔨 Switch TemplateItem to use useOvermind
1 parent a4513fb commit 81e5280

File tree

3 files changed

+80
-74
lines changed

3 files changed

+80
-74
lines changed
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
import React, { FunctionComponent } from 'react';
2+
import {
3+
DropTarget,
4+
DropTargetCollector,
5+
DropTargetConnector,
6+
DropTargetMonitor,
7+
DropTargetSpec,
8+
} from 'react-dnd';
9+
10+
import { MAKE_TEMPLATE_DROP_KEY } from '../Content/SandboxCard';
11+
12+
import { Item } from './Item';
13+
14+
import TemplateIcon from '-!svg-react-loader!@codesandbox/common/lib/icons/template.svg';
15+
16+
type ComponentProps = {
17+
currentPath: string;
18+
teamId?: string;
19+
};
20+
type Props = ComponentProps & CollectedProps;
21+
const TemplateItemComponent: FunctionComponent<Props> = ({
22+
canDrop,
23+
connectDropTarget,
24+
currentPath,
25+
isOver,
26+
teamId,
27+
}) => {
28+
const path = teamId
29+
? `/dashboard/teams/${teamId}/templates`
30+
: `/dashboard/templates`;
31+
32+
return connectDropTarget(
33+
<Item
34+
active={currentPath === path}
35+
Icon={TemplateIcon}
36+
name={teamId ? 'Team Templates' : 'My Templates'}
37+
path={path}
38+
style={isOver && canDrop ? { backgroundColor: 'rgba(0, 0, 0, 0.3)' } : {}}
39+
/>
40+
);
41+
};
42+
43+
const entryTarget: DropTargetSpec<ComponentProps> = {
44+
canDrop: (_props, { getItem }) => getItem() !== null,
45+
drop: ({ teamId }, { isOver }) => {
46+
// Check if only child is selected:
47+
if (!isOver({ shallow: true })) {
48+
return {};
49+
}
50+
51+
// Used in SandboxCard
52+
return {
53+
[MAKE_TEMPLATE_DROP_KEY]: true,
54+
teamId,
55+
};
56+
},
57+
};
58+
59+
const collectTarget: DropTargetCollector<CollectedProps> = (
60+
{ dropTarget },
61+
{ canDrop, isOver }
62+
) => ({
63+
canDrop: canDrop(),
64+
// Call this function inside render()
65+
// to let React DnD handle the drag events:
66+
connectDropTarget: dropTarget(),
67+
// You can ask the monitor about the current drag state:
68+
isOver: isOver({ shallow: true }),
69+
});
70+
71+
type CollectedProps = {
72+
canDrop: ReturnType<DropTargetMonitor['canDrop']>;
73+
connectDropTarget: ReturnType<DropTargetConnector['dropTarget']>;
74+
isOver: ReturnType<DropTargetMonitor['isOver']>;
75+
};
76+
export const TemplateItem = DropTarget<ComponentProps, CollectedProps>(
77+
['SANDBOX'],
78+
entryTarget,
79+
collectTarget
80+
)(TemplateItemComponent);

‎packages/app/src/app/pages/Dashboard/Sidebar/TemplateItem/TemplateItem.tsx‎

Lines changed: 0 additions & 73 deletions
This file was deleted.

‎packages/app/src/app/pages/Dashboard/Sidebar/TemplateItem/index.tsx‎

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)