Newer
Older
import { insertMarkTags } from "@helper/text";
import DOMPurify from 'dompurify';
import { MutableRefObject, useCallback, useRef } from "react";
import { OverlayTrigger, Table, Tooltip } from "react-bootstrap";
import { useTranslation } from "react-i18next";
import { useDeviceTableViewModel } from "../view_model/device.table.viewmodel";
const cropUUID = (uuid: string): string => {
return uuid.substring(0, 3) + "..." + uuid.substring(uuid.length - 3, uuid.length);
}
export const DeviceViewTable = (searchRef: MutableRefObject<HTMLInputElement>) => {
const { devices, pnds, selected: selectedDevice } = useAppSelector(state => state.device);
const tableRef = useRef();
const { trClickHandler } = useDeviceTableViewModel(searchRef, tableRef);
const getDeviceTable = useCallback(() => {
const search = searchRef.current?.value;
let filtered = devices
// filter if something is in search
if (search) {
filtered = devices.filter((device) => {
const user = pnds.find(pnd => pnd.id === device.pid);
return device.id?.includes(search) ||
device.name?.includes(search) ||
user?.name?.includes(search);
})
}
return filtered.map((device, index) => {
const user = pnds.find(pnd => pnd.id === device.pid);
const username = user?.name || ''
const deviceId = device.id!;
const cropedId = cropUUID(deviceId)
const devicename = device.name || '';
const rowData = username + ";" + deviceId + ";" + devicename
<tr data-copy-value={rowData} key={index} onClick={() => trClickHandler(device)} className={selectedDevice?.device.id === deviceId ? 'active' : ''}>
<td data-copy-value={devicename} dangerouslySetInnerHTML={{ __html: search ? insertMarkTags(devicename, search) : DOMPurify.sanitize(devicename) }}></td>
<OverlayTrigger overlay={<Tooltip id={device.id}>{deviceId}</Tooltip>}>
<td data-copy-value={deviceId} dangerouslySetInnerHTML={{ __html: search ? insertMarkTags(cropedId, search) : DOMPurify.sanitize(cropedId) }}></td>
<td data-copy-value={username} dangerouslySetInnerHTML={{ __html: search ? insertMarkTags(username, search) : DOMPurify.sanitize(username) }}></td>
}, [devices, searchRef, pnds, selectedDevice, trClickHandler]);
<Table striped responsive className="device-table" ref={tableRef}>