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