device.jsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. import React, { useEffect, useRef, useState } 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 HintView from '../public/hint-view/hint-view.jsx'
  6. import Alert from '../public/alert-view/alert-view.jsx'
  7. import ComfirmView from '../public/comfirm-view/comfirm-view.jsx'
  8. import { DeviceClass } from './device.js'
  9. import hintView from '../public/hint-view/hint-view.js'
  10. import alertView from '../public/alert-view/alert-view.js'
  11. import comfirmView from '../public/comfirm-view/comfirm-view.js'
  12. import { createHandleClose } from '../home.js'
  13. function Device({ show }) {
  14. const [deviceList, setDeviceList] = useState([])
  15. const [showHintView, setShowHintView] = useState(false)
  16. const [showAlert, setShowAlert] = useState(false)
  17. const [showComfirmView, setShowComfirmView] = useState(false)
  18. const deviceClass = useRef(null)
  19. if (!show) {
  20. return null
  21. }
  22. // 初始化 deviceClass,确保在渲染时就有值
  23. if (!deviceClass.current) {
  24. deviceClass.current = new DeviceClass(setDeviceList)
  25. }
  26. // 设置 API 对象的回调函数并初始化 deviceClass
  27. useEffect(() => {
  28. hintView.setShowCallback(setShowHintView)
  29. alertView.setShowCallback(setShowAlert)
  30. comfirmView.setShowCallback(setShowComfirmView)
  31. // 回调设置完成后,初始化 deviceClass
  32. if (deviceClass.current && !deviceClass.current._initialized) {
  33. deviceClass.current.init()
  34. }
  35. }, [])
  36. // 当 setDeviceList 更新时,同步更新 deviceClass 的引用
  37. useEffect(() => {
  38. if (deviceClass.current) {
  39. deviceClass.current.setDeviceList = setDeviceList
  40. }
  41. }, [setDeviceList])
  42. return (
  43. <>
  44. <div className="device-container">
  45. {/* 更新设备列表 */}
  46. <div className="device-update">
  47. <div className="device-update-title">设备列表</div>
  48. <div className="device-update-btn">
  49. <UpdateBtn
  50. onClick={(e, self) => deviceClass.current?.handleRefresh(e, self)}
  51. title="Refresh device list"
  52. />
  53. </div>
  54. </div>
  55. {/* 设备列表 */}
  56. <div className="device-list">
  57. {deviceList.map((device, index) => (
  58. <ConnectItem
  59. key={index}
  60. ipAddress={deviceList[index]}
  61. onRemove={(ip) => deviceClass.current?.removeDevice(ip)}
  62. />
  63. ))}
  64. </div>
  65. {/* 添加设备 */}
  66. <div className="device-add">
  67. <div className="ip-input">
  68. <input type="text" placeholder="请输入设备IP" defaultValue="192.168." />
  69. </div>
  70. <div className="add-btn" onClick={() => deviceClass.current?.handleAdd()}>
  71. <svg viewBox="0 0 24 24" fill="none" xmlns="http://www.w3.org/2000/svg">
  72. <path d="M12 5V19M5 12H19" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round"/>
  73. </svg>
  74. </div>
  75. </div>
  76. </div>
  77. {showHintView && <HintView show={showHintView} onClose={createHandleClose(setShowHintView)} title={hintView._title} content={hintView._content} />}
  78. {showAlert && <Alert show={showAlert} onClose={createHandleClose(setShowAlert)} title={alertView._title} content={alertView._content} />}
  79. {showComfirmView && <ComfirmView show={showComfirmView} onClose={() => {
  80. comfirmView.hide()
  81. if (comfirmView._onCancel) {
  82. comfirmView._onCancel()
  83. }
  84. }} onConfirm={() => {
  85. console.log('ComfirmView onConfirm clicked, _onConfirm:', comfirmView._onConfirm)
  86. if (comfirmView._onConfirm) {
  87. comfirmView._onConfirm()
  88. } else {
  89. console.warn('comfirmView._onConfirm is null')
  90. }
  91. }} title={comfirmView._title} content={comfirmView._content} />}
  92. </>
  93. )
  94. }
  95. export default Device