From f69ede49ddc9f4a008c914cdfa97338b0c1ac916 Mon Sep 17 00:00:00 2001 From: Greg Richardson Date: Thu, 23 Mar 2023 12:25:15 -0600 Subject: [PATCH] chore(cmdk): command menu context with hooks --- .../ui/src/components/Command/AiCommand.tsx | 4 +- .../ui/src/components/Command/Command.tsx | 24 +++++----- .../src/components/Command/Command.utils.tsx | 46 ++++++++++--------- .../ui/src/components/Command/DocsSearch.tsx | 13 ++---- 4 files changed, 42 insertions(+), 45 deletions(-) diff --git a/packages/ui/src/components/Command/AiCommand.tsx b/packages/ui/src/components/Command/AiCommand.tsx index fbe3deafe94..8460139a9c8 100644 --- a/packages/ui/src/components/Command/AiCommand.tsx +++ b/packages/ui/src/components/Command/AiCommand.tsx @@ -30,7 +30,7 @@ import SearchResult, { SearchResultType } from './SearchResult' import { CommandGroup, CommandItem, CommandInput } from './Command.utils' import { IconCopy } from '../Icon/icons/IconCopy' -import { AiIcon, AiIconChat, COMMAND_ROUTES } from './Command' +import { AiIcon, AiIconChat, COMMAND_ROUTES, useCommandMenu } from './Command' const questions = [ 'How do I get started with Supabase?', @@ -132,13 +132,13 @@ const AiCommand: FC = ({ query, setQuery, page }) => { // const { close, query, setQuery } = useSearch() const [answer, setAnswer] = useState('') const [results, setResults] = useState() - const [isLoading, setIsLoading] = useState(false) const [isResponding, setIsResponding] = useState(false) const [hasClippyError, setHasClippyError] = useState(false) const [hasSearchError, setHasSearchError] = useState(false) const [selectedTab, setSelectedTab] = useState('clippy-panel') const eventSourceRef = useRef() const supabaseClient = useSupabaseClient() + const { isLoading, setIsLoading } = useCommandMenu() const [promptIndex, setPromptIndex] = useState(0) const [promptData, dispatchPromptData] = useReducer(promptDataReducer, []) diff --git a/packages/ui/src/components/Command/Command.tsx b/packages/ui/src/components/Command/Command.tsx index ed0d5fca7b8..a6ab6dd3125 100644 --- a/packages/ui/src/components/Command/Command.tsx +++ b/packages/ui/src/components/Command/Command.tsx @@ -1,4 +1,5 @@ -import React from 'react' +import * as React from 'react' +import { createContext, useContext } from 'react' import { useRouter } from 'next/router' import { Calculator, Calendar, CreditCard, Settings, Smile, User } from 'lucide-react' @@ -92,6 +93,13 @@ const ItemHandler = (props: any) => { return } +export interface CommandMenuContextValue { + isLoading: boolean + setIsLoading: (isLoading: boolean) => void +} +export const CommandMenuContext = createContext(undefined) +export const useCommandMenu = () => useContext(CommandMenuContext) + function CommandMenu({ actions }: { actions: IActions }) { const [open, setOpen] = React.useState(false) const [search, setSearch] = React.useState('') @@ -147,7 +155,7 @@ function CommandMenu({ actions }: { actions: IActions }) { const showCommandInput = page === undefined || !AI_CHAT_ROUTES.includes(page) return ( - <> +

Press{' '} @@ -198,7 +206,6 @@ function CommandMenu({ actions }: { actions: IActions }) { placeholder="Type a command or search..." value={search} onValueChange={setSearch} - loading={isLoading} /> )} {/* @@ -327,14 +334,7 @@ function CommandMenu({ actions }: { actions: IActions }) { )} {page === COMMAND_ROUTES.DOCS_SEARCH && ( <> - + )} {page === COMMAND_ROUTES.AI_RLS_POLICY && ( @@ -436,7 +436,7 @@ function CommandMenu({ actions }: { actions: IActions }) { {/* */} - + ) } diff --git a/packages/ui/src/components/Command/Command.utils.tsx b/packages/ui/src/components/Command/Command.utils.tsx index a98f3e7c6ab..1c9f710975d 100644 --- a/packages/ui/src/components/Command/Command.utils.tsx +++ b/packages/ui/src/components/Command/Command.utils.tsx @@ -9,6 +9,7 @@ import { Modal } from '../Modal' import { ModalProps } from '../Modal/Modal' import { KeyboardEventHandler } from 'react' import { LoadingLine } from './LoadingLine' +import { useCommandMenu } from './Command' const Command = React.forwardRef< React.ElementRef, @@ -71,33 +72,36 @@ const CommandDialog = ({ children, onKeyDown, page, ...props }: CommandDialogPro type CommandPrimitiveInputProps = React.ComponentPropsWithoutRef type CommandInputProps = CommandPrimitiveInputProps & { - loading?: boolean _key?: string | number } const CommandInput = React.forwardRef< React.ElementRef, CommandInputProps ->(({ className, value, loading, onValueChange, _key, ...props }, ref) => ( -

- {/* */} - - -
-)) +>(({ className, value, onValueChange, _key, ...props }, ref) => { + const { isLoading } = useCommandMenu() + + return ( +
+ {/* */} + + +
+ ) +}) CommandInput.displayName = CommandPrimitive.Input.displayName diff --git a/packages/ui/src/components/Command/DocsSearch.tsx b/packages/ui/src/components/Command/DocsSearch.tsx index c7ed2b2f039..434a1bc4ba4 100644 --- a/packages/ui/src/components/Command/DocsSearch.tsx +++ b/packages/ui/src/components/Command/DocsSearch.tsx @@ -12,6 +12,7 @@ import { IconHash, IconLoader, IconSearch, + useCommandMenu, } from 'ui' import { CommandGroup, CommandItem, CommandLabel } from './Command.utils' @@ -100,19 +101,10 @@ export interface DocsSearchProps { query?: string setQuery?: () => void page?: string - isLoading?: boolean - setIsLoading?: () => void router: any } -const DocsSearch = ({ - query, - setQuery, - page, - isLoading, - setIsLoading, - router, -}: DocsSearchProps) => { +const DocsSearch = ({ query, setQuery, page, router }: DocsSearchProps) => { const { isDarkMode } = useTheme() // const { close, query, setQuery } = useSearch() @@ -124,6 +116,7 @@ const DocsSearch = ({ const [selectedTab, setSelectedTab] = useState('clippy-panel') const eventSourceRef = useRef() const supabaseClient = useSupabaseClient() + const { isLoading, setIsLoading } = useCommandMenu() const [promptIndex, setPromptIndex] = useState(0) const [promptData, dispatchPromptData] = useReducer(promptDataReducer, [])