'use client'; import { getNodeConfig, getNodeMultiplier, setNodeMultiplier, updateNodeConfig, } from '@/services/admin/system'; import { useQuery } from '@tanstack/react-query'; import { Button } from '@workspace/ui/components/button'; import { ChartContainer, ChartTooltip } from '@workspace/ui/components/chart'; import { Label } from '@workspace/ui/components/label'; import { Table, TableBody, TableCell, TableRow } from '@workspace/ui/components/table'; import { ArrayInput } from '@workspace/ui/custom-components/dynamic-Inputs'; import { EnhancedInput } from '@workspace/ui/custom-components/enhanced-input'; import { DicesIcon } from 'lucide-react'; import { nanoid } from 'nanoid'; import { useTranslations } from 'next-intl'; import { useMemo, useState } from 'react'; import { Cell, Legend, Pie, PieChart } from 'recharts'; import { toast } from 'sonner'; const COLORS = [ 'hsl(var(--chart-1))', 'hsl(var(--chart-2))', 'hsl(var(--chart-3))', 'hsl(var(--chart-4))', 'hsl(var(--chart-5))', ]; export default function Node() { const t = useTranslations('system.node'); const { data, refetch } = useQuery({ queryKey: ['getNodeConfig'], queryFn: async () => { const { data } = await getNodeConfig(); return data.data; }, }); async function updateConfig(key: string, value: unknown) { if (data?.[key] === value) return; try { await updateNodeConfig({ ...data, [key]: value, } as API.NodeConfig); toast.success(t('saveSuccess')); refetch(); } catch (error) { /* empty */ } } const [timeSlots, setTimeSlots] = useState([]); const { data: NodeMultiplier, refetch: refetchNodeMultiplier } = useQuery({ queryKey: ['getNodeMultiplier'], queryFn: async () => { const { data } = await getNodeMultiplier(); if (timeSlots.length === 0) { setTimeSlots(data.data?.periods || []); } return data.data?.periods || []; }, }); const chartTimeSlots = useMemo(() => { return timeSlots.map((slot) => ({ name: `${slot.start_time} - ${slot.end_time}`, value: slot.multiplier, })); }, [timeSlots]); const chartConfig = useMemo(() => { return chartTimeSlots.reduce( (acc, item, index) => { acc[item.name] = { label: item.name, color: COLORS[index % COLORS.length] || 'hsl(var(--default-chart-color))', }; return acc; }, {} as Record, ); }, [data]); return ( <>

{t('communicationKeyDescription')}

updateConfig('node_secret', value)} suffix={ { updateConfig('node_secret', nanoid()); }} /> } />

{t('nodePullIntervalDescription')}

updateConfig('node_pull_interval', value)} suffix='S' value={data?.node_pull_interval} placeholder={t('inputPlaceholder')} />

{t('nodePushIntervalDescription')}

updateConfig('node_push_interval', value)} placeholder={t('inputPlaceholder')} />

{t('dynamicMultiplierDescription')}

`${value?.toFixed(2)}x (${(percent * 100).toFixed(0)}%)` } > {chartTimeSlots.map((entry, index) => ( ))} { if (payload && payload.length) { const data = payload[0]?.payload; return (
{t('timeSlot')} {data.name || '其他'}
{t('multiplier')} {data.value.toFixed(2)}x
); } return null; }} />
fields={[ { name: 'start_time', prefix: t('startTime'), type: 'time', }, { name: 'end_time', prefix: t('endTime'), type: 'time' }, { name: 'multiplier', prefix: t('multiplier'), type: 'number' }, ]} value={timeSlots} onChange={setTimeSlots} />
); }