Skip to content

An AI-powered search engine with a generative UI

chore: Refactor Copilot and FollowupPanel

Yoshiki Miurac2aa9aee5457
diff --git a/components/copilot.tsx b/components/copilot.tsx
index b8a39a32..276ee59e 100644
--- a/components/copilot.tsx
+++ b/components/copilot.tsx
@@ -14,7 +14,6 @@ import {
useUIState
} from 'ai/rsc'
import type { AI } from '@/app/actions'
-import { IconLogo } from './ui/icons'
import { useAppState } from '@/lib/utils/app-state'
export type CopilotProps = {
@@ -32,7 +31,7 @@ export const Copilot: React.FC<CopilotProps> = ({ inquiry }: CopilotProps) => {
const [isButtonDisabled, setIsButtonDisabled] = useState(true)
const [, setMessages] = useUIState<typeof AI>()
const { submit } = useActions()
- const { setIsGenerating } = useAppState()
+ const { isGenerating, setIsGenerating } = useAppState()
const [object, setObject] = useState<PartialInquiry>()
const handleInputChange = (event: React.ChangeEvent<HTMLInputElement>) => {
@@ -78,6 +77,9 @@ export const Copilot: React.FC<CopilotProps> = ({ inquiry }: CopilotProps) => {
skip?: boolean
) => {
e.preventDefault()
+
+ if (isGenerating) return
+
setIsGenerating(true)
setCompleted(true)
setSkipped(skip || false)
@@ -174,7 +176,7 @@ export const Copilot: React.FC<CopilotProps> = ({ inquiry }: CopilotProps) => {
type="button"
variant="outline"
onClick={handleSkip}
- disabled={pending}
+ disabled={pending || isGenerating}
>
<FastForward size={16} className="mr-1" />
Skip
diff --git a/components/followup-panel.tsx b/components/followup-panel.tsx
index 43034d84..77a74702 100644
--- a/components/followup-panel.tsx
+++ b/components/followup-panel.tsx
@@ -7,14 +7,22 @@ import { useActions, useUIState } from 'ai/rsc'
import type { AI } from '@/app/actions'
import { UserMessage } from './user-message'
import { ArrowRight } from 'lucide-react'
+import { useAppState } from '@/lib/utils/app-state'
export function FollowupPanel() {
const [input, setInput] = useState('')
const { submit } = useActions()
const [, setMessages] = useUIState<typeof AI>()
+ const { isGenerating, setIsGenerating } = useAppState()
const handleSubmit = async (event: React.FormEvent<HTMLFormElement>) => {
event.preventDefault()
+
+ if (isGenerating) return
+
+ setIsGenerating(true)
+ setInput('')
+
const formData = new FormData(event.currentTarget as HTMLFormElement)
const userMessage = {
@@ -29,8 +37,6 @@ export function FollowupPanel() {
userMessage,
responseMessage
])
-
- setInput('')
}
return (
@@ -49,7 +55,7 @@ export function FollowupPanel() {
<Button
type="submit"
size={'icon'}
- disabled={input.length === 0}
+ disabled={input.length === 0 || isGenerating}
variant={'ghost'}
className="absolute right-1"
>