@@ -13,6 +13,7 @@ import { DeviceTable } from "../components/device/DeviceTable";
1313import { DeviceMobileView } from "../components/device/DeviceMobileView" ;
1414import { DeleteDeviceModal } from "../components/device/DeleteDeviceModal" ;
1515import { EditNoteModal } from "../components/device/EditNoteModal" ;
16+ import { SearchInput } from "../components/device/SearchInput" ;
1617
1718export class DeviceList extends Component < { } , DeviceListState > {
1819 removalDevice : StateDevice ;
@@ -40,6 +41,8 @@ export class DeviceList extends Component<{}, DeviceListState> {
4041 editChargerIdx : 0 ,
4142 sortColumn : "none" ,
4243 sortSequence : "asc" ,
44+ searchTerm : "" ,
45+ filteredDevices : [ ] ,
4346 } ;
4447
4548 this . updateChargers ( ) ;
@@ -223,6 +226,24 @@ export class DeviceList extends Component<{}, DeviceListState> {
223226 } ) ;
224227 }
225228
229+ filterDevices ( devices : StateDevice [ ] , searchTerm : string ) : StateDevice [ ] {
230+ if ( ! searchTerm . trim ( ) ) {
231+ return devices ;
232+ }
233+
234+ const lowerSearchTerm = searchTerm . toLowerCase ( ) . trim ( ) ;
235+ return devices . filter ( device => {
236+ return (
237+ device . name . toLowerCase ( ) . includes ( lowerSearchTerm ) ||
238+ device . id . toLowerCase ( ) . includes ( lowerSearchTerm ) ||
239+ device . uid . toString ( ) . includes ( lowerSearchTerm ) ||
240+ device . status . toLowerCase ( ) . includes ( lowerSearchTerm ) ||
241+ device . note . toLowerCase ( ) . includes ( lowerSearchTerm ) ||
242+ device . firmware_version . toLowerCase ( ) . includes ( lowerSearchTerm )
243+ ) ;
244+ } ) ;
245+ }
246+
226247 setSortedDevices ( devices : StateDevice [ ] ) {
227248 devices . sort ( ( a , b ) => {
228249 let sortColumn = this . state . sortColumn ;
@@ -256,7 +277,9 @@ export class DeviceList extends Component<{}, DeviceListState> {
256277 return ret * - 1 ;
257278
258279 } ) ;
259- this . setState ( { devices } ) ;
280+
281+ const filteredDevices = this . filterDevices ( devices , this . state . searchTerm ) ;
282+ this . setState ( { devices, filteredDevices } ) ;
260283 }
261284
262285 handleDelete = ( device : StateDevice ) => {
@@ -311,17 +334,22 @@ export class DeviceList extends Component<{}, DeviceListState> {
311334 } ) ;
312335 }
313336
337+ handleSearchChange = ( searchTerm : string ) => {
338+ const filteredDevices = this . filterDevices ( this . state . devices , searchTerm ) ;
339+ this . setState ( { searchTerm, filteredDevices } ) ;
340+ }
341+
314342 render ( ) {
315343 const { t } = useTranslation ( "" , { useSuspense : false , keyPrefix : "chargers" } ) ;
316344 const { route } = useLocation ( ) ;
317- const devices = this . state . devices ;
345+ const devices = this . state . filteredDevices . length > 0 || this . state . searchTerm ? this . state . filteredDevices : this . state . devices ;
318346
319347 const handleConnect = async ( device : StateDevice ) => {
320348 await this . connect_to_charger ( device , route ) ;
321349 } ;
322350
323- // Show empty state message if no devices
324- if ( devices . length === 0 ) {
351+ // Show empty state message if no devices at all
352+ if ( this . state . devices . length === 0 ) {
325353 return (
326354 < Container fluid className = "text-center mt-5" >
327355 < div className = "text-muted" >
@@ -348,33 +376,50 @@ export class DeviceList extends Component<{}, DeviceListState> {
348376 onCancel = { this . handleEditNoteCancel }
349377 />
350378
351- < DeviceTable
352- devices = { devices }
353- sortColumn = { this . state . sortColumn }
354- sortSequence = { this . state . sortSequence }
355- onSort = { ( column ) => this . setSort ( column ) }
356- onConnect = { handleConnect }
357- onDelete = { this . handleDelete }
358- onEditNote = { this . handleEditNote }
359- connectionPossible = { ( device ) => this . connection_possible ( device ) }
360- formatLastStateChange = { ( t , timestamp ) => this . formatLastStateChange ( t , timestamp ) }
361- />
379+ < Container fluid className = "mb-3" >
380+ < SearchInput
381+ searchTerm = { this . state . searchTerm }
382+ onSearchChange = { this . handleSearchChange }
383+ />
384+ </ Container >
362385
363- < DeviceMobileView
364- devices = { devices }
365- sortColumn = { this . state . sortColumn }
366- sortSequence = { this . state . sortSequence }
367- onMobileSort = { ( column ) => this . setMobileSort ( column ) }
368- onSortSequenceChange = { ( sequence ) => this . setState ( { sortSequence : sequence } , ( ) => {
369- // Re-sort the devices after state update
370- this . setSortedDevices ( [ ...this . state . devices ] ) ;
371- } ) }
372- onConnect = { handleConnect }
373- onDelete = { this . handleDelete }
374- onEditNote = { this . handleEditNote }
375- connectionPossible = { ( device ) => this . connection_possible ( device ) }
376- formatLastStateChange = { ( t , timestamp ) => this . formatLastStateChange ( t , timestamp ) }
377- />
386+ { devices . length === 0 && this . state . searchTerm ? (
387+ < Container fluid className = "text-center mt-5" >
388+ < div className = "text-muted" >
389+ < h5 > { t ( "no_devices_found" ) } </ h5 >
390+ </ div >
391+ </ Container >
392+ ) : (
393+ < >
394+ < DeviceTable
395+ devices = { devices }
396+ sortColumn = { this . state . sortColumn }
397+ sortSequence = { this . state . sortSequence }
398+ onSort = { ( column ) => this . setSort ( column ) }
399+ onConnect = { handleConnect }
400+ onDelete = { this . handleDelete }
401+ onEditNote = { this . handleEditNote }
402+ connectionPossible = { ( device ) => this . connection_possible ( device ) }
403+ formatLastStateChange = { ( t , timestamp ) => this . formatLastStateChange ( t , timestamp ) }
404+ />
405+
406+ < DeviceMobileView
407+ devices = { devices }
408+ sortColumn = { this . state . sortColumn }
409+ sortSequence = { this . state . sortSequence }
410+ onMobileSort = { ( column ) => this . setMobileSort ( column ) }
411+ onSortSequenceChange = { ( sequence ) => this . setState ( { sortSequence : sequence } , ( ) => {
412+ // Re-sort the devices after state update
413+ this . setSortedDevices ( [ ...this . state . devices ] ) ;
414+ } ) }
415+ onConnect = { handleConnect }
416+ onDelete = { this . handleDelete }
417+ onEditNote = { this . handleEditNote }
418+ connectionPossible = { ( device ) => this . connection_possible ( device ) }
419+ formatLastStateChange = { ( t , timestamp ) => this . formatLastStateChange ( t , timestamp ) }
420+ />
421+ </ >
422+ ) }
378423 </ >
379424 ) ;
380425 }
0 commit comments