@@ -4,10 +4,13 @@ import ReactDOM from 'react-dom/client'
44// import { initializeApp } from 'firebase/app';
55import type { Match , Move , GameType , SnapshotOrNullType , UserData , ModalState } from "./Types" ;
66import Avatar from "./Avatar" ;
7- import Friends from "./Dialogues/Friends" ;
8- import Chat from "./Dialogues/Chat" ;
9- import Profile from "./Dialogues/Profile" ;
10- import Login from "./Dialogues/Login" ;
7+ // Dialog components are now rendered by DialogContainer
8+ // import Friends from "./Dialogues/Friends";
9+ // import Chat from "./Dialogues/Chat";
10+ // import Profile from "./Dialogues/Profile";
11+ // import Login from "./Dialogues/Login";
12+ import DialogContext from './Dialogues/DialogContext' ;
13+ import DialogContainer from './Dialogues' ;
1114import Dice from './Board/Dice' ;
1215import Point from './Board/Point' ;
1316import Piece from './Board/Piece' ;
@@ -30,33 +33,32 @@ export function App() {
3033 const [ game , setGame ] = useState < GameType > ( newGame ) ;
3134 const [ user , setUser ] = useState < SnapshotOrNullType > ( null ) ;
3235 const [ hasAttemptedNotificationPermission , setHasAttemptedNotificationPermission ] = useState ( false ) ;
33- const [ state , setState ] = useState < ModalState > ( false ) ;
34- const [ lastState , setLastState ] = useState < ModalState > ( 'friends' ) ;
36+ const [ dialogState , setDialogState ] = useState < ModalState > ( false ) ; // Renamed state to dialogState
37+ const [ lastDialogState , setLastDialogState ] = useState < ModalState > ( 'friends' ) ; // Renamed lastState to lastDialogState
3538 const [ match , setMatch ] = useState < Match | null > ( null ) ;
3639 const [ chats , setChats ] = useState < SnapshotOrNullType > ( null ) ;
3740 const [ friend , setFriend ] = useState < SnapshotOrNullType > ( null ) ;
3841 const [ selected , setSelected ] = useState < number | null > ( null ) ;
3942
40- const toggle = useCallback ( ( newState : ModalState ) => {
41- setState ( prevState => {
42- if ( typeof newState === 'string' ) { // Open
43- if ( prevState ) setLastState ( prevState ) ;
44- return newState
45- } else if ( newState == true ) { // Back
46- setLastState ( lastState => {
47- newState = lastState ;
48- return prevState ;
49- } ) ;
43+ const toggleDialog = useCallback ( ( newState : ModalState ) => { // Renamed toggle to toggleDialog
44+ setDialogState ( prevState => {
45+ if ( typeof newState === 'string' ) { // Open specific dialog
46+ if ( prevState ) setLastDialogState ( prevState ) ;
5047 return newState ;
51- } else if ( newState === false ) { // Close
52- if ( prevState ) setLastState ( prevState ) ;
48+ } else if ( newState === true ) { // Back button: Go to lastDialogState
49+ const actualNewState = lastDialogState ;
50+ setLastDialogState ( prevState || false ) ; // Save current state (or false if none) as the new last state
51+ return actualNewState ;
52+ } else if ( newState === false ) { // Close dialog
53+ if ( prevState ) setLastDialogState ( prevState ) ;
5354 return false ;
54- } else { // Toggle
55- setLastState ( prevState ) ;
56- return prevState === 'friends' ? false : 'friends' ;
55+ } else { // Toggle friends or close (original generic toggle)
56+ const nextState = prevState === 'friends' ? false : 'friends' ;
57+ if ( prevState ) setLastDialogState ( prevState ) ;
58+ return nextState ;
5759 }
5860 } ) ;
59- } , [ ] ) ;
61+ } , [ lastDialogState ] ) ; // Added lastDialogState to dependency array
6062
6163 const load = useCallback ( async ( friendId : string = '' , authUser ?: string ) => {
6264 if ( friendId === 'PeaceInTheMiddleEast' ) return ;
@@ -99,19 +101,19 @@ export function App() {
99101 database . ref ( `matches/${ friendId } /${ authUser } ` ) . set ( data ) ;
100102 setMatch ( data ) ;
101103 }
102- toggle ( false )
103- } , [ toggle ] ) ;
104+ toggleDialog ( false ) // Use renamed function
105+ } , [ toggleDialog , database ] ) ; // Added database to dependency array as it's used inside
104106
105107 const reset = useCallback ( ( ) => {
106108 if ( confirm ( 'Are you sure you want to reset the match?' ) ) {
107109 console . log ( 'Resetting' , match ?. game ) ;
108110 let data = newGame ( )
109111 if ( match ?. game )
110- database . ref ( `games/${ match ?. game } ` ) . set ( data ) ;
112+ database . ref ( `games/${ match ?. game } ` ) . set ( data ) ; // database is used here
111113 setGame ( data ) ;
112- toggle ( false )
114+ toggleDialog ( false ) // Use renamed function
113115 }
114- } , [ match ?. game , toggle ] )
116+ } , [ match ?. game , toggleDialog , database ] ) ; // Added database to dependency array
115117
116118
117119 useEffect ( ( ) => {
@@ -152,7 +154,7 @@ export function App() {
152154
153155 useEffect ( ( ) => {
154156 const requestPermission = async ( ) => {
155- if ( state === 'friends' && user && Notification . permission === 'default' && ! hasAttemptedNotificationPermission ) {
157+ if ( dialogState === 'friends' && user && Notification . permission === 'default' && ! hasAttemptedNotificationPermission ) { // Use renamed state
156158 console . log ( "Friends modal opened, attempting notification permission request via useEffect..." ) ; // Dev log
157159 try {
158160 await saveMessagingDeviceToken ( ) ;
@@ -165,7 +167,7 @@ export function App() {
165167 } ;
166168
167169 requestPermission ( ) ;
168- } , [ state , user , hasAttemptedNotificationPermission ] ) ;
170+ } , [ dialogState , user , hasAttemptedNotificationPermission ] ) ; // Use renamed state
169171
170172 // Subscribe to match
171173 useEffect ( ( ) => {
@@ -270,24 +272,21 @@ export function App() {
270272 const friendData = friend ?. val ( ) ;
271273
272274 return (
273- < >
274- < dialog open = { ( friendData && ! user ) || ! ! state } >
275- { user
276- ? state === 'friends'
277- ? < Friends authUser = { user } load = { load } toggle = { toggle } reset = { reset } />
278- : state === 'profile'
279- ? < Profile authUser = { user } toggle = { toggle } />
280- : state === 'chat'
281- ? < Chat chats = { chats } user = { user } />
282- : null
283- : < Login reset = { reset } friend = { friendData } load = { load } /> }
284- </ dialog >
275+ < DialogContext . Provider value = { { dialogState, toggleDialog, lastDialogState } } >
276+ { /* Old dialog element removed */ }
277+ < DialogContainer
278+ user = { user }
279+ friendData = { friendData }
280+ load = { load }
281+ reset = { reset }
282+ chats = { chats }
283+ />
285284
286285 < div id = "board" >
287- < div id = "toolbar" onPointerUp = { toggle } >
286+ < div id = "toolbar" onPointerUp = { ( ) => toggleDialog ( null ) } >
288287 { friendData
289288 ? < Avatar user = { friendData } />
290- : < a className = { `material-icons notranslate ${ state && 'active' || '' } ` } > account_circle</ a > }
289+ : < a className = { `material-icons notranslate ${ dialogState && 'active' || '' } ` } > account_circle</ a > } { /* Use renamed state */ }
291290 < h2 > { friendData ?. name ?? 'Local' } </ h2 >
292291 </ div >
293292
@@ -317,6 +316,6 @@ export function App() {
317316 < Point key = { index } pieces = { pieces } move = { move } position = { index } selected = { selected } onSelect = { onSelect } />
318317 ) }
319318 </ div >
320- </ >
319+ </ DialogContext . Provider >
321320 ) ;
322321}
0 commit comments