|
| 1 | +import * as Dialog from "@radix-ui/react-dialog"; |
| 2 | +import { Command } from "cmdk"; |
| 3 | +import Mousetrap from "mousetrap"; |
| 4 | +import Image from "next/image"; |
| 5 | +import React from "react"; |
| 6 | +import { Kbd } from "../components/kbd"; |
| 7 | +import { SupernovaCommand } from "../types/command"; |
| 8 | +import { ISupernovaTask } from "../types/supernova-task"; |
| 9 | +import { twMerge } from "tailwind-merge"; |
| 10 | +import { ibmPlexMono } from "../components/fonts"; |
| 11 | + |
| 12 | +export const SupernovaCommandCenter = ({ |
| 13 | + commands, |
| 14 | + context, |
| 15 | +}: { |
| 16 | + commands: SupernovaCommand[]; |
| 17 | + context: { |
| 18 | + chosenTask: ISupernovaTask | null; |
| 19 | + }; |
| 20 | +}) => { |
| 21 | + const [open, setOpen] = React.useState(false); |
| 22 | + |
| 23 | + React.useEffect(() => { |
| 24 | + // open the menu when ⌘K is pressed |
| 25 | + const down = (e: Mousetrap.ExtendedKeyboardEvent) => { |
| 26 | + e.preventDefault(); |
| 27 | + setOpen((open) => !open); |
| 28 | + }; |
| 29 | + |
| 30 | + Mousetrap.bind("mod+k", down); |
| 31 | + return () => { |
| 32 | + Mousetrap.unbind("mod+k"); |
| 33 | + }; |
| 34 | + }, []); |
| 35 | + |
| 36 | + return ( |
| 37 | + <Dialog.Root open={open} onOpenChange={setOpen}> |
| 38 | + <Dialog.Portal> |
| 39 | + <Dialog.Overlay className={`bg-gray-400 opacity-50 fixed inset-0`} /> |
| 40 | + <Dialog.Content |
| 41 | + className={`data-[state=open]:animate-contentShow fixed top-[50%] left-[50%] max-h-[85vh] w-[90vw] max-w-[450px] translate-x-[-50%] translate-y-[-50%] rounded-[6px] shadow-[hsl(206_22%_7%_/_35%)_0px_10px_38px_-10px,_hsl(206_22%_7%_/_20%)_0px_10px_20px_-15px] focus:outline-none`} |
| 42 | + > |
| 43 | + <Command className="bg-white rounded-lg p-4 flex flex-col gap-2"> |
| 44 | + {context.chosenTask !== null && ( |
| 45 | + <div> |
| 46 | + <p |
| 47 | + className={twMerge( |
| 48 | + "text-xs text-slate-300", |
| 49 | + ibmPlexMono.className |
| 50 | + )} |
| 51 | + > |
| 52 | + {">"}{" "} |
| 53 | + {context.chosenTask !== null |
| 54 | + ? '"' + context.chosenTask.title + '"' |
| 55 | + : "No task selected"} |
| 56 | + </p> |
| 57 | + </div> |
| 58 | + )} |
| 59 | + <div className="flex items-center gap-2"> |
| 60 | + <Image |
| 61 | + src={"/supernova-globe.svg"} |
| 62 | + width={20} |
| 63 | + height={20} |
| 64 | + alt="Supernova's logo" |
| 65 | + priority |
| 66 | + /> |
| 67 | + <Command.Input |
| 68 | + placeholder="Find a command..." |
| 69 | + className="outline-none" |
| 70 | + autoFocus |
| 71 | + /> |
| 72 | + </div> |
| 73 | + <Command.Separator alwaysRender className="h-[1px] bg-gray-300" /> |
| 74 | + <Command.List className="flex flex-col gap-2"> |
| 75 | + <Command.Empty className="text-slate-300 text-center"> |
| 76 | + No results found. |
| 77 | + </Command.Empty> |
| 78 | + |
| 79 | + {commands.map((command) => ( |
| 80 | + <Command.Item |
| 81 | + key={command.label} |
| 82 | + className=" data-[selected='true']:bg-slate-100 flex items-center gap-2 justify-between hover:bg-slate-200 rounded-md px-2 py-1" |
| 83 | + onSelect={() => { |
| 84 | + setOpen(false); |
| 85 | + command.cb(); |
| 86 | + }} |
| 87 | + > |
| 88 | + <p>{command.label}</p> |
| 89 | + {Array.isArray(command.shortcut) ? ( |
| 90 | + <div className="flex items-center gap-2"> |
| 91 | + {command.shortcut.map((key) => ( |
| 92 | + <Kbd key={key}>{key}</Kbd> |
| 93 | + ))} |
| 94 | + </div> |
| 95 | + ) : ( |
| 96 | + <Kbd>{command.shortcut}</Kbd> |
| 97 | + )} |
| 98 | + </Command.Item> |
| 99 | + ))} |
| 100 | + </Command.List> |
| 101 | + </Command> |
| 102 | + </Dialog.Content> |
| 103 | + </Dialog.Portal> |
| 104 | + </Dialog.Root> |
| 105 | + ); |
| 106 | +}; |
0 commit comments