Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: navigate to top #2170

Merged
merged 4 commits into from
Jan 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions src/app/ContestSite/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import React from "react";
import { Route, Routes, Navigate } from "react-router-dom";
import React, { useEffect } from "react";
import {
Route,
Routes,
Navigate,
useLocation,
useNavigationType,
} from "react-router-dom";
import MenuPage from "./MenuPage";
import dayjs from "dayjs";
import { useUrl } from "../../api/hooks/url";
Expand All @@ -19,6 +25,17 @@ dayjs.extend(utc);
const ContestSite: React.FC<PageProps> = ({ mode, user }) => {
/* ---------------- States 和常量 Hooks ---------------- */
const url = useUrl();
const navigationType = useNavigationType(); // 获取导航类型
const location = useLocation(); // 获取当前位置

useEffect(() => {
if (navigationType !== "POP") {
window.scrollTo({
top: 0,
behavior: "smooth",
});
}
}, [location.pathname, navigationType]);
/* ---------------- 从数据库获取数据的 Hooks ---------------- */
/* ---------------- useEffect ---------------- */
/* ---------------- 业务逻辑函数 ---------------- */
Expand Down
20 changes: 19 additions & 1 deletion src/app/HomeSite/index.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import React, { Suspense, useEffect, useRef, useState } from "react";
import { Layout, Spin, Card, Tooltip } from "antd";
import styled from "styled-components";
import { Route, Navigate, Routes, useNavigate } from "react-router-dom";
import {
Route,
Navigate,
Routes,
useNavigate,
useLocation,
useNavigationType,
} from "react-router-dom";
import NewsPage from "./NewsPage";
import NotFoundPage from "../Components/NotFound";
import { useUrl } from "../../api/hooks/url";
Expand Down Expand Up @@ -173,6 +180,17 @@ const HomeSite: React.FC<PageProps> = ({ mode, user }) => {
const url = useUrl();
const navigate = useNavigate();
const { Content } = Layout;
const navigationType = useNavigationType(); // 获取导航类型
const location = useLocation(); // 获取当前位置

useEffect(() => {
if (navigationType !== "POP") {
window.scrollTo({
top: 0,
behavior: "smooth",
});
}
}, [location.pathname, navigationType]);

// 加载组件
const Loading = () => (
Expand Down
21 changes: 20 additions & 1 deletion src/app/InfoSite/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
import React, { Suspense, useEffect, useState } from "react";
import { Route, Link, Routes, Navigate, useNavigate } from "react-router-dom";
import {
Route,
Link,
Routes,
Navigate,
useNavigate,
useLocation,
useNavigationType,
} from "react-router-dom";
import { Button, Layout, Menu, MenuProps, message } from "antd";
import {
TrophyOutlined,
Expand Down Expand Up @@ -40,6 +48,17 @@ const InfoSite: React.FC<PageProps> = ({ mode, user }) => {
const savedOpenKeys = sessionStorage.getItem("openKeys");
return savedOpenKeys ? JSON.parse(savedOpenKeys) : [];
});
const navigationType = useNavigationType(); // 获取导航类型
const location = useLocation(); // 获取当前位置

useEffect(() => {
if (navigationType !== "POP") {
window.scrollTo({
top: 0,
behavior: "smooth",
});
}
}, [location.pathname, navigationType]);

// 菜单项和子菜单项
const items = [
Expand Down
14 changes: 7 additions & 7 deletions src/app/ShareSite/CoursePage/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import React, { useState, useEffect } from "react";
import { Button, Layout, message } from "antd";
import { PlusOutlined } from "@ant-design/icons";
import { Layout, message } from "antd";
// import { PlusOutlined } from "@ant-design/icons";
//import { useUrl } from "../../../api/hooks/url";
import * as graphql from "@/generated/graphql";
import { ProColumns, ProTable } from "@ant-design/pro-components";
Expand Down Expand Up @@ -211,11 +211,11 @@ const CoursesPage: React.FC<PageProps> = ({ mode, user }) => {
}}
dateFormatter="string"
headerTitle="课程列表"
toolBarRender={() => [
<Button key="button" icon={<PlusOutlined />} type="primary">
导入
</Button>,
]}
// toolBarRender={() => [
// <Button key="button" icon={<PlusOutlined />} type="primary">
// 导入
// </Button>,
// ]}
/>
</Layout>
);
Expand Down
6 changes: 6 additions & 0 deletions src/app/ShareSite/IntroPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ const IntroPage: React.FC<PageProps> = ({ mode, user }) => {
/* ---------------- 页面组件 ---------------- */
return (
<Space direction="vertical" size="small" style={{ width: "100%" }}>
<Resource
image={`${process.env.REACT_APP_STATIC_URL}/public/images/course_review.jpg`}
title="课程评测"
text="由软件部搭建的课程评测平台,包含了电子系的核心必修、专业限选、限选实验等课程,可以在此查看课程信息,进行课程讨论、课程评价。"
link={url.link("course")}
/>
<Resource
image={`${process.env.REACT_APP_STATIC_URL}/public/images/eesast_docs.png`}
title="EESAST DOCS"
Expand Down
6 changes: 1 addition & 5 deletions src/app/ShareSite/MinecraftPage.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect } from "react";
import React from "react";
import { Typography, Space, Image, Card, Row, Col } from "antd";
import { SmileOutlined } from "@ant-design/icons";
import { PageProps } from "..";
Expand All @@ -9,10 +9,6 @@ const { Meta } = Card;

/* ---------------- 主页面 ---------------- */
const MinecraftPage: React.FC<PageProps> = ({ mode, user }) => {
useEffect(() => {
window.scrollTo(0, 0);
}, []);

/* ---------------- 页面组件 ---------------- */
return (
<Space
Expand Down
39 changes: 26 additions & 13 deletions src/app/ShareSite/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import React, { Suspense } from "react";
import { Link, Navigate, Route, Routes } from "react-router-dom";
import React, { Suspense, useEffect } from "react";
import {
Link,
Navigate,
Route,
Routes,
useLocation,
useNavigationType,
} from "react-router-dom";
import { Layout, Menu, Spin } from "antd";
import RepoPage from "./RepoPage";
import CoursePage from "./CoursePage";
Expand Down Expand Up @@ -56,12 +63,21 @@ const ShareSite: React.FC<PageProps> = ({ mode, user }) => {
width: 100%;
background-color: ${mode === "light" ? `white` : `#141414`};
border-bottom: 1px solid
${mode === "light"
? `rgba(5, 5, 5, 0.06)`
: `rgba(253, 253, 253, 0.12)`};
${mode === "light" ? `rgba(5, 5, 5, 0.06)` : `rgba(253, 253, 253, 0.12)`};
position: sticky;
top: 72px;
`;
const navigationType = useNavigationType(); // 获取导航类型
const location = useLocation(); // 获取当前位置

useEffect(() => {
if (navigationType !== "POP") {
window.scrollTo({
top: 0,
behavior: "smooth",
});
}
}, [location.pathname, navigationType]);

/* ---------------- 随渲染刷新的组件 ---------------- */
let items = [
Expand All @@ -86,10 +102,6 @@ const ShareSite: React.FC<PageProps> = ({ mode, user }) => {
key: "minecraft",
label: <Link to={url.link("minecraft")}>Minecraft</Link>,
},
{
key: "tourguide",
label: <Link to={url.link("tourguide")}>网站说明</Link>,
},
{
key: "division",
label: <Link to={url.link("division")}>部门</Link>,
Expand All @@ -98,6 +110,10 @@ const ShareSite: React.FC<PageProps> = ({ mode, user }) => {
key: "contest",
label: <Link to={url.link("contest")}>比赛</Link>,
},
{
key: "tourguide",
label: <Link to={url.link("tourguide")}>网站说明</Link>,
},
];

if (courseRoles.includes(user.role)) {
Expand Down Expand Up @@ -134,10 +150,7 @@ const ShareSite: React.FC<PageProps> = ({ mode, user }) => {
</Authenticate>
}
/>
<Route
path="repo"
element={<RepoPage mode={mode} user={user} />}
/>
<Route path="repo" element={<RepoPage mode={mode} user={user} />} />
<Route
path="weekly"
element={<WeeklyPage mode={mode} user={user} />}
Expand Down
21 changes: 19 additions & 2 deletions src/app/UserSite/index.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import React from "react";
import { Navigate, Route, Routes } from "react-router-dom";
import React, { useEffect } from "react";
import {
Navigate,
Route,
Routes,
useLocation,
useNavigationType,
} from "react-router-dom";
import { useUrl } from "../../api/hooks/url";
import Authenticate, { userRoles } from "../Components/Authenticate";
import ProfilePage from "./ProfilePage";
Expand All @@ -17,6 +23,17 @@ export interface UserProps extends PageProps {

const UserSite: React.FC<UserProps> = (props) => {
const url = useUrl();
const navigationType = useNavigationType(); // 获取导航类型
const location = useLocation(); // 获取当前位置

useEffect(() => {
if (navigationType !== "POP") {
window.scrollTo({
top: 0,
behavior: "smooth",
});
}
}, [location.pathname, navigationType]);

return (
<Routes>
Expand Down
20 changes: 19 additions & 1 deletion src/app/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,14 @@ import {
NotificationOutlined,
QuestionOutlined,
} from "@ant-design/icons";
import { Route, Link, Routes, Navigate } from "react-router-dom";
import {
Route,
Link,
Routes,
Navigate,
useLocation,
useNavigationType,
} from "react-router-dom";
import styled from "styled-components";
import dayjs from "dayjs";
import "dayjs/locale/zh-cn";
Expand Down Expand Up @@ -74,6 +81,17 @@ const App: React.FC = () => {
});

const [imageUrl, setImageUrl] = useState<string>("");
const navigationType = useNavigationType(); // 获取导航类型
const location = useLocation(); // 获取当前位置

useEffect(() => {
if (navigationType !== "POP") {
window.scrollTo({
top: 0,
behavior: "smooth",
});
}
}, [location.pathname, navigationType]);

useEffect(() => {
let isMounted = true; // 防止组件卸载后更新状态
Expand Down
Loading