|
1 | 1 | import { useStorage } from '@plasmohq/storage/hook'; |
2 | | -import type { Streamer } from '@/lib/types'; |
| 2 | +import type { SortOption, Streamer } from '@/lib/types'; |
| 3 | +import { sortStreamers } from '@/lib/utils'; |
3 | 4 | import { StreamerCard } from './streamer-card'; |
4 | 5 |
|
5 | | -export function StreamerList({ streamers, favoriteOnly = false, offlineOnly = false }: { streamers: Streamer[], favoriteOnly?: boolean, offlineOnly?: boolean }) { |
| 6 | +export function StreamerList({ |
| 7 | + streamers, |
| 8 | + favoriteOnly = false, |
| 9 | + liveOnly = false, |
| 10 | + sortBy = 'default', |
| 11 | +}: { |
| 12 | + streamers: Streamer[]; |
| 13 | + favoriteOnly?: boolean; |
| 14 | + liveOnly?: boolean; |
| 15 | + sortBy?: SortOption; |
| 16 | +}) { |
6 | 17 | const [favorites, setFavorites] = useStorage<Streamer['id'][]>( |
7 | 18 | 'favorites', |
8 | 19 | [] |
9 | 20 | ); |
| 21 | + const [blockedStreamers, setBlockedStreamers] = useStorage<Streamer['id'][]>( |
| 22 | + 'blocked', |
| 23 | + [] |
| 24 | + ); |
10 | 25 |
|
11 | | - const filteredStreamers = streamers.filter((streamer) => { |
12 | | - if (favoriteOnly) { |
13 | | - return favorites.includes(streamer.id); |
14 | | - } |
15 | | - if (offlineOnly) { |
16 | | - return !streamer.isLive; |
17 | | - } |
18 | | - return true; |
19 | | - }); |
| 26 | + const filteredAndSortedStreamers = sortStreamers( |
| 27 | + streamers.filter((streamer) => { |
| 28 | + if (favoriteOnly) { |
| 29 | + return ( |
| 30 | + favorites.includes(streamer.id) && |
| 31 | + !blockedStreamers.includes(streamer.id) |
| 32 | + ); |
| 33 | + } |
| 34 | + |
| 35 | + if (liveOnly) { |
| 36 | + return streamer.isLive && !blockedStreamers.includes(streamer.id); |
| 37 | + } |
| 38 | + return true; |
| 39 | + }), |
| 40 | + sortBy |
| 41 | + ); |
20 | 42 |
|
21 | 43 | return ( |
22 | | - <div className="w-full flex flex-col gap-4 overflow-y-auto h-[320px]"> |
23 | | - {filteredStreamers.length === 0 && ( |
| 44 | + <div className="w-full flex flex-col gap-4 overflow-y-auto h-[320px] overflow-x-hidden"> |
| 45 | + {filteredAndSortedStreamers.length === 0 && ( |
24 | 46 | <div className="w-full flex flex-col gap-4 items-center justify-center"> |
25 | | - <p className="text-sm text-muted-foreground"> |
26 | | - No streamers found. |
27 | | - </p> |
| 47 | + <p className="text-sm text-muted-foreground">No streamers found.</p> |
28 | 48 | </div> |
29 | 49 | )} |
30 | | - {filteredStreamers.map((streamer) => ( |
| 50 | + {filteredAndSortedStreamers.map((streamer) => ( |
31 | 51 | <StreamerCard |
| 52 | + isBlocked={blockedStreamers.includes(streamer.id)} |
32 | 53 | isFavorite={favorites.includes(streamer.id)} |
33 | 54 | key={streamer.id} |
| 55 | + onBlock={(streamer) => { |
| 56 | + setBlockedStreamers((blocked) => { |
| 57 | + const newBlocked = [...blocked]; |
| 58 | + if (newBlocked.includes(streamer.id)) { |
| 59 | + newBlocked.splice(newBlocked.indexOf(streamer.id), 1); |
| 60 | + } else { |
| 61 | + newBlocked.push(streamer.id); |
| 62 | + } |
| 63 | + return newBlocked; |
| 64 | + }); |
| 65 | + }} |
34 | 66 | onFavorite={(streamer) => { |
35 | 67 | setFavorites((favorites) => { |
36 | 68 | const newFavorites = [...favorites]; |
|
0 commit comments