-
-
Notifications
You must be signed in to change notification settings - Fork 568
feat(editor): add cmdk global search #240
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
SwasthK
wants to merge
5
commits into
jnsahaj:main
Choose a base branch
from
SwasthK:feat/cmdk
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,254 @@ | ||
| "use client"; | ||
|
|
||
| import * as React from "react"; | ||
| import { Check, CornerDownLeft, Search } from "lucide-react"; | ||
| import { useRouter } from "next/navigation"; | ||
| import { useCallback, useEffect, useMemo, useState } from "react"; | ||
| import { Input } from "@/components/ui/input"; | ||
| import { Kbd } from "./ui/kbd"; | ||
| import { Button } from "./ui/button"; | ||
| import { useEditorStore } from "@/store/editor-store"; | ||
| import { useThemePresetStore } from "@/store/theme-preset-store"; | ||
| import { authClient } from "@/lib/auth-client"; | ||
| import { Badge } from "./ui/badge"; | ||
| import { isThemeNew } from "@/utils/search/is-theme-new"; | ||
| import { ThemeColors } from "./search/theme-colors"; | ||
| import { NAVIGATION_ITEMS } from "@/utils/search/constants/navigation"; | ||
| import { filterPresets } from "@/utils/search/filter-presets"; | ||
| import { sortThemes } from "@/utils/search/sort-themes"; | ||
| import { | ||
| CommandDialog, | ||
| CommandEmpty, | ||
| CommandGroup, | ||
| CommandItem, | ||
| CommandList, | ||
| CommandSeparator, | ||
| } from "@/components/ui/command"; | ||
| import { Tooltip, TooltipContent, TooltipTrigger } from "./ui/tooltip"; | ||
|
|
||
| export function CmdK() { | ||
| const [open, setOpen] = useState(false); | ||
| const [search, setSearch] = useState(""); | ||
| const router = useRouter(); | ||
|
|
||
| const themeState = useEditorStore((store) => store.themeState); | ||
| const applyThemePreset = useEditorStore((store) => store.applyThemePreset); | ||
| const currentPreset = themeState.preset; | ||
| const mode = themeState.currentMode; | ||
|
|
||
| const presets = useThemePresetStore((store) => store.getAllPresets()); | ||
| const loadSavedPresets = useThemePresetStore((store) => store.loadSavedPresets); | ||
| const unloadSavedPresets = useThemePresetStore((store) => store.unloadSavedPresets); | ||
|
|
||
| const { data: session } = authClient.useSession(); | ||
|
|
||
| useEffect(() => { | ||
| if (session?.user) { | ||
| loadSavedPresets(); | ||
| } else { | ||
| unloadSavedPresets(); | ||
| } | ||
| }, [loadSavedPresets, unloadSavedPresets, session?.user]); | ||
|
|
||
| const isSavedTheme = useCallback( | ||
| (presetId: string) => { | ||
| return presets[presetId]?.source === "SAVED"; | ||
| }, | ||
| [presets] | ||
| ); | ||
|
|
||
| const presetNames = useMemo(() => Array.from(new Set(["default", ...Object.keys(presets)])), [presets]); | ||
|
|
||
| const filteredPresets = useMemo(() => { | ||
| const filteredList = filterPresets(presetNames, presets, search); | ||
|
|
||
| // Separate saved and default themes | ||
| const savedThemesList = filteredList.filter((name) => name !== "default" && isSavedTheme(name)); | ||
| const defaultThemesList = filteredList.filter((name) => !savedThemesList.includes(name)); | ||
|
|
||
| return [...sortThemes(savedThemesList, presets), ...sortThemes(defaultThemesList, presets)]; | ||
| }, [presetNames, search, presets, isSavedTheme]); | ||
|
|
||
| const filteredSavedThemes = useMemo(() => { | ||
| return filteredPresets.filter((name) => name !== "default" && isSavedTheme(name)); | ||
| }, [filteredPresets, isSavedTheme]); | ||
|
|
||
| const filteredDefaultThemes = useMemo(() => { | ||
| return filteredPresets.filter((name) => name === "default" || !isSavedTheme(name)); | ||
| }, [filteredPresets, isSavedTheme]); | ||
|
|
||
| const filteredNavigation = useMemo(() => { | ||
| if (search.trim() === "") { | ||
| return NAVIGATION_ITEMS; | ||
| } | ||
| const searchLower = search.toLowerCase(); | ||
| return NAVIGATION_ITEMS.filter((item) => { | ||
| const matchesLabel = item.label.toLowerCase().includes(searchLower); | ||
| const matchesKeywords = item.keywords?.some((keyword) => | ||
| keyword.toLowerCase().includes(searchLower) | ||
| ); | ||
| return matchesLabel || matchesKeywords; | ||
| }); | ||
| }, [search]); | ||
|
|
||
| useEffect(() => { | ||
| const down = (e: KeyboardEvent) => { | ||
| if (e.key === "k" && (e.metaKey || e.ctrlKey)) { | ||
| e.preventDefault(); | ||
| setOpen((open) => !open); | ||
| } | ||
| }; | ||
|
|
||
| document.addEventListener("keydown", down); | ||
| return () => document.removeEventListener("keydown", down); | ||
| }, []); | ||
|
|
||
| const onThemeSelect = (presetName: string) => { | ||
| applyThemePreset(presetName); | ||
| setOpen(false); | ||
| setSearch(""); | ||
| }; | ||
|
|
||
| const onNavigationSelect = (href: string) => { | ||
| router.push(href); | ||
| setOpen(false); | ||
| setSearch(""); | ||
| }; | ||
|
|
||
| return ( | ||
| <> | ||
| <Tooltip> | ||
| <TooltipTrigger asChild> | ||
| <Button | ||
| variant={"outline"} | ||
| className="flex h-8 items-center justify-between gap-6" | ||
| onClick={() => setOpen(true)} | ||
| aria-label="Open search" | ||
| > | ||
| <Search className="size-4" aria-hidden="true" /> | ||
| </Button> | ||
| </TooltipTrigger> | ||
| <TooltipContent> | ||
| <p>Search (⌘K)</p> | ||
| </TooltipContent> | ||
| </Tooltip> | ||
| <CommandDialog open={open} onOpenChange={setOpen} title="Search"> | ||
| <div className="bg-card m-1 rounded-md"> | ||
| <div className="mb-2 flex items-center border-b px-3"> | ||
| <Search className="mr-2 h-4 w-4 shrink-0 opacity-50" /> | ||
| <Input | ||
| placeholder="Search themes, pages, and more..." | ||
| className="border-0 shadow-none focus-visible:ring-0 focus-visible:ring-offset-0" | ||
| value={search} | ||
| onChange={(e) => setSearch(e.target.value)} | ||
| /> | ||
| </div> | ||
| <CommandList className="mb-2 h-80 p-0 [-ms-overflow-style:none] [scrollbar-width:none] [&::-webkit-scrollbar]:hidden"> | ||
| <CommandEmpty className="flex items-center justify-center py-32"> | ||
| <p>No results found.</p> | ||
| </CommandEmpty> | ||
|
|
||
| {/* Navigation Group */} | ||
| {filteredNavigation.length > 0 && ( | ||
| <> | ||
| <CommandGroup heading="Navigation" className=""> | ||
| {filteredNavigation.map((item) => { | ||
| const Icon = item.icon; | ||
| return ( | ||
| <CommandItem | ||
| key={item.id} | ||
| value={item.id} | ||
| onSelect={() => onNavigationSelect(item.href)} | ||
| className="flex items-center gap-2" | ||
| > | ||
| <Icon className="h-3 w-3 shrink-0" /> | ||
| <span className="text-sm">{item.label}</span> | ||
| </CommandItem> | ||
| ); | ||
| })} | ||
| </CommandGroup> | ||
| {(filteredSavedThemes.length > 0 || filteredDefaultThemes.length > 0) && ( | ||
| <CommandSeparator /> | ||
| )} | ||
| </> | ||
| )} | ||
|
|
||
| {/* Saved Themes Group */} | ||
| {filteredSavedThemes.length > 0 && ( | ||
| <> | ||
| <CommandGroup heading="Saved Themes"> | ||
| {filteredSavedThemes.map((presetName, index) => ( | ||
| <CommandItem | ||
| key={`${presetName}-${index}`} | ||
| value={`${presetName}-${index}`} | ||
| onSelect={() => onThemeSelect(presetName)} | ||
| className="flex items-center gap-2" | ||
| > | ||
| <ThemeColors presetName={presetName} mode={mode} /> | ||
|
|
||
| <div className="flex flex-1 items-center gap-2"> | ||
| <span className="line-clamp-1 text-sm font-medium capitalize"> | ||
| {presets[presetName]?.label || presetName} | ||
| </span> | ||
| {presets[presetName] && isThemeNew(presets[presetName]) && ( | ||
| <Badge variant="secondary" className="rounded-full text-xs"> | ||
| New | ||
| </Badge> | ||
| )} | ||
| </div> | ||
| {presetName === currentPreset && ( | ||
| <Check className="h-4 w-4 shrink-0 opacity-70" /> | ||
| )} | ||
| </CommandItem> | ||
| ))} | ||
| </CommandGroup> | ||
| <CommandSeparator /> | ||
| </> | ||
| )} | ||
|
|
||
| {/* Built-in Themes Group */} | ||
| {filteredDefaultThemes.length > 0 && ( | ||
| <CommandGroup heading="Built-in Themes"> | ||
| {filteredDefaultThemes.map((presetName, index) => ( | ||
| <CommandItem | ||
| key={`${presetName}-${index}`} | ||
| value={`${presetName}-${index}`} | ||
| onSelect={() => onThemeSelect(presetName)} | ||
| className="flex items-center gap-2" | ||
| > | ||
| <ThemeColors presetName={presetName} mode={mode} /> | ||
| <div className="flex flex-1 items-center gap-2"> | ||
| <span className="line-clamp-1 text-sm font-medium capitalize"> | ||
| {presets[presetName]?.label || presetName} | ||
| </span> | ||
| {presets[presetName] && isThemeNew(presets[presetName]) && ( | ||
| <Badge variant="secondary" className="rounded-full text-xs"> | ||
| New | ||
| </Badge> | ||
| )} | ||
| </div> | ||
| {presetName === currentPreset && ( | ||
| <Check className="h-4 w-4 shrink-0 opacity-70" /> | ||
| )} | ||
| </CommandItem> | ||
| ))} | ||
| </CommandGroup> | ||
| )} | ||
| </CommandList> | ||
| </div> | ||
| <CommandFooter /> | ||
| </CommandDialog> | ||
| </> | ||
| ); | ||
| } | ||
|
|
||
| const CommandFooter = () => { | ||
| return ( | ||
| <div className="text-muted-foreground flex items-center gap-2 px-6 py-2 text-xs"> | ||
| <Kbd className="rounded-[4px] px-1"> | ||
| <CornerDownLeft /> | ||
| </Kbd> | ||
| <p className="font-medium">Go to page</p> | ||
| </div> | ||
| ); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🧩 Analysis chain
Harden
filterPresetsagainst missing labelsBecause
filterPresetsultimately doespresets[name]?.label?.toLowerCase().includes(...), any preset without alabelwill cause a runtime error when searching. GivenThemePreset.labelis optional, this is a plausible edge case.You can guard against missing labels inside
filterPresets(file:lib/search/filter-presets.ts) by short‑circuiting:Since CmdK relies on this helper for every keystroke, a defensive check here is worthwhile.
Length of output: 79
🏁 Script executed:
Length of output: 77
🏁 Script executed:
Length of output: 599
🏁 Script executed:
Length of output: 72
Length of output: 1974
🏁 Script executed:
Length of output: 249
🏁 Script executed:
Length of output: 4532
🏁 Script executed:
Length of output: 8723
Guard against missing labels in
filterPresetsThe current implementation has a runtime error: when
presets[name]?.labelis undefined (sinceThemePreset.labelis optional pertypes/theme.ts:104), calling.toLowerCase()returnsundefined, then.includes()fails withTypeError.Fix in
lib/search/filter-presets.ts:11by short-circuiting when label is missing:🤖 Prompt for AI Agents