mirror of
https://github.com/Buriburizaem0n/nezha-dash-v1.git
synced 2026-09-21 02:30:15 +00:00
perf: use biome
This commit is contained in:
+54
-46
@@ -1,60 +1,68 @@
|
||||
import { useEffect, useState } from "react"
|
||||
import { useEffect, useState } from "react";
|
||||
|
||||
declare global {
|
||||
interface Window {
|
||||
CustomBackgroundImage: string
|
||||
CustomMobileBackgroundImage: string
|
||||
ForceShowServices: boolean
|
||||
ForceCardInline: boolean
|
||||
ForceShowMap: boolean
|
||||
ForcePeakCutEnabled: boolean
|
||||
}
|
||||
interface Window {
|
||||
CustomBackgroundImage: string;
|
||||
CustomMobileBackgroundImage: string;
|
||||
ForceShowServices: boolean;
|
||||
ForceCardInline: boolean;
|
||||
ForceShowMap: boolean;
|
||||
ForcePeakCutEnabled: boolean;
|
||||
}
|
||||
}
|
||||
|
||||
const BACKGROUND_CHANGE_EVENT = "backgroundChange"
|
||||
const BACKGROUND_CHANGE_EVENT = "backgroundChange";
|
||||
|
||||
export function useBackground() {
|
||||
const [backgroundImage, setBackgroundImage] = useState<string | undefined>(undefined)
|
||||
const [backgroundImage, setBackgroundImage] = useState<string | undefined>(
|
||||
undefined,
|
||||
);
|
||||
|
||||
useEffect(() => {
|
||||
// 监听背景变化
|
||||
const handleBackgroundChange = () => {
|
||||
setBackgroundImage(window.CustomBackgroundImage || undefined)
|
||||
}
|
||||
useEffect(() => {
|
||||
// 监听背景变化
|
||||
const handleBackgroundChange = () => {
|
||||
setBackgroundImage(window.CustomBackgroundImage || undefined);
|
||||
};
|
||||
|
||||
// 初始化检查
|
||||
const checkInitialBackground = () => {
|
||||
if (window.CustomBackgroundImage) {
|
||||
setBackgroundImage(window.CustomBackgroundImage)
|
||||
} else {
|
||||
const savedImage = sessionStorage.getItem("savedBackgroundImage")
|
||||
if (savedImage) {
|
||||
window.CustomBackgroundImage = savedImage
|
||||
setBackgroundImage(savedImage)
|
||||
}
|
||||
}
|
||||
}
|
||||
// 初始化检查
|
||||
const checkInitialBackground = () => {
|
||||
if (window.CustomBackgroundImage) {
|
||||
setBackgroundImage(window.CustomBackgroundImage);
|
||||
} else {
|
||||
const savedImage = sessionStorage.getItem("savedBackgroundImage");
|
||||
if (savedImage) {
|
||||
window.CustomBackgroundImage = savedImage;
|
||||
setBackgroundImage(savedImage);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
// 设置一个轮询来检查初始背景
|
||||
const intervalId = setInterval(() => {
|
||||
if (window.CustomBackgroundImage || sessionStorage.getItem("savedBackgroundImage")) {
|
||||
checkInitialBackground()
|
||||
clearInterval(intervalId)
|
||||
}
|
||||
}, 100)
|
||||
// 设置一个轮询来检查初始背景
|
||||
const intervalId = setInterval(() => {
|
||||
if (
|
||||
window.CustomBackgroundImage ||
|
||||
sessionStorage.getItem("savedBackgroundImage")
|
||||
) {
|
||||
checkInitialBackground();
|
||||
clearInterval(intervalId);
|
||||
}
|
||||
}, 100);
|
||||
|
||||
window.addEventListener(BACKGROUND_CHANGE_EVENT, handleBackgroundChange)
|
||||
window.addEventListener(BACKGROUND_CHANGE_EVENT, handleBackgroundChange);
|
||||
|
||||
return () => {
|
||||
window.removeEventListener(BACKGROUND_CHANGE_EVENT, handleBackgroundChange)
|
||||
clearInterval(intervalId)
|
||||
}
|
||||
}, [])
|
||||
return () => {
|
||||
window.removeEventListener(
|
||||
BACKGROUND_CHANGE_EVENT,
|
||||
handleBackgroundChange,
|
||||
);
|
||||
clearInterval(intervalId);
|
||||
};
|
||||
}, []);
|
||||
|
||||
const updateBackground = (newBackground: string | undefined) => {
|
||||
window.CustomBackgroundImage = newBackground || ""
|
||||
window.dispatchEvent(new Event(BACKGROUND_CHANGE_EVENT))
|
||||
}
|
||||
const updateBackground = (newBackground: string | undefined) => {
|
||||
window.CustomBackgroundImage = newBackground || "";
|
||||
window.dispatchEvent(new Event(BACKGROUND_CHANGE_EVENT));
|
||||
};
|
||||
|
||||
return { backgroundImage, updateBackground }
|
||||
return { backgroundImage, updateBackground };
|
||||
}
|
||||
|
||||
@@ -1,26 +1,26 @@
|
||||
import { NezhaWebsocketResponse } from "@/types/nezha-api"
|
||||
import { useEffect, useState } from "react"
|
||||
import { useEffect, useState } from "react";
|
||||
import type { NezhaWebsocketResponse } from "@/types/nezha-api";
|
||||
|
||||
export function useChartHistory<T>(
|
||||
messageHistory: { data: string }[],
|
||||
serverId: number,
|
||||
formatFn: (wsData: NezhaWebsocketResponse, serverId: number) => T | null,
|
||||
messageHistory: { data: string }[],
|
||||
serverId: number,
|
||||
formatFn: (wsData: NezhaWebsocketResponse, serverId: number) => T | null,
|
||||
) {
|
||||
const [data, setData] = useState<T[]>([])
|
||||
const [data, setData] = useState<T[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
if (messageHistory.length > 0 && data.length === 0) {
|
||||
const historyData = messageHistory
|
||||
.map((msg) => {
|
||||
const wsData = JSON.parse(msg.data) as NezhaWebsocketResponse
|
||||
return formatFn(wsData, serverId)
|
||||
})
|
||||
.filter((item): item is T => item !== null)
|
||||
.reverse()
|
||||
useEffect(() => {
|
||||
if (messageHistory.length > 0 && data.length === 0) {
|
||||
const historyData = messageHistory
|
||||
.map((msg) => {
|
||||
const wsData = JSON.parse(msg.data) as NezhaWebsocketResponse;
|
||||
return formatFn(wsData, serverId);
|
||||
})
|
||||
.filter((item): item is T => item !== null)
|
||||
.reverse();
|
||||
|
||||
setData(historyData)
|
||||
}
|
||||
}, [messageHistory])
|
||||
setData(historyData);
|
||||
}
|
||||
}, [messageHistory, data.length, formatFn, serverId]);
|
||||
|
||||
return data
|
||||
return data;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { CommandContext } from "@/context/command-context"
|
||||
import { useContext } from "react"
|
||||
import { useContext } from "react";
|
||||
import { CommandContext } from "@/context/command-context";
|
||||
|
||||
export function useCommand() {
|
||||
const context = useContext(CommandContext)
|
||||
if (context === undefined) {
|
||||
throw new Error("useCommand must be used within a CommandProvider")
|
||||
}
|
||||
return context
|
||||
const context = useContext(CommandContext);
|
||||
if (context === undefined) {
|
||||
throw new Error("useCommand must be used within a CommandProvider");
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
@@ -1,10 +1,10 @@
|
||||
import { SortContext } from "@/context/sort-context"
|
||||
import { useContext } from "react"
|
||||
import { useContext } from "react";
|
||||
import { SortContext } from "@/context/sort-context";
|
||||
|
||||
export function useSort() {
|
||||
const context = useContext(SortContext)
|
||||
if (context === undefined) {
|
||||
throw new Error("useStatus must be used within a SortProvider")
|
||||
}
|
||||
return context
|
||||
const context = useContext(SortContext);
|
||||
if (context === undefined) {
|
||||
throw new Error("useStatus must be used within a SortProvider");
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { useContext } from "react"
|
||||
import { useContext } from "react";
|
||||
|
||||
import { StatusContext } from "../context/status-context"
|
||||
import { StatusContext } from "../context/status-context";
|
||||
|
||||
export function useStatus() {
|
||||
const context = useContext(StatusContext)
|
||||
if (context === undefined) {
|
||||
throw new Error("useStatus must be used within a StatusProvider")
|
||||
}
|
||||
return context
|
||||
const context = useContext(StatusContext);
|
||||
if (context === undefined) {
|
||||
throw new Error("useStatus must be used within a StatusProvider");
|
||||
}
|
||||
return context;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,13 @@
|
||||
import { useContext } from "react"
|
||||
import { useContext } from "react";
|
||||
|
||||
import { ThemeProviderContext } from "../components/ThemeProvider"
|
||||
import { ThemeProviderContext } from "../components/ThemeProvider";
|
||||
|
||||
export const useTheme = () => {
|
||||
const context = useContext(ThemeProviderContext)
|
||||
const context = useContext(ThemeProviderContext);
|
||||
|
||||
if (context === undefined) {
|
||||
throw new Error("useTheme must be used within a ThemeProvider")
|
||||
}
|
||||
if (context === undefined) {
|
||||
throw new Error("useTheme must be used within a ThemeProvider");
|
||||
}
|
||||
|
||||
return context
|
||||
}
|
||||
return context;
|
||||
};
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
import { TooltipContext } from "@/context/tooltip-context"
|
||||
import { useContext } from "react"
|
||||
import { useContext } from "react";
|
||||
import { TooltipContext } from "@/context/tooltip-context";
|
||||
|
||||
export const useTooltip = () => {
|
||||
const context = useContext(TooltipContext)
|
||||
if (context === undefined) {
|
||||
throw new Error("useTooltip must be used within a TooltipProvider")
|
||||
}
|
||||
return context
|
||||
}
|
||||
const context = useContext(TooltipContext);
|
||||
if (context === undefined) {
|
||||
throw new Error("useTooltip must be used within a TooltipProvider");
|
||||
}
|
||||
return context;
|
||||
};
|
||||
|
||||
export default useTooltip
|
||||
export default useTooltip;
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import { useContext } from "react"
|
||||
import { useContext } from "react";
|
||||
|
||||
import { WebSocketContext } from "../context/websocket-context"
|
||||
import { WebSocketContext } from "../context/websocket-context";
|
||||
|
||||
export const useWebSocketContext = () => {
|
||||
const context = useContext(WebSocketContext)
|
||||
if (context === undefined) {
|
||||
throw new Error("useWebSocketContext must be used within a WebSocketProvider")
|
||||
}
|
||||
return context
|
||||
}
|
||||
const context = useContext(WebSocketContext);
|
||||
if (context === undefined) {
|
||||
throw new Error(
|
||||
"useWebSocketContext must be used within a WebSocketProvider",
|
||||
);
|
||||
}
|
||||
return context;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user