'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 { useTranslations } from 'next-intl'; import { uid } from 'radash'; 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))', ]; const MINUTES_IN_DAY = 1440; // 24 * 60 function getTimeRangeData(slots: API.TimePeriod[]) { const timePoints = slots .filter((slot) => slot.start_time && slot.end_time) .flatMap((slot) => { const [startH = 0, startM = 0] = slot.start_time.split(':').map(Number); const [endH = 0, endM = 0] = slot?.end_time?.split(':').map(Number); const start = startH * 60 + startM; let end = endH * 60 + endM; if (end < start) end += MINUTES_IN_DAY; return { start, end, multiplier: slot.multiplier }; }) .sort((a, b) => a.start - b.start); const result = []; let currentMinute = 0; timePoints.forEach((point) => { if (point.start > currentMinute) { result.push({ name: `${Math.floor(currentMinute / 60)}:${String(currentMinute % 60).padStart(2, '0')} - ${Math.floor(point.start / 60)}:${String(point.start % 60).padStart(2, '0')}`, value: point.start - currentMinute, multiplier: 1, }); } result.push({ name: `${Math.floor(point.start / 60)}:${String(point.start % 60).padStart(2, '0')} - ${Math.floor((point.end / 60) % 24)}:${String(point.end % 60).padStart(2, '0')}`, value: point.end - point.start, multiplier: point.multiplier, }); currentMinute = point.end % MINUTES_IN_DAY; }); if (currentMinute < MINUTES_IN_DAY) { result.push({ name: `${Math.floor(currentMinute / 60)}:${String(currentMinute % 60).padStart(2, '0')} - 24:00`, value: MINUTES_IN_DAY - currentMinute, multiplier: 1, }); } return result; } export default function NodeConfig() { const t = useTranslations('server.config'); 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 getTimeRangeData(timeSlots); }, [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={
{ const id = uid(32).toLowerCase(); const formatted = `${id.slice(0, 8)}-${id.slice(8, 12)}-${id.slice(12, 16)}-${id.slice(16, 20)}-${id.slice(20)}`; updateConfig('node_secret', formatted); }} className='cursor-pointer' />
} />

{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')}

`${(multiplier || 0)?.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.multiplier.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', placeholder: '0' }, ]} value={timeSlots} onChange={setTimeSlots} />
); }