import { t } from "@lingui/core/macro"; import { Trans } from "@lingui/react/macro"; import type { Bot } from "@rakazo/contracts"; import { Badge, BotAvatar, Command, CommandDialog, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList, CommandShortcut, Kbd, } from "@rakazo/ui-web"; import { useEffect, useMemo, useState } from "react"; function isApplePlatform() { if (typeof navigator === "undefined") return false; // platform is deprecated but still the most reliable Apple check in browsers. return /Mac|iPhone|iPad|iPod/i.test(navigator.platform || navigator.userAgent); } function botSearchText(bot: Bot) { return `${bot.name} ${bot.title} ${bot.description} ${bot.preview}`.toLowerCase(); } function botSubtitle(bot: Bot) { return bot.description.trim() || bot.preview.trim() || bot.title.trim(); } function botTitleTag(bot: Bot) { const title = bot.title.trim(); if (!title) return null; // Title is already the subtitle when there is no description/preview. if (!(bot.description.trim() || bot.preview.trim())) return null; return title; } export function CommandPalette({ open, onOpenChange, bots, onSelectBot, }: { open: boolean; onOpenChange: (open: boolean) => void; bots: Bot[]; onSelectBot: (botId: string) => void; }) { const [search, setSearch] = useState(""); const [modKey, setModKey] = useState("⌘"); useEffect(() => { setModKey(isApplePlatform() ? "⌘" : "Ctrl"); }, []); useEffect(() => { if (!open) setSearch(""); }, [open]); const filteredBots = useMemo(() => { const needle = search.trim().toLowerCase(); if (!needle) return bots; return bots.filter((bot) => botSearchText(bot).includes(needle)); }, [bots, search]); useEffect(() => { if (!open) return; function onKey(event: KeyboardEvent) { if (!(event.metaKey || event.ctrlKey) || event.altKey || event.shiftKey || event.repeat) { return; } if (!/^[1-9]$/.test(event.key)) return; const bot = filteredBots[Number(event.key) - 1]; if (!bot) return; event.preventDefault(); onSelectBot(bot.id); onOpenChange(false); } window.addEventListener("keydown", onKey); return () => window.removeEventListener("keydown", onKey); }, [filteredBots, onOpenChange, onSelectBot, open]); return ( No conversations {filteredBots.map((bot, index) => { const subtitle = botSubtitle(bot); const titleTag = botTitleTag(bot); const shortcut = index < 9 ? `${modKey}${index + 1}` : null; return ( { onSelectBot(bot.id); onOpenChange(false); }} className="items-center gap-3 rounded-xl! px-2.5 py-2.5" >
{bot.name} {titleTag ? ( {titleTag} ) : null}
{subtitle ? (
{subtitle}
) : null}
{shortcut ? ( {shortcut} ) : null}
); })}
); } export { isCommandPaletteHotkey } from "./command-palette-hotkey";