Skip to content

Commit

Permalink
feat: add command menu
Browse files Browse the repository at this point in the history
  • Loading branch information
MattKetmo committed Nov 18, 2024
1 parent eb5de5f commit c903fdc
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 2 deletions.
70 changes: 70 additions & 0 deletions src/components/command/command-menu.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
'use client'

import { useEffect, useState } from "react"
import { CommandDialog, CommandEmpty, CommandGroup, CommandInput, CommandItem, CommandList } from "@/components/ui/command"
import { useRouter } from "next/navigation"
import { Bell, ChevronRight, Settings2, SquareDot } from "lucide-react"
import { useConfig } from "@/contexts/config"

const navigation = [
{
title: "Alerts",
url: "/alerts",
icon: Bell,
},
// {
// title: "Silences",
// url: "/silences",
// icon: CircleSlash2,
// },
{
title: "Settings",
url: "/settings",
icon: Settings2,
},
]

export function CommandMenu() {
const [open, setOpen] = useState(false)
const router = useRouter()
const { config } = useConfig()
const { views, viewCategories } = config

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)
}, [])

return (
<CommandDialog open={open} onOpenChange={setOpen}>
<CommandInput placeholder="Type a command or search..." />
<CommandList>
<CommandEmpty>No results found.</CommandEmpty>
<CommandGroup heading="Suggestions">
{navigation.map((item) => (
<CommandItem key={item.url} onSelect={() => { router.push(item.url); setOpen(false) }}>
<item.icon className="w-6 h-6 mr-2" />
{item.title}
</CommandItem>
))}
{Object.entries(views)
.filter(([handle]) => handle !== "default")
.map(([handle, view]) => (
<CommandItem key={handle} onSelect={() => { router.push(`/alerts/${handle}`); setOpen(false) }}>
<SquareDot className="w-6 h-6 mr-2" />
{viewCategories[view.category]?.name ? `${viewCategories[view.category].name}` : view.category}
{view.category ? <span className="px-1">»</span> : ''}
{view.name || handle}
</CommandItem>
))}
</CommandGroup>
</CommandList>
</CommandDialog>
)
}
2 changes: 2 additions & 0 deletions src/components/layout/app-layout.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { SidebarProvider } from "@/components/ui/sidebar";
import { CommandMenu } from "@/components/command/command-menu";
import { AppSidebar } from "./app-sidebar";
import { AppThemeColor } from "./app-theme-color";

Expand All @@ -7,6 +8,7 @@ export function AppLayout({ children }: { children: React.ReactNode }) {
<SidebarProvider>
<AppThemeColor />
<AppSidebar />
<CommandMenu />
<main className="grow relative">
<div className="absolute inset-0">
{children}
Expand Down
3 changes: 2 additions & 1 deletion src/components/ui/command.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"use client"

import * as React from "react"
import { type DialogProps } from "@radix-ui/react-dialog"
import { DialogTitle, type DialogProps } from "@radix-ui/react-dialog"
import { Command as CommandPrimitive } from "cmdk"
import { Search } from "lucide-react"

Expand All @@ -26,6 +26,7 @@ Command.displayName = CommandPrimitive.displayName
const CommandDialog = ({ children, ...props }: DialogProps) => {
return (
<Dialog {...props}>
<DialogTitle className="sr-only">Command Menu</DialogTitle>
<DialogContent className="overflow-hidden p-0 shadow-lg">
<Command className="[&_[cmdk-group-heading]]:px-2 [&_[cmdk-group-heading]]:font-medium [&_[cmdk-group-heading]]:text-muted-foreground [&_[cmdk-group]:not([hidden])_~[cmdk-group]]:pt-0 [&_[cmdk-group]]:px-2 [&_[cmdk-input-wrapper]_svg]:h-5 [&_[cmdk-input-wrapper]_svg]:w-5 [&_[cmdk-input]]:h-12 [&_[cmdk-item]]:px-2 [&_[cmdk-item]]:py-3 [&_[cmdk-item]_svg]:h-5 [&_[cmdk-item]_svg]:w-5">
{children}
Expand Down
2 changes: 1 addition & 1 deletion src/components/ui/dialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const DialogOverlay = React.forwardRef<
<DialogPrimitive.Overlay
ref={ref}
className={cn(
"fixed inset-0 z-50 bg-black/80 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
"fixed inset-0 z-50 bg-black/30 data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:fade-out-0 data-[state=open]:fade-in-0",
className
)}
{...props}
Expand Down

0 comments on commit c903fdc

Please sign in to comment.