'use client' import { useState, useEffect, useRef } from 'react' export default function ShadowSystemIndicator() { const [isShadowSystem, setIsShadowSystem] = useState(false) const [isDragging, setIsDragging] = useState(false) const [position, setPosition] = useState({ x: 0, y: 0 }) const [dragStart, setDragStart] = useState({ x: 0, y: 0 }) const indicatorRef = useRef(null) // 检测是否为影子系统 useEffect(() => { if (typeof window !== 'undefined') { const isShadow = window.location.port === '5320' || window.location.href.includes(':5320') setIsShadowSystem(isShadow) // 从 localStorage 恢复位置 const savedPosition = localStorage.getItem('shadowIndicatorPosition') if (savedPosition) { try { const pos = JSON.parse(savedPosition) setPosition(pos) } catch (e) { // 默认位置:右上角 setPosition({ x: window.innerWidth - 120, y: 80 }) } } else { // 默认位置:右上角 setPosition({ x: window.innerWidth - 120, y: 80 }) } } }, []) // 拖拽处理 const handleMouseDown = (e: React.MouseEvent) => { if (e.button !== 0) return setIsDragging(true) setDragStart({ x: e.clientX - position.x, y: e.clientY - position.y }) } useEffect(() => { const handleMouseMove = (e: MouseEvent) => { if (!isDragging) return let newX = e.clientX - dragStart.x let newY = e.clientY - dragStart.y // 限制在视窗内 if (indicatorRef.current) { const rect = indicatorRef.current.getBoundingClientRect() const maxX = window.innerWidth - rect.width const maxY = window.innerHeight - rect.height newX = Math.max(0, Math.min(newX, maxX)) newY = Math.max(0, Math.min(newY, maxY)) } setPosition({ x: newX, y: newY }) } const handleMouseUp = () => { if (isDragging) { // 保存位置到 localStorage const newPos = { x: position.x, y: position.y } localStorage.setItem('shadowIndicatorPosition', JSON.stringify(newPos)) } setIsDragging(false) } if (isDragging) { document.addEventListener('mousemove', handleMouseMove) document.addEventListener('mouseup', handleMouseUp) } return () => { document.removeEventListener('mousemove', handleMouseMove) document.removeEventListener('mouseup', handleMouseUp) } }, [isDragging, dragStart, position]) if (!isShadowSystem) return null return (
影子系统
) }