Skip to content

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

refactor(cmdk): move search/ai props to context

Greg Richardson6902624c5935
diff --git a/packages/ui/src/components/Command/AiCommand.tsx b/packages/ui/src/components/Command/AiCommand.tsx
index ae1bdcf8f06..9477b70a4e8 100644
--- a/packages/ui/src/components/Command/AiCommand.tsx
+++ b/packages/ui/src/components/Command/AiCommand.tsx
@@ -105,16 +105,9 @@ function promptDataReducer(
throw Error('Unknown action.')
}
-export interface AiCommandProps {
- query?: string
- setQuery: (query: string) => void
- page?: string
-}
-
-const AiCommand = ({ query, setQuery, page }: AiCommandProps) => {
+const AiCommand = () => {
const { isDarkMode } = useTheme()
- // const { close, query, setQuery } = useSearch()
const [answer, setAnswer] = useState<string | undefined>('')
const [results, setResults] = useState<any[]>()
const [isResponding, setIsResponding] = useState(false)
@@ -122,8 +115,7 @@ const AiCommand = ({ query, setQuery, page }: AiCommandProps) => {
const [hasSearchError, setHasSearchError] = useState(false)
const [selectedTab, setSelectedTab] = useState('clippy-panel')
const eventSourceRef = useRef<SSE>()
- const supabaseClient = useSupabaseClient()
- const { isLoading, setIsLoading } = useCommandMenu()
+ const { isLoading, setIsLoading, currentPage, search, setSearch } = useCommandMenu()
const [promptIndex, setPromptIndex] = useState(0)
const [promptData, dispatchPromptData] = useReducer(promptDataReducer, [])
@@ -185,7 +177,7 @@ const AiCommand = ({ query, setQuery, page }: AiCommandProps) => {
console.log('handleClippyConfirm ran')
console.log('promptIndex', promptIndex)
setAnswer(undefined)
- setQuery('')
+ setSearch('')
dispatchPromptData({ index: promptIndex, answer: undefined, query: query })
setIsResponding(false)
setHasClippyError(false)
@@ -194,7 +186,7 @@ const AiCommand = ({ query, setQuery, page }: AiCommandProps) => {
let queryToSend = query
- switch (page) {
+ switch (currentPage) {
case COMMAND_ROUTES.AI_ASK_ANYTHING:
queryToSend = query
break
@@ -305,14 +297,14 @@ const AiCommand = ({ query, setQuery, page }: AiCommandProps) => {
// console.log(promptData)
useEffect(() => {
- if (query) {
- handleClippyConfirm(query)
+ if (search) {
+ handleClippyConfirm(search)
}
}, [])
const showActions =
- !query &&
- page === COMMAND_ROUTES.AI_RLS_POLICY &&
+ !search &&
+ currentPage === COMMAND_ROUTES.AI_RLS_POLICY &&
!isResponding &&
!isLoading &&
promptData.length > 0
@@ -520,7 +512,7 @@ const AiCommand = ({ query, setQuery, page }: AiCommandProps) => {
<CommandItem
type="command"
onSelect={() => {
- if (!query) {
+ if (!search) {
alert("You've selected me")
}
}}
@@ -534,7 +526,7 @@ const AiCommand = ({ query, setQuery, page }: AiCommandProps) => {
type="command"
forceMount
onSelect={() => {
- if (!query) {
+ if (!search) {
dispatchPromptData({ type: 'remove-last-item' })
setPromptIndex((x) => {
return x - 1
@@ -558,7 +550,7 @@ const AiCommand = ({ query, setQuery, page }: AiCommandProps) => {
})}
</div>
- {promptData.length <= 0 && page === COMMAND_ROUTES.AI_ASK_ANYTHING && (
+ {promptData.length <= 0 && currentPage === COMMAND_ROUTES.AI_ASK_ANYTHING && (
<CommandGroup heading="Examples" forceMount>
{questions.map((question) => {
const key = question.replace(/\s+/g, '_')
@@ -566,7 +558,7 @@ const AiCommand = ({ query, setQuery, page }: AiCommandProps) => {
<CommandItem
type="command"
onSelect={() => {
- if (!query) {
+ if (!search) {
handleClippyConfirm(question)
}
}}
@@ -641,16 +633,16 @@ const AiCommand = ({ query, setQuery, page }: AiCommandProps) => {
autoFocus
type="textarea"
placeholder={selectedTab === 'search-panel' ? 'Search documentation' : 'Ask a question'}
- value={query}
- onChange={(e) => setQuery(e.target.value)}
+ value={search}
+ onChange={(e) => setSearch(e.target.value)}
// icon={<IconSearch size="small" />}
onKeyDown={(e) => {
switch (e.key) {
case 'Enter':
- if (!query) {
+ if (!search) {
return
}
- handleConfirm(selectedTab, query)
+ handleConfirm(selectedTab, search)
return
default:
return
diff --git a/packages/ui/src/components/Command/DocsSearch.tsx b/packages/ui/src/components/Command/DocsSearch.tsx
index c3f61926632..aa1514e0dee 100644
--- a/packages/ui/src/components/Command/DocsSearch.tsx
+++ b/packages/ui/src/components/Command/DocsSearch.tsx
@@ -24,16 +24,11 @@ const questions = [
'How do I set up authentication?',
]
-export interface DocsSearchProps {
- query?: string
- setQuery?: (query: string) => void
-}
-
-const DocsSearch = ({ query, setQuery }: DocsSearchProps) => {
+const DocsSearch = () => {
const [results, setResults] = useState<any[]>()
const [hasSearchError, setHasSearchError] = useState(false)
const supabaseClient = useSupabaseClient()
- const { isLoading, setIsLoading } = useCommandMenu()
+ const { isLoading, setIsLoading, search, setSearch } = useCommandMenu()
const handleSearch = useCallback(
async (query: string) => {
@@ -67,7 +62,7 @@ const DocsSearch = ({ query, setQuery }: DocsSearchProps) => {
)
function handleResetPrompt() {
- setQuery('')
+ setSearch('')
setResults(undefined)
setHasSearchError(false)
}
@@ -76,17 +71,17 @@ const DocsSearch = ({ query, setQuery }: DocsSearchProps) => {
// Search initial query immediately (note - empty useEffect deps)
useEffect(() => {
- if (query) {
- handleSearch(query)
+ if (search) {
+ handleSearch(search)
}
}, [])
// TODO: can we do this w/o useEffect if query comes from context?
useEffect(() => {
- if (query) {
- debouncedSearch(query)
+ if (search) {
+ debouncedSearch(search)
}
- }, [query])
+ }, [search])
const ChevronArrow = () => (
<IconChevronRight
@@ -151,10 +146,10 @@ const DocsSearch = ({ query, setQuery }: DocsSearchProps) => {
</IconContainer>
<div className="flex flex-col gap-0">
<CommandLabel>
- <TextHighlighter text={page.meta.title} query={query} />
+ <TextHighlighter text={page.meta.title} query={search} />
</CommandLabel>
<div className="text-xs text-scale-900">
- <TextHighlighter text={page.meta.description} query={query} />
+ <TextHighlighter text={page.meta.description} query={search} />
</div>
</div>
</div>
@@ -188,11 +183,11 @@ const DocsSearch = ({ query, setQuery }: DocsSearchProps) => {
<TextHighlighter
className="not-italic text-xs rounded-full px-2 py-1 bg-scale-500 text-scale-1200"
text={page.meta.title}
- query={query}
+ query={search}
/>
</cite>
<CommandLabel>
- <TextHighlighter text={section.heading} query={query} />
+ <TextHighlighter text={section.heading} query={search} />
</CommandLabel>
</div>
</div>
@@ -212,9 +207,9 @@ const DocsSearch = ({ query, setQuery }: DocsSearchProps) => {
<CommandItem
disabled={isLoading}
onSelect={() => {
- if (!query) {
+ if (!search) {
handleSearch(question)
- setQuery(question)
+ setSearch(question)
}
}}
type="command"