Skip to content

Commit

Permalink
Refactored connect page panes into separate components
Browse files Browse the repository at this point in the history
  • Loading branch information
ajmcquilkin committed Jun 26, 2023
1 parent 7c55d7f commit d660a5f
Show file tree
Hide file tree
Showing 6 changed files with 183 additions and 86 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"@material-tailwind/react": "^1.2.4",
"@radix-ui/react-accordion": "^1.1.0",
"@radix-ui/react-checkbox": "^1.0.2",
"@radix-ui/react-collapsible": "^1.0.3",
"@radix-ui/react-icons": "^1.3.0",
"@radix-ui/react-switch": "^1.0.1",
"@radix-ui/react-tabs": "^1.0.4",
Expand Down
31 changes: 31 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions src/components/connection/SerialConnectPane.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import React from "react";
import { ArrowPathIcon } from "@heroicons/react/24/outline";
import SerialPortOption from "@app/components/connection/SerialPortOption";

import type { RequestStatus } from "@features/requests/requestReducer";

export interface ISerialConnectPaneProps {
availableSerialPorts: string[] | null;
activePort: string;
activePortState: RequestStatus;
handlePortSelected: (portName: string) => void;
refreshPorts: () => void;
}

const SerialConnectPane = ({
availableSerialPorts,
activePort,
activePortState,
handlePortSelected,
refreshPorts,
}: ISerialConnectPaneProps) => {
return (
<div>
<div className="flex flex-col mt-4">
<div className="flex flex-col gap-4">
{availableSerialPorts?.length ? (
availableSerialPorts.map((portName) => (
<SerialPortOption
key={portName}
name={portName}
connectionState={
activePort === portName ? activePortState : { status: "IDLE" }
}
onClick={handlePortSelected}
/>
))
) : (
<p className="text-base leading-6 font-normal text-gray-500 pl-40 pr-40 text-center">
No ports detected.
</p>
)}
</div>
</div>

<button
type="button"
className="flex flex-row justify-center align-middle mx-auto gap-4 mt-5"
onClick={() => refreshPorts()}
>
<ArrowPathIcon className="text-gray-400 w-6 h-6 hover:cursor-pointer" />
<p className="my-auto text-gray-500">Refresh ports</p>
</button>
</div>
);
};

export default SerialConnectPane;
File renamed without changes.
77 changes: 77 additions & 0 deletions src/components/connection/TcpConnectPane.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import React from "react";
import type { FormEventHandler } from "react";
import {
EllipsisHorizontalCircleIcon,
LinkIcon,
} from "@heroicons/react/24/outline";

import type { RequestStatus } from "@features/requests/requestReducer";

export interface ITcpConnectPaneProps {
socketAddress: string;
setSocketAddress: (socketAddress: string) => void;
socketPort: string;
setSocketPort: (socketPort: string) => void;
activeSocketState: RequestStatus;
handleSocketConnect: FormEventHandler;
}

const TcpConnectPane = ({
socketAddress,
setSocketAddress,
socketPort,
setSocketPort,
activeSocketState,
handleSocketConnect,
}: ITcpConnectPaneProps) => {
return (
<form className="flex flex-col gap-4 mt-4" onSubmit={handleSocketConnect}>
<input
className="flex-1 border border-gray-400 rounded-lg px-5 py-4 text-gray-700 placeholder:text-gray-400 h-full bg-transparent focus:outline-none disabled:cursor-wait"
type="text"
enterKeyHint="go"
placeholder="IP address or host name"
value={socketAddress}
onChange={(e) => setSocketAddress(e.target.value)}
disabled={activeSocketState.status === "PENDING"}
/>

<input
className="flex-1 border border-gray-400 rounded-lg px-5 py-4 text-gray-700 placeholder:text-gray-400 h-full bg-transparent focus:outline-none disabled:cursor-wait"
type="text"
placeholder="Port"
value={socketPort}
onChange={(e) => setSocketPort(e.target.value)}
disabled={activeSocketState.status === "PENDING"}
/>

<button
className="flex flex-row flex-1 justify-center gap-3 border rounded-lg border-gray-400 mx-auto px-5 py-4 hover:bg-gray-50 hover:border-gray-500 hover:shadow-lg disabled:cursor-wait"
disabled={activeSocketState.status === "PENDING"}
type="submit"
>
{activeSocketState.status === "PENDING" ? (
<>
<EllipsisHorizontalCircleIcon className="w-6 h-6 text-gray-500" />
<p className="text-gray-700">Connecting...</p>
</>
) : (
<>
<LinkIcon className="w-6 h-6 text-gray-500" />
<p className="text-gray-700">Connect</p>
</>
)}
</button>

{activeSocketState.status === "FAILED" && (
<div>
<p className="pl-6 pr-2 ml-4 text-sm leading-5 font-light text-red-600 my-1">
{activeSocketState.message}
</p>
</div>
)}
</form>
);
};

export default TcpConnectPane;
103 changes: 17 additions & 86 deletions src/components/pages/ConnectPage.tsx
Original file line number Diff line number Diff line change
@@ -1,18 +1,12 @@
import React, { FormEventHandler, useEffect, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { open } from "@tauri-apps/api/shell";
import {
ArrowPathIcon,
EllipsisHorizontalCircleIcon,
LinkIcon,
} from "@heroicons/react/24/outline";
import * as Tabs from "@radix-ui/react-tabs";

import Hero_Image from "@app/assets/onboard_hero_image.jpg";
import Meshtastic_Logo from "@app/assets/Mesh_Logo_Black.png";

import ConnectTab from "@components/connection/ConnectTab";
import SerialPortOption from "@components/Onboard/SerialPortOption";

import { selectConnectionStatus } from "@features/connection/connectionSelectors";
import { connectionSliceActions } from "@features/connection/connectionSlice";
Expand All @@ -31,6 +25,8 @@ import { requestSliceActions } from "@features/requests/requestReducer";
import { ConnectionType } from "@utils/connections";

import "@components/SplashScreen/SplashScreen.css";
import TcpConnectPane from "../connection/TcpConnectPane";
import SerialConnectPane from "../connection/SerialConnectPane";

const getFullSocketAddress = (address: string, port: string) =>
`${address}:${port}`;
Expand Down Expand Up @@ -197,89 +193,24 @@ const ConnectPage = ({ unmountSelf }: IOnboardPageProps) => {
</Tabs.List>

<Tabs.Content value={ConnectionType.SERIAL}>
<div className="flex flex-col mt-4">
<div className="flex flex-col gap-4">
{availableSerialPorts?.length ? (
availableSerialPorts.map((portName) => (
<SerialPortOption
key={portName}
name={portName}
connectionState={
activePort === portName
? activePortState
: { status: "IDLE" }
}
onClick={handlePortSelected}
/>
))
) : (
<p className="text-base leading-6 font-normal text-gray-500 pl-40 pr-40 text-center">
No ports detected.
</p>
)}
</div>
</div>

<button
type="button"
className="flex flex-row justify-center align-middle mx-auto gap-4 mt-5"
onClick={() => refreshPorts()}
>
<ArrowPathIcon className="text-gray-400 w-6 h-6 hover:cursor-pointer" />
<p className="my-auto text-gray-500">Refresh ports</p>
</button>
<SerialConnectPane
availableSerialPorts={availableSerialPorts}
activePort={activePort}
activePortState={activePortState}
handlePortSelected={handlePortSelected}
refreshPorts={refreshPorts}
/>
</Tabs.Content>

<Tabs.Content value={ConnectionType.TCP}>
<form
className="flex flex-col gap-4 mt-4"
onSubmit={handleSocketConnect}
>
<input
className="flex-1 border border-gray-400 rounded-lg px-5 py-4 text-gray-700 placeholder:text-gray-400 h-full bg-transparent focus:outline-none disabled:cursor-wait"
type="text"
enterKeyHint="go"
placeholder="IP address or host name"
value={socketAddress}
onChange={(e) => setSocketAddress(e.target.value)}
disabled={activeSocketState.status === "PENDING"}
/>

<input
className="flex-1 border border-gray-400 rounded-lg px-5 py-4 text-gray-700 placeholder:text-gray-400 h-full bg-transparent focus:outline-none disabled:cursor-wait"
type="text"
placeholder="Port"
value={socketPort}
onChange={(e) => setSocketPort(e.target.value)}
disabled={activeSocketState.status === "PENDING"}
/>

<button
className="flex flex-row flex-1 justify-center gap-3 border rounded-lg border-gray-400 mx-auto px-5 py-4 hover:bg-gray-50 hover:border-gray-500 hover:shadow-lg disabled:cursor-wait"
disabled={activeSocketState.status === "PENDING"}
type="submit"
>
{activeSocketState.status === "PENDING" ? (
<>
<EllipsisHorizontalCircleIcon className="w-6 h-6 text-gray-500" />
<p className="text-gray-700">Connecting...</p>
</>
) : (
<>
<LinkIcon className="w-6 h-6 text-gray-500" />
<p className="text-gray-700">Connect</p>
</>
)}
</button>

{activeSocketState.status === "FAILED" && (
<div>
<p className="pl-6 pr-2 ml-4 text-sm leading-5 font-light text-red-600 my-1">
{activeSocketState.message}
</p>
</div>
)}
</form>
<TcpConnectPane
socketAddress={socketAddress}
setSocketAddress={setSocketAddress}
socketPort={socketPort}
setSocketPort={setSocketPort}
activeSocketState={activeSocketState}
handleSocketConnect={handleSocketConnect}
/>
</Tabs.Content>
</Tabs.Root>
</div>
Expand Down

0 comments on commit d660a5f

Please sign in to comment.