"use client" import * as React from "react" import { toast } from "sonner" import { Loader, Save, ArrowLeft } from "lucide-react" import Link from "next/link" import { Button } from "@/components/ui/button" import { Card, CardContent, CardHeader, CardTitle, CardDescription } from "@/components/ui/card" import { Input } from "@/components/ui/input" import RichTextEditor from "@/components/rich-text-editor/RichTextEditor" import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/components/ui/tabs" import { Separator } from "@/components/ui/separator" import { Badge } from "@/components/ui/badge" import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select" import { formatApprovalLine } from "@/lib/approval-line/utils/format" import { getApprovalLineOptionsAction } from "@/lib/approval-line/service" import { type ApprovalTemplate } from "@/lib/approval-template/service" import { type Editor } from "@tiptap/react" import { updateApprovalTemplateAction } from "@/lib/approval-template/service" import { useSession } from "next-auth/react" interface ApprovalTemplateEditorProps { templateId: string initialTemplate: ApprovalTemplate staticVariables?: Array<{ variableName: string }> approvalLineOptions: Array<{ id: string; name: string }> approvalLineCategories: string[] } export function ApprovalTemplateEditor({ templateId, initialTemplate, staticVariables = [], approvalLineOptions, approvalLineCategories }: ApprovalTemplateEditorProps) { const { data: session } = useSession() const [rte, setRte] = React.useState(null) const [template, setTemplate] = React.useState(initialTemplate) const [isSaving, startSaving] = React.useTransition() // 편집기에 전달할 변수 목록 // 템플릿(DB) 변수 + 정적(config) 변수 병합 // eslint-disable-next-line @typescript-eslint/no-explicit-any const dbVariables: string[] = Array.isArray((template as any).variables) ? // eslint-disable-next-line @typescript-eslint/no-explicit-any (template as any).variables.map((v: any) => v.variableName) : [] const mergedVariables = Array.from(new Set([...dbVariables, ...staticVariables.map((v) => v.variableName)])) const variableOptions = mergedVariables.map((name) => ({ label: name, html: `{{${name}}}` })) const [form, setForm] = React.useState({ name: template.name, subject: template.subject, content: template.content, description: template.description ?? "", category: template.category ?? "", approvalLineId: (template as { approvalLineId?: string | null }).approvalLineId ?? "", }) const [category, setCategory] = React.useState(form.category ?? (approvalLineCategories[0] ?? "")) // eslint-disable-next-line @typescript-eslint/no-explicit-any const [lineOptions, setLineOptions] = React.useState(approvalLineOptions as Array<{ id: string; name: string; aplns?: any[]; category?: string | null }>) const [isLoadingLines, setIsLoadingLines] = React.useState(false) React.useEffect(() => { let active = true const run = async () => { setIsLoadingLines(true) const { success, data } = await getApprovalLineOptionsAction(category || undefined) if (active) { setIsLoadingLines(false) // eslint-disable-next-line @typescript-eslint/no-explicit-any if (success && data) setLineOptions(data as any) // 카테고리 바뀌면 결재선 선택 초기화 setForm((prev) => ({ ...prev, category, approvalLineId: "" })) } } run() return () => { active = false } }, [category]) function handleChange(e: React.ChangeEvent) { const { name, value } = e.target setForm((prev) => ({ ...prev, [name]: value })) } function handleSave() { startSaving(async () => { if (!session?.user?.id) { toast.error("로그인이 필요합니다") return } const { success, error, data } = await updateApprovalTemplateAction(templateId, { name: form.name, subject: form.subject, content: form.content, description: form.description, category: form.category || undefined, approvalLineId: form.approvalLineId ? form.approvalLineId : null, updatedBy: Number(session.user.id), }) if (!success || error || !data) { toast.error(error ?? "저장에 실패했습니다") return } setTemplate(data) toast.success("저장되었습니다") }) } return (
{/* Header */}

{template.name}

최근 수정: {new Date(template.updatedAt).toLocaleDateString("ko-KR")} {template.category && ( {template.category} )}

{template.description || "결재 템플릿 편집"}

편집 미리보기 기본 정보
HTML 내용 표, 이미지, 변수 등을 자유롭게 편집하세요. {variableOptions.length > 0 && (
{variableOptions.map((v: { label: string; html: string }) => ( ))}
)} setForm((prev) => ({ ...prev, content: val }))} onReady={(editor) => setRte(editor)} height="400px" />
미리보기
) }