1
- import { ChannelFilters , ChannelOptions , ChannelSort } from 'stream-chat' ;
1
+ import {
2
+ ChannelFilters ,
3
+ ChannelOptions ,
4
+ ChannelSort ,
5
+ LiveLocationManagerConstructorParameters ,
6
+ } from 'stream-chat' ;
2
7
import {
3
8
AIStateIndicator ,
4
9
Channel ,
@@ -13,6 +18,9 @@ import {
13
18
useCreateChatClient ,
14
19
ThreadList ,
15
20
ChatView ,
21
+ useChatContext ,
22
+ useLiveLocationSharingManager ,
23
+ Attachment ,
16
24
} from 'stream-chat-react' ;
17
25
import 'stream-chat-react/css/v2/index.css' ;
18
26
@@ -64,13 +72,67 @@ type StreamChatGenerics = {
64
72
userType : LocalUserType ;
65
73
} ;
66
74
75
+ const ShareLiveLocation = ( ) => {
76
+ const { channel } = useChatContext ( ) ;
77
+
78
+ return (
79
+ < button
80
+ onClick = { ( ) => {
81
+ console . log ( 'trying to fetch location' ) ;
82
+ navigator . geolocation . getCurrentPosition (
83
+ ( position ) => {
84
+ const { latitude, longitude } = position . coords ;
85
+ console . log ( 'got location ' , position ) ;
86
+ channel ?. startLiveLocationSharing ( {
87
+ latitude,
88
+ longitude,
89
+ end_time : new Date ( Date . now ( ) + 1 * 1000 * 3600 * 24 ) . toISOString ( ) ,
90
+ } ) ;
91
+ } ,
92
+ console . warn ,
93
+ { timeout : 200 } ,
94
+ ) ;
95
+ } }
96
+ >
97
+ location
98
+ </ button >
99
+ ) ;
100
+ } ;
101
+
102
+ const watchLocationNormal : LiveLocationManagerConstructorParameters [ 'watchLocation' ] = (
103
+ watcher ,
104
+ ) => {
105
+ const watch = navigator . geolocation . watchPosition ( ( position ) => {
106
+ watcher ( { latitude : position . coords . latitude , longitude : position . coords . longitude } ) ;
107
+ } ) ;
108
+
109
+ return ( ) => navigator . geolocation . clearWatch ( watch ) ;
110
+ } ;
111
+
112
+ const watchLocationTimed : LiveLocationManagerConstructorParameters [ 'watchLocation' ] = ( watcher ) => {
113
+ const timer = setInterval ( ( ) => {
114
+ navigator . geolocation . getCurrentPosition ( ( position ) => {
115
+ watcher ( { latitude : position . coords . latitude , longitude : position . coords . longitude } ) ;
116
+ } ) ;
117
+ } , 5000 ) ;
118
+
119
+ return ( ) => clearInterval ( timer ) ;
120
+ } ;
121
+
67
122
const App = ( ) => {
68
123
const chatClient = useCreateChatClient < StreamChatGenerics > ( {
69
124
apiKey,
70
125
tokenOrProvider : userToken ,
71
126
userData : { id : userId } ,
72
127
} ) ;
73
128
129
+ const manager = useLiveLocationSharingManager ( {
130
+ client : chatClient ?? undefined ,
131
+ watchLocation : watchLocationTimed ,
132
+ } ) ;
133
+
134
+ // const s = useStateStore(manager?.state)
135
+
74
136
if ( ! chatClient ) return < > Loading...</ > ;
75
137
76
138
return (
@@ -86,12 +148,27 @@ const App = () => {
86
148
showChannelSearch
87
149
additionalChannelSearchProps = { { searchForChannels : true } }
88
150
/>
89
- < Channel >
151
+ < Channel
152
+ Attachment = { ( props ) => {
153
+ const [ attachment ] = props . attachments ?? [ ] ;
154
+
155
+ if ( attachment ?. type === 'live_location' ) {
156
+ return (
157
+ < div style = { { padding : 25 } } >
158
+ lat: { attachment . latitude } , lng: { attachment . longitude }
159
+ </ div >
160
+ ) ;
161
+ }
162
+
163
+ return < Attachment { ...props } /> ;
164
+ } }
165
+ >
90
166
< Window >
91
167
< ChannelHeader Avatar = { ChannelAvatar } />
92
168
< MessageList returnAllReadData />
93
169
< AIStateIndicator />
94
170
< MessageInput focus />
171
+ < ShareLiveLocation />
95
172
</ Window >
96
173
< Thread virtualized />
97
174
</ Channel >
0 commit comments