import Datatable from "@arcblock/ux/lib/Datatable"; import Confirm from "@arcblock/ux/lib/Dialog/confirm"; import Empty from "@arcblock/ux/lib/Empty"; import { useLocaleContext } from "@arcblock/ux/lib/Locale/context"; import RelativeTime from "@arcblock/ux/lib/RelativeTime"; import Toast from "@arcblock/ux/lib/Toast"; import UserCard from "@arcblock/ux/lib/UserCard"; import { CardType, InfoType } from "@arcblock/ux/lib/UserCard/types"; import TrashIcon from "@mui/icons-material/Delete"; import EditIcon from "@mui/icons-material/Edit"; import { useMediaQuery } from "@mui/material"; import Box from "@mui/material/Box"; import Button from "@mui/material/Button"; import Chip from "@mui/material/Chip"; import Dialog from "@mui/material/Dialog"; import DialogActions from "@mui/material/DialogActions"; import DialogContent from "@mui/material/DialogContent"; import DialogTitle from "@mui/material/DialogTitle"; import IconButton from "@mui/material/IconButton"; import TextField from "@mui/material/TextField"; import { compact } from "lodash"; import prettyMs from "pretty-ms"; import { useState } from "react"; import { joinURL } from "ufo"; import { BlockletComponent } from "../components/blocklet-comp.tsx"; import type { TraceData } from "../components/run/types.ts"; import Status from "../components/status.tsx"; import formatNumber from "../utils/format-number.ts"; import { origin } from "../utils/index.ts"; import { parseDuration } from "../utils/latency.ts"; import Download from "./download.tsx"; const Table = ({ traces, total, loading, onRowClick, page, setPage, isLive, onDelete, selectedRows, setSelectedRows, onRemarkUpdate, }: { traces: TraceData[]; total: number; loading: boolean; onRowClick: (row: TraceData) => void; page: { page: number; pageSize: number }; setPage: (page: { page: number; pageSize: number }) => void; isLive?: boolean; onDelete: (item: string[]) => void; selectedRows: string[]; setSelectedRows: (rows: string[]) => void; onRemarkUpdate?: (id: string, remark: string) => void; }) => { const isBlocklet = !!window.blocklet?.prefix; const { t, locale } = useLocaleContext(); const isMobile = useMediaQuery((x) => x.breakpoints.down("md")); const [open, setOpen] = useState(false); const [remarkDialogOpen, setRemarkDialogOpen] = useState(false); const [currentRemark, setCurrentRemark] = useState<{ id: string; remark: string }>({ id: "", remark: "", }); const [saving, setSaving] = useState(false); const columns = compact([ { label: "ID", name: "id", width: 160, options: { customBodyRender: (value: string) => {value}, }, }, { label: t("agentName"), name: "name", width: 200, options: { customBodyRender: (value: string, { rowIndex }: { rowIndex: number }) => { const item = traces[rowIndex]; return ( {value} {item?.attributes?.metadata?.cliVersion && ( )} {item?.attributes?.metadata?.appName && ( )} {item.remark && ( {item.remark} )} { e.stopPropagation(); setCurrentRemark({ id: item.id, remark: item.remark || "" }); setRemarkDialogOpen(true); }} > ); }, }, }, { label: t("input"), name: "input", minWidth: 120, options: { customBodyRender: (_: unknown, { rowIndex }: { rowIndex: number }) => { const item = traces[rowIndex]; const input = item.attributes?.input; return ( {typeof input === "string" ? input : input ? JSON.stringify(input) : "-"} ); }, }, }, { label: t("output"), name: "output", options: { customBodyRender: (_: unknown, { rowIndex }: { rowIndex: number }) => { const item = traces[rowIndex]; const output = item.attributes?.output; return ( {typeof output === "string" ? output : output ? JSON.stringify(output) : "-"} ); }, }, }, { label: t("latency"), name: "latency", align: "right" as const, width: 160, options: { customBodyRender: (_: unknown, { rowIndex }: { rowIndex: number }) => { const item = traces[rowIndex]; if (item?.endTime && item?.startTime) { return {prettyMs(item.endTime - item.startTime)}; } return {parseDuration(item.startTime, item.endTime)}; }, }, }, { label: `${t("token")}`, name: "token", width: 200, align: "right" as const, options: { customBodyRender: (_: unknown, { rowIndex }: { rowIndex: number }) => { const item = traces[rowIndex]; return {`${formatNumber(item.token || 0, locale)}`}; }, }, }, { label: `${t("cost")}`, name: "token", width: 200, align: "right" as const, options: { customBodyRender: (_: unknown, { rowIndex }: { rowIndex: number }) => { const item = traces[rowIndex]; return {`$${Number(item.cost || 0).toFixed(6)}`}; }, }, }, { label: t("status"), name: "status", minWidth: 150, align: "center" as const, options: { customBodyRender: (_: unknown, { rowIndex }: { rowIndex: number }) => { const item = traces[rowIndex]; const map: Record< number, { color: "default" | "error" | "warning" | "success"; label: string } > = { 0: { color: "warning", label: t("pending"), }, 1: { color: "success", label: t("success"), }, 2: { color: "error", label: t("failed"), }, }; return ( {item.status?.code === 0 ? ( ) : isMobile ? null : ( )} ); }, }, }, isBlocklet ? { label: t("user"), name: "userId", width: 100, options: { customBodyRender: (_: unknown, { rowIndex }: { rowIndex: number }) => { const item = traces[rowIndex]; if (item.userId) { return ( ); } return null; }, }, } : undefined, { label: t("startedAt"), name: "startTime", minWidth: 180, align: "right" as const, options: { customBodyRender: (_: unknown, { rowIndex }: { rowIndex: number }) => { const item = traces[rowIndex]; return item.startTime ? ( ) : ( "-" ); }, }, }, { label: t("endedAt"), name: "endTime", minWidth: 180, align: "right" as const, options: { customBodyRender: (_: unknown, { rowIndex }: { rowIndex: number }) => { const item = traces[rowIndex]; return item.endTime ? ( ) : ( "-" ); }, }, }, ]); if (isBlocklet) { columns.unshift({ label: t("component"), name: "component", minWidth: 180, options: { customBodyRender: (_: unknown, { rowIndex }: { rowIndex: number }) => { const item = traces[rowIndex]; if (!item.componentId) return -; const component = window.blocklet.componentMountPoints?.find( (c) => c.did === item.componentId, ); if (!component) return -; return ; }, }, }); } const onTableChange = ({ page: newPage, rowsPerPage }: { page: number; rowsPerPage: number }) => { setPage({ page: newPage + 1, pageSize: rowsPerPage }); }; const onClose = () => { setOpen(false); setSelectedRows([]); }; const handleRemarkSave = async () => { setSaving(true); try { const res = await fetch(joinURL(origin, "/api/trace/remark"), { method: "POST", headers: { "Content-Type": "application/json" }, body: JSON.stringify({ id: currentRemark.id, remark: currentRemark.remark }), }); if (!res.ok) { throw new Error("Failed to save remark"); } Toast.success(t("remarkSaved")); setRemarkDialogOpen(false); onRemarkUpdate?.(currentRemark.id, currentRemark.remark); } catch (error) { Toast.error((error as Error)?.message || t("remarkSaveFailed")); } finally { setSaving(false); } }; const handleRemarkClose = () => { setRemarkDialogOpen(false); setCurrentRemark({ id: "", remark: "" }); }; const customToolbarSelect = (_selectedRows: { data: { index: number; dataIndex: number }[] }) => { if (!_selectedRows.data.length) return null; return ( setOpen(true)}> setSelectedRows([])} /> ); }; return ( div:first-of-type": { width: "100px !important", }, } : {}, ".MuiPaper-elevation1": { paddingTop: "16px", paddingBottom: "16px", }, }} > { const ids = rowsSelected.map((idx: number) => traces[idx].id); setSelectedRows(ids); }, rowsSelected: traces .map((row, idx) => (selectedRows.includes(row.id) ? idx : null)) .filter((idx) => idx !== null), customToolbarSelect, }} onChange={onTableChange} emptyNode={{t("noData")}} /> { onDelete(selectedRows); onClose(); }} onCancel={onClose} > {t("delConfirmDescription", { id: selectedRows.join(",") })} {t("editRemark")} { const value = e.target.value.slice(0, 50); setCurrentRemark({ ...currentRemark, remark: value }); }} helperText={`${currentRemark.remark.length}/50`} sx={{ mt: 1 }} /> ); }; export default Table;