|
| 1 | +import { ChangeEvent, useState } from 'react'; |
| 2 | +import { Badge } from 'react-bootstrap'; |
| 3 | +import Dropdown from 'react-bootstrap/Dropdown'; |
| 4 | +import Form from 'react-bootstrap/Form'; |
| 5 | +import { IconDarkChevronDown } from '../../shared/icons'; |
| 6 | +import { IdLabel } from '../../shared/types/IdLabel'; |
| 7 | +import { |
| 8 | + selectCheckedDevices, |
| 9 | + selectDeviceLevels, |
| 10 | +} from '../../store/debugConsole/debugConsoleSelectors'; |
| 11 | +import { debugConsoleActions } from '../../store/debugConsole/debugConsoleSlice'; |
| 12 | +import { useAppDispatch, useAppSelector } from '../../store/hooks'; |
| 13 | +import { logLevelOpts } from './debugConsts'; |
| 14 | + |
| 15 | +interface DeviceFilterDropdownProps { |
| 16 | + items: IdLabel[]; |
| 17 | +} |
| 18 | + |
| 19 | +export const DeviceFilterDropdown = ({ items }: DeviceFilterDropdownProps) => { |
| 20 | + const dispatch = useAppDispatch(); |
| 21 | + const checkedDevices = useAppSelector(selectCheckedDevices); |
| 22 | + const deviceLevels = useAppSelector(selectDeviceLevels); |
| 23 | + const [openLevelDropdowns, setOpenLevelDropdowns] = useState<Record<string, boolean>>({}); |
| 24 | + |
| 25 | + function handleCheckChange( |
| 26 | + event: ChangeEvent<HTMLInputElement>, |
| 27 | + deviceId: string |
| 28 | + ) { |
| 29 | + if (event.target.checked) { |
| 30 | + dispatch(debugConsoleActions.checkDevice(deviceId)); |
| 31 | + } else { |
| 32 | + dispatch(debugConsoleActions.uncheckDevice(deviceId)); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + function handleLevelChange(deviceId: string, level: string) { |
| 37 | + dispatch(debugConsoleActions.setDeviceLevel({ deviceId, level })); |
| 38 | + setOpenLevelDropdowns((prev) => ({ ...prev, [deviceId]: false })); |
| 39 | + } |
| 40 | + |
| 41 | + return ( |
| 42 | + <Dropdown className="d-inline-block"> |
| 43 | + <Dropdown.Toggle variant="outline" className="py-1" id="device-filter-dropdown"> |
| 44 | + Devices |
| 45 | + {checkedDevices.length > 0 && ( |
| 46 | + <Badge pill bg="primary" className="ms-1"> |
| 47 | + {checkedDevices.length} |
| 48 | + </Badge> |
| 49 | + )} |
| 50 | + <IconDarkChevronDown className="ms-1" /> |
| 51 | + </Dropdown.Toggle> |
| 52 | + |
| 53 | + <Dropdown.Menu className="scroll-dropdown shadow"> |
| 54 | + {items.map((item) => { |
| 55 | + const stringId = item.id.toString(); |
| 56 | + const isChecked = checkedDevices.includes(stringId); |
| 57 | + const currentLevel = deviceLevels[stringId] ?? 'Information'; |
| 58 | + |
| 59 | + return ( |
| 60 | + <Dropdown.Item |
| 61 | + key={stringId} |
| 62 | + as="div" |
| 63 | + className="d-flex align-items-center gap-2 px-2" |
| 64 | + onClick={(e) => e.stopPropagation()} |
| 65 | + > |
| 66 | + <Form.Check |
| 67 | + type="checkbox" |
| 68 | + id={`device-check-${stringId}`} |
| 69 | + label={item.label} |
| 70 | + checked={isChecked} |
| 71 | + onChange={(e) => handleCheckChange(e, stringId)} |
| 72 | + className="flex-grow-1 m-0" |
| 73 | + /> |
| 74 | + {isChecked && ( |
| 75 | + <Dropdown |
| 76 | + show={openLevelDropdowns[stringId] ?? false} |
| 77 | + onToggle={(isOpen) => |
| 78 | + setOpenLevelDropdowns((prev) => ({ ...prev, [stringId]: isOpen })) |
| 79 | + } |
| 80 | + className="ms-auto" |
| 81 | + > |
| 82 | + <Dropdown.Toggle |
| 83 | + variant="outline-secondary" |
| 84 | + size="sm" |
| 85 | + id={`level-toggle-${stringId}`} |
| 86 | + className="py-0 px-2" |
| 87 | + > |
| 88 | + {currentLevel} |
| 89 | + </Dropdown.Toggle> |
| 90 | + <Dropdown.Menu className="shadow"> |
| 91 | + {logLevelOpts.map((opt) => ( |
| 92 | + <Dropdown.Item |
| 93 | + key={opt.id} |
| 94 | + active={opt.id === currentLevel} |
| 95 | + onClick={(e) => { |
| 96 | + e.stopPropagation(); |
| 97 | + handleLevelChange(stringId, opt.id.toString()); |
| 98 | + }} |
| 99 | + > |
| 100 | + {opt.label} |
| 101 | + </Dropdown.Item> |
| 102 | + ))} </Dropdown.Menu> |
| 103 | + </Dropdown> |
| 104 | + )} |
| 105 | + </Dropdown.Item> |
| 106 | + ); |
| 107 | + })} |
| 108 | + </Dropdown.Menu> |
| 109 | + </Dropdown> |
| 110 | + ); |
| 111 | +}; |
0 commit comments