-
Notifications
You must be signed in to change notification settings - Fork 40
/
HumanWeight.js
182 lines (170 loc) · 3.86 KB
/
HumanWeight.js
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { Trans, useTranslation } from 'react-i18next'
import { correctValue } from '../../components/publicodesUtils'
import { getCurrentLangInfos } from '../../locales/translation'
import { disabledAction, supersededAction } from './ActionVignette'
export const humanWeight = (
{ t, i18n }, // We need to be passed as an argument instead of calling useTranslation inside the body, to avoid 'Rendered more hooks than during the previous render.'
possiblyNegativeValue,
concise = false,
noSign
) => {
const v = Math.abs(possiblyNegativeValue)
const [raw, unit, digits] =
v === 0
? [v, '', 0]
: v < 1
? [v * 1000, 'g', 1]
: v < 1000
? [v, 'kg', 0]
: [v / 1000, concise ? 't' : v > 2000 ? t('tonnes') : t('tonne'), 1]
const abrvLocale = getCurrentLangInfos(i18n).abrvLocale
const signedValue = raw * (possiblyNegativeValue < 0 ? -1 : 1),
resultValue = noSign ? raw : signedValue,
value = resultValue.toLocaleString(abrvLocale, {
minimumFractionDigits: digits,
maximumFractionDigits: digits,
})
return [value, unit]
}
const HumanWeight = ({
nodeValue,
overrideValue,
metric = 'climat',
unitText: givenUnitText,
longUnitText,
}) => {
const { t, i18n } = useTranslation()
const unitText = givenUnitText || (
<span>
<Trans i18nKey="humanWeight.unitSuffix">de</Trans> CO₂-e /{' '}
{t('an')}
</span>
)
const [value, unit] =
metric === 'climat'
? humanWeight({ t, i18n }, nodeValue)
: metric === 'pétrole'
? [nodeValue, '']
: [null]
if (value == null) return null
return (
<span
css={`
display: flex;
flex-wrap: wrap;
flex-direction: column;
justify-content: center;
align-items: center;
del {
color: var(--lightColor2);
font-weight: normal;
text-decoration: none;
position: relative;
}
del::after {
content: '';
position: absolute;
top: 50%;
left: 0;
width: 100%;
height: 1px;
background: var(--lightColor2);
transform: rotate(-7deg);
}
ins {
text-decoration: none;
}
`}
>
<strong
role="status"
className="humanvalue"
css={`
font-size: 160%;
font-weight: 600;
`}
>
<span>
{overrideValue ? (
<>
<del css="">{value}</del>
<ins>
{humanWeight({ t, i18n }, nodeValue - overrideValue)[0]}
</ins>
</>
) : (
value
)}
</span>
<span> {unit}</span>
</strong>{' '}
<span>
<span
className="unitSuffix"
css={
longUnitText &&
`
@media (min-width: 800px) {
display: none;
}
`
}
>
{unitText}
</span>
{longUnitText && (
<span
className="unitSuffix"
css={`
@media (max-width: 800px) {
display: none;
}
`}
>
{longUnitText}
</span>
)}
</span>
</span>
)
}
export const DiffHumanWeight = ({
nodeValue,
engine,
rules,
actionChoices,
metric,
unitSuffix,
}) => {
// Here we compute the sum of all the actions the user has chosen
// we could also use publicode's 'actions' variable sum,
// but each action would need to have a "chosen" question,
// and disactivation rules
const actions = rules['actions'].formule.somme.map((dottedName) =>
engine.evaluate(dottedName)
),
actionTotal = actions.reduce((memo, action) => {
const correctedValue = correctValue({
nodeValue: action.nodeValue,
unit: action.unit,
})
if (
correctedValue &&
actionChoices[action.dottedName] &&
!supersededAction(action.dottedName, rules, actionChoices) &&
!disabledAction(rules[action.dottedName], action.nodeValue)
) {
return memo + correctedValue || 0
} else return memo
}, 0)
return (
<HumanWeight
nodeValue={nodeValue}
overrideValue={actionTotal !== 0 && actionTotal}
metric={metric}
unitSuffix={unitSuffix}
/>
)
}
export default HumanWeight