"use client" import { getDataSyncServerList, updateSyncServerLocation, } from "@/components/label/api/sync" import { Response } from "@/components/label/api/sync/typing" import useAuth from "@/components/label/hooks/useAuth" import { ActionIcon, Badge, Button, Group, Modal, Paper, Select, Stack, TextInput, } from "@mantine/core" import { notifications } from "@mantine/notifications" import { IconEdit, IconRefresh, IconSearch } from "@tabler/icons-react" import { DataTable, DataTableColumn } from "mantine-datatable" import { useCallback, useEffect, useMemo, useState } from "react" export default function TeamSyncServerPage() { const { isShow } = useAuth() const [records, setRecords] = useState([]) const [loading, setLoading] = useState(false) const [totalItems, setTotalItems] = useState(0) const [page, setPage] = useState(1) const [pageSize, setPageSize] = useState(20) const [location, setLocation] = useState("") const [appliedLocation, setAppliedLocation] = useState("") const [online, setOnline] = useState(null) const [appliedOnline, setAppliedOnline] = useState(null) const [open, setOpen] = useState(false) const [editingRecord, setEditingRecord] = useState(null) const [editingLocation, setEditingLocation] = useState("") const [submitting, setSubmitting] = useState(false) const load = useCallback(async () => { try { setLoading(true) const params: any = { page_number: page, page_size: pageSize, } if (appliedLocation.trim()) params.location = appliedLocation.trim() if (appliedOnline === "true") params.is_online = true if (appliedOnline === "false") params.is_online = false const res = await getDataSyncServerList(params) setRecords(res?.sync_servers ?? []) setTotalItems(res?.total_items ?? 0) } catch (e) { setRecords([]) setTotalItems(0) notifications.show({ color: "red", title: "加载失败", message: e instanceof Error ? e.message : "请求失败", }) } finally { setLoading(false) } }, [appliedLocation, appliedOnline, page, pageSize]) useEffect(() => { load() }, [load]) const columns = useMemo(() => { const cols: DataTableColumn[] = [ { accessor: "id", title: "ID", width: 80 }, { accessor: "ip", title: "IP", width: 180 }, { accessor: "location", title: "IP所在地", width: 180 }, { accessor: "is_online", title: "状态", width: 100, render: (record) => record.is_online ? ( 在线 ) : ( 离线 ), }, { accessor: "create_at", title: "创建时间", width: 180 }, { accessor: "update_at", title: "更新时间", width: 180 }, { accessor: "operation", title: "操作", width: 100, textAlign: "center", render: (record) => isShow("server_edit") ? ( { setEditingRecord(record) setEditingLocation(record.location ?? "") setOpen(true) }}> ) : null, }, ] return cols }, [isShow]) const saveLocation = async () => { if (!editingRecord?.id) return try { setSubmitting(true) await updateSyncServerLocation(editingLocation.trim(), editingRecord.id) notifications.show({ color: "green", title: "操作成功", message: "已更新同步服务所在地", }) setOpen(false) setEditingRecord(null) setEditingLocation("") load() } catch (e) { notifications.show({ color: "red", title: "更新失败", message: e instanceof Error ? e.message : "请求失败", }) } finally { setSubmitting(false) } } return ( setLocation(e.currentTarget.value)} />