@@ -2,30 +2,38 @@ import React, { useState, useEffect } from 'react';
22import Landing from './components/Landing' ;
33import ChatRoom from './components/ChatRoom' ;
44import ForkPage from './components/ForkPage' ;
5+ import AboutPage from './components/AboutPage' ;
6+
7+ type Page = 'landing' | 'fork' | 'about' ;
8+
9+ function getInitialPage ( ) : Page {
10+ const path = window . location . pathname ;
11+ if ( path === '/fork' ) return 'fork' ;
12+ if ( path === '/about' ) return 'about' ;
13+ return 'landing' ;
14+ }
515
616function App ( ) {
717 const [ currentRoom , setCurrentRoom ] = useState < string | null > ( null ) ;
818 const [ isCreatingRoom , setIsCreatingRoom ] = useState ( false ) ;
919 const [ isJoiningFromLink , setIsJoiningFromLink ] = useState ( false ) ;
1020 const [ isHost , setIsHost ] = useState ( false ) ;
1121 const [ hostSessionId , setHostSessionId ] = useState ( '' ) ;
12- const [ isForkPage , setIsForkPage ] = useState ( window . location . pathname === '/fork' ) ;
22+ const [ page , setPage ] = useState < Page > ( getInitialPage ) ;
1323
1424 useEffect ( ( ) => {
1525 const checkUrlParams = ( ) => {
16- if ( window . location . pathname === '/fork' ) {
17- setIsForkPage ( true ) ;
18- return ;
19- }
20- setIsForkPage ( false ) ;
26+ const path = window . location . pathname ;
27+ if ( path === '/fork' ) { setPage ( 'fork' ) ; return ; }
28+ if ( path === '/about' ) { setPage ( 'about' ) ; return ; }
29+ setPage ( 'landing' ) ;
30+
2131 const urlParams = new URLSearchParams ( window . location . search ) ;
2232 const roomId = urlParams . get ( 'room' ) ;
23-
2433 if ( roomId && roomId . trim ( ) ) {
2534 setIsJoiningFromLink ( true ) ;
2635 setIsHost ( false ) ;
2736 setHostSessionId ( '' ) ;
28-
2937 setTimeout ( ( ) => {
3038 setCurrentRoom ( roomId . trim ( ) . toUpperCase ( ) ) ;
3139 setIsJoiningFromLink ( false ) ;
@@ -40,14 +48,13 @@ function App() {
4048
4149 useEffect ( ( ) => {
4250 const handlePopState = ( ) => {
43- if ( window . location . pathname === '/fork' ) {
44- setIsForkPage ( true ) ;
45- return ;
46- }
47- setIsForkPage ( false ) ;
51+ const path = window . location . pathname ;
52+ if ( path === '/fork' ) { setPage ( 'fork' ) ; return ; }
53+ if ( path === '/about' ) { setPage ( 'about' ) ; return ; }
54+ setPage ( 'landing' ) ;
55+
4856 const urlParams = new URLSearchParams ( window . location . search ) ;
4957 const roomId = urlParams . get ( 'room' ) ;
50-
5158 if ( roomId && roomId . trim ( ) ) {
5259 setCurrentRoom ( roomId . trim ( ) . toUpperCase ( ) ) ;
5360 setIsHost ( false ) ;
@@ -61,13 +68,8 @@ function App() {
6168 return ( ) => window . removeEventListener ( 'popstate' , handlePopState ) ;
6269 } , [ ] ) ;
6370
64- const generateRoomId = ( ) : string => {
65- return Math . random ( ) . toString ( 36 ) . substring ( 2 , 12 ) . toUpperCase ( ) ;
66- } ;
67-
68- const generateSessionId = ( ) : string => {
69- return Math . random ( ) . toString ( 36 ) . substring ( 2 ) + Date . now ( ) . toString ( 36 ) ;
70- } ;
71+ const generateRoomId = ( ) : string => Math . random ( ) . toString ( 36 ) . substring ( 2 , 12 ) . toUpperCase ( ) ;
72+ const generateSessionId = ( ) : string => Math . random ( ) . toString ( 36 ) . substring ( 2 ) + Date . now ( ) . toString ( 36 ) ;
7173
7274 const handleCreateRoom = ( ) => {
7375 setIsCreatingRoom ( true ) ;
@@ -99,14 +101,10 @@ function App() {
99101 window . history . pushState ( { } , '' , '/' ) ;
100102 } ;
101103
102- const handleNavigateFork = ( ) => {
103- window . history . pushState ( { } , '' , '/fork' ) ;
104- setIsForkPage ( true ) ;
105- } ;
106-
107- const handleBackFromFork = ( ) => {
108- window . history . pushState ( { } , '' , '/' ) ;
109- setIsForkPage ( false ) ;
104+ const navigate = ( to : Page ) => {
105+ const path = to === 'landing' ? '/' : `/${ to } ` ;
106+ window . history . pushState ( { } , '' , path ) ;
107+ setPage ( to ) ;
110108 } ;
111109
112110 if ( isJoiningFromLink ) {
@@ -138,9 +136,8 @@ function App() {
138136 ) ;
139137 }
140138
141- if ( isForkPage ) {
142- return < ForkPage onBack = { handleBackFromFork } /> ;
143- }
139+ if ( page === 'fork' ) return < ForkPage onBack = { ( ) => navigate ( 'landing' ) } /> ;
140+ if ( page === 'about' ) return < AboutPage onBack = { ( ) => navigate ( 'landing' ) } /> ;
144141
145142 if ( currentRoom ) {
146143 return (
@@ -153,7 +150,14 @@ function App() {
153150 ) ;
154151 }
155152
156- return < Landing onCreateRoom = { handleCreateRoom } onJoinRoom = { handleJoinRoom } onFork = { handleNavigateFork } /> ;
153+ return (
154+ < Landing
155+ onCreateRoom = { handleCreateRoom }
156+ onJoinRoom = { handleJoinRoom }
157+ onFork = { ( ) => navigate ( 'fork' ) }
158+ onAbout = { ( ) => navigate ( 'about' ) }
159+ />
160+ ) ;
157161}
158162
159163export default App ;
0 commit comments