feat: update Checker

This commit is contained in:
thehrz 2024-08-21 11:23:38 +08:00
parent 1adb529d2e
commit 6e3ceffb95
Signed by: thehrz
GPG Key ID: C84CBCE7D5F88855

View File

@ -1,21 +1,19 @@
"use client"
import CheckItem from "./CheckItem"
import { CheckItemType } from "@/types/CheckItem"
import { useRef, useEffect, useState } from "react"
import { useEffect, useState } from "react"
import {
QueryClient,
QueryClientProvider,
useQueryClient,
} from "@tanstack/react-query"
const queryClient = new QueryClient()
export default function Checker() {
// const IPv4 = useRef(null)
const [IPv6, setIPv6] = useState(null)
const [IP, setIP] = useState(null)
useEffect(() => {
if (IPv6) {
if (IP) {
queryClient.invalidateQueries(
{
queryKey: ['ISP'],
@ -23,18 +21,16 @@ export default function Checker() {
},
)
}
}, [IPv6])
}, [IP])
const checkList: CheckItemType[] = [
{
id: "IPv6",
name: "公网 IPv6 地址",
fetcher: () => fetch("/api/ip/myip").then((res) => res.json()),
fetcher: () => fetch("https://6.ipv6test.online/ip/myip").then((res) => res.json()),
content: (data) => {
const success = data?.version == 6
if (success && data.address != null) {
setIPv6(data.address)
}
return {
state: success ? "ok" : "error",
text: success ? `${data?.address}` : "未检测到",
@ -45,14 +41,10 @@ export default function Checker() {
id: "IPv4",
name: "公网 IPv4 地址",
fetcher: () =>
fetch("https://test.ipv6test.online/ip/myip").then((res) => res.json()),
fetch("https://4.ipv6test.online/ip/myip").then((res) => res.json()),
content: (data) => {
const success = data?.version == 4
// if (success && data.address != null) {
// IPv4.current = data.address
// }
return {
state: success ? "ok" : "error",
text: success ? `${data?.address}` : "未检测到",
@ -62,11 +54,14 @@ export default function Checker() {
{
id: "ipv4andipv6",
name: "双协议栈测试",
fetcher: () => fetch("/api/ip/myip").then((res) => res.json()),
fetcher: () => fetch("https://test.ipv6test.online/ip/myip").then((res) => res.json()),
content: (data) => {
const isIPv6 = data?.version == 6
const isIPv4 = data?.version == 4
if (data.address != null) {
setIP(data.address)
}
return {
state: isIPv6 ? "ok" : "error",
text: isIPv6 ? "IPv6访问优先" : isIPv4 ? "IPv4访问优先" : "未检测到",
@ -80,13 +75,13 @@ export default function Checker() {
fetch("https://test.ipv6test.online/ip/isp", {
method: "POST",
body: JSON.stringify({
address: IPv6,
address: IP,
}),
}).then((res) => res.json()),
enabled: () => !!IPv6, // 确保在检测到 IPv6 地址时也进行 ISP 检查
enabled: () => !!IP,
content: (data) => {
return {
state: data.isp ? "ok" : "error", // 确保返回 state 字段
state: data.isp ? "ok" : "error",
text: data.isp || "未检测到",
}
},