59 lines
1.6 KiB
TypeScript
59 lines
1.6 KiB
TypeScript
import type React from 'react';
|
|
import { useEffect, useState } from 'react';
|
|
|
|
import { useForm, useWatch } from 'react-hook-form';
|
|
|
|
import { Checkbox } from '@components/generic/form/Checkbox';
|
|
import { Form } from '@components/generic/form/Form';
|
|
import { Input } from '@components/generic/form/Input';
|
|
import { connection } from '@core/connection';
|
|
import { useAppSelector } from '@hooks/useAppSelector';
|
|
import type { Protobuf } from '@meshtastic/meshtasticjs';
|
|
|
|
export const WiFi = (): JSX.Element => {
|
|
const preferences = useAppSelector(
|
|
(state) => state.meshtastic.radio.preferences,
|
|
);
|
|
const [loading, setLoading] = useState(false);
|
|
const { register, handleSubmit, formState, reset, control } =
|
|
useForm<Protobuf.RadioConfig_UserPreferences>({
|
|
defaultValues: preferences,
|
|
});
|
|
|
|
const WifiApMode = useWatch({
|
|
control,
|
|
name: 'wifiApMode',
|
|
defaultValue: false,
|
|
});
|
|
|
|
useEffect(() => {
|
|
reset(preferences);
|
|
}, [reset, preferences]);
|
|
|
|
const onSubmit = handleSubmit((data) => {
|
|
setLoading(true);
|
|
void connection.setPreferences(data, async () => {
|
|
reset({ ...data });
|
|
setLoading(false);
|
|
await Promise.resolve();
|
|
});
|
|
});
|
|
return (
|
|
<Form loading={loading} dirty={!formState.isDirty} submit={onSubmit}>
|
|
<Checkbox label="Enable WiFi AP" {...register('wifiApMode')} />
|
|
<Input
|
|
label="WiFi SSID"
|
|
disabled={WifiApMode}
|
|
{...register('wifiSsid')}
|
|
/>
|
|
<Input
|
|
type="password"
|
|
autoComplete="off"
|
|
label="WiFi PSK"
|
|
disabled={WifiApMode}
|
|
{...register('wifiPassword')}
|
|
/>
|
|
</Form>
|
|
);
|
|
};
|