diff --git a/src/components/Dialog/NewDeviceDialog.tsx b/src/components/Dialog/NewDeviceDialog.tsx index 57cca565..b9fc6b1c 100644 --- a/src/components/Dialog/NewDeviceDialog.tsx +++ b/src/components/Dialog/NewDeviceDialog.tsx @@ -22,9 +22,12 @@ import { Subtle } from "@components/UI/Typography/Subtle.tsx"; import { AlertCircle } from "lucide-react"; import { Link } from "../UI/Typography/Link.tsx"; import { Fragment } from "react/jsx-runtime"; +import { useState } from "react"; export interface TabElementProps { closeDialog: () => void; + connectionInProgress: boolean; + setConnectionInProgress: (inProgress: boolean) => void; } export interface TabManifest { @@ -111,25 +114,28 @@ export const NewDeviceDialog = ({ open, onOpenChange, }: NewDeviceProps) => { + const [connectionInProgress, setConnectionInProgress] = + useState(false); const { unsupported } = useBrowserFeatureDetection(); + const tabs: TabManifest[] = [ { label: "HTTP", element: HTTP, - isDisabled: false, + isDisabled: connectionInProgress, }, { label: "Bluetooth", element: BLE, isDisabled: unsupported.includes("Web Bluetooth") || - unsupported.includes("Secure Context"), + unsupported.includes("Secure Context") || connectionInProgress, }, { label: "Serial", element: Serial, isDisabled: unsupported.includes("Web Serial") || - unsupported.includes("Secure Context"), + unsupported.includes("Secure Context") || connectionInProgress, }, ]; @@ -154,7 +160,7 @@ export const NewDeviceDialog = ({ {tab.isDisabled ? : null} - onOpenChange(false)} /> + onOpenChange(false)} setConnectionInProgress={setConnectionInProgress} connectionInProgress={connectionInProgress} /> ))} diff --git a/src/components/PageComponents/Connect/BLE.tsx b/src/components/PageComponents/Connect/BLE.tsx index ad302321..1f111ad6 100644 --- a/src/components/PageComponents/Connect/BLE.tsx +++ b/src/components/PageComponents/Connect/BLE.tsx @@ -9,7 +9,7 @@ import { BleConnection, ServiceUuid } from "@meshtastic/js"; import { useCallback, useEffect, useState } from "react"; import { useMessageStore } from "@core/stores/messageStore.ts"; -export const BLE = ({ closeDialog }: TabElementProps) => { +export const BLE = ({ setConnectionInProgress, closeDialog }: TabElementProps) => { const [bleDevices, setBleDevices] = useState([]); const { addDevice } = useDeviceStore(); const messageStore = useMessageStore() @@ -45,6 +45,7 @@ export const BLE = ({ closeDialog }: TabElementProps) => { key={device.id} className="dark:bg-slate-900 dark:text-white" onClick={() => { + setConnectionInProgress(true); onConnect(device); }} > @@ -67,6 +68,11 @@ export const BLE = ({ closeDialog }: TabElementProps) => { if (exists === -1) { setBleDevices(bleDevices.concat(device)); } + }).catch((error) => { + console.error("Error requesting device:", error); + setConnectionInProgress(false); + }).finally(() => { + setConnectionInProgress(false); }); }} > @@ -74,4 +80,4 @@ export const BLE = ({ closeDialog }: TabElementProps) => { ); -}; +}; \ No newline at end of file diff --git a/src/components/PageComponents/Connect/HTTP.tsx b/src/components/PageComponents/Connect/HTTP.tsx index 65d6ca29..fc0670ce 100644 --- a/src/components/PageComponents/Connect/HTTP.tsx +++ b/src/components/PageComponents/Connect/HTTP.tsx @@ -20,7 +20,7 @@ interface FormData { tls: boolean; } -export const HTTP = ({ closeDialog }: TabElementProps) => { +export const HTTP = ({ closeDialog, setConnectionInProgress, connectionInProgress }: TabElementProps) => { const isURLHTTPS = location.protocol === "https:"; const { addDevice } = useDeviceStore(); @@ -30,10 +30,10 @@ export const HTTP = ({ closeDialog }: TabElementProps) => { const { control, handleSubmit, register } = useForm({ defaultValues: { ip: ["client.meshtastic.org", "localhost"].includes( - window.location.hostname, + globalThis.location.hostname, ) ? "meshtastic.local" - : window.location.host, + : globalThis.location.host, tls: isURLHTTPS ? true : false, }, }); @@ -42,7 +42,6 @@ export const HTTP = ({ closeDialog }: TabElementProps) => { field: { value: tlsValue, onChange: setTLS }, } = useController({ name: "tls", control }); - const [connectionInProgress, setConnectionInProgress] = useState(false); const [connectionError, setConnectionError] = useState<{ host: string; secure: boolean } | null>(null); const onSubmit = handleSubmit(async (data) => { @@ -76,14 +75,13 @@ export const HTTP = ({ closeDialog }: TabElementProps) => { prefix={tlsValue ? "https://" : "http://"} placeholder="000.000.000.000 / meshtastic.local" className="text-slate-900 dark:text-slate-900" - disabled={connectionInProgress} {...register("ip")} />
@@ -122,7 +120,6 @@ export const HTTP = ({ closeDialog }: TabElementProps) => {