feat: init
This commit is contained in:
@@ -1,3 +1,3 @@
|
|||||||
import useDocumentTitle from "./src/useDocumentTitle";
|
import useDocumentTitle from "./src/useDocumentTitle";
|
||||||
|
import useSafeNavigate from "./src/useSafeNavigate";
|
||||||
export { useDocumentTitle };
|
export { useDocumentTitle, useSafeNavigate };
|
||||||
|
|||||||
23
packages/hooks/src/useSafeNavigate.ts
Normal file
23
packages/hooks/src/useSafeNavigate.ts
Normal file
@@ -0,0 +1,23 @@
|
|||||||
|
// hooks/useSafeNavigate.ts
|
||||||
|
import { useNavigate, NavigateOptions } from "react-router-dom";
|
||||||
|
|
||||||
|
const useSafeNavigate = () => {
|
||||||
|
const navigate = useNavigate();
|
||||||
|
|
||||||
|
return (path: string, options: NavigateOptions = {}) => {
|
||||||
|
const isBaidu = /baiduboxapp|baidubrowser|baidu/i.test(navigator.userAgent.toLowerCase());
|
||||||
|
|
||||||
|
if (isBaidu) {
|
||||||
|
const fullPath = path.startsWith("http") ? path : window.location.origin + path;
|
||||||
|
|
||||||
|
if (options.replace) {
|
||||||
|
window.location.replace(fullPath);
|
||||||
|
} else {
|
||||||
|
window.location.href = fullPath;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
navigate(path, options);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
};
|
||||||
|
export default useSafeNavigate;
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Route, Routes } from "react-router-dom";
|
import { Route, RouterProvider, Routes } from "react-router-dom";
|
||||||
import { routes, AppRoute } from "./routes";
|
import { routes, AppRoute } from "./routes";
|
||||||
import AuthRoute from "./auth.tsx";
|
import AuthRoute from "./auth.tsx";
|
||||||
import { Suspense } from "react";
|
import { Suspense } from "react";
|
||||||
@@ -35,7 +35,7 @@ export const RenderRoutes = () => {
|
|||||||
display: "flex",
|
display: "flex",
|
||||||
alignItems: "center",
|
alignItems: "center",
|
||||||
justifyContent: "center",
|
justifyContent: "center",
|
||||||
fontSize: "12px",
|
fontSize: "24px",
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
<DotLoading />
|
<DotLoading />
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import { Navigate } from "react-router-dom";
|
import { createBrowserRouter, Navigate } from "react-router-dom";
|
||||||
import { lazy } from "react";
|
import { lazy } from "react";
|
||||||
|
import { type Router } from "react-router-dom";
|
||||||
|
|
||||||
export interface AppRoute {
|
export interface AppRoute {
|
||||||
path: string;
|
path: string;
|
||||||
|
|||||||
@@ -1,16 +1,26 @@
|
|||||||
import MainLayout from "@/layout/main/mainLayout";
|
import MainLayout from "@/layout/main/mainLayout";
|
||||||
import { useState } from "react";
|
import { useEffect, useState } from "react";
|
||||||
import { Divider, Space } from "antd-mobile";
|
import { Divider, Space } from "antd-mobile";
|
||||||
import Translate from "./translate/index";
|
import Translate from "./translate/index";
|
||||||
import { Electrocardiogram, Filter, GithubOne } from "@icon-park/react";
|
import { Electrocardiogram, Filter, GithubOne, HamburgerButton } from "@icon-park/react";
|
||||||
import { useNavigate } from "react-router-dom";
|
import { useNavigate } from "react-router-dom";
|
||||||
|
import { useSafeNavigate } from "@workspace/hooks";
|
||||||
import "./index.less";
|
import "./index.less";
|
||||||
|
|
||||||
function Index() {
|
function Index() {
|
||||||
const [visible, setVisible] = useState<boolean>(false);
|
const [visible, setVisible] = useState<boolean>(false);
|
||||||
|
const safeNavigate = useSafeNavigate();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
useEffect(() => {
|
||||||
|
if (window.history) {
|
||||||
|
// // 往历史记录里面添加一条新的当前页面的url
|
||||||
|
// // 给 popstate 绑定一个方法监听页面返回
|
||||||
|
window.addEventListener("popstate", () => onLink(""), false); //false阻止默认事件
|
||||||
|
}
|
||||||
|
}, []);
|
||||||
|
|
||||||
const onLink = (link: string) => {
|
const onLink = (link: string) => {
|
||||||
|
// safeNavigate(link);
|
||||||
navigate(link);
|
navigate(link);
|
||||||
};
|
};
|
||||||
return (
|
return (
|
||||||
@@ -35,6 +45,13 @@ function Index() {
|
|||||||
strokeLinecap="butt"
|
strokeLinecap="butt"
|
||||||
onClick={() => onLink("/translate/archives")}
|
onClick={() => onLink("/translate/archives")}
|
||||||
/>
|
/>
|
||||||
|
<HamburgerButton
|
||||||
|
theme="outline"
|
||||||
|
size="24"
|
||||||
|
fill="#333"
|
||||||
|
strokeWidth={3}
|
||||||
|
strokeLinecap="butt"
|
||||||
|
/>
|
||||||
</Space>
|
</Space>
|
||||||
</div>
|
</div>
|
||||||
<Translate searchVisible={visible} />
|
<Translate searchVisible={visible} />
|
||||||
|
|||||||
@@ -25,5 +25,10 @@
|
|||||||
"@hooks/*": ["packages/hooks/src/*"]
|
"@hooks/*": ["packages/hooks/src/*"]
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"include": ["src", "../../packages/shared/src", "../../packages/utils/src"]
|
"include": [
|
||||||
|
"src",
|
||||||
|
"../../packages/shared/src",
|
||||||
|
"../../packages/utils/src",
|
||||||
|
"../../packages/hooks/src/useSafeNavigate.ts"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user