@@ -23,7 +23,7 @@ import { Table, Header, HeaderRow, HeaderCell, Body, Row, Cell } from '@table-li
23
23
import { useTheme } from '@table-library/react-table-library/theme' ;
24
24
import { useRequest } from 'alova' ;
25
25
import { useState , useEffect , useCallback } from 'react' ;
26
- import { unstable_useBlocker as useBlocker } from 'react-router-dom' ;
26
+ import { unstable_useBlocker as useBlocker , useLocation } from 'react-router-dom' ;
27
27
import { toast } from 'react-toastify' ;
28
28
29
29
import EntityMaskToggle from './EntityMaskToggle' ;
@@ -52,19 +52,23 @@ const SettingsCustomization: FC = () => {
52
52
const [ restarting , setRestarting ] = useState < boolean > ( false ) ;
53
53
const [ restartNeeded , setRestartNeeded ] = useState < boolean > ( false ) ;
54
54
const [ deviceEntities , setDeviceEntities ] = useState < DeviceEntity [ ] > ( [ ] ) ;
55
- const [ selectedDevice , setSelectedDevice ] = useState < number > ( - 1 ) ;
56
55
const [ confirmReset , setConfirmReset ] = useState < boolean > ( false ) ;
57
56
const [ selectedFilters , setSelectedFilters ] = useState < number > ( 0 ) ;
58
57
const [ search , setSearch ] = useState ( '' ) ;
59
58
const [ selectedDeviceEntity , setSelectedDeviceEntity ] = useState < DeviceEntity > ( ) ;
60
59
const [ dialogOpen , setDialogOpen ] = useState < boolean > ( false ) ;
61
60
61
+ // fetch devices first
62
+ const { data : devices } = useRequest ( EMSESP . readDevices ) ;
63
+
64
+ // const { state } = useLocation();
65
+ const [ selectedDevice , setSelectedDevice ] = useState < number > ( useLocation ( ) . state || - 1 ) ;
66
+ const [ selectedDeviceName , setSelectedDeviceName ] = useState < string > ( '' ) ;
67
+
62
68
const { send : resetCustomizations } = useRequest ( EMSESP . resetCustomizations ( ) , {
63
69
immediate : false
64
70
} ) ;
65
71
66
- const { data : devices } = useRequest ( EMSESP . readDevices ) ;
67
-
68
72
const { send : writeCustomizationEntities } = useRequest ( ( data ) => EMSESP . writeCustomizationEntities ( data ) , {
69
73
immediate : false
70
74
} ) ;
@@ -176,6 +180,22 @@ const SettingsCustomization: FC = () => {
176
180
}
177
181
} , [ deviceEntities ] ) ;
178
182
183
+ useEffect ( ( ) => {
184
+ if ( devices && selectedDevice !== - 1 ) {
185
+ void readDeviceEntities ( selectedDevice ) ;
186
+ const id = devices . devices . findIndex ( ( d ) => d . i === selectedDevice ) ;
187
+ if ( id === - 1 ) {
188
+ setSelectedDevice ( - 1 ) ;
189
+ setSelectedDeviceName ( '' ) ;
190
+ } else {
191
+ setSelectedDeviceName ( devices . devices [ id ] . tn || '' ) ;
192
+ setNumChanges ( 0 ) ;
193
+ setRestartNeeded ( false ) ;
194
+ }
195
+ }
196
+ // eslint-disable-next-line react-hooks/exhaustive-deps
197
+ } , [ devices , selectedDevice ] ) ;
198
+
179
199
const restart = async ( ) => {
180
200
await restartCommand ( ) . catch ( ( error ) => {
181
201
toast . error ( error . message ) ;
@@ -246,16 +266,6 @@ const SettingsCustomization: FC = () => {
246
266
) ;
247
267
} ;
248
268
249
- const changeSelectedDevice = ( event : React . ChangeEvent < HTMLInputElement > ) => {
250
- if ( devices ) {
251
- const selected_device = parseInt ( event . target . value , 10 ) ;
252
- setSelectedDevice ( selected_device ) ;
253
- setNumChanges ( 0 ) ;
254
- void readDeviceEntities ( devices ?. devices [ selected_device ] . i ) ;
255
- setRestartNeeded ( false ) ;
256
- }
257
- } ;
258
-
259
269
const resetCustomization = async ( ) => {
260
270
try {
261
271
await resetCustomizations ( ) ;
@@ -314,30 +324,21 @@ const SettingsCustomization: FC = () => {
314
324
return ;
315
325
}
316
326
317
- await writeCustomizationEntities ( { id : devices ?. devices [ selectedDevice ] . i , entity_ids : masked_entities } ) . catch (
318
- ( error ) => {
319
- if ( error . message === 'Reboot required' ) {
320
- setRestartNeeded ( true ) ;
321
- } else {
322
- toast . error ( error . message ) ;
323
- }
327
+ await writeCustomizationEntities ( { id : selectedDevice , entity_ids : masked_entities } ) . catch ( ( error ) => {
328
+ if ( error . message === 'Reboot required' ) {
329
+ setRestartNeeded ( true ) ;
330
+ } else {
331
+ toast . error ( error . message ) ;
324
332
}
325
- ) ;
333
+ } ) ;
326
334
setOriginalSettings ( deviceEntities ) ;
327
335
}
328
336
} ;
329
337
330
338
const renderDeviceList = ( ) => (
331
339
< >
332
- < Box mb = { 2 } color = "warning.main" >
340
+ < Box mb = { 1 } color = "warning.main" >
333
341
< Typography variant = "body2" > { LL . CUSTOMIZATIONS_HELP_1 ( ) } .</ Typography >
334
- < Typography variant = "body2" mt = { 1 } >
335
- < OptionIcon type = "favorite" isSet = { true } /> ={ LL . CUSTOMIZATIONS_HELP_2 ( ) }
336
- < OptionIcon type = "readonly" isSet = { true } /> ={ LL . CUSTOMIZATIONS_HELP_3 ( ) }
337
- < OptionIcon type = "api_mqtt_exclude" isSet = { true } /> ={ LL . CUSTOMIZATIONS_HELP_4 ( ) }
338
- < OptionIcon type = "web_exclude" isSet = { true } /> ={ LL . CUSTOMIZATIONS_HELP_5 ( ) }
339
- < OptionIcon type = "deleted" isSet = { true } /> ={ LL . CUSTOMIZATIONS_HELP_6 ( ) }
340
- </ Typography >
341
342
</ Box >
342
343
< TextField
343
344
name = "device"
@@ -346,15 +347,15 @@ const SettingsCustomization: FC = () => {
346
347
fullWidth
347
348
value = { selectedDevice }
348
349
disabled = { numChanges !== 0 }
349
- onChange = { changeSelectedDevice }
350
+ onChange = { ( e ) => setSelectedDevice ( parseInt ( e . target . value ) ) }
350
351
margin = "normal"
351
352
select
352
353
>
353
354
< MenuItem disabled key = { - 1 } value = { - 1 } >
354
355
{ LL . SELECT_DEVICE ( ) } ...
355
356
</ MenuItem >
356
- { devices . devices . map ( ( device : DeviceShort , index ) => (
357
- < MenuItem key = { index } value = { index } >
357
+ { devices . devices . map ( ( device : DeviceShort ) => (
358
+ < MenuItem key = { device . i } value = { device . i } >
358
359
{ device . s }
359
360
</ MenuItem >
360
361
) ) }
@@ -363,14 +364,19 @@ const SettingsCustomization: FC = () => {
363
364
) ;
364
365
365
366
const renderDeviceData = ( ) => {
366
- if ( deviceEntities . length === 0 ) {
367
- return ;
368
- }
369
-
370
367
const shown_data = deviceEntities . filter ( ( de ) => filter_entity ( de ) ) ;
371
368
372
369
return (
373
370
< >
371
+ < Box color = "warning.main" >
372
+ < Typography variant = "body2" mt = { 1 } >
373
+ < OptionIcon type = "favorite" isSet = { true } /> ={ LL . CUSTOMIZATIONS_HELP_2 ( ) }
374
+ < OptionIcon type = "readonly" isSet = { true } /> ={ LL . CUSTOMIZATIONS_HELP_3 ( ) }
375
+ < OptionIcon type = "api_mqtt_exclude" isSet = { true } /> ={ LL . CUSTOMIZATIONS_HELP_4 ( ) }
376
+ < OptionIcon type = "web_exclude" isSet = { true } /> ={ LL . CUSTOMIZATIONS_HELP_5 ( ) }
377
+ < OptionIcon type = "deleted" isSet = { true } /> ={ LL . CUSTOMIZATIONS_HELP_6 ( ) }
378
+ </ Typography >
379
+ </ Box >
374
380
< Grid container mb = { 1 } mt = { 0 } spacing = { 1 } direction = "row" justifyContent = "flex-start" alignItems = "center" >
375
381
< Grid item xs = { 2 } >
376
382
< TextField
@@ -443,7 +449,7 @@ const SettingsCustomization: FC = () => {
443
449
</ Grid >
444
450
< Grid item >
445
451
< Typography variant = "subtitle2" color = "primary" >
446
- { LL . SHOWING ( ) } { shown_data . length } /{ deviceEntities . length }
452
+ { LL . SHOWING ( ) } { shown_data . length } /{ deviceEntities . length } { LL . ENTITIES ( deviceEntities . length ) }
447
453
</ Typography >
448
454
</ Grid >
449
455
</ Grid >
@@ -467,7 +473,7 @@ const SettingsCustomization: FC = () => {
467
473
</ Cell >
468
474
< Cell >
469
475
{ formatName ( de , false ) } (
470
- < Link target = "_blank" href = { APIURL + devices ?. devices [ selectedDevice ] . tn + '/' + de . id } >
476
+ < Link target = "_blank" href = { APIURL + selectedDeviceName + '/' + de . id } >
471
477
{ de . id }
472
478
</ Link >
473
479
)
@@ -506,7 +512,7 @@ const SettingsCustomization: FC = () => {
506
512
{ LL . DEVICE_ENTITIES ( ) }
507
513
</ Typography >
508
514
{ devices && renderDeviceList ( ) }
509
- { renderDeviceData ( ) }
515
+ { deviceEntities && renderDeviceData ( ) }
510
516
{ restartNeeded && (
511
517
< MessageBox my = { 2 } level = "warning" message = { LL . RESTART_TEXT ( ) } >
512
518
< Button startIcon = { < PowerSettingsNewIcon /> } variant = "contained" color = "error" onClick = { restart } >
@@ -523,7 +529,7 @@ const SettingsCustomization: FC = () => {
523
529
startIcon = { < CancelIcon /> }
524
530
variant = "outlined"
525
531
color = "secondary"
526
- onClick = { ( ) => devices && readDeviceEntities ( devices . devices [ selectedDevice ] . i ) }
532
+ onClick = { ( ) => devices && readDeviceEntities ( selectedDevice ) }
527
533
>
528
534
{ LL . CANCEL ( ) }
529
535
</ Button >
0 commit comments