HEX
Server: LiteSpeed
System: Linux houston.panomity.com 6.8.0-100-generic #100-Ubuntu SMP PREEMPT_DYNAMIC Tue Jan 13 16:40:06 UTC 2026 x86_64
User: nudepix (1011)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: //opt/agentcloud/webapp/src/context/socket.tsx
import { useRouter } from 'next/router';
import React, { createContext, useContext, useEffect, useState } from 'react';
import { toast } from 'react-toastify';
import { io } from 'socket.io-client';
import { NotificationType, WebhookType } from 'struct/notification';

let socketio;
if (typeof window !== 'undefined') {
	socketio = io(/*{
		transports: ["websocket", "polling"],
	}*/);
}

const SocketContext = createContext(socketio);

export function SocketWrapper({ children }) {

	const router = useRouter();
	const { resourceSlug } = router.query;
	const [sharedSocket, _setSharedSocket] = useState(socketio);

	//TODO: move these into a "trigger" context for global events, maybe switch to useReducer
	const [notificationTrigger, setNotificationTrigger] = useState(null);
	const [sessionTrigger, setSessionTrigger] = useState(false);

	function joinRoomAndListen() {
		if (!sharedSocket || !resourceSlug) { return; }
		sharedSocket.emit('join_room', resourceSlug);		
		sharedSocket.on('notification', notification => {

			if (notification?.description
				&& notification?.type === NotificationType.Webhook
				&& notification?.details?.webhookType === WebhookType.SuccessfulSync) {
				toast.success(notification?.description);
			}
		
		    setNotificationTrigger(notification);
		});
	}

	useEffect(() => {
		joinRoomAndListen();
		//TODO: handle leaving old room on changing resourceSlug
		return () => {
			sharedSocket?.off('notification');
		};
	}, [sharedSocket, resourceSlug]);

	useEffect(() => {
		if (router.asPath.includes('/session/')) {
			setSessionTrigger(!sessionTrigger);
		}
	}, [router.asPath]);

	return (
		<SocketContext.Provider value={[sharedSocket, notificationTrigger, sessionTrigger] as any}>
			{children}
		</SocketContext.Provider>
	);

}

export function useSocketContext() {
	return useContext(SocketContext);
}