"use client"; import React, { useState, useMemo } from "react"; import { Button } from "@/components/ui/button"; import { Input } from "@/components/ui/input"; import { Badge } from "@/components/ui/badge"; import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@/components/ui/select"; import { AlertDialog, AlertDialogAction, AlertDialogCancel, AlertDialogContent, AlertDialogDescription, AlertDialogFooter, AlertDialogHeader, AlertDialogTitle, AlertDialogTrigger, } from "@/components/ui/alert-dialog"; import { toast } from "sonner"; import { Plus, Search, Edit, Trash2, Eye, Filter, RotateCcw, Settings, SortAsc, SortDesc, CheckCircle, AlertCircle, } from "lucide-react"; import { useButtonActions } from "@/hooks/admin/useButtonActions"; import Link from "next/link"; export default function ButtonActionsManagePage() { const [searchTerm, setSearchTerm] = useState(""); const [categoryFilter, setCategoryFilter] = useState(""); const [activeFilter, setActiveFilter] = useState("Y"); const [sortField, setSortField] = useState("sort_order"); const [sortDirection, setSortDirection] = useState<"asc" | "desc">("asc"); // 버튼 액션 데이터 조회 const { buttonActions, isLoading, error, deleteButtonAction, isDeleting, deleteError, refetch } = useButtonActions({ active: activeFilter || undefined, search: searchTerm || undefined, category: categoryFilter || undefined, }); // 카테고리 목록 생성 const categories = useMemo(() => { const uniqueCategories = Array.from(new Set(buttonActions.map((ba) => ba.category).filter(Boolean))); return uniqueCategories.sort(); }, [buttonActions]); // 필터링 및 정렬된 데이터 const filteredAndSortedButtonActions = useMemo(() => { let filtered = [...buttonActions]; // 정렬 filtered.sort((a, b) => { let aValue: any = a[sortField as keyof typeof a]; let bValue: any = b[sortField as keyof typeof b]; // 숫자 필드 처리 if (sortField === "sort_order") { aValue = aValue || 0; bValue = bValue || 0; } // 문자열 필드 처리 if (typeof aValue === "string") { aValue = aValue.toLowerCase(); } if (typeof bValue === "string") { bValue = bValue.toLowerCase(); } if (aValue < bValue) return sortDirection === "asc" ? -1 : 1; if (aValue > bValue) return sortDirection === "asc" ? 1 : -1; return 0; }); return filtered; }, [buttonActions, sortField, sortDirection]); // 정렬 변경 핸들러 const handleSort = (field: string) => { if (sortField === field) { setSortDirection(sortDirection === "asc" ? "desc" : "asc"); } else { setSortField(field); setSortDirection("asc"); } }; // 삭제 핸들러 const handleDelete = async (actionType: string, actionName: string) => { try { await deleteButtonAction(actionType); toast.success(`버튼 액션 '${actionName}'이 삭제되었습니다.`); } catch (error) { toast.error(error instanceof Error ? error.message : "삭제 중 오류가 발생했습니다."); } }; // 필터 초기화 const resetFilters = () => { setSearchTerm(""); setCategoryFilter(""); setActiveFilter("Y"); setSortField("sort_order"); setSortDirection("asc"); }; // 로딩 상태 if (isLoading) { return (
버튼 액션 목록을 불러오는 중...
); } // 에러 상태 if (error) { return (
버튼 액션 목록을 불러오는데 실패했습니다.
); } return (
{/* 헤더 */}

버튼 액션 관리

화면관리에서 사용할 버튼 액션들을 관리합니다.

{/* 필터 및 검색 */} 필터 및 검색
{/* 검색 */}
setSearchTerm(e.target.value)} className="pl-10" />
{/* 카테고리 필터 */} {/* 활성화 상태 필터 */} {/* 초기화 버튼 */}
{/* 결과 통계 */}

총 {filteredAndSortedButtonActions.length}개의 버튼 액션이 있습니다.

{/* 버튼 액션 목록 테이블 */} handleSort("sort_order")}>
순서 {sortField === "sort_order" && (sortDirection === "asc" ? : )}
handleSort("action_type")}>
액션 타입 {sortField === "action_type" && (sortDirection === "asc" ? : )}
handleSort("action_name")}>
액션명 {sortField === "action_name" && (sortDirection === "asc" ? : )}
handleSort("category")}>
카테고리 {sortField === "category" && (sortDirection === "asc" ? : )}
기본 텍스트 확인 필요 설명 handleSort("is_active")}>
상태 {sortField === "is_active" && (sortDirection === "asc" ? : )}
handleSort("updated_date")}>
최종 수정일 {sortField === "updated_date" && (sortDirection === "asc" ? : )}
작업
{filteredAndSortedButtonActions.length === 0 ? ( 조건에 맞는 버튼 액션이 없습니다. ) : ( filteredAndSortedButtonActions.map((action) => ( {action.sort_order || 0} {action.action_type} {action.action_name} {action.action_name_eng && (
{action.action_name_eng}
)}
{action.category} {action.default_text || "-"} {action.confirmation_required ? (
필요
) : (
불필요
)}
{action.description || "-"} {action.is_active === "Y" ? "활성화" : "비활성화"} {action.updated_date ? new Date(action.updated_date).toLocaleDateString("ko-KR") : "-"}
버튼 액션 삭제 '{action.action_name}' 버튼 액션을 삭제하시겠습니까?
이 작업은 되돌릴 수 없습니다.
취소 handleDelete(action.action_type, action.action_name)} disabled={isDeleting} className="bg-red-600 hover:bg-red-700" > {isDeleting ? "삭제 중..." : "삭제"}
)) )}
{deleteError && (

삭제 중 오류가 발생했습니다: {deleteError instanceof Error ? deleteError.message : "알 수 없는 오류"}

)}
); }