diff --git a/plugins/lime-plugin-rx/src/sections/wired.tsx b/plugins/lime-plugin-rx/src/sections/wired.tsx index 007778fb..c93e4573 100644 --- a/plugins/lime-plugin-rx/src/sections/wired.tsx +++ b/plugins/lime-plugin-rx/src/sections/wired.tsx @@ -1,4 +1,8 @@ import { Trans } from "@lingui/macro"; +import { useEffect, useState } from "preact/hooks"; + +import Modal, { ModalProps } from "components/Modal/Modal"; +import { useDisclosure } from "components/Modal/useDisclosure"; import { IconsClassName, @@ -128,78 +132,117 @@ const tplink = [ }, ]; -type PortsByRoleAndDevice = { - [port: string]: { [device: string]: SwitchStatus[] }; +type PortsByDevice = { + [device: string]: SwitchStatus[]; }; -const Ports = ({ switches }: { switches: SwitchStatus[] }) => { - const portsByRoleAndDevice: PortsByRoleAndDevice = switches.reduce( - (acc, obj) => { - const { role, device } = obj; +enum SupportedPortRoles { + WAN = "wan", + LAN = "lan", + MESH = "mesh", +} - if (!acc[role]) { - acc[role] = {}; - } - if (!acc[role][device]) { - acc[role][device] = []; - } - acc[role][device].push(obj); +const ChangeRoleConfirmationModal = ({ + newRole, + ...rest +}: { newRole: string } & ModalProps) => { + return ( + Changing role to {newRole}}> +
+

+ + Changing the role of a port may cause network + interruptions. Are you sure you want to continue? + +

+
+
+ ); +}; - return acc; +const PortRoleSelector = ({ port }: { port: SwitchStatus }) => { + const [newRole, setNewRole] = useState(""); + const { open, onOpen, onClose } = useDisclosure({ + onClose() { + setNewRole(""); }, - {} + }); + + const changeRole = async () => { + // await two seconds and then resolve + return new Promise((resolve) => setTimeout(resolve, 2000)); + }; + + useEffect(() => { + if (newRole) { + onOpen(); + } + }, [newRole]); + + const role = port.role.toLowerCase(); + if (role === "cpu") { + return null; + } + let link = "fill-disabled"; + if (port.link?.toLowerCase() === "up") link = "fill-primary-dark"; + return ( +
+ + + +
); +}; +const Ports = ({ switches: s }: { switches: SwitchStatus[] }) => { + const switches = tplink; + const ports: PortsByDevice = switches.reduce((acc, obj) => { + const { device } = obj; + if (!acc[device]) { + acc[device] = []; + } + acc[device].push(obj); + return acc; + }, {}); return (
- {Object.entries(portsByRoleAndDevice).map(([role, devices]) => { - if (role.toLowerCase() === "cpu") return null; + {Object.entries(ports).map(([device, ports], k) => { return ( -
- {/*Print role name*/} -

{role.toUpperCase()}

-
- {Object.entries(devices).map( - ([device, ports], k) => ( - // Print device name -
-

{device.toLowerCase()}

-
- {/*Print port group*/} - {ports.map((port) => { - let link = "fill-disabled"; - if ( - port.link?.toLowerCase() === - "up" - ) - link = "fill-primary-dark"; - return ( -
- -
- ); - })} -
-
- ) - )} +
+

{device.toUpperCase()}

+
+ {ports.map((port, k) => ( + + ))}
);