diff --git a/plugins/lime-plugin-rx/src/rxApi.ts b/plugins/lime-plugin-rx/src/rxApi.ts index b53fc4f0..04aaaff5 100644 --- a/plugins/lime-plugin-rx/src/rxApi.ts +++ b/plugins/lime-plugin-rx/src/rxApi.ts @@ -1,6 +1,8 @@ import { IGetInternetStatus, StatusResponse, + SupportedPortRoles, + SwitchStatus, } from "plugins/lime-plugin-rx/src/rxTypes"; import api from "utils/uhttpd.service"; @@ -10,3 +12,16 @@ export const getNodeStatus = (): Promise => export const getInternetStatus = (): Promise => api.call("lime-metrics", "get_internet_status", {}); + +export type SetPortRoleArgs = { role: SupportedPortRoles } & Omit< + SwitchStatus, + "role" +>; + +export const setPortRole = async ( + newStatus: SetPortRoleArgs +): Promise => { + console.log("Setting port role", newStatus); + await new Promise((resolve) => setTimeout(resolve, 2000)); + return true; +}; diff --git a/plugins/lime-plugin-rx/src/rxQueries.ts b/plugins/lime-plugin-rx/src/rxQueries.ts index 41a3b298..0b8d9f98 100644 --- a/plugins/lime-plugin-rx/src/rxQueries.ts +++ b/plugins/lime-plugin-rx/src/rxQueries.ts @@ -1,6 +1,18 @@ -import { useQuery } from "@tanstack/react-query"; +import { MutationKey } from "@tanstack/query-core/src/types"; +import { + UseMutationOptions, + useMutation, + useQuery, +} from "@tanstack/react-query"; -import { getInternetStatus, getNodeStatus } from "./rxApi"; +import { SupportedPortRoles } from "plugins/lime-plugin-rx/src/rxTypes"; + +import { + SetPortRoleArgs, + getInternetStatus, + getNodeStatus, + setPortRole, +} from "./rxApi"; const refetchInterval = 2000; @@ -24,3 +36,16 @@ export function useInternetStatus(params?) { ...params, }); } + +export const useSetPortRole = ( + options?: Omit< + UseMutationOptions, + "mutationFn" | "mutationKey" + > +) => { + return useMutation({ + mutationFn: setPortRole, + mutationKey: ["useSetPortRole"] as MutationKey, + ...options, + }); +}; diff --git a/plugins/lime-plugin-rx/src/rxTypes.ts b/plugins/lime-plugin-rx/src/rxTypes.ts index 6a5ff82a..eb7b7889 100644 --- a/plugins/lime-plugin-rx/src/rxTypes.ts +++ b/plugins/lime-plugin-rx/src/rxTypes.ts @@ -53,3 +53,9 @@ export interface IGetInternetStatus { IPv4: { working: boolean }; status: string; } + +export enum SupportedPortRoles { + WAN = "wan", + LAN = "lan", + MESH = "mesh", +} diff --git a/plugins/lime-plugin-rx/src/sections/system.tsx b/plugins/lime-plugin-rx/src/sections/system.tsx index 25315e30..53b2cdd9 100644 --- a/plugins/lime-plugin-rx/src/sections/system.tsx +++ b/plugins/lime-plugin-rx/src/sections/system.tsx @@ -40,6 +40,11 @@ const SystemInfo = () => { const boardData = bd as IGetBoardDataResponse; const secNum = parseInt(node?.uptime, 10); + const ips = + node?.ips.map((ip) => ({ + label: `IPv${ip.version}`, + value: ip.address, + })) || []; const attributes = [ { label: t`Uptime`, @@ -47,7 +52,9 @@ const SystemInfo = () => { }, { label: t`Device`, value: boardData.board_name }, { label: t`Firmware`, value: boardData.release.description }, + ...ips, ]; + return (
diff --git a/plugins/lime-plugin-rx/src/sections/wired.tsx b/plugins/lime-plugin-rx/src/sections/wired.tsx index 26b66da0..46a82652 100644 --- a/plugins/lime-plugin-rx/src/sections/wired.tsx +++ b/plugins/lime-plugin-rx/src/sections/wired.tsx @@ -1,4 +1,9 @@ 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 { useToast } from "components/toast/toastProvider"; import { IconsClassName, @@ -6,16 +11,248 @@ import { SectionTitle, } from "plugins/lime-plugin-rx/src/components/components"; import { PortsIcon } from "plugins/lime-plugin-rx/src/icons/portsIcon"; -import { useNodeStatus } from "plugins/lime-plugin-rx/src/rxQueries"; -import { SwitchStatus } from "plugins/lime-plugin-rx/src/rxTypes"; - -const Ports = ({ switches }: { switches: SwitchStatus[] }) => { - const ports = switches.reduce((acc, obj) => { - const { role } = obj; - if (!acc[role]) { - acc[role] = []; +import { + useNodeStatus, + useSetPortRole, +} from "plugins/lime-plugin-rx/src/rxQueries"; +import { + SupportedPortRoles, + SwitchStatus, +} from "plugins/lime-plugin-rx/src/rxTypes"; + +import queryCache from "utils/queryCache"; + +const liro1 = [ + { + device: "eth0.1", + num: 5, + role: "wan", + link: "down", + }, + { + device: "eth0.1", + num: 0, + role: "cpu", + link: "up", + }, + { + device: "eth1.2", + num: 4, + role: "lan", + link: "up", + }, + { + device: "eth1.2", + num: 6, + role: "cpu", + link: "up", + }, +]; + +const mocked = [ + { + device: "eth1", + num: "wan", + role: "wan", + }, + { + device: "eth0", + num: "lan", + role: "lan", + }, + { + device: "eth2", + num: "wan", + role: "wan", + }, + { + device: "eth30", + num: "lan", + role: "lan", + }, + { + device: "eth214", + num: "wan", + role: "wan", + }, + { + device: "eth05", + num: "lan", + role: "lan", + }, +]; + +const tplink = [ + { + device: "eth0.1", + num: 1, + role: "lan", + link: "down", + }, + { + device: "eth0.1", + num: 2, + role: "lan", + link: "up", + }, + { + device: "eth0.1", + num: 3, + role: "lan", + link: "down", + }, + { + device: "eth0.1", + num: 4, + role: "lan", + link: "down", + }, + { + device: "eth0.1", + num: 0, + role: "cpu", + link: "up", + }, + // Modifactions + { + device: "eth0.3", + num: 0, + role: "lan", + link: "up", + }, + { + device: "eth0.3", + num: 1, + role: "lan", + link: "up", + }, + { + device: "eth0.2", + num: 0, + role: "wan", + link: "up", + }, + + { + device: "eth0.2", + num: 1, + role: "wan", + link: "up", + }, +]; + +type PortsByDevice = { + [device: string]: SwitchStatus[]; +}; + +const ChangeRoleConfirmationModal = ({ + newRole, + ...rest +}: { newRole: string } & ModalProps) => { + return ( + Changing role to {newRole}} + successBtnText={Confirm} + > +
+

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

+
+
+ ); +}; + +const PortRoleSelector = ({ port }: { port: SwitchStatus }) => { + const { showToast } = useToast(); + const [newRole, setNewRole] = useState(); + const { open, onOpen, onClose } = useDisclosure({ + onClose() { + setNewRole(null); + }, + }); + const { mutateAsync } = useSetPortRole({ + onSettled: () => { + queryCache.invalidateQueries({ + queryKey: ["lime-rx", "node-status"], + }); + onClose(); + }, + onError: (error) => { + console.error(error); + showToast({ + type: "error", + text: ( +
+ Error changing role: + {error.message} +
+ ), + }); + }, + }); + + const changeRole = async () => { + await mutateAsync({ ...port, role: newRole }); + }; + + 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[role].push(obj); + acc[device].push(obj); return acc; }, {}); return ( @@ -23,26 +260,19 @@ const Ports = ({ switches }: { switches: SwitchStatus[] }) => { className={"flex flex-wrap px-10 gap-4 justify-between"} data-testid="ports-container" > - {Object.keys(ports).map((role) => { - if (role.toLowerCase() === "cpu") return null; + {Object.entries(ports).map(([device, ports], k) => { return ( -
-

{role.toUpperCase()}

-

{ports[role][0].device.toLowerCase()}

-
- {ports[role].map((port) => { - const link = - port.link?.toLowerCase() === "up" - ? "fill-primary-dark" - : "fill-disabled"; - return ( -
- -
- ); - })} +
+

{device.toUpperCase()}

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