All files / src/app/recipes RecipesPage.tsx

0% Statements 0/30
0% Branches 0/16
0% Functions 0/7
0% Lines 0/26

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                                                                                                                                                                                                                                                                                                                                                                                                             
'use client';
 
import { useQuery } from '@apollo/client/react';
import {
  Box,
  Center,
  Container,
  Group,
  Stack,
  Text,
  Title,
  Transition,
} from '@mantine/core';
import {
  IconChefHat,
  IconMoodSmile,
  IconRotateClockwise2,
  IconSearch,
} from '@tabler/icons-react';
import type { Route } from 'next';
import { usePathname, useRouter, useSearchParams } from 'next/navigation';
import { useTranslations } from 'next-intl';
import { type FC, useCallback, useMemo } from 'react';
import { toCleanedOptions } from '@/components/Recipe/Create/utils';
import type { RecipeCardData } from '@/components/Recipe/RecipeCard';
import { RecipeGrid } from '@/components/Recipe/RecipeCard';
import { RecipeCarousel } from '@/components/Recipe/RecipeCarousel';
import RecipeSearch, {
  buildQueryFilter,
  filtersToSearchParams,
  isSearchActive,
  type RecipeSearchFilters,
  searchParamsToFilters,
} from '@/components/Recipe/RecipeSearch';
import { GET_LATEST_RECIPES } from '@/lib/graphql/queries';
import { useCategories, useLabels, useLevels } from '@/lib/store/metadata';
import classes from '../HomePage.module.css';
 
const RecipesPage: FC = () => {
  const translateSidebar = useTranslations('sidebar');
  const translateRecipeSearch = useTranslations('recipeSearch');
  const translateMisc = useTranslations('misc');
 
  const router = useRouter();
  const pathname = usePathname();
  const searchParams = useSearchParams();
 
  // --- URL → filters (single source of truth) ---
  const filtersFromUrl = useMemo(
    () => searchParamsToFilters(searchParams),
    [searchParams],
  );
 
  const searching = isSearchActive(filtersFromUrl);
 
  // --- Metadata → select options ---
  const categoriesFromStore = useCategories();
  const levelsFromStore = useLevels();
  const labelsFromStore = useLabels();
 
  const categoryOptions = useMemo(
    () => toCleanedOptions(categoriesFromStore, translateMisc),
    [categoriesFromStore, translateMisc],
  );
  const difficultyOptions = useMemo(
    () => toCleanedOptions(levelsFromStore, translateMisc),
    [levelsFromStore, translateMisc],
  );
  const labelOptions = useMemo(
    () => toCleanedOptions(labelsFromStore, translateMisc),
    [labelsFromStore, translateMisc],
  );
 
  // --- GraphQL query driven by URL filters ---
  const { data, loading } = useQuery(GET_LATEST_RECIPES, {
    variables: {
      ...(searching ? {} : { limit: 10 }),
      filter: buildQueryFilter(filtersFromUrl),
    },
  });
 
  const recipes: RecipeCardData[] =
    (data as { getRecipes?: { recipes: RecipeCardData[] } })?.getRecipes
      ?.recipes ?? [];
 
  const totalRecipes: number =
    (data as { getRecipes?: { totalRecipes: number } })?.getRecipes
      ?.totalRecipes ?? 0;
 
  // --- Handlers ---
  const handleSearch = useCallback(
    (filters: RecipeSearchFilters) => {
      const params = filtersToSearchParams(filters);
      const qs = params.toString();
      const url = qs ? `${pathname}?${qs}` : pathname;
      router.replace(url as Route, { scroll: false });
    },
    [router, pathname],
  );
 
  return (
    <Container size="xl" py="xl">
      <Stack gap="xl">
        <Box>
          <Group gap="xs" mb="xs">
            <IconChefHat size={32} color="var(--mantine-color-pink-6)" />
            <Title order={1}>{translateSidebar('recipes')}</Title>
          </Group>
          <Text c="dimmed" size="lg">
            {translateRecipeSearch('pageDescription')}
          </Text>
        </Box>
 
        <RecipeSearch
          initialFilters={filtersFromUrl}
          onSearch={handleSearch}
          categoryOptions={categoryOptions}
          difficultyOptions={difficultyOptions}
          labelOptions={labelOptions}
          loading={loading}
        />
 
        <Transition
          mounted={searching}
          transition="fade"
          duration={200}
          exitDuration={150}
        >
          {(styles) => (
            <Box component="section" style={styles}>
              <Group justify="space-between" mb="md">
                <Group gap="xs">
                  <IconSearch size={20} color="var(--mantine-color-pink-6)" />
                  <Title order={2} size="h3">
                    {translateRecipeSearch('searchResults')}
                  </Title>
                </Group>
                {!loading && (
                  <Text c="dimmed" size="sm">
                    {translateRecipeSearch('totalResults', {
                      count: totalRecipes,
                    })}
                  </Text>
                )}
              </Group>
              <RecipeGrid
                loading={loading}
                recipes={recipes}
                emptyMessage={translateRecipeSearch('noResults')}
                withFavorite
              />
            </Box>
          )}
        </Transition>
 
        {!searching && (
          <Box component="section" className={classes.section}>
            <Box className={classes.sectionHeader}>
              <Title order={2} size="h3">
                <IconRotateClockwise2
                  size={22}
                  style={{
                    marginRight: 8,
                    verticalAlign: 'middle',
                    color: 'var(--mantine-color-blue-6)',
                  }}
                />
                {translateRecipeSearch('recentlyAdded')}
              </Title>
            </Box>
            {recipes.length === 0 && !loading ? (
              <Center py="xl">
                <Stack align="center" gap="xs">
                  <IconMoodSmile
                    size={48}
                    color="var(--mantine-color-dimmed)"
                  />
                  <Text c="dimmed" size="lg" ta="center">
                    {translateRecipeSearch('noRecipes')}
                  </Text>
                </Stack>
              </Center>
            ) : (
              <RecipeCarousel
                loading={loading}
                recipes={recipes}
                emptyMessage={translateRecipeSearch('noRecipes')}
                withFavorite
              />
            )}
          </Box>
        )}
      </Stack>
    </Container>
  );
};
 
export default RecipesPage;