mirror of
https://github.com/pterodactyl/panel.git
synced 2024-11-22 17:12:30 +01:00
26 lines
853 B
TypeScript
26 lines
853 B
TypeScript
import { ServerContext } from '@/state/server';
|
|
import { useEffect, useRef } from 'react';
|
|
import { SocketEvent } from '@/components/server/events';
|
|
|
|
const useWebsocketEvent = (event: SocketEvent, callback: (data: string) => void) => {
|
|
const { connected, instance } = ServerContext.useStoreState(state => state.socket);
|
|
const savedCallback = useRef<any>(null);
|
|
|
|
useEffect(() => {
|
|
savedCallback.current = callback;
|
|
}, [callback]);
|
|
|
|
return useEffect(() => {
|
|
const eventListener = (event: SocketEvent) => savedCallback.current(event);
|
|
if (connected && instance) {
|
|
instance.addListener(event, eventListener);
|
|
}
|
|
|
|
return () => {
|
|
instance && instance.removeListener(event, eventListener);
|
|
};
|
|
}, [event, connected, instance]);
|
|
};
|
|
|
|
export default useWebsocketEvent;
|