/** * 작업 이력 위젯 * - 작업 이력 목록 표시 * - 필터링 기능 * - 상태별 색상 구분 * - 쿼리 결과 기반 데이터 표시 */ "use client"; import { useState, useEffect } from "react"; import { DashboardElement } from "@/components/admin/dashboard/types"; import { getApiUrl } from "@/lib/utils/apiUrl"; import { WORK_TYPE_LABELS, WORK_STATUS_LABELS, WORK_STATUS_COLORS, WorkType, WorkStatus } from "@/types/workHistory"; interface WorkHistoryWidgetProps { element: DashboardElement; refreshInterval?: number; } export default function WorkHistoryWidget({ element, refreshInterval = 60000 }: WorkHistoryWidgetProps) { const [data, setData] = useState([]); const [isLoading, setIsLoading] = useState(true); const [error, setError] = useState(null); const [selectedType, setSelectedType] = useState("all"); const [selectedStatus, setSelectedStatus] = useState("all"); // 데이터 로드 const loadData = async () => { try { setIsLoading(true); setError(null); // 쿼리가 설정되어 있으면 쿼리 실행 if (element.dataSource?.query) { const token = localStorage.getItem("authToken"); const response = await fetch(getApiUrl("/api/dashboards/execute-query"), { method: "POST", headers: { "Content-Type": "application/json", Authorization: `Bearer ${token}`, }, body: JSON.stringify({ query: element.dataSource.query, connectionType: element.dataSource.connectionType || "current", connectionId: element.dataSource.connectionId, }), }); if (!response.ok) throw new Error("데이터 로딩 실패"); const result = await response.json(); if (result.success && result.data?.rows) { setData(result.data.rows); } else { throw new Error(result.message || "데이터 로드 실패"); } } else { // 쿼리 미설정 시 안내 메시지 setError("쿼리를 설정해주세요"); setData([]); } } catch (err) { console.error("작업 이력 로드 실패:", err); setError(err instanceof Error ? err.message : "데이터를 불러올 수 없습니다"); } finally { setIsLoading(false); } }; useEffect(() => { loadData(); const interval = setInterval(loadData, refreshInterval); return () => clearInterval(interval); }, [selectedType, selectedStatus, refreshInterval, element.dataSource]); if (isLoading && data.length === 0) { return (
로딩 중...
); } if (error) { return (
⚠️
{error}
{!element.dataSource?.query &&
쿼리를 설정하세요
}
); } return (
{/* 필터 */}
{/* 테이블 */}
{data.length === 0 ? ( ) : ( data .filter((item) => selectedType === "all" || item.work_type === selectedType) .filter((item) => selectedStatus === "all" || item.status === selectedStatus) .map((item, index) => ( )) )}
작업번호 일시 유형 차량 경로 화물 중량 상태
작업 이력이 없습니다
{item.work_number} {item.work_date ? new Date(item.work_date).toLocaleString("ko-KR", { month: "2-digit", day: "2-digit", hour: "2-digit", minute: "2-digit", }) : "-"} {WORK_TYPE_LABELS[item.work_type as WorkType] || item.work_type} {item.vehicle_number || "-"} {item.origin && item.destination ? `${item.origin} → ${item.destination}` : "-"} {item.cargo_name || "-"} {item.cargo_weight ? `${item.cargo_weight} ${item.cargo_unit || "ton"}` : "-"} {WORK_STATUS_LABELS[item.status as WorkStatus] || item.status}
{/* 푸터 */}
전체 {data.length}건
); }