mirror of
https://github.com/hamster1963/nezha-dash.git
synced 2025-04-24 21:10:45 +08:00
feat: add header router.push
This commit is contained in:
parent
78da53fbfa
commit
45c12e3399
@ -28,9 +28,9 @@ import { useLocale } from "next-intl";
|
|||||||
import { useTranslations } from "next-intl";
|
import { useTranslations } from "next-intl";
|
||||||
import { useRouter } from "next/navigation";
|
import { useRouter } from "next/navigation";
|
||||||
import * as React from "react";
|
import * as React from "react";
|
||||||
|
import { useCallback, useMemo } from "react";
|
||||||
import { CartesianGrid, Line, LineChart, XAxis, YAxis } from "recharts";
|
import { CartesianGrid, Line, LineChart, XAxis, YAxis } from "recharts";
|
||||||
import useSWR from "swr";
|
import useSWR from "swr";
|
||||||
import { useMemo, useCallback } from 'react';
|
|
||||||
|
|
||||||
interface ResultItem {
|
interface ResultItem {
|
||||||
created_at: number;
|
created_at: number;
|
||||||
@ -64,7 +64,6 @@ export function NetworkChartClient({ server_id }: { server_id: number }) {
|
|||||||
|
|
||||||
if (!data) return <NetworkChartLoading />;
|
if (!data) return <NetworkChartLoading />;
|
||||||
|
|
||||||
|
|
||||||
const transformedData = transformData(data);
|
const transformedData = transformData(data);
|
||||||
|
|
||||||
const formattedData = formatData(data);
|
const formattedData = formatData(data);
|
||||||
@ -109,16 +108,23 @@ export const NetworkChart = React.memo(function NetworkChart({
|
|||||||
|
|
||||||
const [activeChart, setActiveChart] = React.useState(defaultChart);
|
const [activeChart, setActiveChart] = React.useState(defaultChart);
|
||||||
|
|
||||||
const handleButtonClick = useCallback((chart: string) => {
|
const handleButtonClick = useCallback(
|
||||||
setActiveChart(prev => prev === chart ? defaultChart : chart);
|
(chart: string) => {
|
||||||
}, [defaultChart]);
|
setActiveChart((prev) => (prev === chart ? defaultChart : chart));
|
||||||
|
},
|
||||||
|
[defaultChart],
|
||||||
|
);
|
||||||
|
|
||||||
const getColorByIndex = useCallback((chart: string) => {
|
const getColorByIndex = useCallback(
|
||||||
|
(chart: string) => {
|
||||||
const index = chartDataKey.indexOf(chart);
|
const index = chartDataKey.indexOf(chart);
|
||||||
return `hsl(var(--chart-${(index % 10) + 1}))`;
|
return `hsl(var(--chart-${(index % 10) + 1}))`;
|
||||||
}, [chartDataKey]);
|
},
|
||||||
|
[chartDataKey],
|
||||||
|
);
|
||||||
|
|
||||||
const chartButtons = useMemo(() => (
|
const chartButtons = useMemo(
|
||||||
|
() =>
|
||||||
chartDataKey.map((key) => (
|
chartDataKey.map((key) => (
|
||||||
<button
|
<button
|
||||||
key={key}
|
key={key}
|
||||||
@ -133,8 +139,9 @@ export const NetworkChart = React.memo(function NetworkChart({
|
|||||||
{chartData[key][chartData[key].length - 1].avg_delay.toFixed(2)}ms
|
{chartData[key][chartData[key].length - 1].avg_delay.toFixed(2)}ms
|
||||||
</span>
|
</span>
|
||||||
</button>
|
</button>
|
||||||
))
|
)),
|
||||||
), [chartDataKey, activeChart, chartData, handleButtonClick]);
|
[chartDataKey, activeChart, chartData, handleButtonClick],
|
||||||
|
);
|
||||||
|
|
||||||
const chartLines = useMemo(() => {
|
const chartLines = useMemo(() => {
|
||||||
if (activeChart !== defaultChart) {
|
if (activeChart !== defaultChart) {
|
||||||
@ -180,9 +187,7 @@ export const NetworkChart = React.memo(function NetworkChart({
|
|||||||
{chartDataKey.length} {t("ServerMonitorCount")}
|
{chartDataKey.length} {t("ServerMonitorCount")}
|
||||||
</CardDescription>
|
</CardDescription>
|
||||||
</div>
|
</div>
|
||||||
<div className="flex flex-wrap">
|
<div className="flex flex-wrap">{chartButtons}</div>
|
||||||
{chartButtons}
|
|
||||||
</div>
|
|
||||||
</CardHeader>
|
</CardHeader>
|
||||||
<CardContent className="px-2 sm:p-6">
|
<CardContent className="px-2 sm:p-6">
|
||||||
<ChartContainer
|
<ChartContainer
|
||||||
@ -191,7 +196,11 @@ export const NetworkChart = React.memo(function NetworkChart({
|
|||||||
>
|
>
|
||||||
<LineChart
|
<LineChart
|
||||||
accessibilityLayer
|
accessibilityLayer
|
||||||
data={activeChart === defaultChart ? formattedData : chartData[activeChart]}
|
data={
|
||||||
|
activeChart === defaultChart
|
||||||
|
? formattedData
|
||||||
|
: chartData[activeChart]
|
||||||
|
}
|
||||||
margin={{ left: 12, right: 12 }}
|
margin={{ left: 12, right: 12 }}
|
||||||
>
|
>
|
||||||
<CartesianGrid vertical={false} />
|
<CartesianGrid vertical={false} />
|
||||||
@ -235,7 +244,6 @@ export const NetworkChart = React.memo(function NetworkChart({
|
|||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
const transformData = (data: NezhaAPIMonitor[]) => {
|
const transformData = (data: NezhaAPIMonitor[]) => {
|
||||||
const monitorData: ServerMonitorChart = {};
|
const monitorData: ServerMonitorChart = {};
|
||||||
|
|
||||||
@ -255,7 +263,7 @@ const transformData = (data: NezhaAPIMonitor[]) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
return monitorData;
|
return monitorData;
|
||||||
}
|
};
|
||||||
|
|
||||||
const formatData = (rawData: NezhaAPIMonitor[]) => {
|
const formatData = (rawData: NezhaAPIMonitor[]) => {
|
||||||
const result: { [time: number]: ResultItem } = {};
|
const result: { [time: number]: ResultItem } = {};
|
||||||
|
@ -6,7 +6,9 @@ import { Separator } from "@/components/ui/separator";
|
|||||||
import getEnv from "@/lib/env-entry";
|
import getEnv from "@/lib/env-entry";
|
||||||
import { DateTime } from "luxon";
|
import { DateTime } from "luxon";
|
||||||
import { useTranslations } from "next-intl";
|
import { useTranslations } from "next-intl";
|
||||||
|
import { useLocale } from "next-intl";
|
||||||
import Image from "next/image";
|
import Image from "next/image";
|
||||||
|
import { useRouter } from "next/navigation";
|
||||||
import React, { useEffect, useRef, useState } from "react";
|
import React, { useEffect, useRef, useState } from "react";
|
||||||
|
|
||||||
function Header() {
|
function Header() {
|
||||||
@ -15,10 +17,18 @@ function Header() {
|
|||||||
const customTitle = getEnv("NEXT_PUBLIC_CustomTitle");
|
const customTitle = getEnv("NEXT_PUBLIC_CustomTitle");
|
||||||
const customDescription = getEnv("NEXT_PUBLIC_CustomDescription");
|
const customDescription = getEnv("NEXT_PUBLIC_CustomDescription");
|
||||||
|
|
||||||
|
const router = useRouter();
|
||||||
|
const locale = useLocale();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="mx-auto w-full max-w-5xl">
|
<div className="mx-auto w-full max-w-5xl">
|
||||||
<section className="flex items-center justify-between">
|
<section className="flex items-center justify-between">
|
||||||
<section className="flex items-center text-base font-medium">
|
<section
|
||||||
|
onClick={() => {
|
||||||
|
router.push(`/${locale}/`);
|
||||||
|
}}
|
||||||
|
className="flex cursor-pointer items-center text-base font-medium"
|
||||||
|
>
|
||||||
<div className="mr-1 flex flex-row items-center justify-start">
|
<div className="mr-1 flex flex-row items-center justify-start">
|
||||||
<Image
|
<Image
|
||||||
width={40}
|
width={40}
|
||||||
|
Loading…
Reference in New Issue
Block a user