From a972fbca4152dd393c42a28372d2bd5654218e30 Mon Sep 17 00:00:00 2001 From: Sacha Weatherstone Date: Sun, 11 Dec 2022 19:08:33 +1000 Subject: [PATCH] Add node switching to command palette --- src/components/CommandPalette/Index.tsx | 43 ++++++++++-- .../CommandPalette/SearchResult.tsx | 69 +++++++++++++------ 2 files changed, 83 insertions(+), 29 deletions(-) diff --git a/src/components/CommandPalette/Index.tsx b/src/components/CommandPalette/Index.tsx index 14f6e8de..c8059549 100644 --- a/src/components/CommandPalette/Index.tsx +++ b/src/components/CommandPalette/Index.tsx @@ -18,11 +18,13 @@ import { toast } from "react-hot-toast"; import { useDevice } from "@app/core/providers/useDevice.js"; import { useAppStore } from "@app/core/stores/appStore.js"; +import { useDeviceStore } from "@app/core/stores/deviceStore.js"; import { GroupView } from "@components/CommandPalette/GroupView.js"; import { NoResults } from "@components/CommandPalette/NoResults.js"; import { PaletteTransition } from "@components/CommandPalette/PaletteTransition.js"; import { SearchBox } from "@components/CommandPalette/SearchBox.js"; import { SearchResult } from "@components/CommandPalette/SearchResult.js"; +import { Hashicon } from "@emeraldpay/hashicon-react"; import { Combobox, Dialog, Transition } from "@headlessui/react"; import { ArchiveBoxXMarkIcon, @@ -57,13 +59,27 @@ export interface Group { export interface Command { name: string; icon: (props: React.ComponentProps<"svg">) => JSX.Element; + action?: () => void; + subItems?: SubItem[]; +} + +export interface SubItem { + name: string; + icon: JSX.Element; action: () => void; } export const CommandPalette = (): JSX.Element => { const [query, setQuery] = useState(""); - const { commandPaletteOpen, setCommandPaletteOpen } = useAppStore(); - // const [open, setOpen] = useState(false); + const { + commandPaletteOpen, + setCommandPaletteOpen, + devices, + setSelectedDevice, + removeDevice, + selectedDevice + } = useAppStore(); + const { getDevices } = useDeviceStore(); const { setQRDialogOpen, @@ -72,7 +88,6 @@ export const CommandPalette = (): JSX.Element => { setActivePage, connection } = useDevice(); - const { setSelectedDevice, removeDevice, selectedDevice } = useAppStore(); const groups: Group[] = [ { @@ -142,11 +157,25 @@ export const CommandPalette = (): JSX.Element => { icon: DevicePhoneMobileIcon, commands: [ { - name: "[WIP] Switch Node", + name: "Switch Node", icon: ArrowsRightLeftIcon, - action() { - alert("This feature is not implemented"); - } + subItems: getDevices().map((device) => { + return { + name: + device.nodes.find( + (n) => n.data.num === device.hardware.myNodeNum + )?.data.user?.longName ?? device.hardware.myNodeNum.toString(), + icon: ( + + ), + action() { + setSelectedDevice(device.id); + } + }; + }) }, { name: "Connect New Node", diff --git a/src/components/CommandPalette/SearchResult.tsx b/src/components/CommandPalette/SearchResult.tsx index 94c80b48..c90330e4 100644 --- a/src/components/CommandPalette/SearchResult.tsx +++ b/src/components/CommandPalette/SearchResult.tsx @@ -16,29 +16,54 @@ export const SearchResult = ({ group }: SearchResultProps): JSX.Element => { {group.name} {group.commands.map((command, index) => ( - - `mr-2 ml-4 flex cursor-pointer select-none items-center rounded-md px-3 py-1 ${ - active ? "bg-gray-900 bg-opacity-5 text-gray-900" : "" - }` - } - > - {({ active }) => ( - <> - - {command.name} - {active && ( - - )} - +
+ + `mr-2 ml-4 flex cursor-pointer select-none items-center rounded-md px-3 py-1 ${ + active ? "bg-gray-900 bg-opacity-5 text-gray-900" : "" + }` + } + > + {({ active }) => ( + <> + + {command.name} + {active && ( + + )} + + )} + + {command.subItems && ( +
+ {command.subItems?.map((item) => ( + + `mx-2 flex cursor-pointer select-none items-center rounded-md px-3 py-1 ${ + active ? "bg-gray-900 bg-opacity-5 text-gray-900" : "" + }` + } + > + {({ active }) => ( + <> + {item.name} + {active && ( + + )} + + )} + + ))} +
)} - +
))} );