From 27322c9d234d67499d5c173689a2785748e41535 Mon Sep 17 00:00:00 2001 From: hamster1963 <1410514192@qq.com> Date: Mon, 16 Dec 2024 22:50:14 +0800 Subject: [PATCH] feat: sort type add name uptime system --- src/context/sort-context.ts | 16 +++++++++++++++- src/pages/Server.tsx | 22 +++++++++++++++++++--- 2 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/context/sort-context.ts b/src/context/sort-context.ts index bb7046e..2891400 100644 --- a/src/context/sort-context.ts +++ b/src/context/sort-context.ts @@ -1,9 +1,23 @@ import { createContext } from "react" -export type SortType = "default" | "cpu" | "mem" | "stg" | "up" | "down" | "up total" | "down total" +export type SortType = + | "default" + | "name" + | "uptime" + | "system" + | "cpu" + | "mem" + | "stg" + | "up" + | "down" + | "up total" + | "down total" export const SORT_TYPES: SortType[] = [ "default", + "name", + "uptime", + "system", "cpu", "mem", "stg", diff --git a/src/pages/Server.tsx b/src/pages/Server.tsx index 46adef1..07e4d8d 100644 --- a/src/pages/Server.tsx +++ b/src/pages/Server.tsx @@ -151,13 +151,29 @@ export default function Servers() { const serverAInfo = formatNezhaInfo(nezhaWsData.now, a) const serverBInfo = formatNezhaInfo(nezhaWsData.now, b) - if (!serverAInfo.online && serverBInfo.online) return 1 - if (serverAInfo.online && !serverBInfo.online) return -1 - if (!serverAInfo.online && !serverBInfo.online) return 0 + if (sortType !== "name" && sortType !== "system") { + // 仅在非 "name" 排序时,先按在线状态排序 + if (!serverAInfo.online && serverBInfo.online) return 1 + if (serverAInfo.online && !serverBInfo.online) return -1 + if (!serverAInfo.online && !serverBInfo.online) { + // 如果两者都离线,可以继续按照其他条件排序,或者保持原序 + // 这里选择保持原序 + return 0 + } + } let comparison = 0 switch (sortType) { + case "name": + comparison = a.name.localeCompare(b.name) + break + case "uptime": + comparison = (a.state?.uptime ?? 0) - (b.state?.uptime ?? 0) + break + case "system": + comparison = a.host.platform.localeCompare(b.host.platform) + break case "cpu": comparison = (a.state?.cpu ?? 0) - (b.state?.cpu ?? 0) break