1
- import { Blocked , ChevronLeft , Pencil } from '@osrd-project/ui-icons' ;
1
+ import { useEffect , useRef , useState } from 'react' ;
2
+
3
+ import { Blocked , ChevronLeft , Pencil , X } from '@osrd-project/ui-icons' ;
2
4
import { useTranslation } from 'react-i18next' ;
3
5
4
6
import type { InfraWithState , ScenarioResponse } from 'common/api/osrdEditoastApi' ;
5
7
import { useModal } from 'common/BootstrapSNCF/ModalSNCF' ;
6
8
import AddAndEditScenarioModal from 'modules/scenario/components/AddOrEditScenarioModal' ;
9
+ import useOutsideClick from 'utils/hooks/useOutsideClick' ;
7
10
8
11
import InfraLoadingState from './InfraLoadingState' ;
9
12
@@ -22,6 +25,34 @@ const ScenarioDescription = ({
22
25
} : ScenarioDescriptionProps ) => {
23
26
const { t } = useTranslation ( 'operationalStudies/scenario' ) ;
24
27
const { openModal } = useModal ( ) ;
28
+ const [ isOpenedDescription , setIsOpenedDescription ] = useState < boolean > ( false ) ;
29
+ const expandedDescriptionRef = useRef < HTMLDivElement | null > ( null ) ;
30
+ const collapsedDescriptionRef = useRef < HTMLDivElement | null > ( null ) ;
31
+ const [ isTooLongDescription , setIsTooLongDescription ] = useState < boolean > ( false ) ;
32
+
33
+ const toggleDescription = ( ) => {
34
+ setIsOpenedDescription ( ! isOpenedDescription ) ;
35
+ } ;
36
+
37
+ const checkIfDescriptionIsTooLong = ( ) => {
38
+ if ( collapsedDescriptionRef . current )
39
+ setIsTooLongDescription (
40
+ collapsedDescriptionRef . current . scrollHeight > collapsedDescriptionRef . current . clientHeight
41
+ ) ;
42
+ } ;
43
+
44
+ useOutsideClick ( expandedDescriptionRef , ( ) => setIsOpenedDescription ( false ) ) ;
45
+
46
+ useEffect ( ( ) => {
47
+ checkIfDescriptionIsTooLong ( ) ;
48
+ window . addEventListener ( 'resize' , checkIfDescriptionIsTooLong ) ;
49
+
50
+ return ( ) => {
51
+ window . removeEventListener ( 'resize' , checkIfDescriptionIsTooLong ) ;
52
+ } ;
53
+ } , [ ] ) ;
54
+
55
+ useEffect ( checkIfDescriptionIsTooLong , [ scenario . description ] ) ;
25
56
26
57
return (
27
58
< div >
@@ -30,10 +61,29 @@ const ScenarioDescription = ({
30
61
{ scenario . name }
31
62
</ span >
32
63
</ div >
33
-
34
64
< div className = "scenario-description" >
35
65
{ scenario . description && (
36
- < div className = "scenario-details-description" > { scenario . description } </ div >
66
+ < div ref = { collapsedDescriptionRef } className = "scenario-details-description not-opened" >
67
+ { scenario . description }
68
+ { isTooLongDescription && (
69
+ < span role = "button" onClick = { toggleDescription } tabIndex = { 0 } >
70
+ (plus)
71
+ </ span >
72
+ ) }
73
+ </ div >
74
+ ) }
75
+ { isOpenedDescription && (
76
+ < div ref = { expandedDescriptionRef } className = "scenario-details-description opened" >
77
+ { scenario . description }
78
+ < span
79
+ onClick = { toggleDescription }
80
+ role = "button"
81
+ tabIndex = { 0 }
82
+ className = "displayed-description"
83
+ >
84
+ < X />
85
+ </ span >
86
+ </ div >
37
87
) }
38
88
< div className = "flex justify-end" >
39
89
< button
0 commit comments