Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

refactor(cmdk): shell-style message history into useHistoryKeys hook

Greg Richardsone93fe05154cd
diff --git a/packages/ui/src/components/Command/AiCommand.tsx b/packages/ui/src/components/Command/AiCommand.tsx
index fa0f8077811..e713c95a525 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 } from './Command.utils'
+import { CommandGroup, CommandItem, useHistoryKeys } from './Command.utils'
import { useCommandMenu } from './CommandMenuProvider'
@@ -343,22 +343,15 @@ const AiCommand = () => {
setIsLoading,
})
- const inputRef = useRef<HTMLInputElement>(null)
-
- // Message index (of role === 'user') when hitting up/down on the keyboard (shell style)
- const [, setMessageSelectionIndex] = useState(0)
-
- const userMessages = messages.filter(({ role }) => role === MessageRole.User)
-
- useEffect(() => {
- if (isResponding) {
- return
- }
+ useHistoryKeys({
+ enable: !isResponding,
+ messages: messages
+ .filter(({ role }) => role === MessageRole.User)
+ .map(({ content }) => content),
+ setPrompt: setSearch,
+ })
- // Note: intentionally setting index to 1 greater than array length
- setMessageSelectionIndex(userMessages.length)
- console.log('setting index to', userMessages.length)
- }, [messages, isResponding])
+ const inputRef = useRef<HTMLInputElement>(null)
const handleSubmit = useCallback(
(message: string) => {
@@ -524,20 +517,6 @@ const AiCommand = () => {
}
handleSubmit(search)
return
- case 'ArrowUp':
- setMessageSelectionIndex((index) => {
- const newIndex = Math.max(index - 1, 0)
- setSearch(userMessages[newIndex]?.content ?? '')
- return newIndex
- })
- return
- case 'ArrowDown':
- setMessageSelectionIndex((index) => {
- const newIndex = Math.min(index + 1, userMessages.length)
- setSearch(userMessages[newIndex]?.content ?? '')
- return newIndex
- })
- return
default:
return
}
diff --git a/packages/ui/src/components/Command/Command.utils.tsx b/packages/ui/src/components/Command/Command.utils.tsx
index 6c3fc617479..b42dd2cd370 100644
--- a/packages/ui/src/components/Command/Command.utils.tsx
+++ b/packages/ui/src/components/Command/Command.utils.tsx
@@ -324,3 +324,56 @@ export const TextHighlighter = ({ text, query, ...props }: TextHighlighterProps)
}
TextHighlighter.displayName = 'TextHighlighter'
+
+export interface UseHistoryKeysOptions {
+ enable: boolean
+ messages: string[]
+ setPrompt: (prompt: string) => void
+}
+
+/**
+ * Enables a shell-style message history when hitting
+ * up/down on the keyboard
+ */
+export function useHistoryKeys({ enable, messages, setPrompt }: UseHistoryKeysOptions) {
+ // Message index when hitting up/down on the keyboard (shell style)
+ const [, setMessageSelectionIndex] = React.useState(0)
+
+ React.useEffect(() => {
+ if (enable) {
+ return
+ }
+
+ // Note: intentionally setting index to 1 greater than array length
+ setMessageSelectionIndex(messages.length)
+ }, [messages, enable])
+
+ React.useEffect(() => {
+ function onKeyDown(e: KeyboardEvent) {
+ switch (e.key) {
+ case 'ArrowUp':
+ setMessageSelectionIndex((index) => {
+ const newIndex = Math.max(index - 1, 0)
+ setPrompt(messages[newIndex] ?? '')
+ return newIndex
+ })
+ return
+ case 'ArrowDown':
+ setMessageSelectionIndex((index) => {
+ const newIndex = Math.min(index + 1, messages.length)
+ setPrompt(messages[newIndex] ?? '')
+ return newIndex
+ })
+ return
+ default:
+ return
+ }
+ }
+
+ window.addEventListener('keydown', onKeyDown)
+
+ return () => {
+ window.removeEventListener('keydown', onKeyDown)
+ }
+ }, [messages])
+}
diff --git a/packages/ui/src/components/Command/GenerateSQL/GenerateSQL.tsx b/packages/ui/src/components/Command/GenerateSQL/GenerateSQL.tsx
index bd018484210..f933c818963 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 } from '../Command.utils'
+import { CommandItem, useHistoryKeys } from '../Command.utils'
import { useCommandMenu } from '../CommandMenuProvider'
import { SAMPLE_QUERIES } from '../Command.constants'
import SQLOutputActions from './SQLOutputActions'
@@ -50,6 +50,14 @@ const GenerateSQL = () => {
setIsLoading,
})
+ useHistoryKeys({
+ enable: !isResponding,
+ messages: messages
+ .filter(({ role }) => role === MessageRole.User)
+ .map(({ content }) => content),
+ setPrompt: setSearch,
+ })
+
const handleSubmit = useCallback(
(message: string) => {
setSearch('')