All files / src/shared/components/FiltersPanel FiltersPanel.tsx

87.9% Statements 109/124
91.66% Branches 22/24
66.66% Functions 2/3
87.9% Lines 109/124

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 167 168 169 170 1711x 1x 1x 1x 1x 1x 1x 1x 1x 1x                                         1x 32x 32x 32x 32x 32x 32x 32x   32x   32x 32x   32x 14x 32x   32x 14x 32x   32x 14x 1x 1x 14x   14x 32x   32x 1x 1x 1x     1x 1x 1x 1x 32x   32x       32x   32x 32x 32x 32x   32x 32x 32x 32x 32x 32x 32x 32x 32x   32x 31x 31x 31x 31x 31x 31x 31x 31x 31x     32x 32x 31x 31x 31x 31x 31x 31x 31x   31x   31x 81x 81x 81x 431x 431x 431x 431x 431x 431x 431x 431x 431x   431x 431x   81x 81x 81x 31x 31x   32x   32x 32x                           32x   32x 31x 31x 31x   32x   32x   1x  
import React, { useCallback, useEffect, useMemo, useState } from 'react';
import Collapse from '@/shared/components/collapse/Collapse';
import Button, { ButtonVariant } from '@/shared/components/button/Button';
import { AnimatePresence, motion } from 'framer-motion';
import { FaAngleLeft } from 'react-icons/fa';
import classNames from 'classnames';
import { useFiltersPanelState } from './useFiltersPanelState';
import FiltersPanelHeader from './FiltersPanelHeader';
import { useFallbackTranslation } from '@/hooks/useFallbackTranslation';
import './FiltersPanel.scss';
 
export type FilterConfig = {
  [filterKey: string]: {
    value: string[];
    multiple?: boolean;
  };
};
 
export type MatchModeTypes = 'equals' | 'in';
 
export type FiltersOutput = Record<string, { value: string[]; matchMode: MatchModeTypes }>;
 
interface FiltersPanelProps {
  config: FilterConfig;
  onChange: (filters: FiltersOutput) => void;
  isPinned?: boolean;
  onPinChange?: (pinned: boolean) => void;
  onCollapseChange?: (collapsed: boolean) => void;
}
 
const FiltersPanel: React.FC<FiltersPanelProps> = ({
  config,
  onChange,
  isPinned: externallyPinned = false,
  onPinChange,
  onCollapseChange,
}) => {
  const [selected, setSelected] = useState<Record<string, Set<string>>>({});
 
  const { t } = useFallbackTranslation();
 
  const { collapsed, contentVisible, tabVisible, isPinned, toggleCollapse, togglePin, handleTabVisible } =
    useFiltersPanelState(externallyPinned);
 
  useEffect(() => {
    onPinChange?.(isPinned);
  }, [isPinned]);
 
  useEffect(() => {
    onCollapseChange?.(collapsed);
  }, [collapsed]);
 
  useEffect(() => {
    const filters: FiltersOutput = Object.entries(selected).reduce((acc, [key, set]) => {
      acc[key] = { value: Array.from(set), matchMode: 'in' };
      return acc;
    }, {} as FiltersOutput);
 
    onChange(filters);
  }, [selected]);
 
  const handleToggle = useCallback((category: string, value: string, isMultiple = false) => {
    setSelected((prev) => {
      const current = new Set(prev[category] ?? []);
      if (isMultiple) {
        current.has(value) ? current.delete(value) : current.add(value);
        return { ...prev, [category]: current };
      } else {
        return { ...prev, [category]: new Set([value]) };
      }
    });
  }, []);
 
  const handleClear = () => {
    setSelected({});
  };
 
  const panelWidth = useMemo(() => (collapsed ? 0 : 240), [collapsed]);
 
  const containerClass = classNames('filters-panel-box', {
    fixed: isPinned,
    collapsed: collapsed,
  });
 
  return (
    <motion.aside
      role="complementary"
      aria-labelledby="filters-panel-heading"
      initial={{ width: panelWidth }}
      animate={{ width: panelWidth }}
      transition={{ duration: 0.5 }}
      onAnimationComplete={handleTabVisible}
      className={containerClass}
    >
      {!collapsed && (
        <div className="filters-panel-toggle">
          <Button
            className="btn-pined"
            name={t(`common.buttons.${isPinned ? 'unPin' : 'pin'}`)}
            variant={ButtonVariant.SECONDARY}
            size="xs"
            handleClick={togglePin}
          />
        </div>
      )}
 
      <AnimatePresence>
        {contentVisible && (
          <motion.div
            key="filters-content"
            className="filters-panel"
            initial={{ opacity: 0 }}
            animate={{ opacity: 1 }}
            exit={{ opacity: 0 }}
            transition={{ duration: 0.25 }}
          >
            <FiltersPanelHeader onClear={handleClear} />
 
            {Object.entries(config).map(([category, { value, multiple = false }]) => (
              <Collapse key={category} header={category}>
                <div className="filters-panel__options">
                  {value.map((val) => {
                    const isActive = selected[category]?.has(val);
                    return (
                      <Button
                        key={val}
                        variant={ButtonVariant.SECONDARY}
                        className={isActive ? 'active' : ''}
                        handleClick={() => handleToggle(category, val, multiple)}
                        aria-label={`${category}-${val}`}
                        data-testid={`${category}-${val}`}
                      >
                        {val}
                      </Button>
                    );
                  })}
                </div>
              </Collapse>
            ))}
          </motion.div>
        )}
      </AnimatePresence>
 
      <AnimatePresence>
        {tabVisible && collapsed && (
          <motion.div
            className="filters-tab"
            onClick={toggleCollapse}
            initial={{ opacity: 0, x: -40 }}
            animate={{ opacity: 1, x: 0 }}
            exit={{ opacity: 0, x: -40 }}
            transition={{ duration: 0.4 }}
            aria-label="Filters open"
            role="button"
          >
            Filters
          </motion.div>
        )}
      </AnimatePresence>
 
      {!collapsed && (
        <Button className="btn-collapse" aria-label="collapse" variant={ButtonVariant.ROUND} size="xs" handleClick={toggleCollapse}>
          <FaAngleLeft className={classNames('icon-collapse', { open: collapsed })} />
        </Button>
      )}
    </motion.aside>
  );
};
 
export default FiltersPanel;