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

0% Statements 0/48
0% Branches 0/36
0% Functions 0/10
0% Lines 0/46

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

     'use client';

    import { useMemo, useState, useEffect, Suspense } from 'react';
    import Link from 'next/link';
    import { useRouter, useSearchParams } from 'next/navigation';
    import { useQuery } from '@tanstack/react-query';
    import { useTranslation } from 'react-i18next';
    import { Search, Clapperboard, Film, Tv } from 'lucide-react';

    import { UserRoute } from '@/components/auth/ProtectedRoute';
    import UserSidebar from '@/components/user/UserSidebar';
    import { Button } from '@/components/ui/button';
    import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
    import { Input } from '@/components/ui/input';
    import { Badge } from '@/components/ui/badge';
    import { EmptyState } from '@/components/ui/empty-state';
    import { ROUTES } from '@/constants/app';
    import { contentService } from '@/services';
    import { apiErrorMessage } from '@/lib/utils';
    import type { DeliveryContent } from '@/types';

    function ContentSearchPageInner() {
      const { t } = useTranslation();
      const router = useRouter();
      const params = useSearchParams();

      const initialQ = useMemo(() => params.get('q') || '', [params]);
      const [q, setQ] = useState<string>(initialQ);

      useEffect(() => {
        // Keep local state in sync if user navigates with back/forward and query changes
        const current = params.get('q') || '';
        setQ(current);
      }, [params]);

      const { data: results = [], isFetching, isError, error } = useQuery({
        queryKey: ['content', 'search', q],
        enabled: q.trim().length > 0,
        queryFn: async (): Promise<DeliveryContent[]> => {
          const res = await contentService.searchContent({ q: q.trim(), limit: 30 });
          if (res.success && res.data) return res.data.content as DeliveryContent[];
          throw new Error(apiErrorMessage(res.error, t('common.serverError')));
        },
        staleTime: 2 * 60 * 1000});

      const submitSearch = (next: string) => {
        const trimmed = (next || '').trim();
        const base = ROUTES.CONTENT.SEARCH;
        const url = trimmed ? `${base}?q=${encodeURIComponent(trimmed)}` : base;
        router.push(url);
      };

      const onSubmit = (e: React.FormEvent) => {
        e.preventDefault();
        submitSearch(q);
      };

      const mediaIcon = (c?: DeliveryContent) => {
        const categoryType = String(c?.category?.category_type || c?.category?.name || '').toLowerCase();
        if (categoryType === 'tv') return <Tv className="h-4 w-4" />;
        if (categoryType === 'series') return <Clapperboard className="h-4 w-4" />;
        return <Film className="h-4 w-4" />;
      };

      return (
        <UserRoute>
          <div className="min-h-screen bg-slate-950 text-white">
            <div className="grid min-h-screen md:grid-cols-[260px_1fr]">
              <UserSidebar />
              <main className="relative overflow-x-hidden pb-24">
                <div className="mx-auto w-full max-w-6xl px-6 py-8 sm:px-10">
                  <header className="mb-6 space-y-3">
                    <Card className="border-slate-900 bg-slate-900/60">
                      <CardHeader>
                        <CardTitle className="flex items-center gap-2 text-lg text-white">
                          <Search className="h-5 w-5" />
                          {t('user.content.search.title', {})}
                        </CardTitle>
                      </CardHeader>
                      <CardContent>
                        <form onSubmit={onSubmit} className="flex flex-col gap-3 sm:flex-row">
                          <div className="relative flex-1">
                            <Search className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-slate-500" />
                            <Input
                              value={q}
                              onChange={(e) => setQ(e.target.value)}
                              placeholder={t('user.content.search.placeholder', {})}
                              className="h-10 bg-slate-950/50 pl-9 text-white placeholder:text-slate-500"
                            />
                          </div>
                          <Button type="submit" className="sm:w-auto">
                            {t('common.search', {})}
                          </Button>
                        </form>
                      </CardContent>
                    </Card>
                  </header>

                  <section>
                    {!q.trim().length ? (
                      <div className="py-16">
                        <EmptyState
                          title={t('user.content.search.emptyTitle', {})}
                          description={t('user.content.search.emptyDesc', {
                          })}
                        />
                      </div>
                    ) : isFetching ? (
                      <div className="py-16">
                        <EmptyState
                          title={t('common.loading', {})}
                          description={t('user.content.search.loading', {})}
                        />
                      </div>
                    ) : isError ? (
                      <div className="py-16">
                        <EmptyState
                          title={t('user.content.search.error', {})}
                          description={String((error as any)?.message || t('common.error'))}
                        />
                      </div>
                    ) : !results.length ? (
                      <div className="py-16">
                        <EmptyState
                          title={t('user.content.search.noResults', {})}
                          description={t('user.content.search.tryAgain', {
                          })}
                        />
                      </div>
                    ) : (
                      <div className="grid gap-4 sm:grid-cols-2 lg:grid-cols-3">
                        {results.map((c) => {
                          const poster = c.image_url || '';
                          const backdrop = c.backdrop_url || c.image_url || '';
                          const href = ROUTES.CONTENT.WATCH(c.id);
                          const categoryLabel = c.category?.name || c.category?.category_type || t('contentTypeExtended.content');
                          return (
                            <Link
                              key={`res-${c.id}`}
                              href={href}
                              className="group relative overflow-hidden rounded-2xl border border-slate-800 bg-slate-900/60 transition-transform hover:-translate-y-0.5"
                            >
                              <div
                                className="h-40 w-full bg-cover bg-center"
                                style={{ backgroundImage: poster ? `url(${poster})` : backdrop ? `url(${backdrop})` : undefined }}
                              />
                              <div className="p-4">
                                <div className="flex items-center justify-between">
                                  <h3 className="line-clamp-2 text-sm font-semibold text-white">{c.title}</h3>
                                  <Badge variant="outline" className="ml-2 border-slate-700 bg-slate-900/60 text-slate-200">
                                    <span className="inline-flex items-center gap-1">
                                      {mediaIcon(c)}
                                      <span className="capitalize">{categoryLabel}</span>
                                    </span>
                                  </Badge>
                                </div>
                                {c.year ? (
                                  <p className="mt-1 text-xs text-slate-400">{c.year}</p>
                                ) : null}
                              </div>
                            </Link>
                          );
                        })}
                      </div>
                    )}
                  </section>
                </div>
              </main>
            </div>
          </div>
        </UserRoute>
      );
    }
    export default function ContentSearchPage() {
      const { t } = useTranslation();
      return (
        <Suspense fallback={<div className="p-6 text-slate-400">{t('common.loading')}</div>}>
          <ContentSearchPageInner />
        </Suspense>
      );
    }