-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
front: add waypoint menu in manchette
- When clicking on a waypoint in the manchette, display a menu with some actions - Only action for now is to hide the waypoint in the manchette Signed-off-by: SharglutDev <p.filimon75@gmail.com>
- Loading branch information
1 parent
2b521c9
commit 589c8f7
Showing
5 changed files
with
126 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
front/src/modules/simulationResult/components/SpaceTimeChart/useWaypointMenu.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import React, { useRef, useState } from 'react'; | ||
|
||
import { EyeClosed } from '@osrd-project/ui-icons'; | ||
import { useTranslation } from 'react-i18next'; | ||
|
||
import type { OperationalPoint } from 'applications/operationalStudies/types'; | ||
import type { OSRDMenuItem } from 'common/OSRDMenu'; | ||
import useModalFocusTrap from 'utils/hooks/useModalFocusTrap'; | ||
import useOutsideClick from 'utils/hooks/useOutsideClick'; | ||
|
||
const SPACETIME_CHART_HEADER_HEIGHT = 40; | ||
const WAYPOINT_MENU_OFFSET = 2; | ||
|
||
const useWaypointMenu = ( | ||
manchetteWrapperRef: React.RefObject<HTMLDivElement>, | ||
filteredWaypoints?: OperationalPoint[], | ||
setFilteredWaypoints?: (waypoints: OperationalPoint[]) => void | ||
) => { | ||
const { t } = useTranslation('simulation'); | ||
|
||
const [menuPosition, setMenuPosition] = useState<{ top: number; left: number }>(); | ||
const [activeOperationalPointId, setActiveOperationalPointId] = useState<string>(); | ||
|
||
const menuRef = useRef<HTMLDivElement>(null); | ||
|
||
const closeMenu = () => { | ||
setMenuPosition(undefined); | ||
setActiveOperationalPointId(undefined); | ||
}; | ||
|
||
useOutsideClick(menuRef, closeMenu); | ||
useModalFocusTrap(menuRef, closeMenu); | ||
|
||
const menuItems: OSRDMenuItem[] = [ | ||
{ | ||
title: t('waypointMenu.hide'), | ||
icon: <EyeClosed />, | ||
onClick: () => { | ||
setMenuPosition(undefined); | ||
setActiveOperationalPointId(undefined); | ||
if (filteredWaypoints && setFilteredWaypoints) { | ||
setFilteredWaypoints( | ||
filteredWaypoints.filter((waypoint) => waypoint.id !== activeOperationalPointId) | ||
); | ||
} | ||
}, | ||
}, | ||
]; | ||
|
||
const handleWaypointClick = (id: string, ref: HTMLDivElement | null) => { | ||
if (!ref || !manchetteWrapperRef.current) return; | ||
const position = ref.getBoundingClientRect(); | ||
const manchetteWrapperPosition = manchetteWrapperRef.current.getBoundingClientRect(); | ||
|
||
// The position of the clicked waypoint is relative to the viewport so we need to | ||
// substract the position of the manchetteWrapper to get the accurate position | ||
setMenuPosition({ | ||
top: | ||
position.bottom - | ||
manchetteWrapperPosition.top + | ||
SPACETIME_CHART_HEADER_HEIGHT - | ||
WAYPOINT_MENU_OFFSET, | ||
left: position.left - manchetteWrapperPosition.left, | ||
}); | ||
setActiveOperationalPointId(id); | ||
}; | ||
|
||
return { menuRef, menuPosition, menuItems, activeOperationalPointId, handleWaypointClick }; | ||
}; | ||
|
||
export default useWaypointMenu; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters