The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
refactor(cmdk): hook to auto focus input on key press
Greg Richardson055814070e4f
diff --git a/packages/ui/src/components/Command/AiCommand.tsx b/packages/ui/src/components/Command/AiCommand.tsxindex e713c95a525..d1b76214643 100644--- a/packages/ui/src/components/Command/AiCommand.tsx+++ b/packages/ui/src/components/Command/AiCommand.tsx@@ -17,7 +17,7 @@ import { SSE } from 'sse.js'import { Button, IconAlertTriangle, IconCornerDownLeft, IconUser, Input } from 'ui'import { AiIcon, AiIconChat } from './Command.icons'-import { CommandGroup, CommandItem, useHistoryKeys } from './Command.utils'+import { CommandGroup, CommandItem, useAutoInputFocus, useHistoryKeys } from './Command.utils'import { useCommandMenu } from './CommandMenuProvider'@@ -343,6 +343,8 @@ const AiCommand = () => {setIsLoading,})+ const inputRef = useAutoInputFocus()+useHistoryKeys({enable: !isResponding,messages: messages@@ -351,8 +353,6 @@ const AiCommand = () => {setPrompt: setSearch,})- const inputRef = useRef<HTMLInputElement>(null)-const handleSubmit = useCallback((message: string) => {setSearch('')@@ -372,19 +372,6 @@ const AiCommand = () => {}}, [])- // Focus the input when typing from anywhere- useEffect(() => {- function onKeyDown() {- inputRef.current?.focus()- }-- window.addEventListener('keydown', onKeyDown)-- return () => {- window.removeEventListener('keydown', onKeyDown)- }- }, [])-return (<div onClick={(e) => e.stopPropagation()}><div className={cn('relative mb-[62px] py-4 max-h-[720px] overflow-auto')}>diff --git a/packages/ui/src/components/Command/Command.utils.tsx b/packages/ui/src/components/Command/Command.utils.tsxindex b42dd2cd370..1b2a423a997 100644--- a/packages/ui/src/components/Command/Command.utils.tsx+++ b/packages/ui/src/components/Command/Command.utils.tsx@@ -377,3 +377,41 @@ export function useHistoryKeys({ enable, messages, setPrompt }: UseHistoryKeysOp}}, [messages])}++/**+ * Automatically focuses an input on key press+ * and on load (after the call stack)+ *+ * @returns An input ref for the input to focus+ */+export function useAutoInputFocus() {+ const [input, setInput] = React.useState<HTMLInputElement>()++ // Use a callback-style ref to access the element when it mounts+ const inputRef = React.useCallback((inputElement: HTMLInputElement) => {+ if (inputElement) {+ setInput(inputElement)++ // We need to delay the focus until the end of the call stack+ // due to order of operations+ setTimeout(() => {+ inputElement.focus()+ }, 0)+ }+ }, [])++ // Focus the input when typing from anywhere+ React.useEffect(() => {+ function onKeyDown() {+ input?.focus()+ }++ window.addEventListener('keydown', onKeyDown)++ return () => {+ window.removeEventListener('keydown', onKeyDown)+ }+ }, [input])++ return inputRef+}diff --git a/packages/ui/src/components/Command/GenerateSQL/GenerateSQL.tsx b/packages/ui/src/components/Command/GenerateSQL/GenerateSQL.tsxindex f933c818963..4e540a6ad60 100644--- a/packages/ui/src/components/Command/GenerateSQL/GenerateSQL.tsx+++ b/packages/ui/src/components/Command/GenerateSQL/GenerateSQL.tsx@@ -15,7 +15,7 @@ import {import { cn } from '../../../utils/cn'import { AiIcon, AiIconChat } from '../Command.icons'-import { CommandItem, useHistoryKeys } from '../Command.utils'+import { CommandItem, useAutoInputFocus, useHistoryKeys } from '../Command.utils'import { useCommandMenu } from '../CommandMenuProvider'import { SAMPLE_QUERIES } from '../Command.constants'import SQLOutputActions from './SQLOutputActions'@@ -50,6 +50,8 @@ const GenerateSQL = () => {setIsLoading,})+ const inputRef = useAutoInputFocus()+useHistoryKeys({enable: !isResponding,messages: messages@@ -290,15 +292,7 @@ const GenerateSQL = () => {</>)}<Input- inputRef={(inputElement) => {- if (inputElement) {- // We need to delay the focus until the end of the call stack- // due to order of operations- setTimeout(() => {- inputElement.focus()- }, 0)- }- }}+ inputRef={inputRef}className="bg-scale-100 rounded mx-3"autoFocusplaceholder={