103 lines
2.8 KiB
React
103 lines
2.8 KiB
React
/*
|
|
Copyright (C) 2025 QuantumNous
|
|
|
|
This program is free software: you can redistribute it and/or modify
|
|
it under the terms of the GNU Affero General Public License as
|
|
published by the Free Software Foundation, either version 3 of the
|
|
License, or (at your option) any later version.
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
GNU Affero General Public License for more details.
|
|
|
|
You should have received a copy of the GNU Affero General Public License
|
|
along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
For commercial licensing, please contact support@quantumnous.com
|
|
*/
|
|
|
|
import React, { useState, useEffect } from 'react';
|
|
import { Progress } from '@/components/ui';
|
|
import { Clock } from 'lucide-react';
|
|
import { getServerURL } from '../../helpers/config';
|
|
|
|
export const RefreshCountdown = ({ interval, onComplete }) => {
|
|
const [timeLeft, setTimeLeft] = useState(interval);
|
|
const [percentage, setPercentage] = useState(100);
|
|
|
|
useEffect(() => {
|
|
setTimeLeft(interval);
|
|
setPercentage(100);
|
|
}, [interval]);
|
|
|
|
useEffect(() => {
|
|
const timer = setInterval(() => {
|
|
setTimeLeft((prev) => {
|
|
if (prev <= 1) {
|
|
onComplete && onComplete();
|
|
return interval;
|
|
}
|
|
return prev - 1;
|
|
});
|
|
}, 1000);
|
|
|
|
return () => clearInterval(timer);
|
|
}, [interval, onComplete]);
|
|
|
|
useEffect(() => {
|
|
setPercentage((timeLeft / interval) * 100);
|
|
}, [timeLeft, interval]);
|
|
|
|
const formatTime = (seconds) => {
|
|
const mins = Math.floor(seconds / 60);
|
|
const secs = seconds % 60;
|
|
return `${mins}:${secs.toString().padStart(2, '0')}`;
|
|
};
|
|
|
|
return (
|
|
<div className='flex items-center gap-2 text-sm text-gray-600'>
|
|
<Clock className='w-4 h-4' />
|
|
<span>{formatTime(timeLeft)}</span>
|
|
<Progress
|
|
percent={percentage}
|
|
showInfo={false}
|
|
stroke='var(--ui-color-primary)'
|
|
size='small'
|
|
style={{ width: '60px' }}
|
|
/>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
// 动态配置Hook
|
|
export const useMonitorConfig = () => {
|
|
const [config, setConfig] = useState({
|
|
refresh_interval: 60,
|
|
enable_auto_refresh: true,
|
|
});
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
useEffect(() => {
|
|
const fetchConfig = async () => {
|
|
try {
|
|
const res = await fetch(`${getServerURL()}/api/monitor/config`, {
|
|
credentials: getServerURL() ? 'include' : 'same-origin',
|
|
});
|
|
const data = await res.json();
|
|
if (data.success) {
|
|
setConfig(data.data);
|
|
}
|
|
} catch (error) {
|
|
console.error('Failed to load monitor config:', error);
|
|
} finally {
|
|
setLoading(false);
|
|
}
|
|
};
|
|
|
|
fetchConfig();
|
|
}, []);
|
|
|
|
return { config, loading };
|
|
};
|