diff --git a/app/[locale]/(main)/ClientComponents/ServerDetailClient.tsx b/app/[locale]/(main)/ClientComponents/ServerDetailClient.tsx new file mode 100644 index 0000000..357b96d --- /dev/null +++ b/app/[locale]/(main)/ClientComponents/ServerDetailClient.tsx @@ -0,0 +1,62 @@ +"use client"; + +import useSWR from "swr"; +import { NezhaAPISafe } from "@/app/[locale]/types/nezha-api"; +import { nezhaFetcher } from "@/lib/utils"; +import getEnv from "@/lib/env-entry"; +import { useRouter } from "next/navigation"; +import { useLocale } from "next-intl"; +import { BackIcon } from "@/components/Icon"; +import { Card, CardContent } from "@/components/ui/card"; +import { Badge } from "@/components/ui/badge"; + +export default function ServerDetailClient({ server_id }: { server_id: number }) { + const router = useRouter(); + const locale = useLocale(); + const { data, error } = useSWR( + `/api/detail?server_id=${server_id}`, + nezhaFetcher, + { + refreshInterval: + Number(getEnv("NEXT_PUBLIC_NezhaFetchInterval")) || 5000, + }, + ); + return ( +
+
{ + router.push(`/${locale}/`); + }} + className="flex flex-none cursor-pointer font-semibold leading-none items-center break-all tracking-tight gap-0.5 text-xl" + > + + HomeDash +
+
+ + +
+

+ ID +

+
{server_id}
+
+
+
+ + +
+

+ Tag +

+ {data?.tag} +
+
+
+
+
+ ) + + + +} \ No newline at end of file diff --git a/app/[locale]/(main)/detail/[id]/page.tsx b/app/[locale]/(main)/detail/[id]/page.tsx index 57d462f..a1f10cd 100644 --- a/app/[locale]/(main)/detail/[id]/page.tsx +++ b/app/[locale]/(main)/detail/[id]/page.tsx @@ -1,25 +1,7 @@ -"use client"; - -import { BackIcon } from "@/components/Icon"; -import { useLocale } from "next-intl"; -import { useRouter } from "next/navigation"; +import ServerDetailClient from "@/app/[locale]/(main)/ClientComponents/ServerDetailClient"; export default function Page({ params }: { params: { id: string } }) { - - const router = useRouter(); - const locale = useLocale(); - return ( -
-
{ - router.push(`/${locale}/`); - }} - className="flex flex-none cursor-pointer font-medium items-center break-all tracking-tight gap-0.5 text-2xl" - > - - HomeDash -
-
+ ); } diff --git a/app/[locale]/(main)/[id]/page.tsx b/app/[locale]/(main)/network/[id]/page.tsx similarity index 100% rename from app/[locale]/(main)/[id]/page.tsx rename to app/[locale]/(main)/network/[id]/page.tsx diff --git a/app/api/detail/route.ts b/app/api/detail/route.ts new file mode 100644 index 0000000..0d169a8 --- /dev/null +++ b/app/api/detail/route.ts @@ -0,0 +1,30 @@ +import { NezhaAPISafe } from "@/app/[locale]/types/nezha-api"; +import { GetServerDetail } from "@/lib/serverFetch"; +import { NextResponse } from "next/server"; + +export const dynamic = "force-dynamic"; + +interface NezhaDataResponse { + error?: string; + data?: NezhaAPISafe; +} + +export async function GET(req: Request) { + const { searchParams } = new URL(req.url); + const server_id = searchParams.get("server_id"); + if (!server_id) { + return NextResponse.json( + { error: "server_id is required" }, + { status: 400 }, + ); + } + const response = (await GetServerDetail({ + server_id: parseInt(server_id), + })) as NezhaDataResponse; + if (response.error) { + console.log(response.error); + return NextResponse.json({ error: response.error }, { status: 400 }); + } + return NextResponse.json(response, { status: 200 }); + } + \ No newline at end of file diff --git a/bunfig.toml b/bunfig.toml new file mode 100644 index 0000000..6517a5c --- /dev/null +++ b/bunfig.toml @@ -0,0 +1,2 @@ +[install] +registry = "https://registry.npmmirror.com/" diff --git a/components/ServerCard.tsx b/components/ServerCard.tsx index 7ff341e..8e55c4e 100644 --- a/components/ServerCard.tsx +++ b/components/ServerCard.tsx @@ -36,14 +36,6 @@ export default function ServerCard({ "flex flex-col items-center justify-start gap-3 p-3 md:px-5 lg:flex-row" } > - {/* - - - - - - - */}
{ - router.push(`/${locale}/${id}`); + router.push(`/${locale}/network/${id}`); }} className="flex flex-col gap-2 cursor-pointer" > @@ -113,7 +105,7 @@ export default function ServerCard({ {showNetTransfer && (
{ - router.push(`/${locale}/${id}`); + router.push(`/${locale}/network/${id}`); }} className={"flex items-center justify-between gap-1"} > diff --git a/lib/serverFetch.tsx b/lib/serverFetch.tsx index 5f3812c..16e61a1 100644 --- a/lib/serverFetch.tsx +++ b/lib/serverFetch.tsx @@ -112,3 +112,53 @@ export async function GetServerMonitor({ server_id }: { server_id: number }) { return error; } } + +export async function GetServerDetail({ server_id }: { server_id: number }) { + var nezhaBaseUrl = getEnv("NezhaBaseUrl"); + if (!nezhaBaseUrl) { + console.log("NezhaBaseUrl is not set"); + return { error: "NezhaBaseUrl is not set" }; + } + + // Remove trailing slash + if (nezhaBaseUrl[nezhaBaseUrl.length - 1] === "/") { + nezhaBaseUrl = nezhaBaseUrl.slice(0, -1); + } + + try { + const response = await fetch( + nezhaBaseUrl + `/api/v1/server/details?id=${server_id}`, + { + headers: { + Authorization: getEnv("NezhaAuth") as string, + }, + next: { + revalidate: 0, + }, + }, + ); + const resData = await response.json(); + const detailDataList = resData.result; + if (!detailDataList) { + console.log(resData); + return { error: "MonitorData fetch failed" }; + } + + const timestamp = Date.now() / 1000; + const detailData = detailDataList.map((element: MakeOptional) => { + if (timestamp - element.last_active > 300) { + element.online_status = false; + } else { + element.online_status = true; + } + delete element.ipv4; + delete element.ipv6; + delete element.valid_ip; + return element; + })[0]; + + return detailData; + } catch (error) { + return error; + } +} \ No newline at end of file