import React from 'react'; import { fromByteArray, toByteArray } from 'base64-js'; import { useForm, useWatch } from 'react-hook-form'; import { FaQrcode } from 'react-icons/fa'; import { FiChevronDown, FiChevronUp, FiSave } from 'react-icons/fi'; import { MdRefresh, MdVisibility, MdVisibilityOff } from 'react-icons/md'; import QRCode from 'react-qr-code'; import { Loading } from '@components/generic/Loading'; import { Modal } from '@components/generic/Modal'; import { connection } from '@core/connection'; import { Disclosure } from '@headlessui/react'; import { Card, Checkbox, IconButton, Input, Select, } from '@meshtastic/components'; import { Protobuf } from '@meshtastic/meshtasticjs'; export interface ChannelProps { channel: Protobuf.Channel; } export const Channel = ({ channel }: ChannelProps): JSX.Element => { const [loading, setLoading] = React.useState(false); const [showQr, setShowQr] = React.useState(false); const [keySize, setKeySize] = React.useState<128 | 256>(256); const [pskHidden, setPskHidden] = React.useState(true); const { register, handleSubmit, setValue, control, formState } = useForm< Omit & { psk: string; enabled: boolean } >({ defaultValues: { enabled: [ Protobuf.Channel_Role.SECONDARY, Protobuf.Channel_Role.PRIMARY, ].find((role) => role === channel.role) ? true : false, name: channel.settings?.name, psk: fromByteArray(channel.settings?.psk ?? new Uint8Array(0)), }, }); const watchPsk = useWatch({ control, name: 'psk', defaultValue: '', }); const onSubmit = handleSubmit(async (data) => { setLoading(true); const channelData = Protobuf.Channel.create({ role: channel.role === Protobuf.Channel_Role.PRIMARY ? Protobuf.Channel_Role.PRIMARY : data.enabled ? Protobuf.Channel_Role.SECONDARY : Protobuf.Channel_Role.DISABLED, index: channel.index, settings: { ...data, psk: toByteArray(data.psk ?? ''), }, }); await connection.setChannel(channelData, (): Promise => { setLoading(false); return Promise.resolve(); }); }); return ( <> { setShowQr(false); }} > {({ open }): JSX.Element => ( <> <>
role === channel.role) ? 'bg-green-500' : 'bg-gray-400' }`} />
{channel.settings?.name.length ? channel.settings.name : channel.role === Protobuf.Channel_Role.PRIMARY ? 'Primary' : `Channel: ${channel.index}`}
{open && ( => { e.stopPropagation(); await onSubmit(); }} disabled={loading || !formState.isDirty} icon={} /> )} { e.stopPropagation(); setShowQr(true); }} icon={} /> : } />
{loading && }
{channel.index !== 0 && ( <> )} { setPskHidden(!setPskHidden); }} icon={ pskHidden ? : } /> { const key = new Uint8Array(keySize); crypto.getRandomValues(key); setValue('psk', fromByteArray(key)); }} icon={} /> } {...register('psk')} />
)} ); };