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 { Link } from "@components/UI/Typography/Link.tsx"; import { useAppStore, useDeviceStore, useMessageStore } from "@core/stores"; 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 { AlertTriangle } from "lucide-react"; import { useState } from "react"; import { useController, useForm } from "react-hook-form"; import { useTranslation } from "react-i18next"; interface FormData { ip: string; tls: boolean; } export const HTTP = ({ closeDialog }: TabElementProps) => { const { t } = useTranslation("dialog"); const [connectionInProgress, setConnectionInProgress] = useState(false); 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( globalThis.location.hostname, ) ? "meshtastic.local" : globalThis.location.host, tls: !!isURLHTTPS, }, }); const { field: { value: tlsValue, onChange: setTLS }, } = useController({ name: "tls", control }); 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) { if (error instanceof Error) { console.error("Connection error:", error); } // Capture all connection errors regardless of type setConnectionError({ host: data.ip, secure: data.tls }); setConnectionInProgress(false); } }); return (
{connectionError && (

{t("newDeviceDialog.connectionFailedAlert.title")}

{t("newDeviceDialog.connectionFailedAlert.descriptionPrefix")} {connectionError.secure && t("newDeviceDialog.connectionFailedAlert.httpsHint")} {t("newDeviceDialog.connectionFailedAlert.openLinkPrefix")} {`${ connectionError.secure ? t("newDeviceDialog.https") : t("newDeviceDialog.http") }://${connectionError.host}`} {" "} {t("newDeviceDialog.connectionFailedAlert.openLinkSuffix")} {connectionError.secure ? t( "newDeviceDialog.connectionFailedAlert.acceptTlsWarningSuffix", ) : ""} .{" "} {t("newDeviceDialog.connectionFailedAlert.learnMoreLink")}

)}
); };