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,35 @@ 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 descriptionRef = useRef < HTMLDivElement | null > ( null ) ;
30
+ const ref = useRef < HTMLDivElement | null > ( null ) ;
31
+ const [ isTooLongDescription , setIsTooLongDescription ] = useState < boolean > ( false ) ;
32
+
33
+ const toggleDescription = ( ) => {
34
+ setIsOpenedDescription ( ! isOpenedDescription ) ;
35
+ } ;
36
+
37
+ const checkIfTooLong = ( ) => {
38
+ if ( ref && ref . current ) {
39
+ ref . current . scrollHeight > 60
40
+ ? setIsTooLongDescription ( true )
41
+ : setIsTooLongDescription ( false ) ;
42
+ }
43
+ } ;
44
+
45
+ useOutsideClick ( descriptionRef , ( ) => setIsOpenedDescription ( false ) ) ;
46
+
47
+ useEffect ( ( ) => {
48
+ checkIfTooLong ( ) ;
49
+ window . addEventListener ( 'resize' , checkIfTooLong ) ;
50
+
51
+ return ( ) => {
52
+ window . removeEventListener ( 'resize' , checkIfTooLong ) ;
53
+ } ;
54
+ } , [ ] ) ;
55
+
56
+ useEffect ( checkIfTooLong , [ scenario . description ] ) ;
25
57
26
58
return (
27
59
< div >
@@ -30,10 +62,29 @@ const ScenarioDescription = ({
30
62
{ scenario . name }
31
63
</ span >
32
64
</ div >
33
-
34
65
< div className = "scenario-description" >
35
66
{ scenario . description && (
36
- < div className = "scenario-details-description" > { scenario . description } </ div >
67
+ < div ref = { ref } className = "scenario-details-description not-opened" >
68
+ { scenario . description }
69
+ { isTooLongDescription && (
70
+ < span role = "button" onClick = { toggleDescription } tabIndex = { 0 } >
71
+ (plus)
72
+ </ span >
73
+ ) }
74
+ </ div >
75
+ ) }
76
+ { isOpenedDescription && (
77
+ < div ref = { descriptionRef } className = "scenario-details-description opened" >
78
+ { scenario . description }
79
+ < span
80
+ onClick = { toggleDescription }
81
+ role = "button"
82
+ tabIndex = { 0 }
83
+ className = "displayed-description"
84
+ >
85
+ < X />
86
+ </ span >
87
+ </ div >
37
88
) }
38
89
< div className = "flex justify-end" >
39
90
< button
0 commit comments