feat(All) 修改结果展示样式,增加测试地区覆盖范围

This commit is contained in:
2025-12-19 15:21:55 +08:00
parent 2f6831336e
commit 1a0815759e
15 changed files with 1336 additions and 368 deletions

View File

@@ -3,32 +3,39 @@ import { fetchUserIp } from '../api/latency'
import './IpInput.css'
interface IpInputProps {
onTest: (ip: string) => void
onTest: (target: string) => void
testing: boolean
}
const IP_REGEX = /^(?:(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)\.){3}(?:25[0-5]|2[0-4]\d|1\d{2}|[1-9]?\d)$/
const DOMAIN_REGEX = /^(?!:\/\/)([a-zA-Z0-9-]+\.)+[a-zA-Z]{2,}$/
function isValidTarget(value: string): boolean {
const trimmed = value.trim().toLowerCase()
return IP_REGEX.test(trimmed) || DOMAIN_REGEX.test(trimmed)
}
export default function IpInput({ onTest, testing }: IpInputProps) {
const [ip, setIp] = useState('')
const [target, setTarget] = useState('')
const [loading, setLoading] = useState(true)
const [error, setError] = useState('')
useEffect(() => {
fetchUserIp()
.then(setIp)
.then(setTarget)
.catch(() => setError('Failed to detect IP'))
.finally(() => setLoading(false))
}, [])
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault()
if (!IP_REGEX.test(ip)) {
setError('Invalid IP address')
const trimmed = target.trim()
if (!isValidTarget(trimmed)) {
setError('Invalid IP address or domain')
return
}
setError('')
onTest(ip)
onTest(trimmed)
}
return (
@@ -36,18 +43,18 @@ export default function IpInput({ onTest, testing }: IpInputProps) {
<div className="input-wrapper">
<input
type="text"
value={ip}
value={target}
onChange={(e) => {
setIp(e.target.value)
setTarget(e.target.value)
setError('')
}}
placeholder={loading ? 'Detecting IP...' : 'Enter IP address'}
placeholder={loading ? 'Detecting IP...' : 'Enter IP or domain (e.g., 8.8.8.8 or google.com)'}
className={`ip-input ${error ? 'ip-input-error' : ''}`}
disabled={testing || loading}
/>
{error && <span className="error-hint">{error}</span>}
</div>
<button type="submit" className="test-button" disabled={testing || loading || !ip}>
<button type="submit" className="test-button" disabled={testing || loading || !target.trim()}>
{testing ? (
<>
<span className="spinner" />