"use client"; import { useEffect, useRef, useState } from "react"; import { Dialog, DialogContent, DialogTitle } from '@/components/ui/dialog'; import { Progress } from '@/components/ui/progress'; import { Switch } from '@/components/ui/switch'; import ConfirmModal from './ConfirmModal'; import { ResetIcon, SettingsIcon } from './Icons'; export default function SettingsModal({ onClose, tempSeconds, setTempSeconds, saveSettings, exportLocalData, importFileRef, handleImportFileChange, importMsg, isMobile, containerWidth = 1200, setContainerWidth, onResetContainerWidth, showMarketIndex = true, setShowMarketIndex, }) { const [sliderDragging, setSliderDragging] = useState(false); const [resetWidthConfirmOpen, setResetWidthConfirmOpen] = useState(false); const [localSeconds, setLocalSeconds] = useState(tempSeconds); const [localShowMarketIndex, setLocalShowMarketIndex] = useState(showMarketIndex); const pageWidthTrackRef = useRef(null); const clampedWidth = Math.min(2000, Math.max(600, Number(containerWidth) || 1200)); const pageWidthPercent = ((clampedWidth - 600) / (2000 - 600)) * 100; const updateWidthByClientX = (clientX) => { if (!pageWidthTrackRef.current || !setContainerWidth) return; const rect = pageWidthTrackRef.current.getBoundingClientRect(); if (!rect.width) return; const ratio = (clientX - rect.left) / rect.width; const clampedRatio = Math.min(1, Math.max(0, ratio)); const rawWidth = 600 + clampedRatio * (2000 - 600); const snapped = Math.round(rawWidth / 10) * 10; setContainerWidth(snapped); }; useEffect(() => { if (!sliderDragging) return; const onPointerUp = () => setSliderDragging(false); document.addEventListener('pointerup', onPointerUp); document.addEventListener('pointercancel', onPointerUp); return () => { document.removeEventListener('pointerup', onPointerUp); document.removeEventListener('pointercancel', onPointerUp); }; }, [sliderDragging]); // 外部的 tempSeconds 变更时,同步到本地显示,但不立即生效 useEffect(() => { setLocalSeconds(tempSeconds); }, [tempSeconds]); useEffect(() => { setLocalShowMarketIndex(showMarketIndex); }, [showMarketIndex]); return ( { if (!open) onClose?.(); }} >
设置
刷新频率
{[30, 60, 120, 300].map((s) => ( ))}
setLocalSeconds(Number(e.target.value))} placeholder="自定义秒数" /> {localSeconds < 30 && (
最小 30 秒
)}
{!isMobile && setContainerWidth && (
页面宽度
{onResetContainerWidth && ( )}
{ setSliderDragging(true); updateWidthByClientX(e.clientX); e.currentTarget.setPointerCapture?.(e.pointerId); }} onPointerMove={(e) => { if (!sliderDragging) return; updateWidthByClientX(e.clientX); }} >
{clampedWidth}px
)}
数据导出
数据导入
{importMsg && (
{importMsg}
)}
{resetWidthConfirmOpen && onResetContainerWidth && ( } confirmVariant="primary" onConfirm={() => { onResetContainerWidth(); setResetWidthConfirmOpen(false); }} onCancel={() => setResetWidthConfirmOpen(false)} confirmText="重置" /> )}
); }