Skip to content

Commit

Permalink
feat: dashboard link
Browse files Browse the repository at this point in the history
  • Loading branch information
hamster1963 committed Nov 24, 2024
1 parent 5364d5c commit 97087fe
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 9 deletions.
29 changes: 27 additions & 2 deletions src/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
"use client";

// import { LanguageSwitcher } from "@/components/LanguageSwitcher";
import { ModeToggle } from "@/components/ThemeSwitcher";
import { Separator } from "@/components/ui/separator";
import { Skeleton } from "@/components/ui/skeleton";
import { fetchLoginUser } from "@/lib/nezha-api";
import { useQuery } from "@tanstack/react-query";
import { DateTime } from "luxon";
import { useEffect, useRef, useState } from "react";

Expand Down Expand Up @@ -31,6 +31,7 @@ function Header() {
</p>
</section>
<section className="flex items-center gap-2">
<DashboardLink />
{/* <LanguageSwitcher /> */}
<ModeToggle />
</section>
Expand All @@ -40,6 +41,30 @@ function Header() {
);
}

function DashboardLink() {
const { data: userData } = useQuery({
queryKey: ["login-user"],
queryFn: () => fetchLoginUser(),
refetchOnMount: true,
refetchOnWindowFocus: true,
});

if (!userData?.data?.id) return null;

return (
<div className="flex items-center gap-2">
<a
href={"/dashboard"}
target="_blank"
rel="noopener noreferrer"
className="flex items-center gap-1 text-sm font-medium opacity-50 transition-opacity hover:opacity-100"
>
Dashboard
</a>
</div>
);
}

// https://github.com/streamich/react-use/blob/master/src/useInterval.ts
const useInterval = (callback: () => void, delay: number | null) => {
const savedCallback = useRef<() => void>(() => {});
Expand Down
2 changes: 0 additions & 2 deletions src/components/ServerDetailChart.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use client";

import { Card, CardContent } from "@/components/ui/card";
import { ChartConfig, ChartContainer } from "@/components/ui/chart";
import { formatNezhaInfo, formatRelativeTime } from "@/lib/utils";
Expand Down
2 changes: 0 additions & 2 deletions src/components/ThemeSwitcher.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use client";

import { Button } from "@/components/ui/button";
import {
DropdownMenu,
Expand Down
2 changes: 0 additions & 2 deletions src/components/motion/motion-provider.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
"use client";

import { LazyMotion } from "framer-motion";

const loadFeatures = () =>
Expand Down
11 changes: 10 additions & 1 deletion src/lib/nezha-api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ServerGroupResponse } from "@/types/nezha-api";
import { LoginUserResponse, ServerGroupResponse } from "@/types/nezha-api";

export const fetchServerGroup = async (): Promise<ServerGroupResponse> => {
const response = await fetch("/api/v1/server-group");
Expand All @@ -8,3 +8,12 @@ export const fetchServerGroup = async (): Promise<ServerGroupResponse> => {
}
return data;
};

export const fetchLoginUser = async (): Promise<LoginUserResponse> => {
const response = await fetch("/api/v1/profile");
const data = await response.json();
if (data.error) {
throw new Error(data.error);
}
return data;
};
11 changes: 11 additions & 0 deletions src/types/nezha-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,14 @@ export interface ServerGroup {
};
servers: number[];
}

export interface LoginUserResponse {
success: boolean;
data: {
id: number;
username: string;
password: string;
created_at: string;
updated_at: string;
};
}

0 comments on commit 97087fe

Please sign in to comment.