import type { TabElementProps } from "@components/Dialog/NewDeviceDialog.tsx"; import { Button } from "@components/UI/Button.tsx"; import { Input } from "@components/UI/Input.tsx"; import { Label } from "@components/UI/Label.tsx"; import { Switch } from "@components/UI/Switch.tsx"; import { useAppStore } from "@core/stores/appStore.ts"; import { useDeviceStore } from "@core/stores/deviceStore.ts"; import { subscribeAll } from "@core/subscriptions.ts"; import { randId } from "@core/utils/randId.ts"; import { MeshDevice } from "@meshtastic/core"; import { TransportHTTP } from "@meshtastic/transport-http"; import { useState } from "react"; import { useForm, useController } from "react-hook-form"; import { AlertTriangle } from "lucide-react"; import { useMessageStore } from "@core/stores/messageStore.ts"; interface FormData { ip: string; tls: boolean; } export const HTTP = ({ closeDialog }: TabElementProps) => { const isURLHTTPS = location.protocol === "https:"; const { addDevice } = useDeviceStore(); const messageStore = useMessageStore(); const { setSelectedDevice } = useAppStore(); const { control, handleSubmit, register } = useForm({ defaultValues: { ip: ["client.meshtastic.org", "localhost"].includes( window.location.hostname, ) ? "meshtastic.local" : window.location.host, tls: isURLHTTPS ? true : false, }, }); const { 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) => { setConnectionInProgress(true); setConnectionError(null); try { const id = randId(); const transport = await TransportHTTP.create(data.ip, data.tls); const device = addDevice(id); const connection = new MeshDevice(transport, id); connection.configure(); setSelectedDevice(id); device.addConnection(connection); subscribeAll(device, connection, messageStore); closeDialog(); } catch (error) { console.error("Connection error:", error); // Capture all connection errors regardless of type setConnectionError({ host: data.ip, secure: data.tls }); setConnectionInProgress(false); } }); return (
{connectionError && (

Connection Failed

Could not connect to the device. {connectionError.secure && "If using HTTPS, you may need to accept a self-signed certificate first. "} Please open{" "} {`${connectionError.secure ? "https" : "http"}://${connectionError.host}`} {" "} in a new tab{connectionError.secure ? ", accept any TLS warnings if prompted, then try again" : ""}.{" "} Learn more

)}
); };