All files / src/components/admin/monitoring ErrorCommentsTab.tsx

100% Statements 143/143
86.66% Branches 13/15
100% Functions 4/4
100% Lines 143/143

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 1441x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 31x 31x 31x 31x 1x 1x 1x 1x 1x 1x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 2x 2x 2x 2x 2x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 17x 1x 1x 1x 1x 17x 16x 17x 17x 17x 17x 17x 17x 17x 17x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 31x 17x 17x 17x 1x 1x 1x 17x 17x 17x 17x 1x 1x 1x 1x 1x 17x 17x 17x 17x 1x 1x 1x 17x 17x 17x 17x 17x 1x 1x  
'use client';
 
import { useState } from 'react';
import { formatDistanceToNow } from 'date-fns';
import { Button } from '@/components/ui';
import { Icon } from '@/components/ui/icons';
 
/**
 * Comment data structure
 */
export interface ErrorCommentData {
  id: string;
  userId: number;
  userEmail: string;
  content: string;
  createdAt: Date | string;
  updatedAt: Date | string;
}
 
export interface ErrorCommentsTabProps {
  /** List of comments */
  comments: ErrorCommentData[];
  /** Callback when adding a new comment */
  onAddComment: (content: string) => Promise<void>;
  /** Whether a comment is being added */
  isAdding?: boolean;
  /** Total number of comments (for pagination) */
  total?: number;
  /** Callback to load more comments */
  onLoadMore?: () => void;
  /** Whether more comments are available */
  hasMore?: boolean;
}
 
function formatRelativeTime(date: Date | string): string {
  const d = typeof date === 'string' ? new Date(date) : date;
  return formatDistanceToNow(d, { addSuffix: true });
}
 
/**
 * ErrorCommentsTab Component
 *
 * Displays comments on an error and allows adding new comments.
 */
export function ErrorCommentsTab({
  comments,
  onAddComment,
  isAdding = false,
  total,
  onLoadMore,
  hasMore = false,
}: ErrorCommentsTabProps) {
  const [newComment, setNewComment] = useState('');
 
  const handleSubmit = async (e: React.FormEvent) => {
    e.preventDefault();
    if (!newComment.trim() || isAdding) return;
 
    await onAddComment(newComment.trim());
    setNewComment('');
  };
 
  return (
    <div className="space-y-4" data-testid="error-comments-tab">
      {/* Add Comment Form */}
      <form onSubmit={handleSubmit} className="space-y-2">
        <textarea
          value={newComment}
          onChange={(e) => setNewComment(e.target.value)}
          placeholder="Add a comment..."
          rows={3}
          disabled={isAdding}
          className="w-full p-3 border rounded-lg dark:bg-gray-700 dark:border-gray-600 dark:text-white placeholder-gray-400 dark:placeholder-gray-500 focus:ring-2 focus:ring-blue-500 focus:border-transparent resize-none"
          data-testid="comment-input"
        />
        <div className="flex justify-end">
          <Button
            type="submit"
            disabled={!newComment.trim() || isAdding}
            data-testid="add-comment-button"
          >
            {isAdding ? (
              <>
                <Icon name="loader" size={16} className="mr-1 animate-spin" />
                Adding...
              </>
            ) : (
              'Add Comment'
            )}
          </Button>
        </div>
      </form>
 
      {/* Comments List */}
      <div className="space-y-3" data-testid="comments-list">
        {comments.map((comment) => (
          <div
            key={comment.id}
            className="border-l-2 border-blue-500 pl-3 py-2"
            data-testid="comment-item"
          >
            <div className="flex items-center gap-2 text-sm text-gray-500 dark:text-gray-400">
              <span className="font-medium text-gray-700 dark:text-gray-300">
                {comment.userEmail}
              </span>
              <span>&middot;</span>
              <span title={new Date(comment.createdAt).toLocaleString()}>
                {formatRelativeTime(comment.createdAt)}
              </span>
            </div>
            <p className="mt-1 text-gray-900 dark:text-gray-200 whitespace-pre-wrap">
              {comment.content}
            </p>
          </div>
        ))}
 
        {comments.length === 0 && (
          <p className="text-gray-500 dark:text-gray-400 text-center py-8">
            No comments yet. Be the first to add one!
          </p>
        )}
 
        {/* Load More */}
        {hasMore && onLoadMore && (
          <div className="text-center pt-2">
            <Button variant="ghost" size="sm" onClick={onLoadMore}>
              Load more comments
            </Button>
          </div>
        )}
 
        {/* Total Count */}
        {total !== undefined && total > comments.length && (
          <p className="text-xs text-gray-500 dark:text-gray-400 text-center">
            Showing {comments.length} of {total} comments
          </p>
        )}
      </div>
    </div>
  );
}
 
export default ErrorCommentsTab;