Overview
ThePromptInput component provides a form interface for users to enter prompts for AI image generation. It integrates with ChatGPT to provide intelligent suggestions and uses SWR for efficient data fetching and cache management.
Props
This component does not accept any props.State management
The component uses the following state and hooks:input- Local state for managing the text input valueuseSWR- Fetches AI suggestions from ChatGPT APIuseSWR- Manages image generation updates- Toast notifications for user feedback
Key features
- Real-time prompt input with textarea
- AI-powered suggestion generation via ChatGPT
- Submit custom prompts or use AI suggestions
- Refresh suggestions on demand
- Loading states and disabled button logic
- Integration with DALL-E 3 image generation API
Usage
Component structure
The component renders:- Textarea - Input field for entering custom prompts
- Generate button - Submits the custom prompt for image generation
- Use suggestion button - Submits the AI-generated suggestion
- New suggestion button - Fetches a new suggestion from ChatGPT
- Suggestion display - Shows the current AI suggestion below the form
Methods
submitPrompt
Handles prompt submission to the image generation API.Whether to use the AI suggestion instead of the user’s input
handleSubmit
Form submission handler for the custom prompt.API endpoints
/api/suggestion- Fetches AI-powered prompt suggestions from ChatGPT/api/generateImage- Generates images using DALL-E 3 based on the provided prompt
Dependencies
react- State management with useStateswr- Data fetching and cachingreact-hot-toast- Toast notifications@/lib/fetchSuggestionFromChatGPT- Suggestion fetcher@/lib/fetchImages- Image fetcher
Styling
The component uses Tailwind CSS classes for responsive design:- Flexbox layout with responsive column/row switching
- Violet color scheme for primary actions
- Shadow effects and rounded corners
- Disabled states with gray styling
- Transition animations for smooth interactions