|
| 1 | +import { useState, useMemo, useCallback } from 'react'; |
| 2 | +import { Subscription, SubscriptionCategory, BillingCycle } from '../types/subscription'; |
| 3 | + |
| 4 | +export const useFilteredSubscriptions = (subscriptions: Subscription[]) => { |
| 5 | + const [searchQuery, setSearchQuery] = useState(''); |
| 6 | + const [selectedCategories, setSelectedCategories] = useState<SubscriptionCategory[]>([]); |
| 7 | + const [selectedBillingCycles, setSelectedBillingCycles] = useState<BillingCycle[]>([]); |
| 8 | + const [priceRange, setPriceRange] = useState({ min: 0, max: 1000 }); |
| 9 | + const [showActiveOnly, setShowActiveOnly] = useState(true); |
| 10 | + const [showCryptoOnly, setShowCryptoOnly] = useState(false); |
| 11 | + const [sortBy, setSortBy] = useState<'name' | 'price' | 'nextBilling' | 'category'>('name'); |
| 12 | + const [sortOrder, setSortOrder] = useState<'asc' | 'desc'>('asc'); |
| 13 | + |
| 14 | + const normalizedSearch = useMemo(() => searchQuery.trim().toLowerCase(), [searchQuery]); |
| 15 | + |
| 16 | + const matchesSearch = useCallback( |
| 17 | + (sub: Subscription): boolean => { |
| 18 | + if (!normalizedSearch) return true; |
| 19 | + return ( |
| 20 | + sub.name.toLowerCase().includes(normalizedSearch) || |
| 21 | + sub.description?.toLowerCase().includes(normalizedSearch) === true |
| 22 | + ); |
| 23 | + }, |
| 24 | + [normalizedSearch] |
| 25 | + ); |
| 26 | + |
| 27 | + const matchesCategory = useCallback( |
| 28 | + (sub: Subscription): boolean => { |
| 29 | + if (selectedCategories.length === 0) return true; |
| 30 | + return selectedCategories.includes(sub.category); |
| 31 | + }, |
| 32 | + [selectedCategories] |
| 33 | + ); |
| 34 | + |
| 35 | + const matchesBillingCycle = useCallback( |
| 36 | + (sub: Subscription): boolean => { |
| 37 | + if (selectedBillingCycles.length === 0) return true; |
| 38 | + return selectedBillingCycles.includes(sub.billingCycle); |
| 39 | + }, |
| 40 | + [selectedBillingCycles] |
| 41 | + ); |
| 42 | + |
| 43 | + const matchesPriceRange = useCallback( |
| 44 | + (sub: Subscription): boolean => sub.price >= priceRange.min && sub.price <= priceRange.max, |
| 45 | + [priceRange.max, priceRange.min] |
| 46 | + ); |
| 47 | + |
| 48 | + const matchesActiveOnly = useCallback( |
| 49 | + (sub: Subscription): boolean => !showActiveOnly || sub.isActive, |
| 50 | + [showActiveOnly] |
| 51 | + ); |
| 52 | + |
| 53 | + const matchesCryptoOnly = useCallback( |
| 54 | + (sub: Subscription): boolean => !showCryptoOnly || sub.isCryptoEnabled, |
| 55 | + [showCryptoOnly] |
| 56 | + ); |
| 57 | + |
| 58 | + const comparator = useCallback( |
| 59 | + (a: Subscription, b: Subscription): number => { |
| 60 | + let comp = 0; |
| 61 | + switch (sortBy) { |
| 62 | + case 'name': |
| 63 | + comp = a.name.localeCompare(b.name); |
| 64 | + break; |
| 65 | + case 'price': |
| 66 | + comp = a.price - b.price; |
| 67 | + break; |
| 68 | + case 'nextBilling': |
| 69 | + comp = new Date(a.nextBillingDate).getTime() - new Date(b.nextBillingDate).getTime(); |
| 70 | + break; |
| 71 | + case 'category': |
| 72 | + comp = a.category.localeCompare(b.category); |
| 73 | + break; |
| 74 | + } |
| 75 | + return sortOrder === 'asc' ? comp : -comp; |
| 76 | + }, |
| 77 | + [sortBy, sortOrder] |
| 78 | + ); |
| 79 | + |
| 80 | + const searchedSubscriptions = useMemo( |
| 81 | + () => (subscriptions || []).filter(matchesSearch), |
| 82 | + [subscriptions, matchesSearch] |
| 83 | + ); |
| 84 | + |
| 85 | + const categoryFilteredSubscriptions = useMemo( |
| 86 | + () => searchedSubscriptions.filter(matchesCategory), |
| 87 | + [searchedSubscriptions, matchesCategory] |
| 88 | + ); |
| 89 | + |
| 90 | + const billingCycleFilteredSubscriptions = useMemo( |
| 91 | + () => categoryFilteredSubscriptions.filter(matchesBillingCycle), |
| 92 | + [categoryFilteredSubscriptions, matchesBillingCycle] |
| 93 | + ); |
| 94 | + |
| 95 | + const priceFilteredSubscriptions = useMemo( |
| 96 | + () => billingCycleFilteredSubscriptions.filter(matchesPriceRange), |
| 97 | + [billingCycleFilteredSubscriptions, matchesPriceRange] |
| 98 | + ); |
| 99 | + |
| 100 | + const activeFilteredSubscriptions = useMemo( |
| 101 | + () => priceFilteredSubscriptions.filter(matchesActiveOnly), |
| 102 | + [priceFilteredSubscriptions, matchesActiveOnly] |
| 103 | + ); |
| 104 | + |
| 105 | + const cryptoFilteredSubscriptions = useMemo( |
| 106 | + () => activeFilteredSubscriptions.filter(matchesCryptoOnly), |
| 107 | + [activeFilteredSubscriptions, matchesCryptoOnly] |
| 108 | + ); |
| 109 | + |
| 110 | + const filteredAndSorted = useMemo( |
| 111 | + () => [...cryptoFilteredSubscriptions].sort(comparator), |
| 112 | + [cryptoFilteredSubscriptions, comparator] |
| 113 | + ); |
| 114 | + |
| 115 | + const clearAllFilters = useCallback(() => { |
| 116 | + setSearchQuery(''); |
| 117 | + setSelectedCategories([]); |
| 118 | + setSelectedBillingCycles([]); |
| 119 | + setPriceRange({ min: 0, max: 1000 }); |
| 120 | + setShowActiveOnly(true); |
| 121 | + setShowCryptoOnly(false); |
| 122 | + setSortBy('name'); |
| 123 | + setSortOrder('asc'); |
| 124 | + }, []); |
| 125 | + |
| 126 | + // Basic in-dev profiling: logs expensive filter passes on large lists. |
| 127 | + useMemo(() => { |
| 128 | + if (__DEV__ && (subscriptions?.length ?? 0) >= 200) { |
| 129 | + console.debug('[useFilteredSubscriptions] recalculated', { |
| 130 | + total: subscriptions.length, |
| 131 | + filtered: filteredAndSorted.length, |
| 132 | + }); |
| 133 | + } |
| 134 | + }, [subscriptions, filteredAndSorted.length]); |
| 135 | + |
| 136 | + const activeFilterCount = useMemo(() => { |
| 137 | + let count = 0; |
| 138 | + if (searchQuery.trim()) count++; |
| 139 | + if (selectedCategories.length > 0) count++; |
| 140 | + if (selectedBillingCycles.length > 0) count++; |
| 141 | + if (priceRange.min > 0 || priceRange.max < 1000) count++; |
| 142 | + if (!showActiveOnly) count++; |
| 143 | + if (showCryptoOnly) count++; |
| 144 | + if (sortBy !== 'name' || sortOrder !== 'asc') count++; |
| 145 | + return count; |
| 146 | + }, [ |
| 147 | + searchQuery, |
| 148 | + selectedCategories, |
| 149 | + selectedBillingCycles, |
| 150 | + priceRange, |
| 151 | + showActiveOnly, |
| 152 | + showCryptoOnly, |
| 153 | + sortBy, |
| 154 | + sortOrder, |
| 155 | + ]); |
| 156 | + |
| 157 | + return { |
| 158 | + filters: { |
| 159 | + searchQuery, |
| 160 | + setSearchQuery, |
| 161 | + selectedCategories, |
| 162 | + setSelectedCategories, |
| 163 | + selectedBillingCycles, |
| 164 | + setSelectedBillingCycles, |
| 165 | + priceRange, |
| 166 | + setPriceRange, |
| 167 | + showActiveOnly, |
| 168 | + setShowActiveOnly, |
| 169 | + showCryptoOnly, |
| 170 | + setShowCryptoOnly, |
| 171 | + sortBy, |
| 172 | + setSortBy, |
| 173 | + sortOrder, |
| 174 | + setSortOrder, |
| 175 | + }, |
| 176 | + filteredAndSorted, |
| 177 | + activeFilterCount, |
| 178 | + hasActiveFilters: activeFilterCount > 0, |
| 179 | + clearAllFilters, |
| 180 | + }; |
| 181 | +}; |
0 commit comments