@controleonline/ui-common 1.2.129 → 1.2.131
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.release/rc-manifest.json +6 -6
- package/package.json +1 -1
- package/src/react/components/RuntimeFooterMarqueeText.js +107 -6
- package/src/react/components/RuntimeInfoFooter.js +10 -48
- package/src/react/utils/runtimeFooter.js +2 -0
- package/src/react/utils/runtimeFooterText.js +11 -0
- package/src/tests/react/components/RuntimeFooterMarqueeText.test.js +140 -0
- package/src/tests/react/utils/runtimeFooterText.test.js +34 -0
- package/src/tests/react/utils/translate.missingBatch.test.mjs +101 -0
- package/src/utils/translate.js +104 -5
- package/src/utils/translateDiscovery.js +12 -8
- package/src/utils/translatePending.js +121 -1
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "1.2.
|
|
2
|
+
"version": "1.2.131",
|
|
3
3
|
"rc": 1,
|
|
4
|
-
"branch": "rc/1.2.
|
|
5
|
-
"baseMaster": "
|
|
4
|
+
"branch": "rc/1.2.131-rc.1",
|
|
5
|
+
"baseMaster": "3aeaced0835b88b82621be16259985080203eee1",
|
|
6
6
|
"tasks": [
|
|
7
|
-
|
|
7
|
+
159
|
|
8
8
|
],
|
|
9
9
|
"frozen": true,
|
|
10
10
|
"repositories": {
|
|
11
|
-
"ControleOnline/ui-common": "
|
|
11
|
+
"ControleOnline/ui-common": "96efe92a8b5fd573c7688b21713f75fe7a0b57c2"
|
|
12
12
|
}
|
|
13
|
-
}
|
|
13
|
+
}
|
package/package.json
CHANGED
|
@@ -2,6 +2,7 @@ const React = require('react');
|
|
|
2
2
|
const {useEffect, useRef, useState} = React;
|
|
3
3
|
const RN = require('react-native');
|
|
4
4
|
const Animated = RN.Animated;
|
|
5
|
+
const Easing = RN.Easing;
|
|
5
6
|
const Text = RN.Text;
|
|
6
7
|
const View = RN.View;
|
|
7
8
|
const Platform = RN.Platform || {OS: 'native'};
|
|
@@ -12,10 +13,22 @@ const GAP_PX = 32;
|
|
|
12
13
|
const MS_PER_PX = 28;
|
|
13
14
|
const MIN_DURATION_MS = 4000;
|
|
14
15
|
const HOLD_MS = 1200;
|
|
16
|
+
// Continuous ticker (right -> left): constant, readable speed.
|
|
17
|
+
const TICKER_MS_PER_PX = 20;
|
|
18
|
+
const TICKER_MIN_DURATION_MS = 3000;
|
|
19
|
+
// Native Text is measured with the parent width as the maximum, so a row as
|
|
20
|
+
// narrow as the container would truncate the text (and the measured content
|
|
21
|
+
// width). The ticker row is therefore much wider than any footer; the
|
|
22
|
+
// container clips it (overflow: hidden).
|
|
23
|
+
const TICKER_ROW_WIDTH_PX = 20000;
|
|
15
24
|
|
|
16
25
|
/**
|
|
17
26
|
* Single-line footer text. When content overflows the available width,
|
|
18
27
|
* scrolls horizontally in a continuous loop. Short text stays static.
|
|
28
|
+
*
|
|
29
|
+
* `continuous` turns it into a ticker (marquee): the text ALWAYS scrolls from
|
|
30
|
+
* right to left in a seamless loop (repeated copies fill the width), on web
|
|
31
|
+
* (CSS keyframes) and native (Animated loop with the native driver).
|
|
19
32
|
* On web, avoids Animated opacity/transform issues that hid the label.
|
|
20
33
|
* RN Web also needs intrinsic sizing because Text can collapse to width 0
|
|
21
34
|
* inside a flex row.
|
|
@@ -28,6 +41,7 @@ const RuntimeFooterMarqueeText = props => {
|
|
|
28
41
|
const text = props && props.text;
|
|
29
42
|
const color = props && props.color;
|
|
30
43
|
const style = props && props.style;
|
|
44
|
+
const continuous = !!(props && props.continuous);
|
|
31
45
|
const testID =
|
|
32
46
|
(props && props.testID) || 'runtime-footer-marquee-text';
|
|
33
47
|
|
|
@@ -36,8 +50,9 @@ const RuntimeFooterMarqueeText = props => {
|
|
|
36
50
|
const translateX = useRef(new Animated.Value(0)).current;
|
|
37
51
|
const animationRef = useRef(null);
|
|
38
52
|
|
|
39
|
-
const shouldMarquee =
|
|
40
|
-
|
|
53
|
+
const shouldMarquee = continuous
|
|
54
|
+
? containerWidth > 0 && contentWidth > 0
|
|
55
|
+
: !IS_WEB && containerWidth > 0 && contentWidth > containerWidth + 2;
|
|
41
56
|
|
|
42
57
|
useEffect(() => {
|
|
43
58
|
if (animationRef.current) {
|
|
@@ -47,18 +62,22 @@ const RuntimeFooterMarqueeText = props => {
|
|
|
47
62
|
translateX.stopAnimation();
|
|
48
63
|
translateX.setValue(0);
|
|
49
64
|
|
|
50
|
-
|
|
65
|
+
// On web the continuous ticker uses CSS keyframes (see render below).
|
|
66
|
+
if (!shouldMarquee || (continuous && IS_WEB)) {
|
|
51
67
|
return undefined;
|
|
52
68
|
}
|
|
53
69
|
|
|
54
70
|
const distance = contentWidth + GAP_PX;
|
|
55
|
-
const duration =
|
|
71
|
+
const duration = continuous
|
|
72
|
+
? Math.max(Math.round(distance * TICKER_MS_PER_PX), TICKER_MIN_DURATION_MS)
|
|
73
|
+
: Math.max(Math.round(distance * MS_PER_PX), MIN_DURATION_MS);
|
|
56
74
|
const loop = Animated.loop(
|
|
57
75
|
Animated.sequence([
|
|
58
|
-
Animated.delay(HOLD_MS),
|
|
76
|
+
Animated.delay(continuous ? 0 : HOLD_MS),
|
|
59
77
|
Animated.timing(translateX, {
|
|
60
78
|
toValue: -distance,
|
|
61
79
|
duration: duration,
|
|
80
|
+
easing: continuous && Easing ? Easing.linear : undefined,
|
|
62
81
|
useNativeDriver: USE_NATIVE_DRIVER,
|
|
63
82
|
}),
|
|
64
83
|
Animated.timing(translateX, {
|
|
@@ -75,7 +94,7 @@ const RuntimeFooterMarqueeText = props => {
|
|
|
75
94
|
animationRef.current = null;
|
|
76
95
|
translateX.stopAnimation();
|
|
77
96
|
};
|
|
78
|
-
}, [shouldMarquee, contentWidth, containerWidth, text, translateX]);
|
|
97
|
+
}, [shouldMarquee, continuous, contentWidth, containerWidth, text, translateX]);
|
|
79
98
|
|
|
80
99
|
const handleContainerLayout = event => {
|
|
81
100
|
const nextWidth = Math.round(
|
|
@@ -128,6 +147,88 @@ const RuntimeFooterMarqueeText = props => {
|
|
|
128
147
|
|
|
129
148
|
textStyle.push(shouldMarquee ? {textAlign: 'left'} : {textAlign: 'center'});
|
|
130
149
|
|
|
150
|
+
if (continuous) {
|
|
151
|
+
// Enough copies so the row is always wider than the container + one cycle:
|
|
152
|
+
// the loop restarts (translateX 0) with no visible gap.
|
|
153
|
+
const distance = contentWidth + GAP_PX;
|
|
154
|
+
const copies = shouldMarquee
|
|
155
|
+
? Math.max(2, Math.ceil(containerWidth / distance) + 1)
|
|
156
|
+
: 1;
|
|
157
|
+
const durationMs = Math.max(
|
|
158
|
+
Math.round(distance * TICKER_MS_PER_PX),
|
|
159
|
+
TICKER_MIN_DURATION_MS,
|
|
160
|
+
);
|
|
161
|
+
|
|
162
|
+
const copyNodes = [];
|
|
163
|
+
for (let index = 0; index < copies; index += 1) {
|
|
164
|
+
copyNodes.push(
|
|
165
|
+
React.createElement(
|
|
166
|
+
Text,
|
|
167
|
+
{
|
|
168
|
+
key: 'ticker-copy-' + index,
|
|
169
|
+
numberOfLines: 1,
|
|
170
|
+
ellipsizeMode: 'clip',
|
|
171
|
+
accessible: index === 0,
|
|
172
|
+
importantForAccessibility: index === 0 ? 'auto' : 'no',
|
|
173
|
+
onLayout: index === 0 ? handleTextLayout : undefined,
|
|
174
|
+
testID: index === 0 ? undefined : 'runtime-footer-ticker-copy',
|
|
175
|
+
style: textStyle.concat([
|
|
176
|
+
{textAlign: 'left', marginRight: GAP_PX},
|
|
177
|
+
IS_WEB ? {whiteSpace: 'nowrap'} : null,
|
|
178
|
+
]),
|
|
179
|
+
},
|
|
180
|
+
text,
|
|
181
|
+
),
|
|
182
|
+
);
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
const rowStyle = {
|
|
186
|
+
flexDirection: 'row',
|
|
187
|
+
flexWrap: 'nowrap',
|
|
188
|
+
alignItems: 'center',
|
|
189
|
+
alignSelf: 'flex-start',
|
|
190
|
+
opacity: 1,
|
|
191
|
+
};
|
|
192
|
+
if (!IS_WEB) {
|
|
193
|
+
rowStyle.width = TICKER_ROW_WIDTH_PX;
|
|
194
|
+
}
|
|
195
|
+
if (IS_WEB && shouldMarquee) {
|
|
196
|
+
rowStyle.animationKeyframes = [
|
|
197
|
+
{
|
|
198
|
+
'0%': {transform: [{translateX: 0}]},
|
|
199
|
+
'100%': {transform: [{translateX: -distance}]},
|
|
200
|
+
},
|
|
201
|
+
];
|
|
202
|
+
rowStyle.animationDuration = durationMs + 'ms';
|
|
203
|
+
rowStyle.animationIterationCount = 'infinite';
|
|
204
|
+
rowStyle.animationTimingFunction = 'linear';
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return React.createElement(
|
|
208
|
+
View,
|
|
209
|
+
{
|
|
210
|
+
testID: testID,
|
|
211
|
+
style: {
|
|
212
|
+
flex: 1,
|
|
213
|
+
overflow: 'hidden',
|
|
214
|
+
justifyContent: 'center',
|
|
215
|
+
opacity: 1,
|
|
216
|
+
},
|
|
217
|
+
onLayout: handleContainerLayout,
|
|
218
|
+
},
|
|
219
|
+
React.createElement(
|
|
220
|
+
IS_WEB ? View : Animated.View,
|
|
221
|
+
{
|
|
222
|
+
testID: 'runtime-footer-ticker-row',
|
|
223
|
+
style: IS_WEB
|
|
224
|
+
? rowStyle
|
|
225
|
+
: [rowStyle, {transform: [{translateX: translateX}]}],
|
|
226
|
+
},
|
|
227
|
+
copyNodes,
|
|
228
|
+
),
|
|
229
|
+
);
|
|
230
|
+
}
|
|
231
|
+
|
|
131
232
|
const content = React.createElement(
|
|
132
233
|
Text,
|
|
133
234
|
{
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import React, {
|
|
1
|
+
import React, {useMemo} from 'react';
|
|
2
2
|
import {
|
|
3
3
|
ActivityIndicator,
|
|
4
4
|
Platform,
|
|
@@ -18,14 +18,13 @@ import {
|
|
|
18
18
|
getRuntimeFooterDebugInfo,
|
|
19
19
|
getRuntimeFooterPrimaryText,
|
|
20
20
|
getRuntimeFooterRotationEntries,
|
|
21
|
+
getRuntimeFooterTickerText,
|
|
21
22
|
getRuntimeFooterText,
|
|
22
|
-
getRuntimeFooterTextLines,
|
|
23
23
|
} from '@controleonline/ui-common/src/react/utils/runtimeFooter';
|
|
24
24
|
import styles from './RuntimeInfoFooter.styles';
|
|
25
25
|
import RuntimeFooterMarqueeText from './RuntimeFooterMarqueeText';
|
|
26
26
|
import resolveHomeFooterSpacing from './RuntimeInfoFooter.home.spacing';
|
|
27
27
|
|
|
28
|
-
const ROTATION_INTERVAL_MS = 4000;
|
|
29
28
|
const COMPACT_BREAKPOINT = 720;
|
|
30
29
|
const MAX_INLINE_TEXT_LENGTH = 84;
|
|
31
30
|
const FALLBACK_TEXT_COLOR = '#0f172a';
|
|
@@ -72,7 +71,6 @@ const RuntimeInfoFooter = ({
|
|
|
72
71
|
}) => {
|
|
73
72
|
const {width} = useWindowDimensions();
|
|
74
73
|
const insets = useSafeAreaInsets();
|
|
75
|
-
const [activeIndex, setActiveIndex] = useState(0);
|
|
76
74
|
const allStores = useStores(state => state);
|
|
77
75
|
const deviceConfigStore = useStore('device_config');
|
|
78
76
|
const runtimeDebugStore = useStore('runtime_debug');
|
|
@@ -119,10 +117,6 @@ const RuntimeInfoFooter = ({
|
|
|
119
117
|
deviceConfigItem?.configs,
|
|
120
118
|
storeMainCompany?.configs,
|
|
121
119
|
]);
|
|
122
|
-
const footerTextLines = useMemo(
|
|
123
|
-
() => getRuntimeFooterTextLines(companyFooterText),
|
|
124
|
-
[companyFooterText],
|
|
125
|
-
);
|
|
126
120
|
const deviceConfigs = useMemo(
|
|
127
121
|
() => parseConfigsObject(deviceConfigItem?.configs),
|
|
128
122
|
[deviceConfigItem?.configs],
|
|
@@ -143,14 +137,13 @@ const RuntimeInfoFooter = ({
|
|
|
143
137
|
}),
|
|
144
138
|
[companyFooterText, primaryText],
|
|
145
139
|
);
|
|
140
|
+
// Ticker: the text written by the user (company footer) comes first, then the
|
|
141
|
+
// system text (e.g. "web • MANAGER (ip) / v1.10.x"). Both scroll together,
|
|
142
|
+
// right to left, in a continuous loop (no more alternating between them).
|
|
146
143
|
const inlineText = useMemo(
|
|
147
|
-
() =>
|
|
148
|
-
[
|
|
144
|
+
() => getRuntimeFooterTickerText({companyFooterText, primaryText}),
|
|
145
|
+
[companyFooterText, primaryText],
|
|
149
146
|
);
|
|
150
|
-
// Always rotate when there is more than one entry (e.g. 1 footer line + primaryText).
|
|
151
|
-
// Do not fall back to concatenated inlineText in that case.
|
|
152
|
-
const shouldRotate =
|
|
153
|
-
!showDebugInfo && rotationEntries.length > 1;
|
|
154
147
|
const footerEntries = useMemo(
|
|
155
148
|
() =>
|
|
156
149
|
Object.values(runtimeDebugSummary?.entries || {})
|
|
@@ -222,44 +215,12 @@ const RuntimeInfoFooter = ({
|
|
|
222
215
|
? 0
|
|
223
216
|
: Math.max(Number(insets.bottom) || 0, 16);
|
|
224
217
|
|
|
225
|
-
useEffect(() => {
|
|
226
|
-
if (!shouldRotate || rotationEntries.length <= 1) {
|
|
227
|
-
setActiveIndex(0);
|
|
228
|
-
return undefined;
|
|
229
|
-
}
|
|
230
|
-
|
|
231
|
-
let timeoutId;
|
|
232
|
-
let cancelled = false;
|
|
233
|
-
|
|
234
|
-
const scheduleTransition = () => {
|
|
235
|
-
timeoutId = setTimeout(() => {
|
|
236
|
-
if (cancelled) {
|
|
237
|
-
return;
|
|
238
|
-
}
|
|
239
|
-
setActiveIndex(current => (current + 1) % rotationEntries.length);
|
|
240
|
-
scheduleTransition();
|
|
241
|
-
}, ROTATION_INTERVAL_MS);
|
|
242
|
-
};
|
|
243
|
-
|
|
244
|
-
scheduleTransition();
|
|
245
|
-
|
|
246
|
-
return () => {
|
|
247
|
-
cancelled = true;
|
|
248
|
-
if (timeoutId) {
|
|
249
|
-
clearTimeout(timeoutId);
|
|
250
|
-
}
|
|
251
|
-
};
|
|
252
|
-
}, [rotationEntries.length, shouldRotate]);
|
|
253
|
-
|
|
254
218
|
if (rotationEntries.length === 0 && !showDebugInfo) {
|
|
255
219
|
return null;
|
|
256
220
|
}
|
|
257
221
|
|
|
258
|
-
const displayedText =
|
|
259
|
-
|
|
260
|
-
? rotationEntries[activeIndex]
|
|
261
|
-
: inlineText
|
|
262
|
-
) || primaryText || formatRuntimeFooterIdentifier(device?.id) || '';
|
|
222
|
+
const displayedText =
|
|
223
|
+
inlineText || primaryText || formatRuntimeFooterIdentifier(device?.id) || '';
|
|
263
224
|
const backgroundColor = colors?.footerBackground;
|
|
264
225
|
const homeSpacing = resolveHomeFooterSpacing(appearance, Platform.OS, insets.bottom);
|
|
265
226
|
const borderColor = colors?.footerBorder;
|
|
@@ -305,6 +266,7 @@ const RuntimeInfoFooter = ({
|
|
|
305
266
|
}
|
|
306
267
|
color={textColor}
|
|
307
268
|
opacity={1}
|
|
269
|
+
continuous
|
|
308
270
|
style={styles.primaryText}
|
|
309
271
|
testID="runtime-footer-primary-text"
|
|
310
272
|
/>
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
const {
|
|
2
2
|
DEVICE_RUNTIME_FOOTER_TEXT_CONFIG_KEY,
|
|
3
3
|
getRuntimeFooterRotationEntries,
|
|
4
|
+
getRuntimeFooterTickerText,
|
|
4
5
|
getRuntimeFooterText,
|
|
5
6
|
getRuntimeFooterTextLines,
|
|
6
7
|
normalizeRuntimeFooterText,
|
|
@@ -482,6 +483,7 @@ module.exports = {
|
|
|
482
483
|
getRuntimeFooterNativeIdentifierCandidates,
|
|
483
484
|
getRuntimeFooterPrimaryText,
|
|
484
485
|
getRuntimeFooterRotationEntries,
|
|
486
|
+
getRuntimeFooterTickerText,
|
|
485
487
|
getRuntimeFooterStoredVersion,
|
|
486
488
|
getRuntimeFooterTextLines,
|
|
487
489
|
getRuntimeFooterVersionCandidates,
|
|
@@ -148,8 +148,19 @@ const getRuntimeFooterRotationEntries = ({companyFooterText, primaryText}) =>
|
|
|
148
148
|
Boolean,
|
|
149
149
|
);
|
|
150
150
|
|
|
151
|
+
const RUNTIME_FOOTER_TICKER_SEPARATOR = ' • ';
|
|
152
|
+
|
|
153
|
+
// Text of the footer ticker: the text written by the user (company footer)
|
|
154
|
+
// first, then the system text (e.g. "web • MANAGER (ip) / v1.10.x").
|
|
155
|
+
const getRuntimeFooterTickerText = ({companyFooterText, primaryText}) =>
|
|
156
|
+
getRuntimeFooterRotationEntries({companyFooterText, primaryText}).join(
|
|
157
|
+
RUNTIME_FOOTER_TICKER_SEPARATOR,
|
|
158
|
+
);
|
|
159
|
+
|
|
151
160
|
module.exports = {
|
|
152
161
|
DEVICE_RUNTIME_FOOTER_TEXT_CONFIG_KEY,
|
|
162
|
+
RUNTIME_FOOTER_TICKER_SEPARATOR,
|
|
163
|
+
getRuntimeFooterTickerText,
|
|
153
164
|
getRuntimeFooterRotationEntries,
|
|
154
165
|
getRuntimeFooterText,
|
|
155
166
|
getRuntimeFooterTextLines,
|
|
@@ -7,6 +7,7 @@ const React = require('react');
|
|
|
7
7
|
const originalLoad = Module._load;
|
|
8
8
|
let lastLoopConfig = null;
|
|
9
9
|
let animationStarts = 0;
|
|
10
|
+
let mockPlatformOS = 'android';
|
|
10
11
|
|
|
11
12
|
before(() => {
|
|
12
13
|
Module._load = function mockLoad(request, parent, isMain) {
|
|
@@ -63,6 +64,8 @@ before(() => {
|
|
|
63
64
|
},
|
|
64
65
|
Text: props => React.createElement('Text', props, props.children),
|
|
65
66
|
View: props => React.createElement('View', props, props.children),
|
|
67
|
+
Easing: {linear: 'linear'},
|
|
68
|
+
Platform: {OS: mockPlatformOS},
|
|
66
69
|
StyleSheet: {create: styles => styles},
|
|
67
70
|
};
|
|
68
71
|
}
|
|
@@ -193,3 +196,140 @@ test('native text keeps intrinsic width even when style carries flex: 1 (android
|
|
|
193
196
|
assert.equal(flat.flexGrow, 0);
|
|
194
197
|
assert.equal(flat.flexShrink, 0);
|
|
195
198
|
});
|
|
199
|
+
|
|
200
|
+
const TICKER_TEXT =
|
|
201
|
+
'www.controleonline.com \u2022 (11) 5555-5159 (Comercial) \u2022 web \u2022 MANAGER (179.125.133.217) / v1.10.236';
|
|
202
|
+
|
|
203
|
+
const renderTicker = ({containerWidth, contentWidth, text = TICKER_TEXT}) => {
|
|
204
|
+
const RuntimeFooterMarqueeText = loadComponent();
|
|
205
|
+
const renderer = require('react-test-renderer');
|
|
206
|
+
|
|
207
|
+
animationStarts = 0;
|
|
208
|
+
lastLoopConfig = null;
|
|
209
|
+
|
|
210
|
+
let tree;
|
|
211
|
+
renderer.act(() => {
|
|
212
|
+
tree = renderer.create(
|
|
213
|
+
React.createElement(RuntimeFooterMarqueeText, {
|
|
214
|
+
text,
|
|
215
|
+
color: '#111',
|
|
216
|
+
continuous: true,
|
|
217
|
+
style: {fontSize: 10},
|
|
218
|
+
}),
|
|
219
|
+
);
|
|
220
|
+
});
|
|
221
|
+
|
|
222
|
+
if (containerWidth) {
|
|
223
|
+
const root = tree.root.findByProps({testID: 'runtime-footer-marquee-text'});
|
|
224
|
+
renderer.act(() => {
|
|
225
|
+
root.props.onLayout({
|
|
226
|
+
nativeEvent: {layout: {width: containerWidth, height: 12}},
|
|
227
|
+
});
|
|
228
|
+
});
|
|
229
|
+
}
|
|
230
|
+
if (contentWidth) {
|
|
231
|
+
const firstText = tree.root.findAllByType('Text')[0];
|
|
232
|
+
renderer.act(() => {
|
|
233
|
+
firstText.props.onLayout({
|
|
234
|
+
nativeEvent: {layout: {width: contentWidth, height: 12}},
|
|
235
|
+
});
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
return tree;
|
|
239
|
+
};
|
|
240
|
+
|
|
241
|
+
describe('continuous ticker (right to left, loop)', () => {
|
|
242
|
+
test('native: scrolls even when the text fits, right to left, linear, no hold', () => {
|
|
243
|
+
mockPlatformOS = 'android';
|
|
244
|
+
// content (200) fits inside the container (800): a ticker must still scroll.
|
|
245
|
+
const tree = renderTicker({containerWidth: 800, contentWidth: 200});
|
|
246
|
+
|
|
247
|
+
assert.ok(lastLoopConfig, 'Animated.loop should start for a ticker');
|
|
248
|
+
const [hold, move, reset] = lastLoopConfig.items;
|
|
249
|
+
assert.equal(hold.ms, 0, 'ticker has no hold before moving');
|
|
250
|
+
assert.equal(move.config.toValue, -(200 + 32), 'moves left by one text cycle');
|
|
251
|
+
assert.equal(move.config.easing, 'linear');
|
|
252
|
+
assert.ok(move.config.toValue < 0, 'right to left');
|
|
253
|
+
assert.equal(reset.config.duration, 0, 'instant reset = seamless loop');
|
|
254
|
+
|
|
255
|
+
// The row must be wider than the container, otherwise native Text is
|
|
256
|
+
// measured with the container width as maximum and the text is truncated.
|
|
257
|
+
const row = tree.root.findByProps({testID: 'runtime-footer-ticker-row'});
|
|
258
|
+
const rowStyle = Object.assign({}, ...[].concat(row.props.style));
|
|
259
|
+
assert.ok(rowStyle.width >= 10000, 'ticker row must not be as narrow as the container');
|
|
260
|
+
assert.equal(rowStyle.flexWrap, 'nowrap');
|
|
261
|
+
|
|
262
|
+
// enough copies to always fill the width: ceil(800 / 232) + 1 = 5
|
|
263
|
+
const texts = tree.root.findAllByType('Text');
|
|
264
|
+
assert.equal(texts.length, 5);
|
|
265
|
+
texts.forEach(node => assert.equal(node.props.children, TICKER_TEXT));
|
|
266
|
+
});
|
|
267
|
+
|
|
268
|
+
test('native: speed is constant (duration proportional to the distance)', () => {
|
|
269
|
+
mockPlatformOS = 'android';
|
|
270
|
+
renderTicker({containerWidth: 800, contentWidth: 200});
|
|
271
|
+
const shortDuration = lastLoopConfig.items[1].config.duration;
|
|
272
|
+
renderTicker({containerWidth: 800, contentWidth: 1000});
|
|
273
|
+
const longDuration = lastLoopConfig.items[1].config.duration;
|
|
274
|
+
|
|
275
|
+
assert.ok(shortDuration >= 3000);
|
|
276
|
+
assert.ok(longDuration > shortDuration);
|
|
277
|
+
assert.equal(Math.round(longDuration / (1000 + 32)), 20);
|
|
278
|
+
});
|
|
279
|
+
|
|
280
|
+
test('native: before measuring there is a single copy and no animation', () => {
|
|
281
|
+
mockPlatformOS = 'android';
|
|
282
|
+
const tree = renderTicker({});
|
|
283
|
+
assert.equal(tree.root.findAllByType('Text').length, 1);
|
|
284
|
+
assert.equal(lastLoopConfig, null);
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
test('native: a long text still scrolls with at least two copies', () => {
|
|
288
|
+
mockPlatformOS = 'android';
|
|
289
|
+
const tree = renderTicker({containerWidth: 300, contentWidth: 1500});
|
|
290
|
+
assert.equal(tree.root.findAllByType('Text').length, 2);
|
|
291
|
+
assert.equal(lastLoopConfig.items[1].config.toValue, -(1500 + 32));
|
|
292
|
+
});
|
|
293
|
+
|
|
294
|
+
test('web: uses CSS keyframes (right to left, infinite, linear), not Animated', () => {
|
|
295
|
+
mockPlatformOS = 'web';
|
|
296
|
+
const tree = renderTicker({containerWidth: 800, contentWidth: 200});
|
|
297
|
+
|
|
298
|
+
assert.equal(lastLoopConfig, null, 'web must not use Animated.loop');
|
|
299
|
+
const row = tree.root.findByProps({testID: 'runtime-footer-ticker-row'});
|
|
300
|
+
const style = Object.assign({}, ...[].concat(row.props.style));
|
|
301
|
+
assert.equal(style.animationIterationCount, 'infinite');
|
|
302
|
+
assert.equal(style.animationTimingFunction, 'linear');
|
|
303
|
+
assert.match(style.animationDuration, /^\d+ms$/);
|
|
304
|
+
const [keyframes] = style.animationKeyframes;
|
|
305
|
+
assert.equal(keyframes['0%'].transform[0].translateX, 0);
|
|
306
|
+
assert.equal(keyframes['100%'].transform[0].translateX, -(200 + 32));
|
|
307
|
+
assert.equal(tree.root.findAllByType('Text').length, 5);
|
|
308
|
+
mockPlatformOS = 'android';
|
|
309
|
+
});
|
|
310
|
+
|
|
311
|
+
test('without the continuous prop the previous behaviour is kept', () => {
|
|
312
|
+
mockPlatformOS = 'android';
|
|
313
|
+
const RuntimeFooterMarqueeText = loadComponent();
|
|
314
|
+
const renderer = require('react-test-renderer');
|
|
315
|
+
animationStarts = 0;
|
|
316
|
+
lastLoopConfig = null;
|
|
317
|
+
let tree;
|
|
318
|
+
renderer.act(() => {
|
|
319
|
+
tree = renderer.create(
|
|
320
|
+
React.createElement(RuntimeFooterMarqueeText, {text: 'short', color: '#111'}),
|
|
321
|
+
);
|
|
322
|
+
});
|
|
323
|
+
const root = tree.root.findByProps({testID: 'runtime-footer-marquee-text'});
|
|
324
|
+
renderer.act(() => {
|
|
325
|
+
root.props.onLayout({nativeEvent: {layout: {width: 400, height: 12}}});
|
|
326
|
+
});
|
|
327
|
+
renderer.act(() => {
|
|
328
|
+
tree.root.findAllByType('Text')[0].props.onLayout({
|
|
329
|
+
nativeEvent: {layout: {width: 40, height: 12}},
|
|
330
|
+
});
|
|
331
|
+
});
|
|
332
|
+
assert.equal(lastLoopConfig, null);
|
|
333
|
+
assert.equal(tree.root.findAllByType('Text').length, 1);
|
|
334
|
+
});
|
|
335
|
+
});
|
|
@@ -3,6 +3,7 @@ const {test} = require('node:test')
|
|
|
3
3
|
|
|
4
4
|
const {
|
|
5
5
|
getRuntimeFooterRotationEntries,
|
|
6
|
+
getRuntimeFooterTickerText,
|
|
6
7
|
getRuntimeFooterText,
|
|
7
8
|
getRuntimeFooterTextLines,
|
|
8
9
|
} = require('../../../react/utils/runtimeFooterText')
|
|
@@ -86,3 +87,36 @@ test('returns empty string when footer text is absent', () => {
|
|
|
86
87
|
assert.equal(getRuntimeFooterText(null, []), '')
|
|
87
88
|
assert.deepEqual(getRuntimeFooterTextLines(''), [])
|
|
88
89
|
})
|
|
90
|
+
|
|
91
|
+
test('ticker text: user text first, then the system text, joined by a bullet', () => {
|
|
92
|
+
const companyFooterText =
|
|
93
|
+
'www.controleonline.com \u2022 (11) 5555-5159 (Comercial) \u2022 (11) 3136-0353 (Suporte)'
|
|
94
|
+
const primaryText = 'web \u2022 MANAGER (179.125.133.217) / v1.10.236'
|
|
95
|
+
|
|
96
|
+
assert.equal(
|
|
97
|
+
getRuntimeFooterTickerText({companyFooterText, primaryText}),
|
|
98
|
+
`${companyFooterText} \u2022 ${primaryText}`,
|
|
99
|
+
)
|
|
100
|
+
})
|
|
101
|
+
|
|
102
|
+
test('ticker text: multiple user lines keep their order before the system text', () => {
|
|
103
|
+
assert.equal(
|
|
104
|
+
getRuntimeFooterTickerText({
|
|
105
|
+
companyFooterText: 'linha 1\nlinha 2',
|
|
106
|
+
primaryText: 'web \u2022 PDV / v1',
|
|
107
|
+
}),
|
|
108
|
+
'linha 1 \u2022 linha 2 \u2022 web \u2022 PDV / v1',
|
|
109
|
+
)
|
|
110
|
+
})
|
|
111
|
+
|
|
112
|
+
test('ticker text: shows only the available text, or nothing', () => {
|
|
113
|
+
assert.equal(
|
|
114
|
+
getRuntimeFooterTickerText({companyFooterText: '', primaryText: 'web \u2022 PDV / v1'}),
|
|
115
|
+
'web \u2022 PDV / v1',
|
|
116
|
+
)
|
|
117
|
+
assert.equal(
|
|
118
|
+
getRuntimeFooterTickerText({companyFooterText: 'Somente usuario', primaryText: ''}),
|
|
119
|
+
'Somente usuario',
|
|
120
|
+
)
|
|
121
|
+
assert.equal(getRuntimeFooterTickerText({companyFooterText: '', primaryText: ''}), '')
|
|
122
|
+
})
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
import assert from 'node:assert/strict';
|
|
2
|
+
import test from 'node:test';
|
|
3
|
+
|
|
4
|
+
import Translate from '../../../utils/translate.js';
|
|
5
|
+
import {
|
|
6
|
+
addPendingTranslates,
|
|
7
|
+
removePendingTranslates,
|
|
8
|
+
} from '../../../utils/translatePending.js';
|
|
9
|
+
import {
|
|
10
|
+
installLocalStorage,
|
|
11
|
+
flushAsync,
|
|
12
|
+
createPendingTranslateStore,
|
|
13
|
+
} from './translate.test.helpers.mjs';
|
|
14
|
+
|
|
15
|
+
// Regression: every commit on the translate store re-renders all subscribers.
|
|
16
|
+
// One commit per missing key exceeded React's nested update limit
|
|
17
|
+
// ("Maximum update depth exceeded"). Missing keys of the same tick must be
|
|
18
|
+
// written with a single commit.
|
|
19
|
+
test('writes many missing keys to the pending store with a single commit', async () => {
|
|
20
|
+
installLocalStorage();
|
|
21
|
+
|
|
22
|
+
const translateStore = createPendingTranslateStore({
|
|
23
|
+
resolveQueuedMessages: async () => [],
|
|
24
|
+
});
|
|
25
|
+
let pendingCommits = 0;
|
|
26
|
+
const setPendingMessages = translateStore.actions.setPendingMessages;
|
|
27
|
+
const queueMissingTranslate = translateStore.actions.queueMissingTranslate;
|
|
28
|
+
translateStore.actions.setPendingMessages = messages => {
|
|
29
|
+
pendingCommits += 1;
|
|
30
|
+
translateStore.getters.pendingMessages = messages;
|
|
31
|
+
return setPendingMessages(messages);
|
|
32
|
+
};
|
|
33
|
+
translateStore.actions.queueMissingTranslate = payload => {
|
|
34
|
+
pendingCommits += 1;
|
|
35
|
+
return queueMissingTranslate(payload);
|
|
36
|
+
};
|
|
37
|
+
pendingCommits = 0;
|
|
38
|
+
|
|
39
|
+
const translate = new Translate(
|
|
40
|
+
[{id: 1}],
|
|
41
|
+
{id: 1},
|
|
42
|
+
{id: 1},
|
|
43
|
+
['menu'],
|
|
44
|
+
translateStore,
|
|
45
|
+
);
|
|
46
|
+
pendingCommits = 0;
|
|
47
|
+
|
|
48
|
+
for (let index = 0; index < 120; index += 1) {
|
|
49
|
+
translate.t('menu', 'menu', `missing_key_${index}`);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
await flushAsync();
|
|
53
|
+
await flushAsync();
|
|
54
|
+
|
|
55
|
+
assert.equal(pendingCommits, 1);
|
|
56
|
+
const queued = translate.getQueuedTranslateGroups();
|
|
57
|
+
assert.equal(queued.length, 1);
|
|
58
|
+
assert.equal(queued[0].keys.length, 120);
|
|
59
|
+
});
|
|
60
|
+
|
|
61
|
+
test('does not queue the same missing key twice across renders', async () => {
|
|
62
|
+
installLocalStorage();
|
|
63
|
+
|
|
64
|
+
const translateStore = createPendingTranslateStore({
|
|
65
|
+
resolveQueuedMessages: async () => [],
|
|
66
|
+
});
|
|
67
|
+
const translate = new Translate(
|
|
68
|
+
[{id: 1}],
|
|
69
|
+
{id: 1},
|
|
70
|
+
{id: 1},
|
|
71
|
+
['menu'],
|
|
72
|
+
translateStore,
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
for (let render = 0; render < 5; render += 1) {
|
|
76
|
+
translate.t('menu', 'menu', 'products');
|
|
77
|
+
await flushAsync();
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
assert.deepEqual(translate.getQueuedTranslateGroups(), [
|
|
81
|
+
{store: 'menu', type: 'menu', keys: ['products']},
|
|
82
|
+
]);
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
test('addPendingTranslates / removePendingTranslates are immutable and idempotent', () => {
|
|
86
|
+
const entries = [
|
|
87
|
+
{store: 'menu', type: 'menu', key: 'a', translate: 'A'},
|
|
88
|
+
{store: 'menu', type: 'menu', key: 'b', translate: 'B'},
|
|
89
|
+
];
|
|
90
|
+
const base = {};
|
|
91
|
+
const added = addPendingTranslates(base, 'pt_BR', 1, entries);
|
|
92
|
+
|
|
93
|
+
assert.deepEqual(base, {});
|
|
94
|
+
assert.deepEqual(added['pt-br'].companies['1'].menu.menu, {a: 'A', b: 'B'});
|
|
95
|
+
assert.equal(addPendingTranslates(added, 'pt-br', 1, entries), added);
|
|
96
|
+
|
|
97
|
+
const removed = removePendingTranslates(added, 'pt-br', 1, entries);
|
|
98
|
+
assert.deepEqual(removed, {});
|
|
99
|
+
assert.deepEqual(added['pt-br'].companies['1'].menu.menu, {a: 'A', b: 'B'});
|
|
100
|
+
assert.equal(removePendingTranslates(removed, 'pt-br', 1, entries), removed);
|
|
101
|
+
});
|
package/src/utils/translate.js
CHANGED
|
@@ -15,6 +15,8 @@ import {
|
|
|
15
15
|
writeMessageToBucket,
|
|
16
16
|
} from "./translateStorage.js";
|
|
17
17
|
import {
|
|
18
|
+
addPendingTranslates,
|
|
19
|
+
removePendingTranslates,
|
|
18
20
|
getPendingMessages as readPendingMessages,
|
|
19
21
|
getQueuedTranslateGroups as readQueuedGroups,
|
|
20
22
|
getQueuedTranslateGroupsForStore as readQueuedGroupsForStore,
|
|
@@ -46,6 +48,8 @@ export default class Translate {
|
|
|
46
48
|
this.discoveredStores = new Set();
|
|
47
49
|
this.pendingStoreDiscoveries = new Map();
|
|
48
50
|
this.pendingMissingTranslateSchedules = new Set();
|
|
51
|
+
this.missingTranslateBatch = new Map();
|
|
52
|
+
this.missingTranslateBatchPromise = null;
|
|
49
53
|
this.pendingTranslateResolutionPromise = null;
|
|
50
54
|
this.pendingPersistRequests = new Map();
|
|
51
55
|
this.t = this.t.bind(this);
|
|
@@ -262,6 +266,35 @@ export default class Translate {
|
|
|
262
266
|
return;
|
|
263
267
|
}
|
|
264
268
|
|
|
269
|
+
// Every commit on the translate store re-creates the whole store object and
|
|
270
|
+
// re-renders all subscribers (useStore). Committing once per missing key made
|
|
271
|
+
// the render -> t() -> commit -> render cascade exceed React's nested update
|
|
272
|
+
// limit ("Maximum update depth exceeded"). Collect the keys missing in the
|
|
273
|
+
// same tick and write them with a single commit.
|
|
274
|
+
if (
|
|
275
|
+
typeof this.translateActions?.setPendingMessages === "function" &&
|
|
276
|
+
this.translateStore?.getters
|
|
277
|
+
) {
|
|
278
|
+
return this.enqueueMissingTranslateBatch(
|
|
279
|
+
store,
|
|
280
|
+
type,
|
|
281
|
+
key,
|
|
282
|
+
translate,
|
|
283
|
+
).then(() => {
|
|
284
|
+
if (typeof this.translateActions?.resolveQueuedMessages === "function") {
|
|
285
|
+
// The batch flush already coalesced the keys; no extra timer needed.
|
|
286
|
+
return this.scheduleQueuedTranslateResolution({immediate: true});
|
|
287
|
+
}
|
|
288
|
+
return this.saveMissingTranslate(
|
|
289
|
+
store,
|
|
290
|
+
type,
|
|
291
|
+
key,
|
|
292
|
+
translate,
|
|
293
|
+
mainCompanyId,
|
|
294
|
+
);
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
|
|
265
298
|
if (typeof this.translateActions?.queueMissingTranslate !== "function") {
|
|
266
299
|
return;
|
|
267
300
|
}
|
|
@@ -279,6 +312,10 @@ export default class Translate {
|
|
|
279
312
|
return this.scheduleQueuedTranslateResolution();
|
|
280
313
|
}
|
|
281
314
|
|
|
315
|
+
return this.saveMissingTranslate(store, type, key, translate, mainCompanyId);
|
|
316
|
+
}
|
|
317
|
+
|
|
318
|
+
saveMissingTranslate(store, type, key, translate, mainCompanyId) {
|
|
282
319
|
const requestToken = this.getPersistRequestToken(store, type, key);
|
|
283
320
|
if (this.pendingPersistRequests.has(requestToken)) {
|
|
284
321
|
return this.pendingPersistRequests.get(requestToken);
|
|
@@ -321,6 +358,64 @@ export default class Translate {
|
|
|
321
358
|
return request;
|
|
322
359
|
}
|
|
323
360
|
|
|
361
|
+
enqueueMissingTranslateBatch(store, type, key, translate) {
|
|
362
|
+
this.missingTranslateBatch.set(`${store}:${type}:${key}`, {
|
|
363
|
+
store,
|
|
364
|
+
type,
|
|
365
|
+
key,
|
|
366
|
+
translate,
|
|
367
|
+
});
|
|
368
|
+
|
|
369
|
+
if (!this.missingTranslateBatchPromise) {
|
|
370
|
+
this.missingTranslateBatchPromise = new Promise((resolve) => {
|
|
371
|
+
setTimeout(resolve, 0);
|
|
372
|
+
}).then(() => {
|
|
373
|
+
const entries = Array.from(this.missingTranslateBatch.values());
|
|
374
|
+
this.missingTranslateBatch.clear();
|
|
375
|
+
this.missingTranslateBatchPromise = null;
|
|
376
|
+
|
|
377
|
+
const current = this.getPendingMessages();
|
|
378
|
+
const next = addPendingTranslates(
|
|
379
|
+
current,
|
|
380
|
+
this.language,
|
|
381
|
+
this.mainCompany?.id,
|
|
382
|
+
entries,
|
|
383
|
+
);
|
|
384
|
+
|
|
385
|
+
if (next !== current) {
|
|
386
|
+
this.translateActions.setPendingMessages(next);
|
|
387
|
+
}
|
|
388
|
+
});
|
|
389
|
+
}
|
|
390
|
+
|
|
391
|
+
return this.missingTranslateBatchPromise;
|
|
392
|
+
}
|
|
393
|
+
|
|
394
|
+
removePendingTranslates(entries) {
|
|
395
|
+
if (!entries?.length) return;
|
|
396
|
+
|
|
397
|
+
if (
|
|
398
|
+
typeof this.translateActions?.setPendingMessages === "function" &&
|
|
399
|
+
this.translateStore?.getters
|
|
400
|
+
) {
|
|
401
|
+
const current = this.getPendingMessages();
|
|
402
|
+
const next = removePendingTranslates(
|
|
403
|
+
current,
|
|
404
|
+
this.language,
|
|
405
|
+
this.mainCompany?.id,
|
|
406
|
+
entries,
|
|
407
|
+
);
|
|
408
|
+
if (next !== current) {
|
|
409
|
+
this.translateActions.setPendingMessages(next);
|
|
410
|
+
}
|
|
411
|
+
return;
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
entries.forEach(({store, type, key}) => {
|
|
415
|
+
this.removePendingTranslate(store, type, key);
|
|
416
|
+
});
|
|
417
|
+
}
|
|
418
|
+
|
|
324
419
|
removePendingTranslate(store, type, key) {
|
|
325
420
|
if (typeof this.translateActions?.removePendingTranslate !== "function") {
|
|
326
421
|
return;
|
|
@@ -352,14 +447,18 @@ export default class Translate {
|
|
|
352
447
|
);
|
|
353
448
|
}
|
|
354
449
|
|
|
355
|
-
scheduleQueuedTranslateResolution() {
|
|
450
|
+
scheduleQueuedTranslateResolution({immediate = false} = {}) {
|
|
356
451
|
if (this.pendingTranslateResolutionPromise) {
|
|
357
452
|
return this.pendingTranslateResolutionPromise;
|
|
358
453
|
}
|
|
359
454
|
|
|
360
|
-
this.pendingTranslateResolutionPromise =
|
|
361
|
-
|
|
362
|
-
|
|
455
|
+
this.pendingTranslateResolutionPromise = (
|
|
456
|
+
immediate
|
|
457
|
+
? Promise.resolve()
|
|
458
|
+
: new Promise((resolve) => {
|
|
459
|
+
setTimeout(resolve, 0);
|
|
460
|
+
})
|
|
461
|
+
)
|
|
363
462
|
.then(() => this.resolveQueuedTranslations())
|
|
364
463
|
.catch(() => this.translates)
|
|
365
464
|
.finally(() => {
|
|
@@ -406,9 +505,9 @@ export default class Translate {
|
|
|
406
505
|
resolvedItems.forEach((item) => {
|
|
407
506
|
changed =
|
|
408
507
|
this.cacheTranslateRecord(item, companyId, this.language) || changed;
|
|
409
|
-
this.removePendingTranslate(item.store, item.type, item.key);
|
|
410
508
|
allResolvedItems.push(item);
|
|
411
509
|
});
|
|
510
|
+
this.removePendingTranslates(resolvedItems);
|
|
412
511
|
}
|
|
413
512
|
|
|
414
513
|
if (changed) {
|
|
@@ -30,11 +30,15 @@ export async function discoveryStoreTranslate(translate, store, options = {}) {
|
|
|
30
30
|
|
|
31
31
|
const requests = translate.getQueuedTranslateGroupsForStore(store);
|
|
32
32
|
if (requests.length > 0) {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
33
|
+
translate.removePendingTranslates(
|
|
34
|
+
requests.flatMap((request) =>
|
|
35
|
+
(request.keys || []).map((key) => ({
|
|
36
|
+
store: request.store,
|
|
37
|
+
type: request.type,
|
|
38
|
+
key,
|
|
39
|
+
})),
|
|
40
|
+
),
|
|
41
|
+
);
|
|
38
42
|
}
|
|
39
43
|
|
|
40
44
|
if (translate.hasCachedBootstrapStore(store)) {
|
|
@@ -87,10 +91,10 @@ export async function resolveStoreTranslates(translate, store, company, options
|
|
|
87
91
|
changed =
|
|
88
92
|
translate.cacheTranslateRecord(item, companyId, translate.language) ||
|
|
89
93
|
changed;
|
|
90
|
-
if (clearPending) {
|
|
91
|
-
translate.removePendingTranslate(item.store, item.type, item.key);
|
|
92
|
-
}
|
|
93
94
|
});
|
|
95
|
+
if (clearPending) {
|
|
96
|
+
translate.removePendingTranslates(resolvedItems);
|
|
97
|
+
}
|
|
94
98
|
|
|
95
99
|
return changed;
|
|
96
100
|
}
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {normalizeId} from "./translateNormalize.js";
|
|
1
|
+
import {normalizeId, normalizeLanguageCode} from "./translateNormalize.js";
|
|
2
2
|
|
|
3
3
|
export function getPendingMessages(translateStore) {
|
|
4
4
|
const messages = translateStore?.getters?.pendingMessages;
|
|
@@ -90,3 +90,123 @@ export function getQueuedTranslateGroupsForStore(
|
|
|
90
90
|
language,
|
|
91
91
|
).filter((group) => group.store === store);
|
|
92
92
|
}
|
|
93
|
+
|
|
94
|
+
const isPlainObject = (value) =>
|
|
95
|
+
value != null && typeof value === "object" && !Array.isArray(value);
|
|
96
|
+
|
|
97
|
+
// Adds many pending translates with a SINGLE immutable update. Returns the same
|
|
98
|
+
// reference when nothing changed so callers can skip the store commit.
|
|
99
|
+
export function addPendingTranslates(
|
|
100
|
+
pendingMessages,
|
|
101
|
+
language,
|
|
102
|
+
companyId,
|
|
103
|
+
entries,
|
|
104
|
+
) {
|
|
105
|
+
const normalizedLanguage = normalizeLanguageCode(language);
|
|
106
|
+
const normalizedCompanyId = normalizeId(companyId);
|
|
107
|
+
const base = isPlainObject(pendingMessages) ? pendingMessages : {};
|
|
108
|
+
|
|
109
|
+
if (!normalizedLanguage || !normalizedCompanyId || !entries?.length) {
|
|
110
|
+
return base;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
let changed = false;
|
|
114
|
+
const languageBucket = {...(base[normalizedLanguage] || {})};
|
|
115
|
+
const companies = {...(languageBucket.companies || {})};
|
|
116
|
+
const companyBucket = {...(companies[normalizedCompanyId] || {})};
|
|
117
|
+
|
|
118
|
+
entries.forEach(({store, type, key, translate}) => {
|
|
119
|
+
const normalizedStore = String(store || "").trim();
|
|
120
|
+
const normalizedType = String(type || "").trim();
|
|
121
|
+
const normalizedKey = String(key || "").trim();
|
|
122
|
+
const value = String(translate || "").trim();
|
|
123
|
+
if (!normalizedStore || !normalizedType || !normalizedKey) return;
|
|
124
|
+
|
|
125
|
+
const storeBucket = companyBucket[normalizedStore] || {};
|
|
126
|
+
const typeBucket = storeBucket[normalizedType] || {};
|
|
127
|
+
if (typeBucket[normalizedKey] === value) return;
|
|
128
|
+
|
|
129
|
+
companyBucket[normalizedStore] = {
|
|
130
|
+
...storeBucket,
|
|
131
|
+
[normalizedType]: {...typeBucket, [normalizedKey]: value},
|
|
132
|
+
};
|
|
133
|
+
changed = true;
|
|
134
|
+
});
|
|
135
|
+
|
|
136
|
+
if (!changed) return base;
|
|
137
|
+
|
|
138
|
+
companies[normalizedCompanyId] = companyBucket;
|
|
139
|
+
languageBucket.companies = companies;
|
|
140
|
+
return {...base, [normalizedLanguage]: languageBucket};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
// Removes many pending translates with a SINGLE immutable update.
|
|
144
|
+
export function removePendingTranslates(
|
|
145
|
+
pendingMessages,
|
|
146
|
+
language,
|
|
147
|
+
companyId,
|
|
148
|
+
entries,
|
|
149
|
+
) {
|
|
150
|
+
const normalizedLanguage = normalizeLanguageCode(language);
|
|
151
|
+
const normalizedCompanyId = normalizeId(companyId);
|
|
152
|
+
const base = isPlainObject(pendingMessages) ? pendingMessages : {};
|
|
153
|
+
const currentCompany =
|
|
154
|
+
base[normalizedLanguage]?.companies?.[normalizedCompanyId];
|
|
155
|
+
|
|
156
|
+
if (!currentCompany || !entries?.length) return base;
|
|
157
|
+
|
|
158
|
+
let changed = false;
|
|
159
|
+
const companyBucket = {...currentCompany};
|
|
160
|
+
|
|
161
|
+
entries.forEach(({store, type, key}) => {
|
|
162
|
+
const normalizedStore = String(store || "").trim();
|
|
163
|
+
const normalizedType = String(type || "").trim();
|
|
164
|
+
const normalizedKey = String(key || "").trim();
|
|
165
|
+
const typeBucket = companyBucket[normalizedStore]?.[normalizedType];
|
|
166
|
+
if (
|
|
167
|
+
!typeBucket ||
|
|
168
|
+
!Object.prototype.hasOwnProperty.call(typeBucket, normalizedKey)
|
|
169
|
+
) {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
const nextType = {...typeBucket};
|
|
174
|
+
delete nextType[normalizedKey];
|
|
175
|
+
const nextStore = {...companyBucket[normalizedStore]};
|
|
176
|
+
if (Object.keys(nextType).length > 0) {
|
|
177
|
+
nextStore[normalizedType] = nextType;
|
|
178
|
+
} else {
|
|
179
|
+
delete nextStore[normalizedType];
|
|
180
|
+
}
|
|
181
|
+
if (Object.keys(nextStore).length > 0) {
|
|
182
|
+
companyBucket[normalizedStore] = nextStore;
|
|
183
|
+
} else {
|
|
184
|
+
delete companyBucket[normalizedStore];
|
|
185
|
+
}
|
|
186
|
+
changed = true;
|
|
187
|
+
});
|
|
188
|
+
|
|
189
|
+
if (!changed) return base;
|
|
190
|
+
|
|
191
|
+
const companies = {...base[normalizedLanguage].companies};
|
|
192
|
+
if (Object.keys(companyBucket).length > 0) {
|
|
193
|
+
companies[normalizedCompanyId] = companyBucket;
|
|
194
|
+
} else {
|
|
195
|
+
delete companies[normalizedCompanyId];
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const languageBucket = {...base[normalizedLanguage]};
|
|
199
|
+
if (Object.keys(companies).length > 0) {
|
|
200
|
+
languageBucket.companies = companies;
|
|
201
|
+
} else {
|
|
202
|
+
delete languageBucket.companies;
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
const next = {...base};
|
|
206
|
+
if (Object.keys(languageBucket).length > 0) {
|
|
207
|
+
next[normalizedLanguage] = languageBucket;
|
|
208
|
+
} else {
|
|
209
|
+
delete next[normalizedLanguage];
|
|
210
|
+
}
|
|
211
|
+
return next;
|
|
212
|
+
}
|