"use client"; import { usePathname, useRouter } from "next/navigation"; import { useTheme } from "next-themes"; import { useEffect, useMemo, useRef, useState, type ReactNode } from "react"; import { LiquidGlass } from "@/components/glass/liquid-glass"; import { useLocale, useT } from "@/components/locale-provider"; import { siteConfig } from "@/config/site"; import { localePath, stripLocale } from "@/lib/i18n"; import { switchTheme } from "@/lib/theme-transition"; import { cn } from "@/lib/utils"; import type { View } from "@/lib/view-store"; type Command = { id: string; group: string; label: string; hint?: string; keywords?: string; icon: ReactNode; run: () => void; /** Shown greyed out and inert; used for empty placeholder rows. */ disabled?: boolean; }; const Dot = ({ className }: { className?: string }) => ( ); function score(query: string, text: string) { if (!query) return 1; const q = query.toLowerCase(); const t = text.toLowerCase(); if (t.includes(q)) return 2 - t.indexOf(q) / 100; let i = 0; for (const ch of t) if (ch === q[i]) i++; return i === q.length ? 0.5 : 0; } export default function CommandPalette({ onClose, onSelectTab, }: { onClose: () => void; onSelectTab: (id: View | "blog") => void; }) { const router = useRouter(); const lang = useLocale(); const { palette: t } = useT(); const pathname = usePathname(); const { resolvedTheme, setTheme } = useTheme(); const [query, setQuery] = useState(""); const [active, setActive] = useState(0); const [toast, setToast] = useState(null); const inputRef = useRef(null); const listRef = useRef(null); const commands = useMemo(() => { const go = (id: View | "blog") => () => { onSelectTab(id); onClose(); }; const open = (href: string) => () => { window.open(href, "_blank", "noopener,noreferrer"); onClose(); }; const fire = (event: string, view: View = "about") => () => { onSelectTab(view); onClose(); window.setTimeout( () => window.dispatchEvent(new CustomEvent(event)), 450, ); }; return [ { id: "all", group: t.groups.navigate, label: t.everything, hint: t.allCards, icon: , run: go("all"), }, { id: "about", group: t.groups.navigate, label: t.about, icon: , run: go("about"), }, { id: "work", group: t.groups.navigate, label: t.work, keywords: "aws amazon finops q artifacts side projects packbook secondself", icon: , run: go("work"), }, { id: "blog", group: t.groups.navigate, label: t.blog, keywords: "writing posts", icon: , run: go("blog"), }, // No projects yet: one inert row so the group still reads as a section. { id: "no-projects", group: t.groups.projects, label: t.noProjects, icon: , disabled: true, run: () => {}, }, { id: "email", group: t.groups.connect, label: t.copyEmail, hint: siteConfig.email, keywords: "mail contact", icon: , run: () => { navigator.clipboard?.writeText(siteConfig.email).then( () => setToast(t.emailCopied), () => setToast(siteConfig.email), ); window.setTimeout(onClose, 900); }, }, { id: "theme", group: t.groups.settings, label: resolvedTheme === "dark" ? t.lightsOn : t.lightsOff, keywords: "theme dark light mode", icon: , run: () => { onClose(); switchTheme(resolvedTheme === "dark" ? "light" : "dark", setTheme, { x: window.innerWidth / 2, y: window.innerHeight * 0.2, }); }, }, { id: "language", group: t.groups.settings, label: t.language, hint: t.languageHint, keywords: "language chinese english 语言 中文 英文", icon: , run: () => { // The other language is a different root layout: a full load. window.location.assign( localePath(lang === "en" ? "zh" : "en", stripLocale(pathname)) + window.location.hash, ); }, }, { id: "bert", group: t.groups.fun, label: t.bert, hint: t.bertHint, keywords: "cat avatar", icon: , run: fire("eric:bert"), }, { id: "mini", group: t.groups.fun, label: t.mini, keywords: "car three", icon: , run: fire("eric:mini"), }, { id: "home", group: t.groups.fun, label: t.top, icon: , run: () => { router.push(localePath(lang, "/")); window.scrollTo({ top: 0, behavior: "smooth" }); onClose(); }, }, ]; }, [ lang, onClose, onSelectTab, pathname, resolvedTheme, router, setTheme, t, ]); const results = useMemo( () => commands .map((c) => ({ c, s: score(query, `${c.label} ${c.keywords ?? ""} ${c.group}`), })) .filter((r) => r.s > 0) .sort((a, b) => b.s - a.s) .map((r) => r.c), [commands, query], ); useEffect(() => { const previous = document.activeElement as HTMLElement | null; // Hand focus back only to someone navigating by keyboard; otherwise a // mouse-clicked control would light up with a focus ring on close. const restore = previous?.matches?.(":focus-visible") ?? false; inputRef.current?.focus(); const onKey = (e: KeyboardEvent) => { if (e.key === "Escape") onClose(); }; window.addEventListener("keydown", onKey); return () => { window.removeEventListener("keydown", onKey); if (restore) previous?.focus?.(); }; }, [onClose]); useEffect(() => { listRef.current ?.querySelector(`[data-index="${active}"]`) ?.scrollIntoView({ block: "nearest" }); }, [active]); return (
{ if (e.target === e.currentTarget) onClose(); }} > {/* A sibling, not a parent: refraction nested inside another backdrop filter renders wrong in Chromium. */}
{ setQuery(e.target.value); setActive(0); }} onKeyDown={(e) => { if (e.key === "ArrowDown") { e.preventDefault(); setActive((i) => Math.min(results.length - 1, i + 1)); } else if (e.key === "ArrowUp") { e.preventDefault(); setActive((i) => Math.max(0, i - 1)); } else if (e.key === "Enter") { e.preventDefault(); const picked = results[active]; if (picked && !picked.disabled) picked.run(); } }} /> esc
{results.length === 0 && (

{t.empty(query)}

)} {results.map((c, i) => { const header = i === 0 || results[i - 1].group !== c.group; return (
{header && !query && (

{c.group}

)}
setActive(i)} > {c.icon} {c.label} {c.hint && ( {c.hint} )}
); })}
{t.help} {toast ?? "ericwu.me"}
); }