device.jsx 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. import React, { useState, useRef, useEffect } from 'react'
  2. import './device.scss'
  3. import UpdateBtn from './update-btn/update-btn.jsx'
  4. import ConnectItem from './connect-item/connect-item.jsx'
  5. import { DeviceClass } from './device.js'
  6. import { alertView, hintView, comfirmView } from '../home.jsx'
  7. function Device({ show }) {
  8. const [deviceList, setDeviceList] = useState([])
  9. const deviceClass = useRef(null)
  10. const [inputValue, setInputValue] = useState('192.168.')
  11. const [selectedDevice, setSelectedDevice] = useState(null)
  12. if (!show) {
  13. return null
  14. }
  15. useEffect(() => {
  16. if (!deviceClass.current) {
  17. deviceClass.current = new DeviceClass()
  18. deviceClass.current.init(setDeviceList, inputValue, setSelectedDevice, setInputValue)
  19. }
  20. }, [])
  21. useEffect(() => {
  22. if (deviceClass.current) deviceClass.current.inputValue = inputValue
  23. }, [inputValue])
  24. useEffect(() => {
  25. if (deviceClass.current) deviceClass.current.currentDeviceList = deviceList
  26. }, [deviceList])
  27. return (
  28. <>
  29. <div className="device-container">
  30. {/* 更新设备列表 */}
  31. <div className="device-update">
  32. <div className="device-update-title">设备列表</div>
  33. <div className="device-update-btn">
  34. <UpdateBtn
  35. onClick={(e, self) => deviceClass.current?.onRefresh(e, self)}
  36. title="Refresh device list"
  37. />
  38. </div>
  39. </div>
  40. {/* 设备列表 */}
  41. <div className="device-list">
  42. {deviceList.map((device, index) => (
  43. <ConnectItem
  44. key={index}
  45. ipAddress={deviceList[index]}
  46. selected={selectedDevice === deviceList[index]}
  47. onSelect={(ip) => setSelectedDevice(ip)}
  48. onRemove={(ip) => deviceClass.current?.onRemoveDevice(ip)}
  49. />
  50. ))}
  51. </div>
  52. {/* 添加设备 */}
  53. <div className="device-add">
  54. <div className="ip-input">
  55. <input
  56. type="text"
  57. placeholder="请输入设备IP"
  58. value={inputValue}
  59. onChange={(e) => setInputValue(e.target.value)}
  60. onKeyDown={(e) => { if (e.key === 'Enter') { deviceClass.current?.onAddDevice() } }}
  61. />
  62. </div>
  63. <div className="add-btn" onClick={() => deviceClass.current?.onAddDevice()}>
  64. <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
  65. <path d="M12 5V19M5 12H19" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
  66. </svg>
  67. </div>
  68. </div>
  69. </div>
  70. </>
  71. )
  72. }
  73. export default Device