"use client"; import { useState } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Badge } from "@/components/ui/badge"; import { LoadingSpinner } from "@/components/common/LoadingSpinner"; import { useCommonCode } from "@/hooks/useCommonCode"; // import { useMultiLang } from "@/hooks/useMultiLang"; // 무한 루프 방지를 위해 임시 제거 import { CodeCategoryFormModal } from "./CodeCategoryFormModal"; import { AlertModal } from "@/components/common/AlertModal"; import { Search, Plus, Edit, Trash2 } from "lucide-react"; import { cn } from "@/lib/utils"; interface CodeCategoryPanelProps { selectedCategoryCode: string; onSelectCategory: (categoryCode: string) => void; } export function CodeCategoryPanel({ selectedCategoryCode, onSelectCategory }: CodeCategoryPanelProps) { // useMultiLang 호출 제거 - 상위에서 전달받도록 수정 const { categories, categoriesLoading, categoriesError, fetchCategories, createCategory, updateCategory, deleteCategory, } = useCommonCode(); // 로컬 상태 const [searchTerm, setSearchTerm] = useState(""); const [showActiveOnly, setShowActiveOnly] = useState(false); // 활성 필터 상태 const [showFormModal, setShowFormModal] = useState(false); const [editingCategory, setEditingCategory] = useState(""); const [showDeleteModal, setShowDeleteModal] = useState(false); const [deletingCategory, setDeletingCategory] = useState(""); // 검색 및 활성 상태 필터링 const filteredCategories = categories.filter((category) => { // 검색 조건 const matchesSearch = category.category_name.toLowerCase().includes(searchTerm.toLowerCase()) || category.category_code.toLowerCase().includes(searchTerm.toLowerCase()); // 활성 상태 필터 조건 const matchesActiveFilter = showActiveOnly ? category.is_active : true; return matchesSearch && matchesActiveFilter; }); // 카테고리 생성 핸들러 const handleCreateCategory = () => { setEditingCategory(""); setShowFormModal(true); }; // 카테고리 수정 핸들러 const handleEditCategory = (categoryCode: string) => { setEditingCategory(categoryCode); setShowFormModal(true); }; // 카테고리 삭제 핸들러 const handleDeleteCategory = (categoryCode: string) => { setDeletingCategory(categoryCode); setShowDeleteModal(true); }; // 삭제 확인 핸들러 const handleConfirmDelete = async () => { if (!deletingCategory) return; try { await deleteCategory(deletingCategory); setShowDeleteModal(false); setDeletingCategory(""); } catch (error) { console.error("카테고리 삭제 오류:", error); // 에러 처리는 useCommonCode 훅에서 처리됨 } }; if (categoriesError) { return (

❌ {categoriesError}

); } return (
{/* 검색 및 추가 버튼 */}
setSearchTerm(e.target.value)} className="pl-10" />
{/* 활성 상태 필터 토글 */}
{/* 카테고리 목록 */}
{categoriesLoading ? (

카테고리를 불러오는 중...

) : filteredCategories.length === 0 ? (

카테고리가 없습니다.

) : (
{filteredCategories.map((category) => (
onSelectCategory(category.category_code)} >
{category.category_name} {category.is_active === "Y" ? ( 활성 ) : ( 비활성 )}

{category.category_code}

{category.description && (

{category.description}

)}
{/* 액션 버튼 */}
))}
)}
{/* 카테고리 폼 모달 */} {showFormModal && ( setShowFormModal(false)} editingCategoryCode={editingCategory} categories={categories} onCreateCategory={createCategory} onUpdateCategory={updateCategory} /> )} {/* 삭제 확인 모달 */} {showDeleteModal && ( setShowDeleteModal(false)} onConfirm={handleConfirmDelete} type="error" title="삭제 확인" message="이 카테고리를 삭제하시겠습니까? 관련된 모든 코드도 함께 삭제됩니다." confirmText="삭제" /> )}
); }