Skip to content

Commit

Permalink
minor fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
sachaw committed Dec 17, 2023
1 parent e3aee4c commit f7ba8ad
Show file tree
Hide file tree
Showing 14 changed files with 112 additions and 89 deletions.
12 changes: 6 additions & 6 deletions src/components/PageComponents/Connect/BLE.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ export const BLE = (): JSX.Element => {
}, []);

useEffect(() => {
void updateBleDeviceList();
updateBleDeviceList();
}, [updateBleDeviceList]);

const onConnect = async (BLEDevice: BluetoothDevice) => {
const onConnect = async (bleDevice: BluetoothDevice) => {
const id = randId();
const device = addDevice(id);
setSelectedDevice(id);
const connection = new IBLEConnection(id);
await connection.connect({
device: BLEDevice,
device: bleDevice,
});
device.addConnection(connection);
subscribeAll(device, connection);
Expand All @@ -39,7 +39,7 @@ export const BLE = (): JSX.Element => {
<Button
key={device.id}
onClick={() => {
void onConnect(device);
onConnect(device);
}}
>
{device.name}
Expand All @@ -50,8 +50,8 @@ export const BLE = (): JSX.Element => {
)}
</div>
<Button
onClick={() => {
void navigator.bluetooth
onClick={async () => {
await navigator.bluetooth
.requestDevice({
filters: [{ services: [Constants.ServiceUuid] }],
})
Expand Down
10 changes: 4 additions & 6 deletions src/components/PageComponents/Connect/HTTP.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Button } from "@components/UI/Button.js";
import { Input } from "@components/UI/Input.js";
import { Label } from "@components/UI/Label.js";
import { SelectLabel } from "@components/UI/Select.js";
import { Switch } from "@components/UI/Switch.js";
import { useAppStore } from "@core/stores/appStore.js";
import { useDeviceStore } from "@core/stores/deviceStore.js";
Expand All @@ -27,19 +26,19 @@ export const HTTP = (): JSX.Element => {
},
});

const TLSEnabled = useWatch({
const tlsEnabled = useWatch({
control,
name: "tls",
defaultValue: location.protocol === "https:",
});

const onSubmit = handleSubmit((data) => {
const onSubmit = handleSubmit(async (data) => {
const id = randId();
const device = addDevice(id);
setSelectedDevice(id);
const connection = new IHTTPConnection(id);
// TODO: Promise never resolves
void connection.connect({
await connection.connect({
address: data.ip,
fetchInterval: 2000,
tls: data.tls,
Expand All @@ -49,13 +48,12 @@ export const HTTP = (): JSX.Element => {
});

return (
// eslint-disable-next-line @typescript-eslint/no-misused-promises
<form className="flex w-full flex-col gap-2 p-4" onSubmit={onSubmit}>
<div className="flex h-48 flex-col gap-2">
<Label>IP Address/Hostname</Label>
<Input
// label="IP Address/Hostname"
prefix={TLSEnabled ? "https://" : "http://"}
prefix={tlsEnabled ? "https://" : "http://"}
placeholder="000.000.000.000 / meshtastic.local"
{...register("ip")}
/>
Expand Down
14 changes: 7 additions & 7 deletions src/components/PageComponents/Connect/Serial.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export const Serial = (): JSX.Element => {
}, []);

navigator.serial.addEventListener("connect", () => {
void updateSerialPortList();
updateSerialPortList();
});
navigator.serial.addEventListener("disconnect", () => {
void updateSerialPortList();
updateSerialPortList();
});
useEffect(() => {
void updateSerialPortList();
updateSerialPortList();
}, [updateSerialPortList]);

const onConnect = async (port: SerialPort) => {
Expand All @@ -49,8 +49,8 @@ export const Serial = (): JSX.Element => {
<Button
key={index}
disabled={port.readable !== null}
onClick={() => {
void onConnect(port);
onClick={async () => {
await onConnect(port);
}}
>
{`# ${index} - ${port.getInfo().usbVendorId ?? "UNK"} - ${
Expand All @@ -63,8 +63,8 @@ export const Serial = (): JSX.Element => {
)}
</div>
<Button
onClick={() => {
void navigator.serial.requestPort().then((port) => {
onClick={async () => {
await navigator.serial.requestPort().then((port) => {
setSerialPorts(serialPorts.concat(port));
});
}}
Expand Down
2 changes: 1 addition & 1 deletion src/components/PageComponents/Messages/MessageInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const MessageInput = ({
<div className="flex flex-grow gap-2">
<span className="w-full">
<Input
autoFocus
autoFocus={true}
minLength={2}
placeholder="Enter Message"
value={messageDraft}
Expand Down
13 changes: 7 additions & 6 deletions src/core/hooks/useToast.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as React from "react";
import { useEffect, useState, ReactNode } from "react";

import type { ToastActionElement, ToastProps } from "@components/UI/Toast.js";

Expand All @@ -7,8 +7,8 @@ const TOAST_REMOVE_DELAY = 1000000;

type ToasterToast = ToastProps & {
id: string;
title?: React.ReactNode;
description?: React.ReactNode;
title?: ReactNode;
description?: ReactNode;
action?: ToastActionElement;
};

Expand Down Expand Up @@ -109,7 +109,7 @@ export const reducer = (state: State, action: Action): State => {
),
};
}
case "REMOVE_TOAST":
case "REMOVE_TOAST": {
if (action.toastId === undefined) {
return {
...state,
Expand All @@ -120,6 +120,7 @@ export const reducer = (state: State, action: Action): State => {
...state,
toasts: state.toasts.filter((t) => t.id !== action.toastId),
};
}
}
};

Expand Down Expand Up @@ -166,9 +167,9 @@ function toast({ ...props }: Toast) {
}

function useToast() {
const [state, setState] = React.useState<State>(memoryState);
const [state, setState] = useState<State>(memoryState);

React.useEffect(() => {
useEffect(() => {
listeners.push(setState);
return () => {
const index = listeners.indexOf(setState);
Expand Down
6 changes: 3 additions & 3 deletions src/core/stores/appStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export interface RasterSource {
tileSize: number;
}

export type accentColor =
export type AccentColor =
| "red"
| "orange"
| "yellow"
Expand All @@ -26,7 +26,7 @@ interface AppState {
rasterSources: RasterSource[];
commandPaletteOpen: boolean;
darkMode: boolean;
accent: accentColor;
accent: AccentColor;
connectDialogOpen: boolean;

setRasterSources: (sources: RasterSource[]) => void;
Expand All @@ -38,7 +38,7 @@ interface AppState {
removeDevice: (deviceId: number) => void;
setCommandPaletteOpen: (open: boolean) => void;
setDarkMode: (enabled: boolean) => void;
setAccent: (color: accentColor) => void;
setAccent: (color: AccentColor) => void;
setConnectDialogOpen: (open: boolean) => void;
}

Expand Down
85 changes: 53 additions & 32 deletions src/core/stores/deviceStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import { produce } from "immer";
import { create } from "zustand";

import { Protobuf, Types } from "@meshtastic/meshtasticjs";
import { channel } from "diagnostics_channel";

export type Page = "messages" | "map" | "config" | "channels" | "peers";

Expand All @@ -14,7 +13,7 @@ export interface MessageWithState extends Types.PacketMetadata<string> {

export type MessageState = "ack" | "waiting" | Protobuf.Routing_Error;

export interface processPacketParams {
export interface ProcessPacketParams {
from: number;
snr: number;
time: number;
Expand Down Expand Up @@ -84,7 +83,7 @@ export interface Device {
state: MessageState,
) => void;
setDialogOpen: (dialog: DialogVariant, open: boolean) => void;
processPacket: (data: processPacketParams) => void;
processPacket: (data: ProcessPacketParams) => void;
setMessageDraft: (message: string) => void;
}

Expand Down Expand Up @@ -152,27 +151,34 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({

if (device) {
switch (config.payloadVariant.case) {
case "device":
case "device": {
device.config.device = config.payloadVariant.value;
break;
case "position":
}
case "position": {
device.config.position = config.payloadVariant.value;
break;
case "power":
}
case "power": {
device.config.power = config.payloadVariant.value;
break;
case "network":
}
case "network": {
device.config.network = config.payloadVariant.value;
break;
case "display":
}
case "display": {
device.config.display = config.payloadVariant.value;
break;
case "lora":
}
case "lora": {
device.config.lora = config.payloadVariant.value;
break;
case "bluetooth":
}
case "bluetooth": {
device.config.bluetooth = config.payloadVariant.value;
break;
}
}
}
}),
Expand All @@ -185,43 +191,58 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({

if (device) {
switch (config.payloadVariant.case) {
case "mqtt":
case "mqtt": {
device.moduleConfig.mqtt = config.payloadVariant.value;
break;
case "serial":
}
case "serial": {
device.moduleConfig.serial = config.payloadVariant.value;
break;
case "externalNotification":
}
case "externalNotification": {
device.moduleConfig.externalNotification =
config.payloadVariant.value;
break;
case "storeForward":
}
case "storeForward": {
device.moduleConfig.storeForward =
config.payloadVariant.value;
break;
case "rangeTest":
}
case "rangeTest": {
device.moduleConfig.rangeTest =
config.payloadVariant.value;
break;
case "telemetry":
}
case "telemetry": {
device.moduleConfig.telemetry =
config.payloadVariant.value;
break;
case "cannedMessage":
}
case "cannedMessage": {
device.moduleConfig.cannedMessage =
config.payloadVariant.value;
break;
case "audio":
}
case "audio": {
device.moduleConfig.audio = config.payloadVariant.value;
break;
case "neighborInfo":
device.moduleConfig.neighborInfo = config.payloadVariant.value;
}
case "neighborInfo": {
device.moduleConfig.neighborInfo =
config.payloadVariant.value;
break;
}
case "ambientLighting": {
device.moduleConfig.ambientLighting =
config.payloadVariant.value;
break;
case "ambientLighting":
device.moduleConfig.ambientLighting = config.payloadVariant.value;
}
case "detectionSensor": {
device.moduleConfig.detectionSensor =
config.payloadVariant.value;
break;
case "detectionSensor":
device.moduleConfig.detectionSensor = config.payloadVariant.value;
}
}
}
}),
Expand Down Expand Up @@ -519,15 +540,21 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
}),
);
},
processPacket(data: processPacketParams) {
processPacket(data: ProcessPacketParams) {
set(
produce<DeviceState>((draft) => {
const device = draft.devices.get(id);
if (!device) {
return;
}
const node = device.nodes.get(data.from);
if (!node) {
if (node) {
device.nodes.set(data.from, {
...node,
lastHeard: data.time,
snr: data.snr,
});
} else {
device.nodes.set(
data.from,
new Protobuf.NodeInfo({
Expand All @@ -536,12 +563,6 @@ export const useDeviceStore = create<DeviceState>((set, get) => ({
snr: data.snr,
}),
);
} else {
device.nodes.set(data.from, {
...node,
lastHeard: data.time,
snr: data.snr,
});
}
}),
);
Expand Down
Loading

0 comments on commit f7ba8ad

Please sign in to comment.