Skip to content

Commit

Permalink
feat: add header router.push
Browse files Browse the repository at this point in the history
  • Loading branch information
hamster1963 committed Oct 15, 2024
1 parent 78da53f commit 45c12e3
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 34 deletions.
74 changes: 41 additions & 33 deletions app/[locale]/(main)/ClientComponents/NetworkChart.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ import { useLocale } from "next-intl";
import { useTranslations } from "next-intl";
import { useRouter } from "next/navigation";
import * as React from "react";
import { useCallback, useMemo } from "react";
import { CartesianGrid, Line, LineChart, XAxis, YAxis } from "recharts";
import useSWR from "swr";
import { useMemo, useCallback } from 'react';

interface ResultItem {
created_at: number;
Expand Down Expand Up @@ -64,7 +64,6 @@ export function NetworkChartClient({ server_id }: { server_id: number }) {

if (!data) return <NetworkChartLoading />;


const transformedData = transformData(data);

const formattedData = formatData(data);
Expand Down Expand Up @@ -109,32 +108,40 @@ export const NetworkChart = React.memo(function NetworkChart({

const [activeChart, setActiveChart] = React.useState(defaultChart);

const handleButtonClick = useCallback((chart: string) => {
setActiveChart(prev => prev === chart ? defaultChart : chart);
}, [defaultChart]);
const handleButtonClick = useCallback(
(chart: string) => {
setActiveChart((prev) => (prev === chart ? defaultChart : chart));
},
[defaultChart],
);

const getColorByIndex = useCallback((chart: string) => {
const index = chartDataKey.indexOf(chart);
return `hsl(var(--chart-${(index % 10) + 1}))`;
}, [chartDataKey]);
const getColorByIndex = useCallback(
(chart: string) => {
const index = chartDataKey.indexOf(chart);
return `hsl(var(--chart-${(index % 10) + 1}))`;
},
[chartDataKey],
);

const chartButtons = useMemo(() => (
chartDataKey.map((key) => (
<button
key={key}
data-active={activeChart === key}
className={`relative z-30 flex flex-1 flex-col justify-center gap-1 border-b px-6 py-4 text-left data-[active=true]:bg-muted/50 sm:border-l sm:border-t-0 sm:px-6`}
onClick={() => handleButtonClick(key)}
>
<span className="whitespace-nowrap text-xs text-muted-foreground">
{key}
</span>
<span className="text-md font-bold leading-none sm:text-lg">
{chartData[key][chartData[key].length - 1].avg_delay.toFixed(2)}ms
</span>
</button>
))
), [chartDataKey, activeChart, chartData, handleButtonClick]);
const chartButtons = useMemo(
() =>
chartDataKey.map((key) => (
<button
key={key}
data-active={activeChart === key}
className={`relative z-30 flex flex-1 flex-col justify-center gap-1 border-b px-6 py-4 text-left data-[active=true]:bg-muted/50 sm:border-l sm:border-t-0 sm:px-6`}
onClick={() => handleButtonClick(key)}
>
<span className="whitespace-nowrap text-xs text-muted-foreground">
{key}
</span>
<span className="text-md font-bold leading-none sm:text-lg">
{chartData[key][chartData[key].length - 1].avg_delay.toFixed(2)}ms
</span>
</button>
)),
[chartDataKey, activeChart, chartData, handleButtonClick],
);

const chartLines = useMemo(() => {
if (activeChart !== defaultChart) {
Expand Down Expand Up @@ -180,9 +187,7 @@ export const NetworkChart = React.memo(function NetworkChart({
{chartDataKey.length} {t("ServerMonitorCount")}
</CardDescription>
</div>
<div className="flex flex-wrap">
{chartButtons}
</div>
<div className="flex flex-wrap">{chartButtons}</div>
</CardHeader>
<CardContent className="px-2 sm:p-6">
<ChartContainer
Expand All @@ -191,7 +196,11 @@ export const NetworkChart = React.memo(function NetworkChart({
>
<LineChart
accessibilityLayer
data={activeChart === defaultChart ? formattedData : chartData[activeChart]}
data={
activeChart === defaultChart
? formattedData
: chartData[activeChart]
}
margin={{ left: 12, right: 12 }}
>
<CartesianGrid vertical={false} />
Expand Down Expand Up @@ -235,7 +244,6 @@ export const NetworkChart = React.memo(function NetworkChart({
);
});


const transformData = (data: NezhaAPIMonitor[]) => {
const monitorData: ServerMonitorChart = {};

Expand All @@ -255,7 +263,7 @@ const transformData = (data: NezhaAPIMonitor[]) => {
});

return monitorData;
}
};

const formatData = (rawData: NezhaAPIMonitor[]) => {
const result: { [time: number]: ResultItem } = {};
Expand All @@ -282,4 +290,4 @@ const formatData = (rawData: NezhaAPIMonitor[]) => {
});

return Object.values(result).sort((a, b) => a.created_at - b.created_at);
};
};
12 changes: 11 additions & 1 deletion app/[locale]/(main)/header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { Separator } from "@/components/ui/separator";
import getEnv from "@/lib/env-entry";
import { DateTime } from "luxon";
import { useTranslations } from "next-intl";
import { useLocale } from "next-intl";
import Image from "next/image";
import { useRouter } from "next/navigation";
import React, { useEffect, useRef, useState } from "react";

function Header() {
Expand All @@ -15,10 +17,18 @@ function Header() {
const customTitle = getEnv("NEXT_PUBLIC_CustomTitle");
const customDescription = getEnv("NEXT_PUBLIC_CustomDescription");

const router = useRouter();
const locale = useLocale();

return (
<div className="mx-auto w-full max-w-5xl">
<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">
<Image
width={40}
Expand Down
Binary file modified bun.lockb
Binary file not shown.

0 comments on commit 45c12e3

Please sign in to comment.