Chat Window
Component Documentation: ChatWindow
.tsx
ChatWindow
.tsx
Overview
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)
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
<div className="flex flex-col flex-1 overflow-hidden">
{/* Message history container */}
<div className="flex-1 overflow-auto p-6 space-y-4 flex flex-col">
{chat.map(/* renders message bubbles */)}
</div>
{/* Input area */}
<div className="p-4 border-t border-zinc-700 bg-zinc-900">
<form onSubmit={handleSubmit}>
<input /* message input */ />
<button /* submit button */ />
</form>
</div>
</div>
Message Rendering Logic
chat.map((message, index) => {
const isUser = !!message.user;
return (
<div key={index} className={`flex ${isUser ? 'justify-end' : 'justify-start'}`}>
<div className={`p-4 rounded-lg ${isUser ? 'bg-blue-600' : 'bg-zinc-800'}`}>
{isUser ? message.user.content : message.system.content}
</div>
</div>
);
})
Backend Integration
Uses
backend.chat()
method fromdeclarations/backend
Payload structure: Array of message objects
Error handling for Internet Computer-specific errors:
const match = String(e).match(/(SysTransient|CanisterReject), \\+"([^\\"]+)/);
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
import { useEffect, useRef, useState } from 'react';
import { backend } from 'declarations/backend'; // Internet Computer backend interface
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