Repository navigation
[Customer Portal][FE][Web] Implement get chat history API with test cases for both mock and real environments - #100
Conversation
Adds a new test suite for the useGetChatHistory React Query hook. Tests cover initial loading state, behavior when mocks are enabled, query options (staleTime), real API fetching with auth token, API error handling, and behavior when projectId is missing. Mocks are provided for logger, Asgardeo auth, mock config, API delay constant, and global fetch; tests run within a QueryClientProvider.
Introduce useGetChatHistory hook to fetch project chat history via react-query. Supports mock mode (uses getMockChatHistory with API_MOCK_DELAY) and real API calls (gets idToken from Asgardeo, builds request to CUSTOMER_PORTAL_BACKEND_BASE_URL, and adds headers via addApiHeaders). Includes logging, error handling, query key scoping, enabled gating based on auth/mock state, and 5 minute staleTime.
Introduce a new CHAT_HISTORY entry in ApiQueryKeys (apps/customer-portal/webapp/src/constants/apiConstants.ts) with value 'chat-history' to support caching/identification of chat history API/query operations.
Introduce getMockChatHistory() returning a ChatHistoryResponse with sample chat entries (chatId, title, startedTime, messages, kbArticles, status) for the customer portal webapp. Also update imports to include ChatHistoryResponse. This provides mock data for UI development and testing of chat/history components.
Introduce ChatHistoryItem and ChatHistoryResponse interfaces to represent support chat session summaries and project chat history lists. ChatHistoryItem includes chatId, title, startedTime, messages, kbArticles, and status to support chat listing APIs.
Move hardcoded chat history out of mockFunctions and into a reusable mockChatHistory export in mockData.ts. Update imports and have getMockChatHistory return the shared mockChatHistory to centralize mock data and avoid duplication.
📝 WalkthroughWalkthroughThis pull request introduces a new chat history fetching feature for the customer portal. It adds a Changes
Sequence DiagramsequenceDiagram
actor User
participant Hook as useGetChatHistory<br/>Hook
participant MockMode as Mock Mode<br/>Logic
participant Auth as Auth Service
participant Backend as Backend API
participant ReactQuery as React Query
User->>Hook: Call with projectId
Hook->>ReactQuery: Initialize query
activate ReactQuery
alt Mock Mode Enabled
Hook->>MockMode: Fetch mock data
MockMode-->>Hook: Return mockChatHistory
Hook->>ReactQuery: Return cached data
else Real Backend Mode
Hook->>Auth: Retrieve ID Token
Auth-->>Hook: Return token
Hook->>Backend: GET /chat-history<br/>(with auth header)
alt Success
Backend-->>Hook: Return ChatHistoryResponse
Hook->>ReactQuery: Return data
else Error
Backend-->>Hook: Error response
Hook->>ReactQuery: Return error
end
end
ReactQuery-->>User: Expose query result
deactivate ReactQuery
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Pull request overview
Adds a new React Query hook for retrieving project chat history, including mock-data support and Vitest coverage for both mock and real API execution paths in the Customer Portal webapp.
Changes:
- Introduces
useGetChatHistoryhook to fetch chat history from the backend (or mock data when mocks are enabled). - Adds new response models and mock data for chat history.
- Adds query key constant and unit tests for the new hook.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| apps/customer-portal/webapp/src/models/responses.ts | Adds ChatHistoryItem and ChatHistoryResponse response types. |
| apps/customer-portal/webapp/src/models/mockFunctions.ts | Exposes getMockChatHistory() for mock-enabled API flows. |
| apps/customer-portal/webapp/src/models/mockData.ts | Adds mockChatHistory dataset used by the new hook. |
| apps/customer-portal/webapp/src/constants/apiConstants.ts | Adds CHAT_HISTORY query key constant. |
| apps/customer-portal/webapp/src/api/useGetChatHistory.ts | Implements the chat history data-fetching hook (mock + real API). |
| apps/customer-portal/webapp/src/api/tests/useGetChatHistory.test.tsx | Adds tests for loading, mock mode, real mode, error handling, and query options. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
…tanding-cases-chat-history
7e129c1
into
wso2-open-operations:customer-portal-milestone-1
Overview
This PR implements the frontend infrastructure for fetching and managing chat history within the Customer Portal. It introduces a custom React Query hook,
useGetChatHistory, which is designed to handle dual-mode execution: fetching from a live backend API (with authentication) or utilizing mock data for local development and testing environments.Key Features
useGetChatHistoryusing@tanstack/react-query, allowing for seamless caching, loading states, and error handling.ChatHistoryItemandChatHistoryResponseto ensure data consistency across the application.projectIdto prevent unnecessary or invalid API calls.Technical Changes
API & Hooks
useGetChatHistory.ts: The core hook implementation. Handles the conditional logic between mock and real environments.apiConstants.ts: AddedCHAT_HISTORYquery key for consistent React Query cache management.Models & Data
responses.ts: Defined the structure for chat history entries (IDs, timestamps, messages, etc.).mockData.ts&mockFunctions.ts: Created a realistic dataset and helper functions to simulate backend responses.Testing
useGetChatHistory.test.tsx: Added 6 comprehensive test cases using Vitest/React Testing Library:projectIdis missing.Sequence Diagram
sequenceDiagram actor User participant Hook as useGetChatHistory Hook participant MockMode as Mock Mode Logic participant Auth as Auth Service participant Backend as Backend API participant ReactQuery as React Query User->>Hook: Call with projectId Hook->>ReactQuery: Initialize query activate ReactQuery alt Mock Mode Enabled Hook->>MockMode: Fetch mock data MockMode-->>Hook: Return mockChatHistory Hook->>ReactQuery: Return cached data else Real Backend Mode Hook->>Auth: Retrieve ID Token Auth-->>Hook: Return token Hook->>Backend: GET /chat-history (with auth header) alt Success Backend-->>Hook: Return ChatHistoryResponse Hook->>ReactQuery: Return data else Error Backend-->>Hook: Error response Hook->>ReactQuery: Return error end end ReactQuery-->>User: Expose query result deactivate ReactQuery