"use client"; import React, { useEffect, useState } from "react"; import { Label } from "@/components/ui/label"; import { Input } from "@/components/ui/input"; import { Button } from "@/components/ui/button"; import { Switch } from "@/components/ui/switch"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue, } from "@/components/ui/select"; import { Plus, X, GripVertical, ChevronDown, ChevronRight, Trash2, Move, } from "lucide-react"; import { cn } from "@/lib/utils"; import type { TabItem, TabInlineComponent } from "@/types/screen-management"; import { Collapsible, CollapsibleContent, CollapsibleTrigger, } from "@/components/ui/collapsible"; interface TabsConfigPanelProps { config: any; onChange: (config: any) => void; } export function TabsConfigPanel({ config, onChange }: TabsConfigPanelProps) { const [localTabs, setLocalTabs] = useState(config.tabs || []); const [expandedTabs, setExpandedTabs] = useState>(new Set()); const [isUserEditing, setIsUserEditing] = useState(false); useEffect(() => { if (!isUserEditing) { setLocalTabs(config.tabs || []); } }, [config.tabs, isUserEditing]); // 탭 확장/축소 토글 const toggleTabExpand = (tabId: string) => { setExpandedTabs((prev) => { const newSet = new Set(prev); if (newSet.has(tabId)) { newSet.delete(tabId); } else { newSet.add(tabId); } return newSet; }); }; // 탭 추가 const handleAddTab = () => { const newTab: TabItem = { id: `tab-${Date.now()}`, label: `새 탭 ${localTabs.length + 1}`, order: localTabs.length, disabled: false, components: [], }; const updatedTabs = [...localTabs, newTab]; setLocalTabs(updatedTabs); onChange({ ...config, tabs: updatedTabs }); // 새 탭 자동 확장 setExpandedTabs((prev) => new Set([...prev, newTab.id])); }; // 탭 제거 const handleRemoveTab = (tabId: string) => { const updatedTabs = localTabs.filter((tab) => tab.id !== tabId); setLocalTabs(updatedTabs); onChange({ ...config, tabs: updatedTabs }); }; // 탭 라벨 변경 (입력 중) const handleLabelChange = (tabId: string, label: string) => { setIsUserEditing(true); const updatedTabs = localTabs.map((tab) => tab.id === tabId ? { ...tab, label } : tab ); setLocalTabs(updatedTabs); }; // 탭 라벨 변경 완료 const handleLabelBlur = () => { setIsUserEditing(false); onChange({ ...config, tabs: localTabs }); }; // 탭 비활성화 토글 const handleDisabledToggle = (tabId: string, disabled: boolean) => { const updatedTabs = localTabs.map((tab) => tab.id === tabId ? { ...tab, disabled } : tab ); setLocalTabs(updatedTabs); onChange({ ...config, tabs: updatedTabs }); }; // 탭 순서 변경 const handleMoveTab = (tabId: string, direction: "up" | "down") => { const index = localTabs.findIndex((tab) => tab.id === tabId); if ( (direction === "up" && index === 0) || (direction === "down" && index === localTabs.length - 1) ) { return; } const newTabs = [...localTabs]; const targetIndex = direction === "up" ? index - 1 : index + 1; [newTabs[index], newTabs[targetIndex]] = [ newTabs[targetIndex], newTabs[index], ]; const updatedTabs = newTabs.map((tab, idx) => ({ ...tab, order: idx })); setLocalTabs(updatedTabs); onChange({ ...config, tabs: updatedTabs }); }; // 컴포넌트 제거 const handleRemoveComponent = (tabId: string, componentId: string) => { const updatedTabs = localTabs.map((tab) => { if (tab.id === tabId) { return { ...tab, components: (tab.components || []).filter( (comp) => comp.id !== componentId ), }; } return tab; }); setLocalTabs(updatedTabs); onChange({ ...config, tabs: updatedTabs }); }; // 컴포넌트 위치 변경 const handleComponentPositionChange = ( tabId: string, componentId: string, field: "x" | "y" | "width" | "height", value: number ) => { const updatedTabs = localTabs.map((tab) => { if (tab.id === tabId) { return { ...tab, components: (tab.components || []).map((comp) => { if (comp.id === componentId) { if (field === "x" || field === "y") { return { ...comp, position: { ...comp.position, [field]: value }, }; } else { return { ...comp, size: { ...comp.size, [field]: value }, }; } } return comp; }), }; } return tab; }); setLocalTabs(updatedTabs); onChange({ ...config, tabs: updatedTabs }); }; return (

탭 설정

{/* 탭 방향 */}
{/* 탭 스타일 */}
{/* 선택 상태 유지 */}

페이지 새로고침 후에도 선택한 탭이 유지됩니다

onChange({ ...config, persistSelection: checked }) } />
{/* 탭 닫기 버튼 */}

각 탭에 닫기 버튼을 표시합니다

onChange({ ...config, allowCloseable: checked }) } />

탭 목록

{localTabs.length === 0 ? (

탭이 없습니다

탭 추가 버튼을 클릭하여 새 탭을 생성하세요

) : (
{localTabs.map((tab, index) => ( toggleTabExpand(tab.id)} >
{/* 탭 헤더 */}
{tab.label || `탭 ${index + 1}`} {tab.components && tab.components.length > 0 && ( {tab.components.length}개 컴포넌트 )}
{/* 탭 컨텐츠 */}
{/* 탭 라벨 */}
handleLabelChange(tab.id, e.target.value) } onBlur={handleLabelBlur} placeholder="탭 이름" className="h-8 text-xs sm:h-9 sm:text-sm" />
{/* 비활성화 */}
handleDisabledToggle(tab.id, checked) } />
{/* 컴포넌트 목록 */}
{!tab.components || tab.components.length === 0 ? (

디자인 화면에서 컴포넌트를 드래그하여 추가하세요

) : (
{tab.components.map((comp: TabInlineComponent) => (

{comp.label || comp.componentType}

{comp.componentType} | 위치: ({comp.position?.x || 0},{" "} {comp.position?.y || 0}) | 크기: {comp.size?.width || 0}x {comp.size?.height || 0}

))}
)}
))}
)}
{/* 사용 안내 */}

컴포넌트 추가 방법

  1. 디자인 화면에서 탭을 선택합니다
  2. 좌측 패널에서 원하는 컴포넌트를 드래그합니다
  3. 선택한 탭 영역에 드롭하여 배치합니다
  4. 컴포넌트를 드래그하여 위치를 조정합니다
); }