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