@@ -179,6 +179,8 @@ const CollaborativeEditor = forwardRef(
179179 } ,
180180 } ) ) ;
181181
182+ let sessionEndTimeout : any ;
183+
182184 useEffect ( ( ) => {
183185 if ( process . env . NEXT_PUBLIC_SIGNALLING_SERVICE_URL === undefined ) {
184186 error ( "Missing Signalling Service Url" ) ;
@@ -213,6 +215,27 @@ const CollaborativeEditor = forwardRef(
213215 // Listen for awareness changes
214216 provider . awareness . on ( "change" , ( ) => {
215217 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+
216239 for ( const [ clientID , state ] of Array . from ( updatedStates ) ) {
217240 if ( state . sessionEnded && state . user . name !== props . user ) {
218241 if ( ! sessionEndNotified ) {
0 commit comments