Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 1 | import React from 'react'; |
| 2 | import { Route, useLocation } from 'wouter'; |
| 3 | import { UserProvider, useUser } from './context/UserContext'; |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 4 | |
Krishya | 1df0589 | 2025-04-05 21:16:30 +0800 | [diff] [blame] | 5 | import AuthPage from './pages/AuthPage/AuthPage'; |
Krishya | 75e43c0 | 2025-04-05 21:16:30 +0800 | [diff] [blame] | 6 | import HomePage from './pages/HomePage'; |
Krishya | e71688a | 2025-04-10 21:25:17 +0800 | [diff] [blame] | 7 | import FriendMoments from './pages/FriendMoments/FriendMoments'; |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 8 | import ForumPage from './pages/Forum/posts-main/ForumPage'; |
22301009 | ecc1c1c | 2025-04-09 21:56:23 +0800 | [diff] [blame] | 9 | import SeedList from './pages/SeedList/SeedList'; |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 10 | import PostDetailPage from './pages/Forum/posts-detail/PostDetailPage'; |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 11 | import { GroupProvider } from './context/useGroupStore'; |
22301009 | 5b28c67 | 2025-04-10 20:12:45 +0800 | [diff] [blame] | 12 | import PublishSeed from './pages/PublishSeed/PublishSeed'; |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 13 | import SeedDetail from './pages/SeedList/SeedDetail/SeedDetail'; |
22301009 | 3a876cc | 2025-04-14 16:22:20 +0800 | [diff] [blame] | 14 | import InterestGroup from './pages/InterestGroup/InterestGroup'; |
22301009 | 7ff51f2 | 2025-04-15 21:35:28 +0800 | [diff] [blame] | 15 | import UserProfile from './pages/UserCenter/UserProfile'; |
Krishya | 7ec1dd0 | 2025-04-19 15:29:03 +0800 | [diff] [blame] | 16 | import CreatePostPage from './pages/Forum/posts-create/CreatePostPage'; |
Krishya | f1d0ea8 | 2025-05-03 17:01:58 +0800 | [diff] [blame] | 17 | import MessagePage from './pages/MessagePage/MessagePage'; |
| 18 | import CreateMoment from './pages/FriendMoments/CreateMoment'; |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 19 | // import LevelPage from './pages/LevelPage/LevelPage'; |
22301009 | 4952a0f | 2025-06-07 18:58:16 +0800 | [diff] [blame] | 20 | import NewbieTasks from './pages/NewUserGuide/NewbieTasks'; |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 21 | import UserDynamics from './pages/UserCenter/UserDynamics'; |
22301009 | 648cb7e | 2025-06-04 08:54:23 +0800 | [diff] [blame] | 22 | import UserFriends from './pages/UserCenter/UserFriends'; |
22301009 | 6401163 | 2025-06-04 21:57:22 +0800 | [diff] [blame] | 23 | import UserCollect from './pages/UserCenter/UserCollect'; |
Krishya | 7096ab1 | 2025-06-05 17:15:46 +0800 | [diff] [blame] | 24 | import UserInvite from './pages/UserCenter/UserInvite'; |
Krishya | 767f9b9 | 2025-06-05 23:59:37 +0800 | [diff] [blame] | 25 | import UserInfo from './pages/UserInfo/UserInfo'; |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 26 | import UserLayout from './pages/UserCenter/UserLayout'; |
22301009 | 3d8132e | 2025-06-07 11:34:38 +0800 | [diff] [blame] | 27 | import NewUserGuide from './pages/NewUserGuide/NewUserGuide'; |
22301009 | 4952a0f | 2025-06-07 18:58:16 +0800 | [diff] [blame] | 28 | import UserRecharge from './pages/UserCenter/UserRecharge'; |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 29 | import GroupDetail from './pages/InterestGroup/GroupDetail'; |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 30 | |
Krishya | 8f2fec8 | 2025-06-04 21:54:46 +0800 | [diff] [blame] | 31 | function RedirectToAuth() { |
| 32 | if (typeof window !== 'undefined') { |
| 33 | window.location.replace('/auth'); |
| 34 | } |
| 35 | return null; |
| 36 | } |
| 37 | |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 38 | // 私有路由保护组件 |
| 39 | function PrivateRoute({ component: Component }) { |
| 40 | const [location, setLocation] = useLocation(); |
| 41 | const { user, loading } = useUser(); |
| 42 | |
| 43 | if (loading) { |
| 44 | return <div>加载中...</div>; |
| 45 | } |
| 46 | |
| 47 | if (!user) { |
| 48 | setLocation('/auth'); |
| 49 | return null; |
| 50 | } |
| 51 | |
| 52 | return <Component />; |
| 53 | } |
| 54 | |
Krishya | 75e43c0 | 2025-04-05 21:16:30 +0800 | [diff] [blame] | 55 | function App() { |
| 56 | return ( |
| 57 | <UserProvider> |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 58 | <GroupProvider> |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 59 | <> |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 60 | {/* 公开路由 */} |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 61 | <Route path="/auth" component={AuthPage} /> |
Krishya | 767f9b9 | 2025-06-05 23:59:37 +0800 | [diff] [blame] | 62 | |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 63 | {/* 根路径跳转到登录页 */} |
| 64 | <Route path="/" component={() => <RedirectToAuth />} /> |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 65 | |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 66 | {/* 私有路由用 PrivateRoute 包裹 */} |
| 67 | <Route path="/friend-moments" component={() => <PrivateRoute component={FriendMoments} />} /> |
| 68 | <Route path="/friend-moments/create" component={() => <PrivateRoute component={CreateMoment} />} /> |
| 69 | <Route path="/forum" component={() => <PrivateRoute component={ForumPage} />} /> |
| 70 | <Route path="/forum/post/:postId" component={({ postId }) => <PrivateRoute component={() => <PostDetailPage postId={postId} />} />} /> |
| 71 | <Route path="/forum/create-post" component={() => <PrivateRoute component={CreatePostPage} />} /> |
| 72 | <Route path="/seed-list" component={() => <PrivateRoute component={SeedList} />} /> |
| 73 | <Route path="/publish-seed" component={() => <PrivateRoute component={PublishSeed} />} /> |
| 74 | <Route path="/seed/:id" component={({ id }) => <PrivateRoute component={() => <SeedDetail id={id} />} />} /> |
| 75 | <Route path="/interest-groups" component={() => <PrivateRoute component={InterestGroup} />} /> |
| 76 | <Route path="/messages" component={() => <PrivateRoute component={MessagePage} />} /> |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 77 | {/* <Route path="/level" component={() => <PrivateRoute component={LevelPage} />} /> */} |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 78 | <Route path="/information/:userId" component={({ userId }) => <PrivateRoute component={() => <UserInfo userId={userId} />} />} /> |
22301009 | 3d8132e | 2025-06-07 11:34:38 +0800 | [diff] [blame] | 79 | <Route path="/new-user-guide" component={() => <PrivateRoute component={NewUserGuide} />} /> |
Krishya | affe810 | 2025-06-08 00:44:46 +0800 | [diff] [blame] | 80 | <Route path="/group/:groupId" component={({ groupId }) => <PrivateRoute component={() => <GroupDetail groupId={groupId} />} />} /> |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 81 | |
| 82 | {/* 用户中心路由 */} |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 83 | <Route path="/user/profile" component={() => ( |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 84 | <PrivateRoute component={() => <UserLayout><UserProfile /></UserLayout>} /> |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 85 | )} /> |
22301009 | 4952a0f | 2025-06-07 18:58:16 +0800 | [diff] [blame] | 86 | <Route path="/user/newbie-tasks" component={() => ( |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 87 | <PrivateRoute component={() => <UserLayout><NewbieTasks /></UserLayout>} /> |
22301009 | 4952a0f | 2025-06-07 18:58:16 +0800 | [diff] [blame] | 88 | )} /> |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 89 | <Route path="/user/dynamics" component={() => ( |
| 90 | <PrivateRoute component={() => <UserLayout><UserDynamics /></UserLayout>} /> |
| 91 | )} /> |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 92 | <Route path="/user/friends" component={() => ( |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 93 | <PrivateRoute component={() => <UserLayout><UserFriends /></UserLayout>} /> |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 94 | )} /> |
| 95 | <Route path="/user/collections" component={() => ( |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 96 | <PrivateRoute component={() => <UserLayout><UserCollect /></UserLayout>} /> |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 97 | )} /> |
| 98 | <Route path="/user/invite" component={() => ( |
Krishya | 6bf199c | 2025-06-06 21:14:23 +0800 | [diff] [blame] | 99 | <PrivateRoute component={() => <UserLayout><UserInvite /></UserLayout>} /> |
Krishya | c6b2483 | 2025-06-05 20:13:20 +0800 | [diff] [blame] | 100 | )} /> |
22301009 | 4952a0f | 2025-06-07 18:58:16 +0800 | [diff] [blame] | 101 | <Route path="/user/recharge" component={() => ( |
| 102 | <PrivateRoute component={() => <UserLayout><UserRecharge /></UserLayout>} /> |
22301009 | 01d3ff9 | 2025-06-07 16:16:26 +0800 | [diff] [blame] | 103 | )} /> |
22301009 | 4952a0f | 2025-06-07 18:58:16 +0800 | [diff] [blame] | 104 | </> |
Krishya | b5ef96d | 2025-06-05 13:57:05 +0800 | [diff] [blame] | 105 | </GroupProvider> |
Krishya | 75e43c0 | 2025-04-05 21:16:30 +0800 | [diff] [blame] | 106 | </UserProvider> |
| 107 | ); |
| 108 | } |
| 109 | |
Krishya | 73cd882 | 2025-06-07 15:48:41 +0800 | [diff] [blame] | 110 | export default App; |