Files
wanderer/assets/js/hooks/Mapper/components/mapRootContent/hooks/useTrackAndFollowHandlers.ts
Dmitry Popov 3825fc831a
Some checks are pending
Build / 🚀 Deploy to test env (fly.io) (push) Waiting to run
Build / Manual Approval (push) Blocked by required conditions
Build / 🛠 Build (1.17, 18.x, 27) (push) Blocked by required conditions
Build / 🛠 Build Docker Images (linux/amd64) (push) Blocked by required conditions
Build / 🛠 Build Docker Images (linux/arm64) (push) Blocked by required conditions
Build / merge (push) Blocked by required conditions
Build / 🏷 Create Release (push) Blocked by required conditions
refactor: removal of legacy event
2025-03-17 22:51:29 +01:00

116 lines
3.0 KiB
TypeScript

import { useCallback } from 'react';
import { useMapRootState } from '@/hooks/Mapper/mapRootProvider';
import { OutCommand, CommandData, Commands } from '@/hooks/Mapper/types/mapHandlers';
import type { TrackingCharacter } from '@/hooks/Mapper/components/mapRootContent/components/TrackAndFollow/types';
/**
* Hook for track and follow related handlers
*/
export const useTrackAndFollowHandlers = () => {
const { outCommand, update } = useMapRootState();
/**
* Handle hiding the track and follow dialog
*/
const handleHideTracking = useCallback(() => {
// Then update local state to hide the dialog
update(state => ({
...state,
showTrackAndFollow: false,
}));
}, [update]);
/**
* Handle showing the track and follow dialog
*/
const handleShowTracking = useCallback(() => {
// Update local state to show the dialog
update(state => ({
...state,
showTrackAndFollow: true,
}));
// Send the command to the server
outCommand({
type: OutCommand.showTracking,
data: {},
});
}, [outCommand, update]);
/**
* Handle updating tracking data
*/
const handleUpdateTracking = useCallback(
(trackingData: { characters: TrackingCharacter[] }) => {
if (!trackingData || !trackingData.characters) {
console.error('Invalid tracking data received:', trackingData);
return;
}
// Update local state with the tracking data
update(state => ({
...state,
trackingCharactersData: trackingData.characters,
showTrackAndFollow: true,
}));
},
[update],
);
/**
* Handle toggling character tracking
*/
const handleToggleTrack = useCallback(
(characterId: string) => {
if (!characterId) return;
// Send the toggle track command to the server
outCommand({
type: OutCommand.toggleTrack,
data: { 'character-id': characterId },
});
// Note: The local state is now updated in the TrackAndFollow component
// for immediate UI feedback, while we wait for the server response
},
[outCommand],
);
/**
* Handle toggling character following
*/
const handleToggleFollow = useCallback(
(characterId: string) => {
if (!characterId) return;
// Send the toggle follow command to the server
outCommand({
type: OutCommand.toggleFollow,
data: { 'character-id': characterId },
});
// Note: The local state is now updated in the TrackAndFollow component
// for immediate UI feedback, while we wait for the server response
},
[outCommand],
);
/**
* Handle user settings updates
*/
const handleUserSettingsUpdated = useCallback((settingsData: CommandData[Commands.userSettingsUpdated]) => {
if (!settingsData || !settingsData.settings) {
console.error('Invalid settings data received:', settingsData);
}
}, []);
return {
handleHideTracking,
handleShowTracking,
handleUpdateTracking,
handleToggleTrack,
handleToggleFollow,
handleUserSettingsUpdated,
};
};