fix: prettier config

This commit is contained in:
hamster1963
2024-12-13 17:26:28 +08:00
parent 1483ce56fa
commit 9a2f3ea8e6
81 changed files with 1666 additions and 2286 deletions

View File

@@ -1,21 +1,11 @@
export function formatBytes(bytes: number, decimals: number = 2) {
if (!+bytes) return "0 Bytes";
if (!+bytes) return "0 Bytes"
const k = 1024;
const dm = decimals < 0 ? 0 : decimals;
const sizes = [
"Bytes",
"KiB",
"MiB",
"GiB",
"TiB",
"PiB",
"EiB",
"ZiB",
"YiB",
];
const k = 1024
const dm = decimals < 0 ? 0 : decimals
const sizes = ["Bytes", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB", "ZiB", "YiB"]
const i = Math.floor(Math.log(bytes) / Math.log(k));
const i = Math.floor(Math.log(bytes) / Math.log(k))
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`
}

File diff suppressed because one or more lines are too long

View File

@@ -1,7 +1,4 @@
export const countryCoordinates: Record<
string,
{ lat: number; lng: number; name: string }
> = {
export const countryCoordinates: Record<string, { lat: number; lng: number; name: string }> = {
// 亚洲
AF: { lat: 33.0, lng: 65.0, name: "Afghanistan" }, // 阿富汗
AM: { lat: 40.0, lng: 45.0, name: "Armenia" }, // 亚美尼亚
@@ -208,4 +205,4 @@ export const countryCoordinates: Record<
EH: { lat: 24.5, lng: -13.0, name: "Western Sahara" }, // 西撒哈拉
ZM: { lat: -15.0, lng: 30.0, name: "Zambia" }, // 赞比亚
ZW: { lat: -20.0, lng: 30.0, name: "Zimbabwe" }, // 津巴布韦
};
}

View File

@@ -1,4 +1,4 @@
import type { SVGProps } from "react";
import type { SVGProps } from "react"
export function GetFontLogoClass(platform: string): string {
if (
@@ -47,24 +47,24 @@ export function GetFontLogoClass(platform: string): string {
"zorin",
].indexOf(platform) > -1
) {
return platform;
return platform
}
if (platform == "darwin") {
return "apple";
return "apple"
}
if (["openwrt", "linux", "immortalwrt"].indexOf(platform) > -1) {
return "tux";
return "tux"
}
if (platform == "amazon") {
return "redhat";
return "redhat"
}
if (platform == "arch") {
return "archlinux";
return "archlinux"
}
if (platform.toLowerCase().includes("opensuse")) {
return "opensuse";
return "opensuse"
}
return "tux";
return "tux"
}
export function GetOsName(platform: string): string {
@@ -110,39 +110,33 @@ export function GetOsName(platform: string): string {
"zorin",
].indexOf(platform) > -1
) {
return platform.charAt(0).toUpperCase() + platform.slice(1);
return platform.charAt(0).toUpperCase() + platform.slice(1)
}
if (platform == "darwin") {
return "macOS";
return "macOS"
}
if (["openwrt", "linux", "immortalwrt"].indexOf(platform) > -1) {
return "Linux";
return "Linux"
}
if (platform == "amazon") {
return "Redhat";
return "Redhat"
}
if (platform == "arch") {
return "Archlinux";
return "Archlinux"
}
if (platform.toLowerCase().includes("opensuse")) {
return "Opensuse";
return "Opensuse"
}
return "Linux";
return "Linux"
}
export function MageMicrosoftWindows(props: SVGProps<SVGSVGElement>) {
return (
<svg
xmlns="http://www.w3.org/2000/svg"
width="1em"
height="1em"
viewBox="0 0 24 24"
{...props}
>
<svg xmlns="http://www.w3.org/2000/svg" width="1em" height="1em" viewBox="0 0 24 24" {...props}>
<path
fill="currentColor"
d="M2.75 7.189V2.865c0-.102 0-.115.115-.115h8.622c.128 0 .14 0 .14.128V11.5c0 .128 0 .128-.14.128H2.865c-.102 0-.115 0-.115-.116zM7.189 21.25H2.865c-.102 0-.115 0-.115-.116V12.59c0-.128 0-.128.128-.128h8.635c.102 0 .115 0 .115.115v8.57c0 .09 0 .103-.116.103zM21.25 7.189v4.31c0 .116 0 .116-.116.116h-8.557c-.102 0-.128 0-.128-.115V2.865c0-.09 0-.102.115-.102h8.48c.206 0 .206 0 .206.205zm-8.763 9.661v-4.273c0-.09 0-.115.103-.09h8.621c.026 0 0 .09 0 .142v8.518a.06.06 0 0 1-.017.06a.06.06 0 0 1-.06.017H12.54s-.09 0-.077-.09V16.85z"
></path>
</svg>
);
)
}

View File

@@ -4,51 +4,49 @@ import {
ServerGroupResponse,
ServiceResponse,
SettingResponse,
} from "@/types/nezha-api";
} from "@/types/nezha-api"
export const fetchServerGroup = async (): Promise<ServerGroupResponse> => {
const response = await fetch("/api/v1/server-group");
const data = await response.json();
const response = await fetch("/api/v1/server-group")
const data = await response.json()
if (data.error) {
throw new Error(data.error);
throw new Error(data.error)
}
return data;
};
return data
}
export const fetchLoginUser = async (): Promise<LoginUserResponse> => {
const response = await fetch("/api/v1/profile");
const data = await response.json();
const response = await fetch("/api/v1/profile")
const data = await response.json()
if (data.error) {
throw new Error(data.error);
throw new Error(data.error)
}
return data;
};
return data
}
export const fetchMonitor = async (
server_id: number,
): Promise<MonitorResponse> => {
const response = await fetch(`/api/v1/service/${server_id}`);
const data = await response.json();
export const fetchMonitor = async (server_id: number): Promise<MonitorResponse> => {
const response = await fetch(`/api/v1/service/${server_id}`)
const data = await response.json()
if (data.error) {
throw new Error(data.error);
throw new Error(data.error)
}
return data;
};
return data
}
export const fetchService = async (): Promise<ServiceResponse> => {
const response = await fetch("/api/v1/service");
const data = await response.json();
const response = await fetch("/api/v1/service")
const data = await response.json()
if (data.error) {
throw new Error(data.error);
throw new Error(data.error)
}
return data;
};
return data
}
export const fetchSetting = async (): Promise<SettingResponse> => {
const response = await fetch("/api/v1/setting");
const data = await response.json();
const response = await fetch("/api/v1/setting")
const data = await response.json()
if (data.error) {
throw new Error(data.error);
throw new Error(data.error)
}
return data;
};
return data
}

View File

@@ -1,15 +1,15 @@
import { NezhaServer } from "@/types/nezha-api";
import { type ClassValue, clsx } from "clsx";
import { twMerge } from "tailwind-merge";
import { NezhaServer } from "@/types/nezha-api"
import { type ClassValue, clsx } from "clsx"
import { twMerge } from "tailwind-merge"
export function cn(...inputs: ClassValue[]) {
return twMerge(clsx(inputs));
return twMerge(clsx(inputs))
}
export function formatNezhaInfo(now: number, serverInfo: NezhaServer) {
const lastActiveTime = serverInfo.last_active.startsWith("000")
? 0
: parseISOTimestamp(serverInfo.last_active);
: parseISOTimestamp(serverInfo.last_active)
return {
...serverInfo,
cpu: serverInfo.state.cpu || 0,
@@ -17,9 +17,7 @@ export function formatNezhaInfo(now: number, serverInfo: NezhaServer) {
process: serverInfo.state.process_count || 0,
up: serverInfo.state.net_out_speed / 1024 / 1024 || 0,
down: serverInfo.state.net_in_speed / 1024 / 1024 || 0,
last_active_time_string: lastActiveTime
? new Date(lastActiveTime).toLocaleString()
: "",
last_active_time_string: lastActiveTime ? new Date(lastActiveTime).toLocaleString() : "",
online: now - lastActiveTime <= 30000,
uptime: serverInfo.state.uptime || 0,
version: serverInfo.host.version || null,
@@ -45,88 +43,88 @@ export function formatNezhaInfo(now: number, serverInfo: NezhaServer) {
load_5: serverInfo.state.load_5?.toFixed(2) || 0.0,
load_15: serverInfo.state.load_15?.toFixed(2) || 0.0,
public_note: handlePublicNote(serverInfo.id, serverInfo.public_note || ""),
};
}
}
export function getDaysBetweenDates(date1: string, date2: string): number {
const oneDay = 24 * 60 * 60 * 1000; // 一天的毫秒数
const firstDate = new Date(date1);
const secondDate = new Date(date2);
const oneDay = 24 * 60 * 60 * 1000 // 一天的毫秒数
const firstDate = new Date(date1)
const secondDate = new Date(date2)
// 计算两个日期之间的天数差异
return Math.round((firstDate.getTime() - secondDate.getTime()) / oneDay);
return Math.round((firstDate.getTime() - secondDate.getTime()) / oneDay)
}
export const fetcher = (url: string) =>
fetch(url)
.then((res) => {
if (!res.ok) {
throw new Error(res.statusText);
throw new Error(res.statusText)
}
return res.json();
return res.json()
})
.then((data) => data.data)
.catch((err) => {
console.error(err);
throw err;
});
console.error(err)
throw err
})
export const nezhaFetcher = async (url: string) => {
const res = await fetch(url);
const res = await fetch(url)
if (!res.ok) {
const error = new Error("An error occurred while fetching the data.");
const error = new Error("An error occurred while fetching the data.")
// @ts-expect-error - res.json() returns a Promise<any>
error.info = await res.json();
error.info = await res.json()
// @ts-expect-error - res.status is a number
error.status = res.status;
throw error;
error.status = res.status
throw error
}
return res.json();
};
return res.json()
}
export function parseISOTimestamp(isoString: string): number {
return new Date(isoString).getTime();
return new Date(isoString).getTime()
}
export function formatRelativeTime(timestamp: number): string {
const now = Date.now();
const diff = now - timestamp;
const hours = Math.floor(diff / (1000 * 60 * 60));
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((diff % (1000 * 60)) / 1000);
const now = Date.now()
const diff = now - timestamp
const hours = Math.floor(diff / (1000 * 60 * 60))
const minutes = Math.floor((diff % (1000 * 60 * 60)) / (1000 * 60))
const seconds = Math.floor((diff % (1000 * 60)) / 1000)
if (hours > 24) {
const days = Math.floor(hours / 24);
return `${days}d`;
const days = Math.floor(hours / 24)
return `${days}d`
} else if (hours > 0) {
return `${hours}h`;
return `${hours}h`
} else if (minutes > 0) {
return `${minutes}m`;
return `${minutes}m`
} else if (seconds >= 0) {
return `${seconds}s`;
return `${seconds}s`
}
return "0s";
return "0s"
}
export function formatTime(timestamp: number): string {
const date = new Date(timestamp);
const year = date.getFullYear();
const month = date.getMonth() + 1;
const day = date.getDate();
const hours = date.getHours().toString().padStart(2, "0");
const minutes = date.getMinutes().toString().padStart(2, "0");
const seconds = date.getSeconds().toString().padStart(2, "0");
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
const date = new Date(timestamp)
const year = date.getFullYear()
const month = date.getMonth() + 1
const day = date.getDate()
const hours = date.getHours().toString().padStart(2, "0")
const minutes = date.getMinutes().toString().padStart(2, "0")
const seconds = date.getSeconds().toString().padStart(2, "0")
return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`
}
interface BillingData {
startDate: string;
endDate: string;
autoRenewal: string;
cycle: string;
amount: string;
startDate: string
endDate: string
autoRenewal: string
cycle: string
amount: string
}
// interface PlanData {
@@ -140,16 +138,16 @@ interface BillingData {
// }
interface PublicNoteData {
billingDataMod: BillingData;
billingDataMod: BillingData
// planDataMod: PlanData;
}
export function parsePublicNote(publicNote: string): PublicNoteData | null {
try {
if (!publicNote) {
return null;
return null
}
const data = JSON.parse(publicNote);
const data = JSON.parse(publicNote)
return {
billingDataMod: {
startDate: data.billingDataMod.startDate || "",
@@ -167,26 +165,26 @@ export function parsePublicNote(publicNote: string): PublicNoteData | null {
// networkRoute: data.planDataMod.networkRoute || "",
// extra: data.planDataMod.extra || "",
// },
};
}
} catch (error) {
console.error("Error parsing public note:", error);
return null;
console.error("Error parsing public note:", error)
return null
}
}
// Function to handle public_note with sessionStorage
export function handlePublicNote(serverId: number, publicNote: string): string {
const storageKey = `server_${serverId}_public_note`;
const storedNote = sessionStorage.getItem(storageKey);
const storageKey = `server_${serverId}_public_note`
const storedNote = sessionStorage.getItem(storageKey)
if (!publicNote && storedNote) {
return storedNote;
return storedNote
}
if (publicNote) {
sessionStorage.setItem(storageKey, publicNote);
return publicNote;
sessionStorage.setItem(storageKey, publicNote)
return publicNote
}
return "";
return ""
}