From 6c9ab908437853b4f0f0b0e7cb39a2a6192c4716 Mon Sep 17 00:00:00 2001 From: Paul Pan Date: Sun, 16 Jul 2023 22:25:07 +0800 Subject: [PATCH] fix: warnings --- src/pages/problem.tsx | 11 +++-------- src/pages/root.tsx | 40 ++++++++++++++++++++++++---------------- src/routes.tsx | 14 ++++++++++++-- 3 files changed, 39 insertions(+), 26 deletions(-) diff --git a/src/pages/problem.tsx b/src/pages/problem.tsx index 824c96b..15400fe 100644 --- a/src/pages/problem.tsx +++ b/src/pages/problem.tsx @@ -6,7 +6,6 @@ import { Descriptions, Tag, Space, - Typography, Button, } from "antd"; import { PlayCircleOutlined, SearchOutlined } from "@ant-design/icons"; @@ -23,7 +22,7 @@ export async function ProblemLoader({ params }: { params: { id: string } }) { return await ProblemApi.Details({ pid: id }); } -function ProblemPage() { +export function ProblemPage() { const details = useLoaderData() as DetailsResp; const navigate = useNavigate(); @@ -31,15 +30,13 @@ function ProblemPage() { - - {details.problem.provider.nick_name} - + {details.problem.provider.nick_name} {details.context.Languages.map((l) => ( - {l.Lang} + {l.Lang} ))} @@ -114,5 +111,3 @@ function ProblemPage() { ); } - -export { ProblemPage }; diff --git a/src/pages/root.tsx b/src/pages/root.tsx index 56692e3..62536a5 100644 --- a/src/pages/root.tsx +++ b/src/pages/root.tsx @@ -1,22 +1,35 @@ import { Link, Outlet, useLocation, useNavigation } from "react-router-dom"; -import { Layout, Menu, Skeleton, theme } from "antd"; +import { Layout, Menu, MenuProps, Skeleton, theme } from "antd"; import { NavConfigs } from "../routes.tsx"; +import { useEffect, useState } from "react"; const { Header, Footer, Content } = Layout; +const navItems: MenuProps["items"] = NavConfigs.map((c) => { + return { + label: {c.label}, + key: c.key, + }; +}); + function Root() { const { token: { colorBgContainer }, } = theme.useToken(); - const nav = useNavigation(); - const pathname = useLocation().pathname; - const pathToKey = () => { - const routes = NavConfigs.filter((c) => pathname.startsWith(c.prefix)); - if (routes.length === 0) return ["home"]; - return [routes[0].key]; - }; + const nav = useNavigation(); + const location = useLocation(); + + const [curTab, setCurTab] = useState("home"); + + useEffect(() => { + setCurTab( + NavConfigs.filter((c) => c.regex.test(location.pathname)) + .map((c) => c.key) + .concat(["home"])[0], + ); + }, [location]); return ( <> @@ -26,14 +39,9 @@ function Root() { theme="dark" mode="horizontal" defaultSelectedKeys={["home"]} - selectedKeys={pathToKey()} - > - {NavConfigs.map((c) => ( - - {c.label} - - ))} - + selectedKeys={[curTab]} + items={navItems} + />