San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 1 | import React, { use } from "react"; |
San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 2 | import { Outlet } from "react-router"; |
| 3 | import { useEffect, useState } from "react"; |
| 4 | import { |
| 5 | SearchOutlined, |
| 6 | FontSizeOutlined, |
| 7 | MessageOutlined, |
| 8 | SunOutlined, |
| 9 | MoonOutlined |
| 10 | } from "@ant-design/icons"; |
| 11 | import style from "./frame.module.css"; |
| 12 | import logo from "&/assets/logo.png"; |
| 13 | import { useAppDispatch } from "@/hooks/store"; |
| 14 | import { useSelector } from "react-redux"; |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 15 | import { checkAndRefreshToken } from "@/utils/jwt"; |
阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame] | 16 | import { useNavigate } from "react-router-dom"; |
San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 17 | const Frame:React.FC = () => { |
| 18 | |
San3yuan | a2ee30b | 2025-06-05 21:20:17 +0800 | [diff] [blame] | 19 | useEffect(() => { |
| 20 | checkAndRefreshToken(); |
| 21 | }, []); |
San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 22 | const dispatch = useAppDispatch(); |
| 23 | |
| 24 | const showSearch = useSelector((state: any) => state.setting.showSearch); |
| 25 | const theme= useSelector((state: any) => state.setting.theme); |
阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame] | 26 | |
| 27 | const navigate = useNavigate(); // ✅ 用于跳转 |
| 28 | const [searchText, setSearchText] = useState(""); // ✅ 存储搜索输入内容 |
| 29 | |
| 30 | |
San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 31 | const toggleSearch = () => { |
| 32 | dispatch({ type: "setting/toggleSearch" }); |
| 33 | } |
| 34 | |
| 35 | const toggleFontSize = () => { |
| 36 | dispatch({ type: "setting/toggleFontSize" }); |
| 37 | }; |
| 38 | |
| 39 | const toggleTheme = () => { |
| 40 | dispatch({ type: "setting/toggleTheme" }); |
| 41 | }; |
| 42 | |
阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame] | 43 | // ✅ 用于跳转 |
| 44 | const handleSearch = (e: React.KeyboardEvent<HTMLInputElement>) => { |
| 45 | if (e.key === "Enter" && searchText.trim() !== "") { |
| 46 | navigate(`/search?keyword=${encodeURIComponent(searchText.trim())}`); |
| 47 | } |
| 48 | }; |
| 49 | |
San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 50 | |
| 51 | return ( |
| 52 | <div style={{ display: 'block', height: '100vh' }}> |
| 53 | <header className={style.header}> |
San3yuan | 30e245f | 2025-06-07 20:04:23 +0800 | [diff] [blame] | 54 | <img className={style.logo} src={logo} alt="website logo" onClick={()=>navigate('/')}></img> |
阳菜,放晴! | 7e1e3a5 | 2025-06-05 23:00:51 +0800 | [diff] [blame] | 55 | {showSearch && ( |
| 56 | // <input className={style.searchInput} placeholder="输入关键词进行搜索"/> |
| 57 | <input |
| 58 | className={style.searchInput} |
| 59 | placeholder="输入关键词进行搜索" |
| 60 | value={searchText} |
| 61 | onChange={(e) => setSearchText(e.target.value)} |
| 62 | onKeyDown={handleSearch} // ⌨️ 按下回车时执行跳转 |
| 63 | />)} |
San3yuan | 03ab064 | 2025-04-29 18:00:25 +0800 | [diff] [blame] | 64 | <div className={style.toollist}> |
| 65 | <SearchOutlined onClick={toggleSearch}/> |
| 66 | <FontSizeOutlined onClick={toggleFontSize}/> |
| 67 | <MessageOutlined /> |
| 68 | {theme === 'dark' ? <MoonOutlined onClick={toggleTheme}/> : <SunOutlined onClick={toggleTheme}/>} |
| 69 | </div> |
| 70 | </header> |
| 71 | <div className={style.container}> |
| 72 | <Outlet/> |
| 73 | </div> |
| 74 | |
| 75 | |
| 76 | </div> |
| 77 | ); |
| 78 | } |
| 79 | |
| 80 | export default Frame; |