11import React , { useCallback } from "react" ;
22import styled from "@emotion/styled" ;
33import {
4- FolderCard ,
5- SceneCard ,
4+ FolderCard as _FolderCard ,
5+ SceneCard as _SceneCard ,
66 SectionHeaderAction ,
77 SectionHeader ,
88 DashboardItemCardProps ,
@@ -15,7 +15,12 @@ import {
1515 MenuItem ,
1616} from "@editor-ui/context-menu" ;
1717import * as Collapsible from "@radix-ui/react-collapsible" ;
18- import { useDashboard , DashboardItem } from "../core" ;
18+ import {
19+ useDashboard ,
20+ DashboardItem ,
21+ DashboardFolderItem ,
22+ SceneItem ,
23+ } from "../core" ;
1924
2025export function Dashboard ( ) {
2126 const {
@@ -107,12 +112,6 @@ export function Dashboard() {
107112 ) ;
108113}
109114
110- interface SceneCardMeta {
111- id : string ;
112- name : string ;
113- $type : unknown ;
114- }
115-
116115function RootDirectory ( {
117116 label,
118117 contents,
@@ -222,16 +221,31 @@ type DndMetaItem<T = object> = T & {
222221function DashboardItemCard (
223222 props : DashboardItem &
224223 Omit < DashboardItemCardProps , "label" | "preview" | "icon" >
224+ ) {
225+ switch ( props . $type ) {
226+ case "frame-scene" :
227+ case "component" : {
228+ return < SceneCard { ...props } /> ;
229+ }
230+ case "folder" : {
231+ return < FolderCard { ...props } /> ;
232+ }
233+ default : {
234+ throw new Error ( `Unknown item type ${ props . $type } ` ) ;
235+ }
236+ }
237+ }
238+
239+ function SceneCard (
240+ props : SceneItem & Omit < DashboardItemCardProps , "label" | "preview" | "icon" >
225241) {
226242 const [ { isActive } , drop ] = useDrop ( ( ) => ( {
227- accept : "frame- scene" , // todo
243+ accept : "scene" ,
228244 collect : ( monitor ) => ( {
229245 isActive : monitor . canDrop ( ) && monitor . isOver ( ) ,
230246 } ) ,
231247 canDrop ( item : DndMetaItem , monitor ) {
232- if ( item . $type === props . $type ) {
233- return item . id !== props . id ;
234- }
248+ return item . id !== props . id ;
235249 } ,
236250 drop ( item , monitor ) {
237251 console . log ( "drop" , item , monitor ) ;
@@ -240,22 +254,58 @@ function DashboardItemCard(
240254 } ) ) ;
241255
242256 const [ { opacity } , drag ] = useDrag ( ( ) => {
243- let item : unknown = props ;
244- switch ( props . $type ) {
245- case "frame-scene" : {
246- item = props . scene ;
247- Object . assign ( item , { $type : props . $type } ) ;
248- break ;
249- }
250- case "folder" : {
251- item = props ;
252- break ;
253- }
254- }
257+ return {
258+ type : "scene" ,
259+ item : props . scene ,
260+ collect : ( monitor ) => ( {
261+ opacity : monitor . isDragging ( ) ? 0.5 : 1 ,
262+ } ) ,
263+ } ;
264+ } , [ ] ) ;
265+
266+ function attachRef ( el ) {
267+ drag ( el ) ;
268+ drop ( el ) ;
269+ }
270+
271+ const defaultprops = {
272+ isOver : isActive ,
273+ style : { opacity } ,
274+ } ;
275+
276+ return (
277+ < _SceneCard
278+ // @ts -ignore
279+ scene = { props . scene as any }
280+ ref = { attachRef }
281+ { ...defaultprops }
282+ { ...props }
283+ />
284+ ) ;
285+ }
255286
287+ function FolderCard (
288+ props : DashboardFolderItem &
289+ Omit < DashboardItemCardProps , "label" | "preview" | "icon" >
290+ ) {
291+ const [ { isActive } , drop ] = useDrop ( ( ) => ( {
292+ accept : [ "scene" , "folder" ] ,
293+ collect : ( monitor ) => ( {
294+ isActive : monitor . canDrop ( ) && monitor . isOver ( ) ,
295+ } ) ,
296+ canDrop ( item : DndMetaItem , monitor ) {
297+ return item . id !== props . id ;
298+ } ,
299+ drop ( item , monitor ) {
300+ console . log ( "drop" , item , monitor ) ;
301+ // todo:
302+ } ,
303+ } ) ) ;
304+
305+ const [ { opacity } , drag ] = useDrag ( ( ) => {
256306 return {
257307 type : props . $type ,
258- item : item ,
308+ item : props ,
259309 collect : ( monitor ) => ( {
260310 opacity : monitor . isDragging ( ) ? 0.5 : 1 ,
261311 } ) ,
@@ -272,44 +322,17 @@ function DashboardItemCard(
272322 style : { opacity } ,
273323 } ;
274324
275- switch ( props . $type ) {
276- case "frame-scene" : {
277- return (
278- < SceneCard
279- scene = { props . scene }
280- ref = { attachRef }
281- { ...defaultprops }
282- { ...props }
283- />
284- ) ;
285- }
286- case "component" : {
287- return (
288- < SceneCard
289- scene = { props as any } // todo
290- ref = { attachRef }
291- { ...defaultprops }
292- { ...props }
293- />
294- ) ;
295- }
296- case "folder" : {
297- return (
298- < FolderCard
299- ref = { attachRef }
300- id = { props . id }
301- path = { props . path }
302- name = { props . name }
303- contents = { props . contents }
304- { ...defaultprops }
305- { ...props }
306- />
307- ) ;
308- }
309- default : {
310- throw new Error ( `Unknown item type ${ props . $type } ` ) ;
311- }
312- }
325+ return (
326+ < _FolderCard
327+ ref = { attachRef }
328+ id = { props . id }
329+ path = { props . path }
330+ name = { props . name }
331+ contents = { props . contents }
332+ { ...defaultprops }
333+ { ...props }
334+ />
335+ ) ;
313336}
314337
315338const SceneGrid = styled . div `
0 commit comments