Skip to content

Latest commit

 

History

History
179 lines (129 loc) · 5.37 KB

File metadata and controls

179 lines (129 loc) · 5.37 KB

Comments Feature Implementation

Overview

This implementation provides a complete comments system for bugs with both backend and frontend components. While a basic comments endpoint already existed, this enhancement improves the functionality with better user experience and reusable components.

Backend Improvements

New Query Handler

  • File: GetBugCommentsWithUserQuery.cs
  • Enhancement: Created an enhanced version of the comments query that includes user information (name, email) using SQL joins
  • Benefits:
    • Provides complete user information with comments
    • Uses efficient SQL query for better performance
    • Includes user names without additional API calls

Updated Endpoint

  • File: BugsEndpoints.cs
  • Change: Updated the existing GetComments endpoint to use the new query handler
  • Result: The endpoint now returns comments with user information included

Frontend Components

1. CommentItem Component

  • File: CommentItem.tsx
  • Purpose: Displays individual comments with user avatars, names, and timestamps
  • Features:
    • User avatar with initials
    • Formatted timestamps using relative time
    • Edit indicator for modified comments
    • Responsive design with hover effects

2. CommentForm Component

  • File: CommentForm.tsx
  • Purpose: Provides a form for adding new comments
  • Features:
    • Character limit validation (2000 characters)
    • Real-time character counter with color coding
    • Disabled state during submission
    • Auto-clear on successful submission
    • Loading state with spinner

3. CommentSection Component

  • File: CommentSection.tsx
  • Purpose: Complete comments section combining display and input
  • Features:
    • Shows all comments for a bug
    • Includes comment form
    • Loading skeletons
    • Empty state message
    • Configurable height and styling
    • Comment count display

4. useComments Hook

  • File: useComments.ts
  • Purpose: Custom hook for managing comment state and operations
  • Features:
    • Fetches comments for a specific bug
    • Handles adding new comments
    • Manages loading states
    • Error handling with toast notifications
    • Automatic refresh after adding comments

UI Pages

1. BugCommentsTab Component

  • File: BugCommentsTab.tsx
  • Purpose: Simplified component for use within the existing BugDetailsPage
  • Usage: Can replace the current comments tab content

2. BugCommentsPage Component

  • File: BugCommentsPage.tsx
  • Purpose: Standalone page dedicated to viewing and managing comments for a bug
  • Features:
    • Bug summary information
    • Full comments section
    • Navigation back to bug details
    • Error handling for missing bugs

API Interface Updates

Updated Comment Interface

  • File: api.ts
  • Enhancement: Extended the Comment interface to include userName and userEmail fields
  • Compatibility: Maintains backward compatibility with existing code

Usage Examples

Using the Comment Components

// In any component
import { CommentSection } from "@/components/Comments";
import { useComments } from "@/hooks/useComments";

const MyComponent = ({ bugId }: { bugId: string }) => {
  const { comments, isLoading, isAddingComment, addComment } =
    useComments(bugId);

  return (
    <CommentSection
      comments={comments}
      isLoading={isLoading}
      onAddComment={addComment}
      isAddingComment={isAddingComment}
    />
  );
};

Replacing Comments in BugDetailsPage

// Replace the existing comments TabsContent with:
<TabsContent value="comments" className="space-y-4">
  <BugCommentsTab bugId={bugId} />
</TabsContent>

Benefits of This Implementation

  1. Reusability: Comment components can be used throughout the application
  2. Better UX: Improved visual design with user avatars and better feedback
  3. Performance: Efficient SQL queries reduce API calls
  4. Maintainability: Modular components are easier to test and maintain
  5. Extensibility: Easy to add features like editing, deleting, or reactions

API Endpoints

The implementation uses the existing endpoint structure:

  • GET /api/bugs/{bugId}/comments - Now returns enhanced data with user information
  • POST /api/bugs/{bugId}/comments - Unchanged, still works as before

Files Modified/Created

Backend

  • GetBugCommentsWithUserQuery.cs - New enhanced query handler
  • BugsEndpoints.cs - Updated to use new query handler

Frontend Components

  • CommentItem.tsx - Individual comment display component
  • CommentForm.tsx - Comment input form component
  • CommentSection.tsx - Complete comments section
  • index.ts - Component exports
  • BugCommentsTab.tsx - Tab component for existing pages

Frontend Hooks

  • useComments.ts - Comment management hook

Frontend Pages

  • BugCommentsPage.tsx - Standalone comments page

Frontend Types

  • api.ts - Updated Comment interface

Next Steps

  1. Add routes for the new BugCommentsPage (optional)
  2. Replace existing comment implementation in BugDetailsPage with new components
  3. Add comment editing/deletion functionality if needed
  4. Add comment reactions (like/dislike) if desired
  5. Add real-time updates using SignalR if required

The implementation is production-ready and maintains backward compatibility while providing enhanced functionality.