import { Key, History, Edit } from "lucide-react"; import { useState } from "react"; import { User } from "@/types/user"; import { USER_TABLE_COLUMNS } from "@/constants/user"; import { Button } from "@/components/ui/button"; import { Switch } from "@/components/ui/switch"; import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"; import { PaginationInfo } from "@/components/common/Pagination"; import { UserStatusConfirmDialog } from "./UserStatusConfirmDialog"; import { UserHistoryModal } from "./UserHistoryModal"; interface UserTableProps { users: User[]; isLoading: boolean; paginationInfo: PaginationInfo; onStatusToggle: (user: User, newStatus: string) => void; onPasswordReset: (userId: string, userName: string) => void; onEdit: (user: User) => void; } /** * 사용자 목록 테이블 컴포넌트 */ export function UserTable({ users, isLoading, paginationInfo, onStatusToggle, onPasswordReset, onEdit, }: UserTableProps) { // 확인 모달 상태 관리 const [confirmDialog, setConfirmDialog] = useState<{ isOpen: boolean; user: User | null; newStatus: string; }>({ isOpen: false, user: null, newStatus: "", }); // 히스토리 모달 상태 관리 const [historyModal, setHistoryModal] = useState<{ isOpen: boolean; userId: string; userName: string; }>({ isOpen: false, userId: "", userName: "", }); // NO 컬럼 계산 함수 (페이지네이션 고려) const getRowNumber = (index: number) => { return paginationInfo.startItem + index; }; // 날짜 포맷팅 함수 const formatDate = (dateString: string) => { if (!dateString) return "-"; return dateString.split(" ")[0]; // "2024-01-15 14:30:00" -> "2024-01-15" }; // 상태 토글 핸들러 (확인 모달 표시) const handleStatusToggle = (user: User, checked: boolean) => { const newStatus = checked ? "active" : "inactive"; setConfirmDialog({ isOpen: true, user, newStatus, }); }; // 상태 변경 확인 const handleConfirmStatusChange = () => { if (confirmDialog.user) { onStatusToggle(confirmDialog.user, confirmDialog.newStatus); } setConfirmDialog({ isOpen: false, user: null, newStatus: "" }); }; // 상태 변경 취소 const handleCancelStatusChange = () => { setConfirmDialog({ isOpen: false, user: null, newStatus: "" }); }; // 변경이력 모달 열기 const handleOpenHistoryModal = (user: User) => { setHistoryModal({ isOpen: true, userId: user.userId, userName: user.userName || user.userId, }); }; // 변경이력 모달 닫기 const handleCloseHistoryModal = () => { setHistoryModal({ isOpen: false, userId: "", userName: "", }); }; // 로딩 상태 렌더링 if (isLoading) { return ( <> {/* 데스크톱 테이블 스켈레톤 */}
{USER_TABLE_COLUMNS.map((column) => ( {column.label} ))} 작업 {Array.from({ length: 10 }).map((_, index) => ( {USER_TABLE_COLUMNS.map((column) => (
))}
{Array.from({ length: 2 }).map((_, i) => (
))}
))}
{/* 모바일/태블릿 카드 스켈레톤 */}
{Array.from({ length: 6 }).map((_, index) => (
{Array.from({ length: 5 }).map((_, i) => (
))}
))}
); } // 데이터가 없을 때 if (users.length === 0) { return (

등록된 사용자가 없습니다.

); } // 실제 데이터 렌더링 return ( <> {/* 데스크톱 테이블 뷰 (lg 이상) */}
{USER_TABLE_COLUMNS.map((column) => ( {column.label} ))} 작업 {users.map((user, index) => ( {getRowNumber(index)} {user.sabun || "-"} {user.companyCode || "-"} {user.deptName || "-"} {user.positionName || "-"} {user.userId} {user.userName} {user.tel || user.cellPhone || "-"} {user.email || "-"} {formatDate(user.regDate || "")}
handleStatusToggle(user, checked)} aria-label={`${user.userName} 상태 토글`} />
))}
{/* 모바일/태블릿 카드 뷰 (lg 미만) */}
{users.map((user, index) => (
{/* 헤더: 이름과 상태 */}

{user.userName}

{user.userId}

handleStatusToggle(user, checked)} aria-label={`${user.userName} 상태 토글`} />
{/* 정보 그리드 */}
{user.sabun && (
사번 {user.sabun}
)} {user.companyCode && (
회사 {user.companyCode}
)} {user.deptName && (
부서 {user.deptName}
)} {user.positionName && (
직책 {user.positionName}
)} {(user.tel || user.cellPhone) && (
연락처 {user.tel || user.cellPhone}
)} {user.email && (
이메일 {user.email}
)}
등록일 {formatDate(user.regDate || "")}
{/* 액션 버튼 */}
))}
{/* 상태 변경 확인 모달 */} {/* 사용자 변경이력 모달 */} ); }