From aff92737158e48e6716867b8d4cbe875a3bee8cf Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Thu, 26 Sep 2024 17:57:49 +0800 Subject: [PATCH 1/5] refactor: better error state --- .../ClientComponents/ServerListClient.tsx | 11 ++++++++- app/[locale]/(main)/page.tsx | 24 ++++--------------- app/api/server/route.ts | 24 ++++++++++--------- lib/serverFetch.tsx | 12 ++++++---- 4 files changed, 36 insertions(+), 35 deletions(-) diff --git a/app/[locale]/(main)/ClientComponents/ServerListClient.tsx b/app/[locale]/(main)/ClientComponents/ServerListClient.tsx index 756c5ca..8a20f66 100644 --- a/app/[locale]/(main)/ClientComponents/ServerListClient.tsx +++ b/app/[locale]/(main)/ClientComponents/ServerListClient.tsx @@ -6,9 +6,18 @@ import { nezhaFetcher } from "../../../../lib/utils"; import useSWR from "swr"; import getEnv from "../../../../lib/env-entry"; export default function ServerListClient() { - const { data } = useSWR("/api/server", nezhaFetcher, { + const { data, error } = useSWR("/api/server", nezhaFetcher, { refreshInterval: Number(getEnv("NEXT_PUBLIC_NezhaFetchInterval")) || 2000, }); + if (error) + return ( +
+

{error.message}

+

+ Please check your env variables +

+
+ ); if (!data) return null; const sortedServers = data.result.sort((a, b) => { if (a.display_index && b.display_index) { diff --git a/app/[locale]/(main)/page.tsx b/app/[locale]/(main)/page.tsx index e6b1cf1..1f4cdb6 100644 --- a/app/[locale]/(main)/page.tsx +++ b/app/[locale]/(main)/page.tsx @@ -1,25 +1,11 @@ import ServerList from "../../../components/ServerList"; import ServerOverview from "../../../components/ServerOverview"; -import getEnv from "../../../lib/env-entry"; -import { GetNezhaData } from "../../../lib/serverFetch"; -import { SWRConfig } from "swr"; -const disablePrefetch = getEnv("ServerDisablePrefetch") === "true"; -const fallback = disablePrefetch - ? {} - : { - "/api/server": GetNezhaData(), - }; + export default function Home() { return ( - -
- - -
-
+
+ + +
); } diff --git a/app/api/server/route.ts b/app/api/server/route.ts index f47afb7..e34a83c 100644 --- a/app/api/server/route.ts +++ b/app/api/server/route.ts @@ -1,17 +1,19 @@ +import { ServerApi } from "@/app/[locale]/types/nezha-api"; import { GetNezhaData } from "@/lib/serverFetch"; import { NextResponse } from "next/server"; export const dynamic = "force-dynamic"; -export async function GET(_: Request) { - try { - const response = await GetNezhaData(); - return NextResponse.json(response, { status: 200 }); - } catch (error) { - console.error(error); - return NextResponse.json( - { error: "fetch nezha data failed" }, - { status: 400 }, - ); - } +interface NezhaDataResponse { + error?: string; + data?: ServerApi; +} + +export async function GET(_: Request) { + const response = (await GetNezhaData()) as NezhaDataResponse; + if (response.error) { + console.log(response.error); + return NextResponse.json({ error: response.error }, { status: 400 }); + } + return NextResponse.json(response, { status: 200 }); } diff --git a/lib/serverFetch.tsx b/lib/serverFetch.tsx index 51687a8..d8fa983 100644 --- a/lib/serverFetch.tsx +++ b/lib/serverFetch.tsx @@ -2,7 +2,6 @@ import { NezhaAPI, ServerApi } from "../app/[locale]/types/nezha-api"; import { MakeOptional } from "../app/[locale]/types/utils"; -import { error } from "console"; import { unstable_noStore as noStore } from "next/cache"; import getEnv from "./env-entry"; @@ -11,8 +10,8 @@ export async function GetNezhaData() { var nezhaBaseUrl = getEnv("NezhaBaseUrl"); if (!nezhaBaseUrl) { - error("NezhaBaseUrl is not set"); - return; + console.log("NezhaBaseUrl is not set"); + return { error: "NezhaBaseUrl is not set" }; } // Remove trailing slash @@ -28,7 +27,12 @@ export async function GetNezhaData() { revalidate: 0, }, }); - const nezhaData = (await response.json()).result as NezhaAPI[]; + const resData = await response.json(); + const nezhaData = resData.result as NezhaAPI[]; + if (!nezhaData) { + console.log(resData); + return { error: "NezhaData fetch failed" }; + } const data: ServerApi = { live_servers: 0, offline_servers: 0, From c26dc99502d2d3719f2ccf29463dd0749d2734c0 Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Thu, 26 Sep 2024 17:59:52 +0800 Subject: [PATCH 2/5] doc: new env example --- .env.example | 9 ++++----- README.md | 1 - docker/.env.example | 5 ++--- 3 files changed, 6 insertions(+), 9 deletions(-) diff --git a/.env.example b/.env.example index b64cef0..b69c8b8 100644 --- a/.env.example +++ b/.env.example @@ -1,6 +1,5 @@ -NezhaBaseUrl=http://1.1.1.1:8008 -NezhaAuth=nezha-token -ServerDisablePrefetch=false -NEXT_PUBLIC_NezhaFetchInterval=2000 +NezhaBaseUrl=http://124.XX.XX.XX:8008 +NezhaAuth=your-nezha-api-token +NEXT_PUBLIC_NezhaFetchInterval=5000 NEXT_PUBLIC_ShowFlag=true -NEXT_PUBLIC_DisableCartoon=false \ No newline at end of file +NEXT_PUBLIC_DisableCartoon=true \ No newline at end of file diff --git a/README.md b/README.md index bf886ce..72059bc 100644 --- a/README.md +++ b/README.md @@ -16,7 +16,6 @@ | ------------------------------ | -------------------- | -------------------------------- | | NezhaBaseUrl | nezha 面板地址 | http://120.x.x.x:8008 | | NezhaAuth | nezha 面板 API Token | 5hAY3QX6Nl9B3Uxxxx26KMvOMyXS1Udi | -| ServerDisablePrefetch | 是否禁用预加载 | **默认**:false | | NEXT_PUBLIC_NezhaFetchInterval | 获取数据间隔(毫秒) | **默认**:2000 | | NEXT_PUBLIC_ShowFlag | 是否显示旗帜 | **默认**:false | | NEXT_PUBLIC_DisableCartoon | 是否禁用卡通人物 | **默认**:false | diff --git a/docker/.env.example b/docker/.env.example index b811421..b69c8b8 100644 --- a/docker/.env.example +++ b/docker/.env.example @@ -1,6 +1,5 @@ -NezhaBaseUrl=http://0.0.0.0:8008 -NezhaAuth=5hAY3QX6Nl9B3UOQgB26KdsdS1dsdUdM -ServerDisablePrefetch=false +NezhaBaseUrl=http://124.XX.XX.XX:8008 +NezhaAuth=your-nezha-api-token NEXT_PUBLIC_NezhaFetchInterval=5000 NEXT_PUBLIC_ShowFlag=true NEXT_PUBLIC_DisableCartoon=true \ No newline at end of file From f53766389240d578d1765f05158479271961249c Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Thu, 26 Sep 2024 18:03:03 +0800 Subject: [PATCH 3/5] doc: error text --- app/[locale]/(main)/ClientComponents/ServerListClient.tsx | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/[locale]/(main)/ClientComponents/ServerListClient.tsx b/app/[locale]/(main)/ClientComponents/ServerListClient.tsx index 8a20f66..c111d03 100644 --- a/app/[locale]/(main)/ClientComponents/ServerListClient.tsx +++ b/app/[locale]/(main)/ClientComponents/ServerListClient.tsx @@ -14,7 +14,7 @@ export default function ServerListClient() {

{error.message}

- Please check your env variables + Please check your environment variables and review the server console logs for more details.

); From b3f8e8ca8e136345882c3468bc2be9af31a12023 Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Sun, 29 Sep 2024 11:10:25 +0800 Subject: [PATCH 4/5] fix: display index sort --- .../ClientComponents/ServerListClient.tsx | 32 +++++++++++++------ 1 file changed, 23 insertions(+), 9 deletions(-) diff --git a/app/[locale]/(main)/ClientComponents/ServerListClient.tsx b/app/[locale]/(main)/ClientComponents/ServerListClient.tsx index c111d03..ab96e56 100644 --- a/app/[locale]/(main)/ClientComponents/ServerListClient.tsx +++ b/app/[locale]/(main)/ClientComponents/ServerListClient.tsx @@ -14,19 +14,33 @@ export default function ServerListClient() {

{error.message}

- Please check your environment variables and review the server console logs for more details. + Please check your environment variables and review the server console + logs for more details.

); if (!data) return null; - const sortedServers = data.result.sort((a, b) => { - if (a.display_index && b.display_index) { - return b.display_index - a.display_index; - } - if (a.display_index) return -1; - if (b.display_index) return 1; - return a.id - b.id; - }); + + const { result } = data; + + const positiveDisplayIndex = result + .filter((server) => server.display_index > 0) + .sort((a, b) => b.display_index - a.display_index); + + const noDisplayIndex = result + .filter((server) => !server.display_index) + .sort((a, b) => a.id - b.id); + + const negativeDisplayIndex = result + .filter((server) => server.display_index < 0) + .sort((a, b) => b.display_index - a.display_index); + + const sortedServers = [ + ...positiveDisplayIndex, + ...noDisplayIndex, + ...negativeDisplayIndex, + ]; + return (
{sortedServers.map((serverInfo) => ( From 9d02f35522c8cc39bc80575ccc16c0467d5e0f6f Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Sun, 29 Sep 2024 11:28:01 +0800 Subject: [PATCH 5/5] feat: add DefaultLocale config --- .env.example | 1 + README.md | 15 ++++++++------- docker/.env.example | 1 + i18n-metadata.ts | 4 +++- middleware.ts | 4 ++-- 5 files changed, 15 insertions(+), 10 deletions(-) diff --git a/.env.example b/.env.example index b69c8b8..e2c860e 100644 --- a/.env.example +++ b/.env.example @@ -1,5 +1,6 @@ NezhaBaseUrl=http://124.XX.XX.XX:8008 NezhaAuth=your-nezha-api-token +DefaultLocale=zh NEXT_PUBLIC_NezhaFetchInterval=5000 NEXT_PUBLIC_ShowFlag=true NEXT_PUBLIC_DisableCartoon=true \ No newline at end of file diff --git a/README.md b/README.md index 72059bc..2291b6d 100644 --- a/README.md +++ b/README.md @@ -12,13 +12,14 @@ #### 环境变量 -| 变量名 | 含义 | 示例 | -| ------------------------------ | -------------------- | -------------------------------- | -| NezhaBaseUrl | nezha 面板地址 | http://120.x.x.x:8008 | -| NezhaAuth | nezha 面板 API Token | 5hAY3QX6Nl9B3Uxxxx26KMvOMyXS1Udi | -| NEXT_PUBLIC_NezhaFetchInterval | 获取数据间隔(毫秒) | **默认**:2000 | -| NEXT_PUBLIC_ShowFlag | 是否显示旗帜 | **默认**:false | -| NEXT_PUBLIC_DisableCartoon | 是否禁用卡通人物 | **默认**:false | +| 变量名 | 含义 | 示例 | +| ------------------------------ | -------------------------------- | -------------------------------- | +| NezhaBaseUrl | nezha 面板地址 | http://120.x.x.x:8008 | +| NezhaAuth | nezha 面板 API Token | 5hAY3QX6Nl9B3Uxxxx26KMvOMyXS1Udi | +| DefaultLocale | 面板默认显示语言(代码参考下表) | **默认**:en | +| NEXT_PUBLIC_NezhaFetchInterval | 获取数据间隔(毫秒) | **默认**:2000 | +| NEXT_PUBLIC_ShowFlag | 是否显示旗帜 | **默认**:false | +| NEXT_PUBLIC_DisableCartoon | 是否禁用卡通人物 | **默认**:false | #### 多语言支持 diff --git a/docker/.env.example b/docker/.env.example index b69c8b8..e2c860e 100644 --- a/docker/.env.example +++ b/docker/.env.example @@ -1,5 +1,6 @@ NezhaBaseUrl=http://124.XX.XX.XX:8008 NezhaAuth=your-nezha-api-token +DefaultLocale=zh NEXT_PUBLIC_NezhaFetchInterval=5000 NEXT_PUBLIC_ShowFlag=true NEXT_PUBLIC_DisableCartoon=true \ No newline at end of file diff --git a/i18n-metadata.ts b/i18n-metadata.ts index acf3a7c..3151be8 100644 --- a/i18n-metadata.ts +++ b/i18n-metadata.ts @@ -1,5 +1,7 @@ // @auto-i18n-check. Please do not delete the line. +import getEnv from "./lib/env-entry"; + export const localeItems = [ { code: "en", name: "English" }, { code: "ja", name: "日本語" }, @@ -21,4 +23,4 @@ export const localeItems = [ ]; export const locales = localeItems.map((item) => item.code); -export const defaultLocale = "en"; +export const defaultLocale = getEnv("DefaultLocale") || "en"; diff --git a/middleware.ts b/middleware.ts index a4b787c..ebcec7e 100644 --- a/middleware.ts +++ b/middleware.ts @@ -1,14 +1,14 @@ // @auto-i18n-check. Please do not delete the line. import createMiddleware from "next-intl/middleware"; -import { locales } from "./i18n-metadata"; +import { defaultLocale, locales } from "./i18n-metadata"; export default createMiddleware({ // A list of all locales that are supported locales: locales, // Used when no locale matches - defaultLocale: "en", + defaultLocale: defaultLocale, // 'always': This is the default, The home page will also be redirected to the default language, such as www.abc.com to www.abc.com/en // 'as-needed': The default page is not redirected. For example, if you open www.abc.com, it is still www.abc.com