import { Fragment, useEffect, useState } from "react"; import { Combobox, Dialog, Transition } from "@headlessui/react"; import UrlUtils from "../../../utils/app/UrlUtils"; import { Command } from "../../../application/dtos/layout/Command"; import { useApp } from "../../../hooks"; import clsx from "../../../utils/shared/ClassesUtils"; import { useParams, useRouter } from "next/navigation"; interface Props { isOpen: boolean; onClosed: () => void; } export function AppCommandPalette({ onClosed, isOpen }: Props) { const { t } = useApp(); const appData: any = {};//useAppData(); const navigate = useRouter(); const params = useParams(); 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 Admin", description: "Go to the admin panel", bgClassName: "bg-indigo-600", textClassName: "text-indigo-200", toPath: "/admin/dashboard", adminOnly: true, }, ]; 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(() => { setItems(getAllowedCommands(commands)); // eslint-disable-next-line react-hooks/exhaustive-deps }, []); useEffect(() => { setQuery(""); if (!selectedCommand) { setCommandSearchTitle(t("app.commands.type")); setItems(getAllowedCommands(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") { let idx = 0; appData.myTenants.forEach((tenant) => { items.push({ title: `${t("app.commands.tenants.switchTo")} ${tenant.name}`, description: tenant.name, command: (++idx).toString(), bgClassName: "bg-indigo-600", textClassName: "text-indigo-200", toPath: `/app/${tenant.slug}/dashboard`, }); }); // 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.edit")}`, // description: ``, // command: "V", // bgClassName: "bg-gray-600", // textClassName: "text-white", // toPath: UrlUtils.currentTenantUrl(params, "settings/account"), // }); 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: UrlUtils.currentTenantUrl(params, "settings/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(getAllowedCommands(items)); } } // eslint-disable-next-line react-hooks/exhaustive-deps }, [selectedCommand]); 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.command.trim().toLowerCase() === query.toLowerCase().trim() || // item.title.toLowerCase().trim().includes(query.toLowerCase().trim()) || // item.description.toLowerCase().trim().includes(query.toLowerCase().trim()) // ) // ); // } // } // }, [items, query]); function getAllowedCommands(items: Command[]) { return items.filter((f) => !f.adminOnly || appData.user?.admin); } function onChange(value: any) { setSelectedCommand(value as Command); } function onClose() { setSelectedCommand(undefined); setItems(getAllowedCommands(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
*/}
); }