"use client"; import { 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 { projects, siteConfig } from "@/config/site"; 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; }; const Arrow = () => ( ); 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 { 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: "Navigate", label: "Everything", hint: "All cards", icon: , run: go("all"), }, { id: "about", group: "Navigate", label: "About me", icon: , run: go("about"), }, { id: "work", group: "Navigate", label: "Work and projects", keywords: "aws amazon finops q artifacts side projects packbook secondself", icon: , run: go("work"), }, { id: "blog", group: "Navigate", label: "Read the blog", keywords: "writing posts", icon: , run: go("blog"), }, ...Object.values(projects).map((p) => ({ id: p.id, group: "Projects", label: p.name, hint: p.linkLabel, keywords: p.kicker, icon: , run: open(p.href), })), { id: "github", group: "Connect", label: "GitHub", hint: "@itsEricWu", icon: , run: open(siteConfig.links.github), }, { id: "linkedin", group: "Connect", label: "LinkedIn", icon: , run: open(siteConfig.links.linkedin), }, { id: "email", group: "Connect", label: "Copy email address", hint: siteConfig.email, keywords: "mail contact", icon: , run: () => { navigator.clipboard?.writeText(siteConfig.email).then( () => setToast("Email copied"), () => setToast(siteConfig.email), ); window.setTimeout(onClose, 900); }, }, { id: "theme", group: "Settings", label: resolvedTheme === "dark" ? "Turn the lights on" : "Turn the lights off", keywords: "theme dark light mode", icon: , run: () => { onClose(); switchTheme(resolvedTheme === "dark" ? "light" : "dark", setTheme, { x: window.innerWidth / 2, y: window.innerHeight * 0.2, }); }, }, { id: "bert", group: "Fun", label: "Meet Bert", hint: "the dog", keywords: "dog avatar", icon: , run: fire("eric:bert"), }, { id: "mini", group: "Fun", label: "Spin up the Mini in 3D", keywords: "car three", icon: , run: fire("eric:mini"), }, { id: "home", group: "Fun", label: "Back to the top", icon: , run: () => { router.push("/"); window.scrollTo({ top: 0, behavior: "smooth" }); onClose(); }, }, ]; }, [onClose, onSelectTab, resolvedTheme, router, setTheme]); 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(); results[active]?.run(); } }} /> esc
{results.length === 0 && (

Nothing matches “{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} )}
); })}
↑↓ to move · ↵ to open {toast ?? "ericwu.me"}
); }