All files / src/web/components GroupFilter.tsx

98.21% Statements 110/112
91.42% Branches 32/35
100% Functions 3/3
98.21% Lines 110/112

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 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162  1x 1x 1x                       1x 35x 35x 35x 35x 35x 35x 35x 35x 35x   35x 4x   3x 3x 1x 2x   4x 35x   35x 1x   1x 1x 35x   35x 1x 1x 35x   35x 35x 35x   35x 1x 1x   33x 33x   33x 33x 33x 33x 15x 15x 15x   35x   35x 15x 15x 15x 15x 15x 15x 15x   15x   35x     35x 4x 4x 4x 4x       35x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x 15x       35x 29x 29x 29x 29x   29x 29x 29x 29x 29x 29x 29x 29x   29x 26x 26x 26x 26x 26x 26x   26x 26x 26x     29x 2x 2x 2x 2x 2x 2x   2x   2x   29x       35x         35x   35x   1x  
import { NewsletterGroup } from '@common/types';
import { ChevronDown } from 'lucide-react';
import React, { useCallback, useState } from 'react';
import GroupBadgeList from './GroupBadgeList';
 
interface GroupFilterProps {
  groups: NewsletterGroup[];
  activeGroupIds: string[];
  onGroupFilterChange: (groupIds: string[]) => void;
  isLoading?: boolean;
  disabled?: boolean;
  maxVisible?: number;
  className?: string;
}
 
const GroupFilter: React.FC<GroupFilterProps> = ({
  groups,
  activeGroupIds,
  onGroupFilterChange,
  isLoading = false,
  disabled = false,
  maxVisible = 5,
  className = '',
}) => {
  const [isExpanded, setIsExpanded] = useState(false);
 
  const handleGroupClick = useCallback((groupId: string) => {
    if (disabled) return;
 
    const isActive = activeGroupIds.includes(groupId);
    const newActiveIds = isActive
      ? activeGroupIds.filter(id => id !== groupId)
      : [...activeGroupIds, groupId];
 
    onGroupFilterChange(newActiveIds);
  }, [activeGroupIds, onGroupFilterChange, disabled]);
 
  const handleGroupRemove = useCallback((groupId: string) => {
    if (disabled) return;
 
    const newActiveIds = activeGroupIds.filter(id => id !== groupId);
    onGroupFilterChange(newActiveIds);
  }, [activeGroupIds, onGroupFilterChange, disabled]);
 
  const handleClearAll = useCallback(() => {
    if (disabled) return;
    onGroupFilterChange([]);
  }, [onGroupFilterChange, disabled]);
 
  const hasActiveFilters = activeGroupIds.length > 0;
  const visibleGroups = isExpanded ? groups : groups.slice(0, maxVisible);
  const hasMoreGroups = groups.length > maxVisible;
 
  if (groups.length === 0 && !isLoading) {
    return null;
  }
 
  return (
    <div className={`space-y-3 ${className}`} data-testid="group-filter">
      {/* Header */}
      <div className="flex items-center justify-between">
        <div className="flex items-center gap-2">
          <h3 className="text-sm font-medium text-gray-700">Groups</h3>
          {hasActiveFilters && (
            <span className="text-xs text-gray-500 bg-gray-100 px-2 py-0.5 rounded-full">
              {activeGroupIds.length} active
            </span>
          )}
        </div>
 
        {hasActiveFilters && (
          <button
            type="button"
            onClick={handleClearAll}
            disabled={disabled}
            className="text-xs text-gray-500 hover:text-gray-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
            data-testid="clear-group-filters"
          >
            Clear all
          </button>
        )}
      </div>
 
      {/* Loading state */}
      {isLoading && (
        <div className="flex items-center justify-center py-4">
          <div className="animate-spin rounded-full h-4 w-4 border-b-2 border-primary-600"></div>
          <span className="ml-2 text-sm text-gray-500">Loading groups...</span>
        </div>
      )}
 
      {/* Active filters */}
      {hasActiveFilters && (
        <div className="space-y-2">
          <div className="text-xs text-gray-600 font-medium uppercase tracking-wide">Active Filters</div>
          <GroupBadgeList
            groups={groups.filter(group => activeGroupIds.includes(group.id))}
            activeGroupIds={activeGroupIds}
            variant="filter"
            size="sm"
            onGroupClick={handleGroupClick}
            onGroupRemove={handleGroupRemove}
          />
        </div>
      )}
 
      {/* Available groups */}
      {!isLoading && groups.length > 0 && (
        <div className="space-y-2">
          <div className="text-xs text-gray-600 font-medium uppercase tracking-wide">
            {hasActiveFilters ? 'More Groups' : 'All Groups'}
          </div>
 
          <GroupBadgeList
            groups={visibleGroups.filter(group => !activeGroupIds.includes(group.id))}
            activeGroupIds={activeGroupIds}
            variant="default"
            size="sm"
            onGroupClick={handleGroupClick}
            maxVisible={maxVisible}
          />
 
          {hasMoreGroups && !isExpanded && (
            <button
              type="button"
              onClick={() => setIsExpanded(true)}
              disabled={disabled}
              className="flex items-center gap-1 text-xs text-primary-600 hover:text-primary-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
              data-testid="show-more-groups"
            >
              <ChevronDown size={14} />
              Show {groups.length - maxVisible} more groups
            </button>
          )}
 
          {isExpanded && hasMoreGroups && (
            <button
              type="button"
              onClick={() => setIsExpanded(false)}
              disabled={disabled}
              className="flex items-center gap-1 text-xs text-primary-600 hover:text-primary-700 disabled:opacity-50 disabled:cursor-not-allowed transition-colors"
              data-testid="show-less-groups"
            >
              <ChevronDown size={14} className="rotate-180" />
              Show less
            </button>
          )}
        </div>
      )}
 
      {/* No groups state */}
      {!isLoading && groups.length === 0 && (
        <div className="text-center py-4 text-sm text-gray-500">
          No groups available. Create your first group to organize newsletters.
        </div>
      )}
    </div>
  );
};
 
export default GroupFilter;