All files / src/components/public FilterSidebar.tsx

100% Statements 24/24
96.29% Branches 26/27
100% Functions 12/12
100% Lines 23/23

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 163 164 165 166                                            2x                             116x 116x     116x 74x 3x     74x         116x 59x     116x 6x 6x 1x     6x       116x     116x     116x                                                                 16x                               377x   377x         6x                                               3x         348x                  
/**
 * FilterSidebar Component (Story 4.2 - Task 2b)
 *
 * Desktop filter sidebar for archive browsing
 * Handles time period, topic, search, and sort filters
 */
 
import { useState, useEffect } from 'react';
import { useTranslation } from 'react-i18next';
import type { ArchiveFilters } from '@/types/event.types';
import type { Topic } from '@/types/topic.types';
 
interface FilterSidebarProps {
  filters: ArchiveFilters;
  topics: Topic[];
  onFilterChange: (filters: ArchiveFilters) => void;
  onClearFilters: () => void;
  onSortChange: (sort: string) => void;
  currentSort: string;
  loading?: boolean;
}
 
const SORT_OPTIONS = [
  { value: '-date', label: 'archive.sort.newest' },
  { value: 'date', label: 'archive.sort.oldest' },
  { value: '-attendance', label: 'archive.sort.mostAttended' },
];
 
export function FilterSidebar({
  filters,
  topics,
  onFilterChange,
  onClearFilters,
  onSortChange,
  currentSort,
  loading = false,
}: FilterSidebarProps) {
  const { t } = useTranslation();
  const [searchValue, setSearchValue] = useState(filters.search || '');
 
  // Debounce search input (300ms)
  useEffect(() => {
    const timer = setTimeout(() => {
      onFilterChange({ ...filters, search: searchValue });
    }, 300);
 
    return () => clearTimeout(timer);
    // eslint-disable-next-line react-hooks/exhaustive-deps -- intentional: debounce fires only on searchValue change
  }, [searchValue]);
 
  // Sync search value with filters prop
  useEffect(() => {
    setSearchValue(filters.search || '');
  }, [filters.search]);
 
  const handleTopicToggle = (topicCode: string) => {
    const currentTopics = filters.topics || [];
    const newTopics = currentTopics.includes(topicCode)
      ? currentTopics.filter((t) => t !== topicCode)
      : [...currentTopics, topicCode];
 
    onFilterChange({ ...filters, topics: newTopics });
  };
 
  const hasActiveFilters =
    (filters.topics && filters.topics.length > 0) || (filters.search && filters.search.length > 0);
 
  // Count active filters
  const activeFilterCount =
    (filters.topics?.length || 0) + (filters.search && filters.search.length > 0 ? 1 : 0);
 
  return (
    <aside
      className="space-y-6"
      role="complementary"
      aria-label={t('filters.ariaLabel')}
      data-testid="filter-sidebar"
    >
      {/* Header */}
      <div className="flex items-center justify-between">
        <div>
          <h2 className="text-lg font-semibold">{t('archive.filters.title')}</h2>
          {activeFilterCount > 0 && (
            <p className="text-xs text-muted-foreground mt-1">
              {t('archive.filters.activeCount', { count: activeFilterCount })}
            </p>
          )}
        </div>
        <button
          onClick={onClearFilters}
          disabled={!hasActiveFilters}
          className="text-sm text-primary hover:text-primary/80 disabled:text-muted-foreground disabled:cursor-not-allowed"
          data-testid="clear-filters"
        >
          {t('archive.filters.clearAll')}
        </button>
      </div>
 
      {/* Search */}
      <div>
        <input
          type="text"
          placeholder={t('archive.filters.search')}
          value={searchValue}
          onChange={(e) => setSearchValue(e.target.value)}
          className="w-full px-3 py-2 border border-border rounded-md focus:outline-none focus:ring-2 focus:ring-ring bg-background text-foreground placeholder:text-muted-foreground"
          data-testid="search-input"
        />
      </div>
 
      {/* Topics */}
      <div data-testid="topic-filter">
        <h3 className="text-sm font-medium text-foreground mb-2">{t('archive.filters.topics')}</h3>
        <div className="space-y-2">
          {loading ? (
            <div className="text-sm text-muted-foreground">{t('archive.loadingTopics')}</div>
          ) : topics.length === 0 ? (
            <div className="text-sm text-muted-foreground">{t('archive.noTopicsAvailable')}</div>
          ) : (
            topics.map((topic) => {
              const isChecked = filters.topics?.includes(topic.topicCode) || false;
 
              return (
                <label key={topic.topicCode} className="flex items-center cursor-pointer">
                  <input
                    type="checkbox"
                    checked={isChecked}
                    onChange={() => handleTopicToggle(topic.topicCode)}
                    aria-label={`${topic.title} (${topic.usageCount})`}
                    data-testid={`topic-checkbox-${topic.topicCode}`}
                    className="h-4 w-4 text-primary focus:ring-ring border-border rounded"
                  />
                  <span className="ml-2 text-sm text-foreground">
                    {topic.title} ({topic.usageCount})
                  </span>
                </label>
              );
            })
          )}
        </div>
      </div>
 
      {/* Sort */}
      <div>
        <label htmlFor="sort-select" className="block text-sm font-medium text-foreground mb-2">
          {t('labels.sortBy')}
        </label>
        <select
          id="sort-select"
          data-testid="sort-select"
          value={currentSort}
          onChange={(e) => onSortChange(e.target.value)}
          aria-label={t('labels.sortBy')}
          className="w-full px-3 py-2 border border-border rounded-md focus:outline-none focus:ring-2 focus:ring-ring bg-background text-foreground"
        >
          {SORT_OPTIONS.map((option) => (
            <option key={option.value} value={option.value}>
              {t(option.label)}
            </option>
          ))}
        </select>
      </div>
    </aside>
  );
}