11// Import FirebaseAuth and firebase.
2- import { useState , useCallback , useRef , ReactNode , useEffect , useContext } from 'react' ; // Added useContext
2+ import { useState , useCallback , useRef , ReactNode , useEffect , useContext } from 'react' ;
33import type { ChangeEventHandler } from 'react' ;
44import { formatDistance } from 'date-fns' ;
5- import DialogContext , { DialogContextType } from './DialogContext' ; // Added DialogContext
5+ import { DialogContext } from '.' ;
66import firebase from 'firebase/compat/app' ;
77import 'firebase/compat/auth' ;
88import 'firebase/compat/database' ;
9- import { UserData , Match } from '../Types' ;
9+ import { UserData , Match , SnapshotOrNullType } from '../Types' ;
1010import Avatar from '../Avatar' ;
1111import './Friends.css'
12- import ToggleFullscreen from '../ToggleFullscreen' ;
12+ import ToggleFullscreen from './ToggleFullscreen' ;
13+ import { saveMessagingDeviceToken } from '../firebase-messaging-setup' ;
14+
1315type Users = { [ key : string ] : UserData }
1416
15- export default function Friends ( { authUser, load, reset } ) { // Removed toggle from props
16- const context = useContext ( DialogContext ) ;
17- if ( ! context ) {
18- console . error ( 'DialogContext not found in Friends component' ) ;
19- return null ;
20- }
21- const { toggleDialog } = context ; // Destructure toggleDialog from context
17+ type FriendsProps = {
18+ user : SnapshotOrNullType ;
19+ load : ( userId : string , key : string ) => void ;
20+ reset : ( ) => void ;
21+ }
22+
23+ export default function Friends ( { user, load, reset } : FriendsProps ) {
24+ const { toggle } = useContext ( DialogContext ) ! ;
2225
2326 const searchRef = useRef < HTMLInputElement > ( null ) ;
2427 const [ users , setUsers ] = useState < Users > ( { } ) ;
2528 const [ isExpanded , setIsExpanded ] = useState ( false ) ;
2629 const [ matches , setMatches ] = useState < firebase . database . DataSnapshot > ( [ ] ) ;
2730 const [ searchResults , setSearchResults ] = useState < firebase . database . DataSnapshot > ( [ ] ) ;
31+ const [ hasAttemptedNotificationPermission , setHasAttemptedNotificationPermission ] = useState ( false ) ;
32+
33+ // Request notification permission when Friends component mounts
34+ useEffect ( ( ) => {
35+ const requestPermission = async ( ) => {
36+ if ( user && Notification . permission === 'default' && ! hasAttemptedNotificationPermission ) {
37+ console . log ( "Friends modal opened, attempting notification permission request..." ) ;
38+ try {
39+ await saveMessagingDeviceToken ( ) ;
40+ } catch ( error ) {
41+ console . error ( "Error requesting notification permission:" , error ) ;
42+ } finally {
43+ setHasAttemptedNotificationPermission ( true ) ;
44+ }
45+ }
46+ } ;
47+ requestPermission ( ) ;
48+ } , [ user , hasAttemptedNotificationPermission ] ) ;
2849
2950 // Synchronize Matches
3051 useEffect ( ( ) => {
31- if ( ! authUser ) return ;
52+ if ( ! user ) return ;
3253
33- const queryMatches = firebase . database ( ) . ref ( `matches/${ authUser . key } ` ) . orderByChild ( 'sort' ) . limitToLast ( 100 ) ;
54+ const queryMatches = firebase . database ( ) . ref ( `matches/${ user . key } ` ) . orderByChild ( 'sort' ) . limitToLast ( 100 ) ;
3455 const subscriber = ( snapshot : firebase . database . DataSnapshot ) => {
3556 setMatches ( snapshot ) ;
3657 snapshot . forEach ( match => {
@@ -47,31 +68,38 @@ export default function Friends({ authUser, load, reset }) { // Removed toggle f
4768 return ( ) => {
4869 queryMatches . off ( 'value' , subscriber ) ;
4970 }
50- } , [ authUser ] ) ;
71+ } , [ user ] ) ;
5172
5273 const onSearch : ChangeEventHandler < HTMLInputElement > = useCallback ( async ( ) => {
5374 if ( searchRef . current ?. value ) {
5475 const search = searchRef . current . value
5576 const searchSnapshot = await firebase . database ( ) . ref ( 'users' ) . orderByChild ( 'search' ) . startAt ( search . toLocaleLowerCase ( ) ) . get ( ) ;
56- // const results: UserData[] = []
57- // searchSnapshot.forEach(result => {
58- // results.push(result.val())
59- // })
6077 setSearchResults ( searchSnapshot )
6178 } else {
6279 setSearchResults ( [ ] )
6380 }
6481 } , [ ] ) ;
6582
66- if ( ! authUser ) return null ;
83+ if ( ! user ) return null ;
6784
6885 const renderFriends : ReactNode [ ] = [ ]
6986 const friends : string [ ] = [ ]
7087
7188 const NOW = new Date ( )
7289
90+ const handleLoad = useCallback ( ( userId : string ) => {
91+ if ( ! user ?. key ) return ;
92+ load ( userId , user . key ) ;
93+ toggle ( false ) ;
94+ } , [ load , user ?. key , toggle ] ) ;
95+
96+ const handleReset = useCallback ( ( ) => {
97+ reset ( ) ;
98+ toggle ( false ) ;
99+ } , [ reset , toggle ] ) ;
100+
73101 const row = ( user : UserData , match ?: Match ) =>
74- < li key = { user . uid } onPointerUp = { ( ) => { load ( user . uid , authUser . key ) ; toggleDialog ( false ) ; } } > { /* Use toggleDialog(false) */ }
102+ < li key = { user . uid } onPointerUp = { ( ) => handleLoad ( user . uid ) } >
75103 < Avatar user = { user } />
76104 < div >
77105 < h3 > { user . name } </ h3 >
@@ -95,15 +123,15 @@ export default function Friends({ authUser, load, reset }) { // Removed toggle f
95123 } )
96124 searchResults . forEach ( result => {
97125 const resultData : UserData = result . val ( )
98- if ( result . key === authUser . key || friends . includes ( result . key ) || searchReject ( resultData ) ) {
126+ if ( result . key === user . key || friends . includes ( result . key ) || searchReject ( resultData ) ) {
99127 return ;
100128 }
101129 renderFriends . push ( row ( resultData ) )
102130 } )
103131
104132 const invite = ( ) => {
105- if ( authUser . key ) {
106- const shareUrl = ( new URL ( authUser . key , location . href ) ) . toString ( )
133+ if ( user . key ) {
134+ const shareUrl = ( new URL ( user . key , location . href ) ) . toString ( )
107135 navigator . clipboard ?. writeText ?.( shareUrl )
108136 navigator . share ?.( {
109137 url : shareUrl ,
@@ -135,13 +163,13 @@ export default function Friends({ authUser, load, reset }) { // Removed toggle f
135163 </ li >
136164 : null }
137165 < li >
138- < a onPointerUp = { ( ) => toggleDialog ( 'profile' ) } > { /* Use toggleDialog('profile') */ }
166+ < a onPointerUp = { ( ) => toggle ( 'profile' ) } >
139167 < span className = "material-icons notranslate" > manage_accounts</ span >
140168 Edit Profile
141169 </ a >
142170 </ li >
143171 < li >
144- < a onPointerUp = { reset } >
172+ < a onPointerUp = { handleReset } >
145173 < span className = "material-icons notranslate" > restart_alt</ span >
146174 Reset Match
147175 </ a >
@@ -167,7 +195,7 @@ export default function Friends({ authUser, load, reset }) { // Removed toggle f
167195 </ menu >
168196 < h1 >
169197 < span >
170- < span > { authUser . val ( ) . name } 's</ span >
198+ < span > { user . val ( ) . name } 's</ span >
171199 Matches
172200 </ span >
173201 </ h1 >
0 commit comments