"use client"; import React, { useState, useEffect, useMemo } from "react"; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from "@/components/ui/card"; import { Label } from "@/components/ui/label"; import { Checkbox } from "@/components/ui/checkbox"; import { Button } from "@/components/ui/button"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { RadioGroup, RadioGroupItem } from "@/components/ui/radio-group"; import { Badge } from "@/components/ui/badge"; import { Alert, AlertDescription } from "@/components/ui/alert"; import { Workflow, Info, CheckCircle, XCircle, Loader2 } from "lucide-react"; import { ComponentData } from "@/types/screen"; import { FlowVisibilityConfig } from "@/types/control-management"; import { getFlowById } from "@/lib/api/flow"; import type { FlowDefinition, FlowStep } from "@/types/flow"; import { toast } from "sonner"; interface FlowVisibilityConfigPanelProps { component: ComponentData; // 현재 선택된 버튼 allComponents: ComponentData[]; // 화면의 모든 컴포넌트 onUpdateProperty: (path: string, value: any) => void; } /** * 플로우 단계별 버튼 표시 설정 패널 * * 플로우 위젯이 화면에 있을 때, 버튼이 특정 플로우 단계에서만 표시되도록 설정할 수 있습니다. */ export const FlowVisibilityConfigPanel: React.FC = ({ component, allComponents, onUpdateProperty, }) => { // 현재 설정 const currentConfig: FlowVisibilityConfig | undefined = (component as any).webTypeConfig?.flowVisibilityConfig; // 화면의 모든 플로우 위젯 찾기 const flowWidgets = useMemo(() => { return allComponents.filter((comp) => { const isFlowWidget = comp.type === "flow" || (comp.type === "component" && (comp as any).componentConfig?.type === "flow-widget"); return isFlowWidget; }); }, [allComponents]); // State const [enabled, setEnabled] = useState(currentConfig?.enabled || false); const [selectedFlowComponentId, setSelectedFlowComponentId] = useState( currentConfig?.targetFlowComponentId || null ); const [mode, setMode] = useState<"whitelist" | "blacklist" | "all">(currentConfig?.mode || "whitelist"); const [visibleSteps, setVisibleSteps] = useState(currentConfig?.visibleSteps || []); const [hiddenSteps, setHiddenSteps] = useState(currentConfig?.hiddenSteps || []); const [layoutBehavior, setLayoutBehavior] = useState<"preserve-position" | "auto-compact">( currentConfig?.layoutBehavior || "auto-compact" ); // 선택된 플로우의 스텝 목록 const [flowSteps, setFlowSteps] = useState([]); const [flowInfo, setFlowInfo] = useState(null); const [loading, setLoading] = useState(false); // 플로우가 없을 때 if (flowWidgets.length === 0) { return ( 플로우 단계별 표시 설정 화면에 플로우 위젯을 추가하면 단계별 버튼 표시 제어가 가능합니다. ); } // 선택된 플로우의 스텝 로드 useEffect(() => { if (!selectedFlowComponentId) { setFlowSteps([]); setFlowInfo(null); return; } const loadFlowSteps = async () => { try { setLoading(true); // 선택된 플로우 위젯 찾기 const flowWidget = flowWidgets.find((fw) => fw.id === selectedFlowComponentId); if (!flowWidget) return; // flowId 추출 const flowConfig = (flowWidget as any).componentConfig || {}; const flowId = flowConfig.flowId; if (!flowId) { toast.error("플로우 ID를 찾을 수 없습니다"); return; } // 플로우 정보 조회 const flowResponse = await getFlowById(flowId); if (!flowResponse.success || !flowResponse.data) { throw new Error("플로우를 찾을 수 없습니다"); } setFlowInfo(flowResponse.data); // 스텝 목록 조회 const stepsResponse = await fetch(`/api/flow/definitions/${flowId}/steps`); if (!stepsResponse.ok) { throw new Error("스텝 목록을 불러올 수 없습니다"); } const stepsData = await stepsResponse.json(); if (stepsData.success && stepsData.data) { const sortedSteps = stepsData.data.sort((a: FlowStep, b: FlowStep) => a.stepOrder - b.stepOrder); setFlowSteps(sortedSteps); } } catch (error: any) { console.error("플로우 스텝 로딩 실패:", error); toast.error(error.message || "플로우 정보를 불러오는데 실패했습니다"); } finally { setLoading(false); } }; loadFlowSteps(); }, [selectedFlowComponentId, flowWidgets]); // 설정 저장 const handleSave = () => { const config: FlowVisibilityConfig = { enabled, targetFlowComponentId: selectedFlowComponentId || "", targetFlowId: flowInfo?.id, targetFlowName: flowInfo?.name, mode, visibleSteps: mode === "whitelist" ? visibleSteps : undefined, hiddenSteps: mode === "blacklist" ? hiddenSteps : undefined, layoutBehavior, }; onUpdateProperty("webTypeConfig.flowVisibilityConfig", config); toast.success("플로우 단계별 표시 설정이 저장되었습니다"); }; // 체크박스 토글 const toggleStep = (stepId: number) => { if (mode === "whitelist") { setVisibleSteps((prev) => prev.includes(stepId) ? prev.filter((id) => id !== stepId) : [...prev, stepId] ); } else if (mode === "blacklist") { setHiddenSteps((prev) => prev.includes(stepId) ? prev.filter((id) => id !== stepId) : [...prev, stepId] ); } }; // 빠른 선택 const selectAll = () => { if (mode === "whitelist") { setVisibleSteps(flowSteps.map((s) => s.id)); } else if (mode === "blacklist") { setHiddenSteps([]); } }; const selectNone = () => { if (mode === "whitelist") { setVisibleSteps([]); } else if (mode === "blacklist") { setHiddenSteps(flowSteps.map((s) => s.id)); } }; const invertSelection = () => { if (mode === "whitelist") { const allStepIds = flowSteps.map((s) => s.id); setVisibleSteps(allStepIds.filter((id) => !visibleSteps.includes(id))); } else if (mode === "blacklist") { const allStepIds = flowSteps.map((s) => s.id); setHiddenSteps(allStepIds.filter((id) => !hiddenSteps.includes(id))); } }; return ( 플로우 단계별 표시 설정 플로우의 특정 단계에서만 이 버튼을 표시하거나 숨길 수 있습니다 {/* 활성화 체크박스 */}
setEnabled(!!checked)} />
{enabled && ( <> {/* 대상 플로우 선택 */}
{/* 플로우가 선택되면 스텝 목록 표시 */} {selectedFlowComponentId && flowSteps.length > 0 && ( <> {/* 모드 선택 */}
setMode(value)}>
{/* 단계 선택 (all 모드가 아닐 때만) */} {mode !== "all" && (
{/* 스텝 체크박스 목록 */}
{flowSteps.map((step) => { const isChecked = mode === "whitelist" ? visibleSteps.includes(step.id) : hiddenSteps.includes(step.id); return (
toggleStep(step.id)} />
); })}
)} {/* 레이아웃 옵션 */}
setLayoutBehavior(value)}>
{/* 미리보기 */} {mode === "whitelist" && visibleSteps.length > 0 && (

표시 단계:

{visibleSteps.map((stepId) => { const step = flowSteps.find((s) => s.id === stepId); return ( {step?.stepName || `Step ${stepId}`} ); })}
)} {mode === "blacklist" && hiddenSteps.length > 0 && (

숨김 단계:

{hiddenSteps.map((stepId) => { const step = flowSteps.find((s) => s.id === stepId); return ( {step?.stepName || `Step ${stepId}`} ); })}
)} {mode === "all" &&

이 버튼은 모든 단계에서 표시됩니다.

} {mode === "whitelist" && visibleSteps.length === 0 &&

표시할 단계를 선택해주세요.

}
{/* 저장 버튼 */} )} {/* 플로우 선택 안내 */} {selectedFlowComponentId && flowSteps.length === 0 && !loading && ( 선택한 플로우에 단계가 없습니다. )} {loading && (
플로우 정보를 불러오는 중...
)} )}
); };