'use client'; import { Fragment, useEffect, useState } from "react"; import { Combobox, Dialog, Transition } from "@headlessui/react"; import clsx from "../../../utils/shared/ClassesUtils"; import { useRouter } from "next/navigation"; import { useApp } from "../../../hooks"; import { Command } from "../../../application/dtos/layout/Command"; interface Props { isOpen: boolean; onClosed: () => void; } export default function AppCommandPalette({ onClosed, isOpen }: Props) { const { t } = useApp(); const navigate = useRouter(); const commands: Command[] = [ { command: "T", title: t("app.commands.tenants.title"), description: t("app.commands.tenants.description"), }, { command: "P", title: t("app.commands.profile.title"), description: t("app.commands.profile.description"), }, { command: "Z", title: "Switch to App", description: "Go to /app", bgClassName: "bg-indigo-600", textClassName: "text-indigo-200", toPath: "/app", }, ]; const [query, setQuery] = useState(""); const [items, setItems] = useState([]); const [filteredItems, setFilteredItems] = useState([]); const [selectedCommand, setSelectedCommand] = useState(); const [commandSearchTitle, setCommandSearchTitle] = useState(t("app.commands.type")); useEffect(() => { setQuery(""); if (!selectedCommand) { setCommandSearchTitle(t("app.commands.type")); setItems(commands); } else { if (selectedCommand.toPath) { navigate.push(selectedCommand.toPath); onClose(); } else if (selectedCommand.onSelected) { selectedCommand.onSelected(); onClose(); } else { setCommandSearchTitle(`${selectedCommand.title}`); const items: Command[] = []; if (selectedCommand.command === "T") { items.push({ title: `${t("app.commands.tenants.viewAll")}`, description: ``, command: "V", bgClassName: "bg-gray-600", textClassName: "text-white", toPath: "/app", }); items.push({ title: `${t("app.commands.tenants.create")}`, description: ``, command: "+", bgClassName: "bg-teal-600", textClassName: "text-teal-200", toPath: "/new-account", }); } if (selectedCommand.command === "P") { items.push({ title: `${t("app.commands.profile.update")}`, description: `${t("app.commands.profile.updateDescription")}`, command: "U", bgClassName: "bg-pink-600", textClassName: "text-pink-200", toPath: "/admin/profile", }); items.push({ title: `${t("app.commands.profile.logout")}`, description: `${t("app.commands.profile.logoutDescription")}`, command: "L", bgClassName: "bg-gray-600", textClassName: "text-white", toPath: "/logout", }); } setItems(items); } } // eslint-disable-next-line react-hooks/exhaustive-deps }, [selectedCommand]); useEffect(() => { setItems(commands); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); useEffect(() => { if (!query || query.trim() === "") { setFilteredItems(items); } else { setFilteredItems( items.filter( (item) => item.toPath?.toLowerCase().trim().includes(query.toLowerCase().trim()) || item.title.toLowerCase().trim().includes(query.toLowerCase().trim()) || item.description.toLowerCase().trim().includes(query.toLowerCase().trim()) || item.command.trim().toLowerCase() === query.toLowerCase().trim() ) ); } }, [items, query]); // useEffect(() => { // if (!query || query.trim() === "") { // setFilteredItems(items); // } else { // const itemsByCommand = items.filter((f) => f.command.trim().toLowerCase() === query.toLowerCase().trim()); // if (itemsByCommand.length === 1) { // setQuery(""); // setSelectedCommand(itemsByCommand[0]); // setFilteredItems(itemsByCommand); // } else { // setFilteredItems( // itemsByCommand.filter( // (item) => // item.title.toLowerCase().trim().includes(query.toLowerCase().trim()) || // item.description.toLowerCase().trim().includes(query.toLowerCase().trim()) || // item.command.trim().toLowerCase() === query.toLowerCase().trim() // ) // ); // } // } // }, [items, query]); function onChange(value: any) { setSelectedCommand(value as Command); } function onClose() { setSelectedCommand(undefined); setItems(commands); onClosed(); } return ( <> setQuery("")}>
setQuery(event.target.value)} autoComplete="off" />
{filteredItems.length > 0 && ( {filteredItems.map((item) => ( clsx("flex cursor-pointer select-none rounded-xl p-3", active && "bg-gray-100")} > {({ active }) => (
{/*
{item.command}
*/}

{item.title}

{item.description}

)}
))} {/* clsx("flex cursor-default select-none rounded-xl p-3", active && "bg-gray-100")}> {({ active }) => (

New tenant

Create a new organization

)}
*/}
)} {query !== "" && filteredItems.length === 0 && (

{t("app.commands.empty.title")}

{t("app.commands.empty.description")}

)} {/*
Create an organization
*/}
); }