-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathScenarioDescription.tsx
154 lines (140 loc) · 5.03 KB
/
ScenarioDescription.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
import { useEffect, useRef, useState } from 'react';
import { Blocked, ChevronLeft, Pencil, X } from '@osrd-project/ui-icons';
import { useTranslation } from 'react-i18next';
import type { InfraWithState, ScenarioResponse } from 'common/api/osrdEditoastApi';
import { useModal } from 'common/BootstrapSNCF/ModalSNCF';
import AddAndEditScenarioModal from 'modules/scenario/components/AddOrEditScenarioModal';
import useOutsideClick from 'utils/hooks/useOutsideClick';
import InfraLoadingState from './InfraLoadingState';
type ScenarioDescriptionProps = {
scenario: ScenarioResponse;
infra?: InfraWithState;
infraReloadCount: number;
collapseTimetable: () => void;
};
const ScenarioDescription = ({
scenario,
infra,
infraReloadCount,
collapseTimetable,
}: ScenarioDescriptionProps) => {
const { t } = useTranslation('operationalStudies/scenario');
const { openModal } = useModal();
const [isOpenedDescription, setIsOpenedDescription] = useState<boolean>(false);
const expandedDescriptionRef = useRef<HTMLDivElement | null>(null);
const collapsedDescriptionRef = useRef<HTMLDivElement | null>(null);
const [isTooLongDescription, setIsTooLongDescription] = useState<boolean>(false);
const toggleDescription = () => {
setIsOpenedDescription(!isOpenedDescription);
};
const checkIfDescriptionIsTooLong = () => {
if (collapsedDescriptionRef.current)
setIsTooLongDescription(
collapsedDescriptionRef.current.scrollHeight > collapsedDescriptionRef.current.clientHeight
);
};
useOutsideClick(expandedDescriptionRef, () => setIsOpenedDescription(false));
useEffect(() => {
checkIfDescriptionIsTooLong();
window.addEventListener('resize', checkIfDescriptionIsTooLong);
return () => {
window.removeEventListener('resize', checkIfDescriptionIsTooLong);
};
}, []);
useEffect(checkIfDescriptionIsTooLong, [scenario.description]);
return (
<div>
<div className="scenario-details-name">
<span className="flex-grow-1 scenario-name text-truncate" title={scenario.name}>
{scenario.name}
</span>
</div>
<div className="scenario-description">
{scenario.description && (
<div ref={collapsedDescriptionRef} className="scenario-details-description not-opened">
{scenario.description}
{isTooLongDescription && (
<span role="button" onClick={toggleDescription} tabIndex={0}>
(plus)
</span>
)}
</div>
)}
{isOpenedDescription && (
<div ref={expandedDescriptionRef} className="scenario-details-description opened">
{scenario.description}
<span
onClick={toggleDescription}
role="button"
tabIndex={0}
className="displayed-description"
>
<X />
</span>
</div>
)}
<div className="flex justify-end">
<button
data-testid="scenario-collapse-button"
type="button"
className="scenario-collapse-button"
aria-label={t('toggleTimetable')}
onClick={() => collapseTimetable()}
>
<ChevronLeft />
</button>
</div>
<button
data-testid="editScenario"
className="update-scenario"
type="button"
aria-label={t('editScenario')}
onClick={() =>
openModal(
<AddAndEditScenarioModal editionMode scenario={scenario} />,
'xl',
'no-close-modal'
)
}
title={t('editScenario')}
>
<Pencil />
</button>
</div>
<div className="scenario-details-electrical-profile-set">
{scenario.electrical_profile_set_id
? scenario.electrical_profile_set_id
: t('noElectricalProfileSet')}
</div>
<div className="scenario-details-infra-name">
{t('infrastructure')} :
{infra && <InfraLoadingState infra={infra} />}
<span className="scenario-infra-name">{scenario.infra_name}</span> | ID
{scenario.infra_id}
</div>
{infra &&
infra.state === 'TRANSIENT_ERROR' &&
(infraReloadCount <= 5 ? (
<div className="scenario-details-infra-error">
<Blocked variant="fill" />
<span className="error-description">
{t('errorMessages.unableToLoadInfra', { infraReloadCount })}
</span>
</div>
) : (
<div className="scenario-details-infra-error">
<Blocked variant="fill" />
<span className="error-description">{t('errorMessages.softErrorInfra')}</span>
</div>
))}
{infra && infra.state === 'ERROR' && (
<div className="scenario-details-infra-error">
<Blocked variant="fill" />
<span className="error-description">{t('errorMessages.hardErrorInfra')}</span>
</div>
)}
</div>
);
};
export default ScenarioDescription;