@@ -179,6 +179,8 @@ const CollaborativeEditor = forwardRef(
179
179
} ,
180
180
} ) ) ;
181
181
182
+ let sessionEndTimeout : any ;
183
+
182
184
useEffect ( ( ) => {
183
185
if ( process . env . NEXT_PUBLIC_SIGNALLING_SERVICE_URL === undefined ) {
184
186
error ( "Missing Signalling Service Url" ) ;
@@ -213,6 +215,27 @@ const CollaborativeEditor = forwardRef(
213
215
// Listen for awareness changes
214
216
provider . awareness . on ( "change" , ( ) => {
215
217
const updatedStates = provider . awareness . getStates ( ) ;
218
+
219
+ // Check the length of updatedStates, if it is equal to 1, we trigger endsession in 15s but if updated to 2, cancel endsession
220
+ if ( sessionEndTimeout && updatedStates . size == 2 ) {
221
+ clearTimeout ( sessionEndTimeout ) ;
222
+ sessionEndTimeout = null ;
223
+ }
224
+
225
+ // If there's only one participant, set a timeout for 15 seconds
226
+ if ( updatedStates . size === 1 ) {
227
+ sessionEndTimeout = setTimeout ( ( ) => {
228
+ // Trigger end session logic here
229
+ info (
230
+ `Session has ended due to inactivity from matched user ${ props . matchedUser } `
231
+ ) ;
232
+ props . handleCloseCollaboration ( "peer" ) ;
233
+ if ( props . providerRef . current ) {
234
+ props . providerRef . current . disconnect ( ) ;
235
+ }
236
+ } , 15000 ) ; // 15 seconds
237
+ }
238
+
216
239
for ( const [ clientID , state ] of Array . from ( updatedStates ) ) {
217
240
if ( state . sessionEnded && state . user . name !== props . user ) {
218
241
if ( ! sessionEndNotified ) {
0 commit comments