All files / sportident-testbench-client/src index.tsx

0% Statements 0/33
0% Branches 0/7
0% Functions 0/9
0% Lines 0/33

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78                                                                                                                                                           
import React from 'react';
import ReactDOM from 'react-dom';
import {MainStationList} from './MainStationList';
import {Terminal} from './Terminal';
import {SiDevicesContext, SiDevicesContextPayload} from './SiDevicesContext';
import {useSiDevices} from 'sportident-react/lib';
import {getWebUsbSiDeviceDriver} from 'sportident-webusb/lib';
import * as nav from 'sportident-webusb/lib/INavigatorWebUsb';
 
import './styles.css';
 
const Testbench = () => {
    const getIdentFromWindowHash = (windowHash: string) => {
        const res = /^#?(\S+)$/.exec(windowHash);
        return res && res[1];
    };
    const [windowHash, setWindowHash] = React.useState(window.location.hash);
    const {webUsbSiDevices, addNewDevice} = React.useContext(SiDevicesContext);
    const ident = getIdentFromWindowHash(windowHash);
    const selectedDevice = ident ? webUsbSiDevices.get(ident) : undefined;
    React.useEffect(() => {
        const onHashChange = () => {
            setWindowHash(window.location.hash);
        };
        window.addEventListener('hashchange', onHashChange);
        return () => {
            window.removeEventListener('hashchange', onHashChange);
        };
    }, []);
    return (
        <>
            <MainStationList
                devices={[...webUsbSiDevices.values()]}
                selectedDevice={selectedDevice}
                addNewDevice={addNewDevice}
            />
            <Terminal
                selectedDevice={selectedDevice}
                key={selectedDevice && selectedDevice.ident}
            />
        </>
    );
};
 
const WebUsbSiDeviceProvider = (
    props: {
        children: React.ReactElement,
    },
) => {
    const webUsbSiDeviceDriver = React.useMemo(
        () => getWebUsbSiDeviceDriver(window.navigator.usb as unknown as nav.WebUsb),
        [],
    );
    const webUsbSiDevices = useSiDevices(webUsbSiDeviceDriver);
    const providedValue = {
        addNewDevice: () => webUsbSiDeviceDriver.detect(),
        webUsbSiDevices: webUsbSiDevices,
    } as unknown as SiDevicesContextPayload;
    return (
        <SiDevicesContext.Provider value={providedValue}>
            {props.children}
        </SiDevicesContext.Provider>
    );
};
 
Iif (window.addEventListener) {
    window.addEventListener('load', () => {
        ReactDOM.render(
            (
                <WebUsbSiDeviceProvider>
                    <Testbench />
                </WebUsbSiDeviceProvider>
            ),
            window.document.getElementById('root'),
        );
    });
}