'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 { Label } from '@workspace/ui/components/label'; import { Table, TableBody, TableCell, TableRow } from '@workspace/ui/components/table'; import { EnhancedInput } from '@workspace/ui/custom-components/enhanced-input'; import { DicesIcon } from 'lucide-react'; import { nanoid } from 'nanoid'; import { useTranslations } from 'next-intl'; import { useState } from 'react'; import { toast } from 'sonner'; 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 addTimeSlot = () => { setTimeSlots([...timeSlots, { start_time: '', end_time: '', multiplier: 1 }]); }; const removeTimeSlot = (index: number) => { setTimeSlots(timeSlots.filter((_, i) => i !== index)); }; const updateTimeSlot = (index: number, field: keyof API.TimePeriod, value: string | number) => { const updatedSlots = timeSlots.map((slot, i) => { if (i === index) { return { ...slot, [field]: value }; } return slot; }); setTimeSlots(updatedSlots); }; 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')}

{timeSlots.map((slot, index) => (
updateTimeSlot(index, 'start_time', value as string)} />
updateTimeSlot(index, 'end_time', value as string)} />
updateTimeSlot(index, 'multiplier', value as number)} min={1} step='0.1' />
))}
); }