diff --git a/src/components/ComponentTreeManager.tsx b/src/components/ComponentTreeManager.tsx index f997474..5190c3a 100644 --- a/src/components/ComponentTreeManager.tsx +++ b/src/components/ComponentTreeManager.tsx @@ -1,24 +1,10 @@ -import { useState } from 'react' +import { useState, useRef } from 'react' import { ComponentTree, ComponentNode } from '@/types/project' -import { Button } from '@/components/ui/button' -import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' -import { Input } from '@/components/ui/input' -import { Label } from '@/components/ui/label' -import { Textarea } from '@/components/ui/textarea' -import { ScrollArea } from '@/components/ui/scroll-area' -import { Badge } from '@/components/ui/badge' -import { - Dialog, - DialogContent, - DialogDescription, - DialogFooter, - DialogHeader, - DialogTitle, -} from '@/components/ui/dialog' -import { Plus, Tree, Trash, Pencil, Copy, FolderOpen } from '@phosphor-icons/react' +import { TreeFormDialog } from '@/components/molecules' +import { TreeListPanel } from '@/components/organisms' import { ComponentTreeBuilder } from '@/components/ComponentTreeBuilder' +import { TreeIcon } from '@/components/atoms' import { toast } from 'sonner' -import { Tabs, TabsContent, TabsList, TabsTrigger } from '@/components/ui/tabs' interface ComponentTreeManagerProps { trees: ComponentTree[] @@ -32,6 +18,7 @@ export function ComponentTreeManager({ trees, onTreesChange }: ComponentTreeMana const [newTreeName, setNewTreeName] = useState('') const [newTreeDescription, setNewTreeDescription] = useState('') const [editingTree, setEditingTree] = useState(null) + const fileInputRef = useRef(null) const selectedTree = trees.find(t => t.id === selectedTreeId) @@ -123,92 +110,88 @@ export function ComponentTreeManager({ trees, onTreesChange }: ComponentTreeMana setEditDialogOpen(true) } + const handleExportJson = () => { + if (!selectedTree) { + toast.error('No tree selected to export') + return + } + + try { + const json = JSON.stringify(selectedTree, null, 2) + const blob = new Blob([json], { type: 'application/json' }) + const url = URL.createObjectURL(blob) + const a = document.createElement('a') + a.href = url + a.download = `${selectedTree.name.toLowerCase().replace(/\s+/g, '-')}-tree.json` + document.body.appendChild(a) + a.click() + document.body.removeChild(a) + URL.revokeObjectURL(url) + toast.success('Component tree exported as JSON') + } catch (error) { + console.error('Export failed:', error) + toast.error('Failed to export component tree') + } + } + + const handleImportJson = () => { + fileInputRef.current?.click() + } + + const handleFileChange = async (event: React.ChangeEvent) => { + const file = event.target.files?.[0] + if (!file) return + + try { + const text = await file.text() + const importedTree = JSON.parse(text) as ComponentTree + + if (!importedTree.id || !importedTree.name || !Array.isArray(importedTree.rootNodes)) { + toast.error('Invalid component tree JSON format') + return + } + + const newTree: ComponentTree = { + ...importedTree, + id: `tree-${Date.now()}`, + createdAt: Date.now(), + updatedAt: Date.now(), + } + + onTreesChange((current) => [...current, newTree]) + setSelectedTreeId(newTree.id) + toast.success(`Component tree "${newTree.name}" imported successfully`) + } catch (error) { + console.error('Import failed:', error) + toast.error('Failed to import component tree. Please check the JSON format.') + } finally { + if (fileInputRef.current) { + fileInputRef.current.value = '' + } + } + } + return (
-
-
-

- - Component Trees -

- -
+ - -
- {trees.map((tree) => ( - setSelectedTreeId(tree.id)} - > - -
-
- {tree.name} - {tree.description && ( - - {tree.description} - - )} -
- - {tree.rootNodes.length} components - -
-
-
-
e.stopPropagation()}> - - - -
-
-
- ))} -
-
- - {trees.length === 0 && ( -
-
- -

No component trees yet

- -
-
- )} -
+ setCreateDialogOpen(true)} + onImportJson={handleImportJson} + onExportJson={handleExportJson} + />
{selectedTree ? ( @@ -219,84 +202,38 @@ export function ComponentTreeManager({ trees, onTreesChange }: ComponentTreeMana ) : (
- +

Select a component tree to edit

)}
- - - - Create Component Tree - - Create a new component tree to organize your UI components - - -
-
- - setNewTreeName(e.target.value)} - placeholder="e.g., Main App, Dashboard, Admin Panel" - /> -
-
- -