mirror of
https://github.com/wanderer-industries/wanderer
synced 2025-12-11 18:26:04 +00:00
Initial commit
This commit is contained in:
42
assets/js/hooks/Mapper/useMapperHandlers.ts
Normal file
42
assets/js/hooks/Mapper/useMapperHandlers.ts
Normal file
@@ -0,0 +1,42 @@
|
||||
import { RefObject, useCallback } from 'react';
|
||||
|
||||
import { MapHandlers } from '@/hooks/Mapper/types/mapHandlers.ts';
|
||||
import { getQueryVariable } from './utils';
|
||||
|
||||
export const useMapperHandlers = (handlerRefs: RefObject<MapHandlers>[], hooksRef: RefObject<any>) => {
|
||||
const handleCommand = useCallback(
|
||||
async ({ type, data }) => {
|
||||
if (!hooksRef.current) {
|
||||
return;
|
||||
}
|
||||
|
||||
return await hooksRef.current.pushEventAsync(type, data);
|
||||
},
|
||||
[hooksRef.current],
|
||||
);
|
||||
|
||||
const handleMapEvent = useCallback(({ type, body }) => {
|
||||
if (getQueryVariable('debug') === 'true') {
|
||||
console.log(type, body);
|
||||
}
|
||||
|
||||
handlerRefs.forEach(ref => {
|
||||
if (!ref.current) {
|
||||
return;
|
||||
}
|
||||
|
||||
ref.current?.command(type, body);
|
||||
});
|
||||
}, []);
|
||||
|
||||
const handleMapEvents = useCallback(
|
||||
events => {
|
||||
events.forEach(event => {
|
||||
handleMapEvent(event);
|
||||
});
|
||||
},
|
||||
[handleMapEvent],
|
||||
);
|
||||
|
||||
return { handleCommand, handleMapEvent, handleMapEvents };
|
||||
};
|
||||
Reference in New Issue
Block a user