All files / src/components/CommentCard index.tsx

100% Statements 98/98
100% Branches 14/14
100% Functions 2/2
100% Lines 98/98

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  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 81x 81x 81x 45x 28x 28x                                 1x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x   81x 81x 81x 81x   81x 81x 22x   59x 59x 59x 59x 59x 59x 59x 59x   81x   81x 81x 81x 81x   81x 81x 81x 81x 81x   81x 81x 81x   81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x 81x   81x 81x 81x   81x  
// Types
import {
  BaseVariant,
  FeedbackAnonymousName,
  FeedbackRating,
  Sentiment,
} from '@/types';
 
// Utils
import { combineClasses } from '@shared/utils';
import { formatAbsoluteTime, formatShortDate } from '@/utils';
 
// Components
import { ImageFallback } from '@/components/common';
import { ProfileCircleIcon } from '@/components/icons/reacts';
import { CommentSelectCheckbox } from '@/components/CommentSelectCheckbox';
 
// Constants
import { FALLBACK_SRC } from '@shared/constants';
import { RATING_PENDING_LABEL } from '@/constants/dailyFeedback';
 
// Styles
import './styles/comment-card.css';
 
const ratingClass: Record<FeedbackRating, string> = {
  [FeedbackRating.GOOD]: 'comment-card-rating-good',
  [FeedbackRating.BAD]: 'comment-card-rating-bad',
};
 
const variantTheme = {
  [BaseVariant.PRIMARY]: {
    root: 'comment-card-root',
    avatarContainer: 'comment-card-avatar-container',
    avatarFull: 'comment-card-avatar-full',
    avatarIcon: 'comment-card-avatar-icon',
    body: 'comment-card-body',
    header: 'comment-card-header',
    left: 'comment-card-left',
    name: 'comment-card-name',
    ratingBadge: 'comment-card-rating-badge',
    ratingPending: 'comment-card-rating-pending',
    right: 'comment-card-right',
    time: 'comment-card-time',
    content: 'comment-card-content',
  },
};
 
const sentimentToRating = (
  s: string | null | undefined,
): FeedbackRating | null => {
  if (s === Sentiment.POSITIVE) return FeedbackRating.GOOD;
  if (s === Sentiment.NEGATIVE) return FeedbackRating.BAD;
  return null;
};
 
export interface CommentCardProps {
  name: string;
  avatarUrl: string;
  isAnonymous?: boolean;
  feedback: string;
  createdTime: string;
  feedbackSentiment?: string | null;
  isSelected?: boolean;
  isLoading?: boolean;
  disabled?: boolean;
  onToggleSelect?: () => void;
  className?: string;
  variant?: BaseVariant;
}
 
export const CommentCard = ({
  name,
  avatarUrl,
  isAnonymous = false,
  feedback,
  createdTime,
  feedbackSentiment,
  isSelected = false,
  isLoading = false,
  disabled = false,
  onToggleSelect,
  className = '',
  variant = BaseVariant.PRIMARY,
}: CommentCardProps) => {
  const theme = variantTheme[variant];
  const displayName = isAnonymous ? FeedbackAnonymousName.ANONYMOUS : name;
  const rating = sentimentToRating(feedbackSentiment);
 
  return (
    <div
      data-variant={variant}
      className={combineClasses(theme.root, className)}
    >
      <div className={theme.avatarContainer}>
        {isAnonymous ? (
          <ProfileCircleIcon className={theme.avatarIcon} />
        ) : (
          <ImageFallback
            src={avatarUrl}
            alt={displayName}
            width={52}
            height={52}
            className={theme.avatarFull}
            fallbackSrc={FALLBACK_SRC.AVATAR}
          />
        )}
      </div>
 
      <div className={theme.body}>
        <div className={theme.header}>
          <div className={theme.left}>
            <span className={theme.name}>{displayName}</span>
 
            <span
              className={combineClasses(
                theme.ratingBadge,
                rating ? ratingClass[rating] : theme.ratingPending,
              )}
            >
              {rating ?? RATING_PENDING_LABEL}
            </span>
          </div>
 
          <div className={theme.right}>
            <span className={theme.time}>
              {formatShortDate(createdTime)}, {formatAbsoluteTime(createdTime)}
            </span>
            <CommentSelectCheckbox
              isSelected={isSelected}
              isLoading={isLoading}
              disabled={disabled}
              onToggleSelect={onToggleSelect}
            />
          </div>
        </div>
 
        <p className={theme.content}>{feedback}</p>
      </div>
    </div>
  );
};