Chat Window
Component Documentation: ChatWindow .tsx
ChatWindow .tsxOverview
ChatWindow is a React chat interface component that enables users to interact with a financial agent backend hosted on the Internet Computer blockchain. It displays a conversation history, handles user input, and manages API communication with the backend service.
Key Features
Real-time message display with user/system differentiation
Asynchronous communication with blockchain backend
Loading states and error handling
Responsive UI with Tailwind CSS styling
State Management
chat
Array
Stores chat history objects with user/system messages
inputValue
string
Tracks current value of message input field
isLoading
boolean
Indicates when backend request is in progress (disables UI interactions)
Core Functions
formatDate(date: Date)&#xNAN;Formats timestamps to HH:MM (currently unused in UI)askAgent(messages: any[])Handles backend communication:Sends conversation history to
backend.chat()endpointUpdates chat with response on success
Implements error parsing for blockchain-specific errors
Manages loading states
handleSubmit(e: React.FormEvent)Message submission handler:Validates input
Updates chat with user message + "Thinking..." placeholder
Triggers backend request
Resets input field
Component Structure
Message Rendering Logic
Backend Integration
Uses
backend.chat()method fromdeclarations/backendPayload structure: Array of message objects
Error handling for Internet Computer-specific errors:
UI Features
Message bubbles color-coded by sender (blue for user, dark gray for system)
Responsive layout with flexbox
Input disabled during loading states
Auto-scroll functionality (currently commented out)
Usage Notes
Initial State: Displays welcome message from system agent
Submit Workflow: User input → Add user message → Show "Thinking..." → API call → Replace with response
Error Handling:
Alerts user with parsed error message
Removes "Thinking..." placeholder on failure
Optimization Opportunities
Enable auto-scroll (uncomment useEffect)
Add message timestamps using
formatDate()Implement markdown rendering for system messages
Add rate limiting for submissions
Dependencies
Styling Reference
User Message
bg-blue-600 text-white justify-end
System Message
bg-zinc-800 text-white justify-start
Input Field
bg-zinc-800 text-white outline-none
Submit Button
bg-blue-600 hover:bg-blue-500 text-white
Container
bg-zinc-900 border-zinc-700
Last updated