Skip to content

Commit 6b07fea

Browse files
committed
Fixed race condition
1 parent c4f9678 commit 6b07fea

1 file changed

Lines changed: 21 additions & 11 deletions

File tree

examples/dashboard/src/DeviceActionsForm.tsx

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,20 @@ function findClosest(arr: number[], target: number): number {
1515
);
1616
}
1717

18+
function getDefaultActionValue(action: DeviceTypeAction): number {
19+
if (action.d !== undefined) {
20+
return action.d;
21+
}
22+
if (action.map) {
23+
const firstMapKey = Object.keys(action.map).sort((a, b) => parseFloat(a) - parseFloat(b))[0];
24+
return firstMapKey !== undefined ? parseFloat(firstMapKey) : 0;
25+
}
26+
if (action.r && action.r.length > 1) {
27+
return (action.r[1] + action.r[0]) / 2;
28+
}
29+
return 0;
30+
}
31+
1832
type DeviceActionsTableProps = {
1933
deviceKey: string;
2034
};
@@ -51,13 +65,7 @@ const DeviceActionsForm: React.FC<DeviceActionsTableProps> = ({ deviceKey }: Dev
5165
setHasConfRate(!!confRateAction);
5266
// Initialize input values with defaults
5367
const initialValues: InputValues = actions.reduce((acc, action) => {
54-
acc[action.n] =
55-
action.d ??
56-
(action.r
57-
? action.r.length > 1
58-
? (action.r[1] + action.r[0]) / 2
59-
: 0
60-
: 0);
68+
acc[action.n] = getDefaultActionValue(action);
6169
return acc;
6270
}, {} as InputValues);
6371

@@ -183,12 +191,13 @@ const DeviceActionsForm: React.FC<DeviceActionsTableProps> = ({ deviceKey }: Dev
183191
const mapKeys = Object.keys(action.map).sort((a, b) => parseFloat(a) - parseFloat(b));
184192
// Use "Rate Hz" label for _conf.rate actions
185193
const actionLabel = action.n === '_conf.rate' ? 'Rate Hz' : (action.desc ?? action.n);
194+
const actionValue = inputValues[action.n] ?? getDefaultActionValue(action);
186195
return (
187196
<tr key={action.n}>
188197
<td>{actionLabel}</td>
189198
<td>
190199
<select
191-
value={inputValues[action.n]}
200+
value={actionValue}
192201
onChange={(e) =>
193202
handleInputChange(
194203
action.n,
@@ -208,7 +217,7 @@ const DeviceActionsForm: React.FC<DeviceActionsTableProps> = ({ deviceKey }: Dev
208217
onClick={() =>
209218
handleSendAction(
210219
action,
211-
inputValues[action.n]
220+
actionValue
212221
)
213222
}
214223
>
@@ -218,6 +227,7 @@ const DeviceActionsForm: React.FC<DeviceActionsTableProps> = ({ deviceKey }: Dev
218227
</tr>
219228
);
220229
} else {
230+
const actionValue = inputValues[action.n] ?? getDefaultActionValue(action);
221231
return (
222232
<tr key={action.n}>
223233
<td>{action.n}</td>
@@ -227,7 +237,7 @@ const DeviceActionsForm: React.FC<DeviceActionsTableProps> = ({ deviceKey }: Dev
227237
type="number"
228238
min={action.r?.[0] ?? 0}
229239
max={action.r?.[1] ?? 100}
230-
value={inputValues[action.n]}
240+
value={actionValue}
231241
onChange={(e) =>
232242
handleInputChange(
233243
action.n,
@@ -242,7 +252,7 @@ const DeviceActionsForm: React.FC<DeviceActionsTableProps> = ({ deviceKey }: Dev
242252
onClick={() =>
243253
handleSendAction(
244254
action,
245-
inputValues[action.n]
255+
actionValue
246256
)
247257
}
248258
>

0 commit comments

Comments
 (0)