-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathuseWaypointMenu.tsx
96 lines (79 loc) · 3.27 KB
/
useWaypointMenu.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { useEffect, useRef, useState } from 'react';
import { EyeClosed } from '@osrd-project/ui-icons';
import { omit } from 'lodash';
import { useTranslation } from 'react-i18next';
import { useSelector } from 'react-redux';
import { useOsrdConfSelectors } from 'common/osrdContext';
import type { OSRDMenuItem } from 'common/OSRDMenu';
import type { WaypointsPanelData } from 'modules/simulationResult/types';
import useModalFocusTrap from 'utils/hooks/useModalFocusTrap';
const useWaypointMenu = (waypointsPanelData?: WaypointsPanelData) => {
const { filteredWaypoints, setFilteredWaypoints, projectionPath } = waypointsPanelData || {};
const { t } = useTranslation('simulation');
const { getTimetableID } = useOsrdConfSelectors();
const timetableId = useSelector(getTimetableID);
const [activeWaypointId, setActiveWaypointId] = useState<string>();
const [isClickOnWaypoint, setIsClickOnWaypoint] = useState(false);
const menuRef = useRef<HTMLDivElement>(null);
const closeMenu = () => {
setActiveWaypointId(undefined);
};
useModalFocusTrap(menuRef, closeMenu, { focusOnFirstElement: true });
useEffect(() => {
const handleClickOutside = (event: MouseEvent) => {
// Avoid closing the menu when clicking on another waypoint
if (activeWaypointId && (event.target as HTMLElement).closest('.waypoint')) {
setIsClickOnWaypoint(true);
}
// Close the menu if the user clicks outside of it
if (!menuRef.current?.contains(event.target as Node)) {
closeMenu();
}
};
if (activeWaypointId) {
document.addEventListener('mousedown', handleClickOutside);
} else {
document.removeEventListener('mousedown', handleClickOutside);
}
return () => {
document.removeEventListener('mousedown', handleClickOutside);
};
}, [activeWaypointId]);
const menuItems: OSRDMenuItem[] = [
{
title: t('waypointMenu.hide'),
icon: <EyeClosed />,
disabled: filteredWaypoints ? filteredWaypoints.length <= 2 : false,
disabledMessage: t('waypointsPanel.warning'),
onClick: () => {
closeMenu();
setFilteredWaypoints?.((prevFilteredWaypoints) => {
const newFilteredWaypoints = prevFilteredWaypoints.filter(
(waypoint) => waypoint.id !== activeWaypointId
);
// We need to removed the id because it can change for waypoints added by map click
const simplifiedPath = projectionPath?.map((waypoint) =>
omit(waypoint, ['id', 'deleted'])
);
// TODO : when switching to the manchette back-end manager, remove all logic using
// cleanScenarioLocalStorage from projet/study/scenario components (single/multi select)
localStorage.setItem(
`${timetableId}-${JSON.stringify(simplifiedPath)}`,
JSON.stringify(newFilteredWaypoints)
);
return newFilteredWaypoints;
});
},
},
];
const handleWaypointClick = (id: string) => {
// Avoid reopening the menu when clicking on another waypoint or on the same one
if (isClickOnWaypoint) {
setIsClickOnWaypoint(false);
return;
}
setActiveWaypointId(id);
};
return { menuRef, menuItems, activeWaypointId, handleWaypointClick };
};
export default useWaypointMenu;