feat: sort type add name uptime system

This commit is contained in:
hamster1963 2024-12-16 22:50:14 +08:00
parent e988be502e
commit 27322c9d23
2 changed files with 34 additions and 4 deletions

View File

@ -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",

View File

@ -151,13 +151,29 @@ export default function Servers() {
const serverAInfo = formatNezhaInfo(nezhaWsData.now, a)
const serverBInfo = formatNezhaInfo(nezhaWsData.now, b)
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
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