|
| 1 | +import { useRouter } from "next/router"; |
| 2 | +import { |
| 3 | + Combobox, |
| 4 | + ComboboxInput, |
| 5 | + ComboboxOption, |
| 6 | + ComboboxOptions, |
| 7 | + Dialog, |
| 8 | + DialogBackdrop, |
| 9 | + DialogPanel, |
| 10 | +} from "@headlessui/react"; |
| 11 | +import { t } from "@lingui/macro"; |
| 12 | +import { useState } from "react"; |
| 13 | +import { HiDocumentText, HiFolder, HiMagnifyingGlass } from "react-icons/hi2"; |
| 14 | + |
| 15 | +import { useDebounce } from "~/hooks/useDebounce"; |
| 16 | +import { useWorkspace } from "~/providers/workspace"; |
| 17 | +import { api } from "~/utils/api"; |
| 18 | + |
| 19 | +type SearchResult = |
| 20 | + | { |
| 21 | + publicId: string; |
| 22 | + title: string; |
| 23 | + description: string | null; |
| 24 | + slug: string; |
| 25 | + updatedAt: Date | null; |
| 26 | + createdAt: Date; |
| 27 | + type: "board"; |
| 28 | + } |
| 29 | + | { |
| 30 | + publicId: string; |
| 31 | + title: string; |
| 32 | + description: string | null; |
| 33 | + boardPublicId: string; |
| 34 | + boardName: string; |
| 35 | + listName: string; |
| 36 | + updatedAt: Date | null; |
| 37 | + createdAt: Date; |
| 38 | + type: "card"; |
| 39 | + }; |
| 40 | + |
| 41 | +export default function CommandPallette({ |
| 42 | + isOpen, |
| 43 | + onClose, |
| 44 | +}: { |
| 45 | + isOpen: boolean; |
| 46 | + onClose: () => void; |
| 47 | +}) { |
| 48 | + const [query, setQuery] = useState(""); |
| 49 | + const { workspace } = useWorkspace(); |
| 50 | + const router = useRouter(); |
| 51 | + |
| 52 | + // Debounce to avoid too many reqs |
| 53 | + const [debouncedQuery] = useDebounce(query, 300); |
| 54 | + |
| 55 | + const { |
| 56 | + data: searchResults, |
| 57 | + isLoading, |
| 58 | + isFetched, |
| 59 | + isPlaceholderData, |
| 60 | + } = api.workspace.search.useQuery( |
| 61 | + { |
| 62 | + workspacePublicId: workspace.publicId, |
| 63 | + query: debouncedQuery, |
| 64 | + }, |
| 65 | + { |
| 66 | + enabled: Boolean(workspace.publicId && debouncedQuery.trim().length > 0), |
| 67 | + placeholderData: (previousData) => previousData, |
| 68 | + }, |
| 69 | + ); |
| 70 | + |
| 71 | + // Clear results when query is empty, otherwise show search results |
| 72 | + const results = |
| 73 | + debouncedQuery.trim().length === 0 |
| 74 | + ? [] |
| 75 | + : ((searchResults ?? []) as SearchResult[]); |
| 76 | + |
| 77 | + const hasSearched = Boolean(debouncedQuery.trim().length > 0); |
| 78 | + |
| 79 | + return ( |
| 80 | + <Dialog |
| 81 | + className="relative z-50" |
| 82 | + open={isOpen} |
| 83 | + onClose={() => { |
| 84 | + onClose(); |
| 85 | + setQuery(""); |
| 86 | + }} |
| 87 | + > |
| 88 | + <DialogBackdrop |
| 89 | + transition |
| 90 | + className="data-closed:opacity-0 data-enter:duration-300 data-enter:ease-out data-leave:duration-200 data-leave:ease-in fixed inset-0 bg-light-50 bg-opacity-40 transition-opacity dark:bg-dark-50 dark:bg-opacity-40" |
| 91 | + /> |
| 92 | + |
| 93 | + <div className="fixed inset-0 z-50 w-screen overflow-y-auto"> |
| 94 | + <div className="flex min-h-full items-start justify-center p-4 text-center sm:items-start sm:p-0"> |
| 95 | + <DialogPanel |
| 96 | + transition |
| 97 | + className="data-closed:opacity-0 data-closed:translate-y-4 data-closed:sm:translate-y-0 data-closed:sm:scale-95 data-enter:duration-300 data-enter:ease-out data-leave:duration-200 data-leave:ease-in relative mt-[25vh] w-full max-w-[550px] transform divide-y divide-gray-100 overflow-hidden rounded-lg border border-light-600 bg-white/90 shadow-3xl-light backdrop-blur-[6px] transition-all dark:divide-white/10 dark:border-dark-600 dark:bg-dark-100/90 dark:shadow-3xl-dark" |
| 98 | + > |
| 99 | + <Combobox> |
| 100 | + <div className="grid grid-cols-1"> |
| 101 | + <ComboboxInput |
| 102 | + autoFocus |
| 103 | + className="col-start-1 row-start-1 h-12 w-full border-0 bg-transparent pl-11 pr-4 text-sm text-light-900 placeholder:text-light-700 focus:outline-none focus:ring-0 dark:text-dark-900 dark:placeholder:text-dark-700" |
| 104 | + placeholder={t`Search boards and cards...`} |
| 105 | + value={query} |
| 106 | + onChange={(event) => setQuery(event.target.value)} |
| 107 | + onKeyDown={(event) => { |
| 108 | + if (event.key === "Enter" && results.length > 0) { |
| 109 | + event.preventDefault(); |
| 110 | + |
| 111 | + // Find the active option or fallback to first option |
| 112 | + const targetOption = |
| 113 | + document.querySelector( |
| 114 | + '[data-headlessui-state*="active"][role="option"]', |
| 115 | + ) ?? document.querySelector('[role="option"]'); |
| 116 | + |
| 117 | + if (targetOption) { |
| 118 | + (targetOption as HTMLElement).click(); |
| 119 | + } |
| 120 | + } |
| 121 | + }} |
| 122 | + /> |
| 123 | + <HiMagnifyingGlass |
| 124 | + className="pointer-events-none col-start-1 row-start-1 ml-4 size-5 self-center text-light-700 dark:text-dark-700" |
| 125 | + aria-hidden="true" |
| 126 | + /> |
| 127 | + </div> |
| 128 | + |
| 129 | + {results.length > 0 && ( |
| 130 | + <ComboboxOptions |
| 131 | + static |
| 132 | + className={`max-h-72 scroll-py-2 overflow-y-auto py-2 ${ |
| 133 | + isPlaceholderData ? "opacity-75" : "" |
| 134 | + }`} |
| 135 | + > |
| 136 | + {results.map((result) => { |
| 137 | + const url = |
| 138 | + result.type === "board" |
| 139 | + ? `/boards/${result.publicId}` |
| 140 | + : `/cards/${result.publicId}`; |
| 141 | + |
| 142 | + return ( |
| 143 | + <ComboboxOption |
| 144 | + key={`${result.type}-${result.publicId}`} |
| 145 | + value={result} |
| 146 | + className="cursor-pointer select-none px-4 py-3 data-[focus]:bg-light-200 hover:bg-light-200 focus:outline-none dark:data-[focus]:bg-dark-200 dark:hover:bg-dark-200" |
| 147 | + onClick={() => { |
| 148 | + console.log("clicked", url); |
| 149 | + void router.push(url); |
| 150 | + onClose(); |
| 151 | + setQuery(""); |
| 152 | + }} |
| 153 | + > |
| 154 | + <div className="flex items-start gap-3"> |
| 155 | + <div className="mt-0.5 flex-shrink-0"> |
| 156 | + {result.type === "board" ? ( |
| 157 | + <HiFolder className="h-4 w-4 text-light-600 dark:text-dark-600" /> |
| 158 | + ) : ( |
| 159 | + <HiDocumentText className="h-4 w-4 text-light-600 dark:text-dark-600" /> |
| 160 | + )} |
| 161 | + </div> |
| 162 | + <div className="min-w-0 flex-1 text-left"> |
| 163 | + <div className="truncate text-sm font-bold text-light-900 dark:text-dark-900"> |
| 164 | + {result.title} |
| 165 | + </div> |
| 166 | + {result.type === "card" && ( |
| 167 | + <div className="truncate text-xs text-light-700 dark:text-dark-700"> |
| 168 | + {`${t`in`} ${result.boardName} → ${result.listName}`} |
| 169 | + </div> |
| 170 | + )} |
| 171 | + </div> |
| 172 | + </div> |
| 173 | + </ComboboxOption> |
| 174 | + ); |
| 175 | + })} |
| 176 | + </ComboboxOptions> |
| 177 | + )} |
| 178 | + |
| 179 | + {hasSearched && |
| 180 | + !isLoading && |
| 181 | + searchResults !== undefined && |
| 182 | + results.length === 0 && ( |
| 183 | + <div className="p-4 text-sm text-light-950 dark:text-dark-950"> |
| 184 | + {t`No results found for "${debouncedQuery}".`} |
| 185 | + </div> |
| 186 | + )} |
| 187 | + </Combobox> |
| 188 | + </DialogPanel> |
| 189 | + </div> |
| 190 | + </div> |
| 191 | + </Dialog> |
| 192 | + ); |
| 193 | +} |
0 commit comments