# All files / src/app/user/search page.tsx

0% Statements 0/77
0% Branches 0/46
0% Functions 0/29
0% Lines 0/65

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
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    291
    292
    293
    294
    295
    296
    297
    298
    299
    300
    301
    302
    303
    304
    305
    306
    307
    308
    309
    310
    311
    312
    313
    314
    315
    316
    317
    318
    319
    320
    321
    322
    323
    324
    325
    326
    327
    328
    329
    330
    331
    332
    333
    334
    335
    336
    337
    338
    339
    340
    341
    342
    343
    344
    345
    346
    347
    348
    349
    350
    351
    352
    353
    354
    355
    356
    357
    358
    359
    360
    361
    362
    363
    364
    365
    366
    367
    368
    369
    370
    371
    372
    373
    374
    375
    376
    377
    378

     'use client';

    import React, { useState, useMemo, useCallback } from 'react';
    import { useTranslation } from 'react-i18next';
    import { useQuery } from '@tanstack/react-query';
    import { Search, Film, Tv, Radio, X, SlidersHorizontal } from 'lucide-react';
    import { Button } from '@/components/ui/button';
    import { Input } from '@/components/ui/input';
    import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from '@/components/ui/select';
    import { Badge } from '@/components/ui/badge';
    import ModernUserSidebar from '@/components/user/ModernUserSidebar';
    import { UserRoute } from '@/components/auth/ProtectedRoute';
    import { contentService } from '@/services';
    import { cn } from '@/lib/utils';
    import Link from 'next/link';
    import { GridSkeleton, ContentCardSkeleton } from '@/components/ui/skeleton-loader';
    import { getGridLayout } from '@/lib/grid-layouts';
    import { getCategoryColors, getCategoryLabel, normalizeCategory } from '@/constants/categoryColors';
    import { ResponsiveVirtualizedGrid, useVirtualization } from '@/components/ui/virtualized-grid';

    interface SearchFilters {
      query: string;
      category: string;
      year: string;
      sortBy: 'relevance' | 'title' | 'year' | 'rating';
    }

    export default function SearchPage() {
      const { t } = useTranslation();
      const [filters, setFilters] = useState<SearchFilters>({
        query: '',
        category: 'all',
        year: 'all',
        sortBy: 'relevance'});
      const [showFilters, setShowFilters] = useState(false);

      // Debounced search
      const [debouncedQuery, setDebouncedQuery] = useState('');

      // Debounce effect
      React.useEffect(() => {
        const timer = setTimeout(() => {
          setDebouncedQuery(filters.query);
        }, 300);

        return () => clearTimeout(timer);
      }, [filters.query]);

      // Search query
      const { data: results = [], isLoading } = useQuery({
        queryKey: ['user-search', debouncedQuery, filters.category],
        enabled: debouncedQuery.trim().length > 0,
        queryFn: async () => {
          const res = await contentService.searchContent({
            q: debouncedQuery.trim(),
            category: filters.category !== 'all' ? filters.category : undefined,
            limit: 50
          });
          if (res.success && res.data) {
            return res.data.content || [];
          }
          return [];
        }});

      // Filter and sort results
      const filteredResults = useMemo(() => {
        let filtered = [...results];

        // Filter by year
        if (filters.year !== 'all') {
          filtered = filtered.filter(item => item.year?.toString() === filters.year);
        }

        // Sort results
        switch (filters.sortBy) {
          case 'title':
            filtered.sort((a, b) => (a.title || '').localeCompare(b.title || ''));
            break;
          case 'year':
            filtered.sort((a, b) => (b.year || 0) - (a.year || 0));
            break;
          case 'rating':
            filtered.sort((a, b) => (b.year || 0) - (a.year || 0));
            break;
          default:
            // relevance - keep original order
            break;
        }

        return filtered;
      }, [results, filters.year, filters.sortBy]);

      // Get unique years from results
      const availableYears = useMemo(() => {
        const years = new Set(results.map(item => item.year).filter(Boolean));
        return Array.from(years).sort((a, b) => b! - a!);
      }, [results]);

      // Get category icon
      const getCategoryIcon = (category: string | null | undefined) => {
        const normalized = normalizeCategory(category);

        switch (normalized) {
          case 'series':
            return <Tv className="h-4 w-4" />;
          case 'live':
            return <Radio className="h-4 w-4" />;
          default:
            return <Film className="h-4 w-4" />;
        }
      };

      // Clear all filters
      const clearFilters = useCallback(() => {
        setFilters({
          query: filters.query,
          category: 'all',
          year: 'all',
          sortBy: 'relevance'});
      }, [filters.query]);

      const hasActiveFilters = filters.category !== 'all' || filters.year !== 'all' || filters.sortBy !== 'relevance';

      // Determine if we should use virtualization
      const shouldVirtualize = useVirtualization(filteredResults.length, 100);

      // Render item function for virtualized grid
      const renderContentItem = useCallback((item: any) => {
        const categoryKey = normalizeCategory(item?.category);
        const categoryColors = getCategoryColors(categoryKey);

        return (
          <Link
            key={item.id}
            href={`/user/content/${item.id}/watch`}
            className="group relative bg-slate-800/30 rounded-lg overflow-hidden hover:bg-slate-700/50 transition-all duration-200 hover:scale-105 h-full"
          >
            {/* Image */}
            <div className="aspect-[2/3] bg-gradient-to-br from-blue-600/20 to-purple-600/20 flex items-center justify-center relative">
              {item.image_url ? (
                <img
                  src={item.image_url}
                  alt={item.title}
                  className="w-full h-full object-cover"
                  loading="lazy"
                />
              ) : (
                getCategoryIcon(categoryKey)
              )}
              <div className="absolute inset-0 bg-black/0 group-hover:bg-black/40 transition-all duration-200" />
            </div>

            {/* Info */}
            <div className="p-3 space-y-2">
              <h3 className="font-medium text-white truncate text-sm">
                {item.title}
              </h3>

              <div className="flex items-center justify-between">
                {item.category && (
                  <span
                    className={cn(
                      'inline-flex items-center gap-1 px-2 py-0.5 rounded-full text-xs',
                      categoryColors.bg,
                      categoryColors.text
                    )}
                  >
                    {getCategoryIcon(categoryKey)}
                    {getCategoryLabel(item.category)}
                  </span>
                )}

                {item.year && (
                  <span className="text-xs text-slate-500">
                    {item.year}
                  </span>
                )}
              </div>
            </div>
          </Link>
        );
      }, []);

      return (
        <UserRoute>
          <div className="min-h-screen bg-gradient-to-br from-slate-900 via-slate-800 to-slate-900">
            <main className="flex-1 overflow-y-auto h-full">
              <div className="container mx-auto px-6 py-8">
                {/* Header */}
                <div className="mb-8">
                  <div className="flex items-center gap-3 mb-2">
                    <Search className="h-8 w-8 text-blue-500" />
                    <h1 className="text-4xl font-bold text-white">
                      {t('search.title')}
                    </h1>
                  </div>
                  <p className="text-slate-400">
                    {t('search.description')}
                  </p>
                </div>

                {/* Search Bar */}
                <div className="mb-6 space-y-4">
                  <div className="flex gap-3">
                    <div className="relative flex-1">
                      <Search className="absolute left-3 top-1/2 transform -translate-y-1/2 h-5 w-5 text-slate-400" />
                      <Input
                        type="text"
                        placeholder={t('search.placeholder')}
                        value={filters.query}
                        onChange={(e) => setFilters(prev => ({ ...prev, query: e.target.value }))}
                        className="pl-12 pr-12 h-12 bg-slate-800/50 border-slate-700/50 text-white placeholder:text-slate-400 text-lg"
                      />
                      {filters.query && (
                        <button
                          onClick={() => setFilters(prev => ({ ...prev, query: '' }))}
                          className="absolute right-3 top-1/2 transform -translate-y-1/2 text-slate-400 hover:text-white"
                        >
                          <X className="h-5 w-5" />
                        </button>
                      )}
                    </div>

                    <Button
                      onClick={() => setShowFilters(!showFilters)}
                      variant="ghost"
                      className={cn(
                        'h-12 border border-slate-600/50 bg-slate-800/60 text-slate-200 hover:bg-slate-700/80 hover:text-white',
                        hasActiveFilters && 'border-blue-500/60 bg-blue-600/20 text-blue-200'
                      )}
                    >
                      <SlidersHorizontal className="h-4 w-4 mr-2" />
                      {t('search.filters')}
                      {hasActiveFilters && (
                        <Badge className="ml-2 bg-blue-600 text-white">
                          {[filters.category !== 'all', filters.year !== 'all', filters.sortBy !== 'relevance'].filter(Boolean).length}
                        </Badge>
                      )}
                    </Button>
                  </div>

                  {/* Advanced Filters */}
                  {showFilters && (
                    <div className="p-4 rounded-lg bg-slate-800/30 border border-slate-700/50 space-y-4">
                      <div className="grid grid-cols-1 md:grid-cols-3 gap-4">
                        {/* Category Filter */}
                        <div>
                          <label className="text-sm font-medium text-slate-300 mb-2 block">
                            {t('search.category')}
                          </label>
                          <Select value={filters.category} onValueChange={(v) => setFilters(prev => ({ ...prev, category: v }))}>
                            <SelectTrigger className="bg-slate-800/50 border-slate-700/50">
                              <SelectValue />
                            </SelectTrigger>
                            <SelectContent>
                              <SelectItem value="all">{t('search.allCategories')}</SelectItem>
                              <SelectItem value="movie">{t('search.movies')}</SelectItem>
                              <SelectItem value="series">{t('search.series')}</SelectItem>
                              <SelectItem value="live">{t('search.live')}</SelectItem>
                              <SelectItem value="kids">{t('search.kids')}</SelectItem>
                              <SelectItem value="anime">{t('search.anime')}</SelectItem>
                              <SelectItem value="events">{t('search.events')}</SelectItem>
                            </SelectContent>
                          </Select>
                        </div>

                        {/* Year Filter */}
                        <div>
                          <label className="text-sm font-medium text-slate-300 mb-2 block">
                            {t('search.year')}
                          </label>
                          <Select value={filters.year} onValueChange={(v) => setFilters(prev => ({ ...prev, year: v }))}>
                            <SelectTrigger className="bg-slate-800/50 border-slate-700/50">
                              <SelectValue />
                            </SelectTrigger>
                            <SelectContent>
                              <SelectItem value="all">{t('search.allYears')}</SelectItem>
                              {availableYears.map(year => (
                                <SelectItem key={year} value={year!.toString()}>{year}</SelectItem>
                              ))}
                            </SelectContent>
                          </Select>
                        </div>

                        {/* Sort By */}
                        <div>
                          <label className="text-sm font-medium text-slate-300 mb-2 block">
                            {t('search.sortBy')}
                          </label>
                          <Select value={filters.sortBy} onValueChange={(v: any) => setFilters(prev => ({ ...prev, sortBy: v }))}>
                            <SelectTrigger className="bg-slate-800/50 border-slate-700/50">
                              <SelectValue />
                            </SelectTrigger>
                            <SelectContent>
                              <SelectItem value="relevance">{t('search.relevance')}</SelectItem>
                              <SelectItem value="title">{t('search.titleAZ')}</SelectItem>
                              <SelectItem value="year">{t('search.yearDesc')}</SelectItem>
                              <SelectItem value="rating">{t('search.ratingDesc')}</SelectItem>
                            </SelectContent>
                          </Select>
                        </div>
                      </div>

                      {hasActiveFilters && (
                        <Button onClick={clearFilters} variant="ghost" size="sm" className="border border-slate-600/50 bg-slate-800/60 text-slate-200 hover:bg-slate-700/80 hover:text-white">
                          <X className="h-4 w-4 mr-2" />
                          {t('search.clearFilters')}
                        </Button>
                      )}
                    </div>
                  )}
                </div>

                {/* Results */}
                {!filters.query.trim() ? (
                  <div className="text-center py-20">
                    <Search className="h-16 w-16 text-slate-600 mx-auto mb-4" />
                    <h3 className="text-xl font-semibold text-white mb-2">
                      {t('search.startSearching')}
                    </h3>
                    <p className="text-slate-400">
                      {t('search.startSearchingDescription')}
                    </p>
                  </div>
                ) : isLoading ? (
                  <GridSkeleton items={24} CardComponent={ContentCardSkeleton} />
                ) : filteredResults.length === 0 ? (
                  <div className="text-center py-20">
                    <Search className="h-16 w-16 text-slate-600 mx-auto mb-4" />
                    <h3 className="text-xl font-semibold text-white mb-2">
                      {t('search.noResults')}
                    </h3>
                    <p className="text-slate-400 mb-6">
                      {t('search.noResultsDescription')}
                    </p>
                    {hasActiveFilters && (
                      <Button onClick={clearFilters} variant="ghost" className="border border-slate-600/50 bg-slate-800/60 text-slate-200 hover:bg-slate-700/80 hover:text-white">
                        {t('search.clearFilters')}
                      </Button>
                    )}
                  </div>
                ) : (
                  <>
                    <div className="mb-4 flex items-center justify-between">
                      <p className="text-sm text-slate-400">
                        {filteredResults.length} {t('search.resultsFound')}
                        {shouldVirtualize && (
                          <span className="ml-2 text-xs text-blue-400">
                            ({t('search.virtualizedMode')})
                          </span>
                        )}
                      </p>
                    </div>

                    {shouldVirtualize ? (
                      <div style={{ height: 'calc(100vh - 400px)', minHeight: '600px' }}>
                        <ResponsiveVirtualizedGrid
                          items={filteredResults}
                          renderItem={renderContentItem}
                          minColumnWidth={180}
                          rowHeight={320}
                          gap={16}
                        />
                      </div>
                    ) : (
                      <div className={getGridLayout('content')}>
                        {filteredResults.map((item: any) => renderContentItem(item))}
                      </div>
                    )}
                  </>
                )}
              </div>
            </main>
          </div>
        </UserRoute>
      );
    }