|
| 1 | +import React from 'react'; |
| 2 | +import { DropTarget } from 'react-dnd'; |
| 3 | +import { withRouter, RouteComponentProps } from 'react-router-dom'; |
| 4 | +// @ts-ignore |
| 5 | +import TemplateIcon from '-!svg-react-loader!@codesandbox/common/lib/icons/template.svg'; |
| 6 | +import { Item } from '../Item'; |
| 7 | +import { MAKE_TEMPLATE_DROP_KEY } from '../../Content/SandboxCard'; |
| 8 | + |
| 9 | +interface ITemplateItemProps { |
| 10 | + currentPath: string; |
| 11 | + teamId?: string; |
| 12 | + canDrop?: boolean; |
| 13 | + isOver?: boolean; |
| 14 | + connectDropTarget?: any; |
| 15 | +} |
| 16 | + |
| 17 | +const TemplateItemComponent: React.FC< |
| 18 | + ITemplateItemProps & RouteComponentProps |
| 19 | +> = ({ currentPath, isOver, canDrop, connectDropTarget, teamId }) => { |
| 20 | + const url = teamId |
| 21 | + ? `/dashboard/teams/${teamId}/templates` |
| 22 | + : `/dashboard/templates`; |
| 23 | + |
| 24 | + return connectDropTarget( |
| 25 | + <div> |
| 26 | + <Item |
| 27 | + active={currentPath === url} |
| 28 | + path={url} |
| 29 | + Icon={TemplateIcon} |
| 30 | + name={teamId ? 'Team Templates' : 'My Templates'} |
| 31 | + style={ |
| 32 | + isOver && canDrop ? { backgroundColor: 'rgba(0, 0, 0, 0.3)' } : {} |
| 33 | + } |
| 34 | + /> |
| 35 | + </div> |
| 36 | + ); |
| 37 | +}; |
| 38 | + |
| 39 | +export const entryTarget = { |
| 40 | + drop: (props, monitor) => { |
| 41 | + if (monitor == null) return {}; |
| 42 | + |
| 43 | + // Check if only child is selected: |
| 44 | + if (!monitor.isOver({ shallow: true })) return {}; |
| 45 | + |
| 46 | + // Used in SandboxCard |
| 47 | + return { [MAKE_TEMPLATE_DROP_KEY]: true, teamId: props.teamId }; |
| 48 | + }, |
| 49 | + |
| 50 | + canDrop: (props, monitor) => { |
| 51 | + if (monitor == null) return false; |
| 52 | + const source = monitor.getItem(); |
| 53 | + if (source == null) return false; |
| 54 | + |
| 55 | + return !props.removedAt; |
| 56 | + }, |
| 57 | +}; |
| 58 | + |
| 59 | +export function collectTarget(connectMonitor, monitor) { |
| 60 | + return { |
| 61 | + // Call this function inside render() |
| 62 | + // to let React DnD handle the drag events: |
| 63 | + connectDropTarget: connectMonitor.dropTarget(), |
| 64 | + // You can ask the monitor about the current drag state: |
| 65 | + isOver: monitor.isOver({ shallow: true }), |
| 66 | + canDrop: monitor.canDrop(), |
| 67 | + itemType: monitor.getItemType(), |
| 68 | + }; |
| 69 | +} |
| 70 | + |
| 71 | +export const TemplateItem = DropTarget(['SANDBOX'], entryTarget, collectTarget)( |
| 72 | + withRouter(TemplateItemComponent) |
| 73 | +); |
0 commit comments