react-icons/md#MdVisibility TypeScript Examples

The following examples show how to use react-icons/md#MdVisibility. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Channels.tsx    From meshtastic-web with GNU General Public License v3.0 4 votes vote down vote up
Channels = ({ channel }: SettingsPanelProps): JSX.Element => {
  const [loading, setLoading] = useState(false);
  const [keySize, setKeySize] = useState<128 | 256>(256);
  const [pskHidden, setPskHidden] = useState(true);

  const { register, handleSubmit, setValue, formState, reset } = useForm<
    Omit<Protobuf.ChannelSettings, 'psk'> & { psk: string; enabled: boolean }
  >({
    defaultValues: {
      enabled: [
        Protobuf.Channel_Role.SECONDARY,
        Protobuf.Channel_Role.PRIMARY,
      ].find((role) => role === channel?.role)
        ? true
        : false,
      ...channel?.settings,
      psk: fromByteArray(channel?.settings?.psk ?? new Uint8Array(0)),
    },
  });

  useEffect(() => {
    reset({
      enabled: [
        Protobuf.Channel_Role.SECONDARY,
        Protobuf.Channel_Role.PRIMARY,
      ].find((role) => role === channel?.role)
        ? true
        : false,
      ...channel?.settings,
      psk: fromByteArray(channel?.settings?.psk ?? new Uint8Array(0)),
    });
  }, [channel, reset]);

  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<void> => {
      reset({ ...data });
      setLoading(false);
      return Promise.resolve();
    });
  });

  return (
    <Form loading={loading} dirty={!formState.isDirty} submit={onSubmit}>
      {channel?.index !== 0 && (
        <>
          <Checkbox
            label="Enabled"
            {...register('enabled', { valueAsNumber: true })}
          />
          <Input label="Name" {...register('name')} />
        </>
      )}

      <Select
        label="Key Size"
        options={[
          { name: '128 Bit', value: 128 },
          { name: '256 Bit', value: 256 },
        ]}
        value={keySize}
        onChange={(e): void => {
          setKeySize(parseInt(e.target.value) as 128 | 256);
        }}
      />
      <Input
        label="Pre-Shared Key"
        type={pskHidden ? 'password' : 'text'}
        disabled
        action={
          <>
            <IconButton
              onClick={(): void => {
                setPskHidden(!pskHidden);
              }}
              icon={pskHidden ? <MdVisibility /> : <MdVisibilityOff />}
            />
            <IconButton
              onClick={(): void => {
                const key = new Uint8Array(keySize);
                crypto.getRandomValues(key);
                setValue('psk', fromByteArray(key));
              }}
              icon={<MdRefresh />}
            />
          </>
        }
        {...register('psk')}
      />
      <Checkbox label="Uplink Enabled" {...register('uplinkEnabled')} />
      <Checkbox label="Downlink Enabled" {...register('downlinkEnabled')} />
    </Form>
  );
}