diff --git a/dashboard/src/features/search-command/command-box.tsx b/dashboard/src/features/search-command/command-box.tsx index 4bf600d..bff7ebf 100644 --- a/dashboard/src/features/search-command/command-box.tsx +++ b/dashboard/src/features/search-command/command-box.tsx @@ -1,29 +1,19 @@ -import { - UsersIcon, - ServerIcon, - BoxIcon, - ServerCogIcon, - SettingsIcon, -} from "lucide-react"; import { useState, useEffect } from "react"; -import { useTranslation } from "react-i18next"; import { CommandDialog, CommandEmpty, - CommandGroup, CommandInput, - CommandItem, CommandList, - CommandSeparator, + ScrollArea, } from "@marzneshin/components"; -import { useNavigate } from "@tanstack/react-router"; import { SearchBox } from "./search-box"; import { useAuth } from "@marzneshin/features/auth"; +import { CommandItems } from "./command-items"; +import { commandItems } from "./commands"; export function CommandBox() { const [open, setOpen] = useState(false); - const { t } = useTranslation(); - const navigate = useNavigate(); + const { isSudo } = useAuth(); useEffect(() => { const down = (e: KeyboardEvent) => { @@ -36,8 +26,6 @@ export function CommandBox() { return () => document.removeEventListener("keydown", down); }, []); - const { isSudo } = useAuth(); - return ( <> setOpen(true)} /> @@ -46,68 +34,15 @@ export function CommandBox() { placeholder="Type a command or search..." className="focus:ring-0 ring-0 m-1 border-none" /> - - No results found. - - { - navigate({ to: "/users" }); - setOpen(false); - }} - > - - {t("users")} - - {isSudo() && ( - <> - { - navigate({ to: "/services" }); - setOpen(false); - }} - > - - {t("services")} - - { - navigate({ to: "/nodes" }); - setOpen(false); - }} - > - - {t("nodes")} - - { - navigate({ to: "/hosts" }); - setOpen(false); - }} - > - - {t("hosts")} - - - )} - - {isSudo() && ( - <> - - - { - navigate({ to: "/settings" }); - setOpen(false); - }} - > - - {t("settings")} - - - - )} - + + + No results found. + + + ); } + + diff --git a/dashboard/src/features/search-command/command-items.tsx b/dashboard/src/features/search-command/command-items.tsx new file mode 100644 index 0000000..7874d96 --- /dev/null +++ b/dashboard/src/features/search-command/command-items.tsx @@ -0,0 +1,47 @@ +// CommandItems.tsx +import React, { FC } from "react"; +import { + CommandGroup, + CommandItem, + CommandSeparator, +} from "@marzneshin/components"; +import { useNavigate } from "@tanstack/react-router"; +import { useTranslation } from "react-i18next"; +import { CommandGroupConfig, CommandItemConfig } from "./commands"; + +interface CommandItemsProps { + items: CommandGroupConfig[]; + isSudo: () => boolean; + setOpen: (open: boolean) => void; +} + +export const CommandItems: FC = ({ items, isSudo, setOpen }) => { + const navigate = useNavigate(); + const { t } = useTranslation(); + + return ( + <> + {items.map((group: CommandGroupConfig, groupIndex: number) => ( + + + {group.items.map((item: CommandItemConfig) => ( + (!item.sudo || isSudo()) && ( + { + navigate({ to: item.path }); + setOpen(false); + }} + > + + {t(item.label)} + + ) + ))} + + {groupIndex < items.length - 1 && } + + ))} + + ); +}; diff --git a/dashboard/src/features/search-command/commands.ts b/dashboard/src/features/search-command/commands.ts new file mode 100644 index 0000000..d3e474f --- /dev/null +++ b/dashboard/src/features/search-command/commands.ts @@ -0,0 +1,99 @@ +import { + UsersIcon, + ServerIcon, + BoxIcon, + ServerCogIcon, + SettingsIcon, + LucideIcon, + ShieldIcon, +} from "lucide-react"; + +export interface CommandItemConfig { + icon: LucideIcon; + label: string; + path: string; + sudo: boolean; +} + +export interface CommandGroupConfig { + group: string; + items: CommandItemConfig[]; +} + +export const commandItems: CommandGroupConfig[] = [ + { + group: "Pages", + items: [ + { + icon: UsersIcon, + label: "users", + path: "/users", + sudo: false, + }, + { + icon: ServerIcon, + label: "services", + path: "/services", + sudo: true, + }, + { + icon: BoxIcon, + label: "nodes", + path: "/nodes", + sudo: true, + }, + { + icon: ServerCogIcon, + label: "hosts", + path: "/hosts", + sudo: true, + }, + { + icon: ShieldIcon, + label: "admins", + path: "/admins", + sudo: true, + }, + ], + }, + { + group: "Actions", + items: [ + { + icon: UsersIcon, + label: "page.users.dialogs.creation.title", + path: "/users/create", + sudo: false, + }, + { + icon: ServerIcon, + label: "page.services.dialogs.creation.title", + path: "/services/create", + sudo: true, + }, + { + icon: BoxIcon, + label: "page.nodes.dialogs.creation.title", + path: "/nodes/create", + sudo: true, + }, + { + icon: ShieldIcon, + label: "page.admins.dialogs.creation.title", + path: "/admins/create", + sudo: true, + }, + ], + }, + { + group: "Settings", + items: [ + { + icon: SettingsIcon, + label: "settings", + path: "/settings", + sudo: true, + }, + ], + }, +];