All files / src/components/Recipe/Create RecipeComposer.tsx

0% Statements 0/31
0% Branches 0/16
0% Functions 0/10
0% Lines 0/25

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                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 
'use client';
 
import {
  ActionIcon,
  Box,
  Drawer,
  Group,
  LoadingOverlay,
  ScrollArea,
} from '@mantine/core';
import { useDebouncedValue } from '@mantine/hooks';
import { IconArrowLeft } from '@tabler/icons-react';
import { useRouter } from 'next/navigation';
import { useCallback, useEffect, useRef, useState } from 'react';
import ComposerHeader from './components/ComposerHeader';
import ComposerSidebar from './components/ComposerSidebar';
import { RecipeFormProvider } from './FormContext';
import { useRecipeMetadata } from './hooks/useRecipeMetadata';
import { Preview } from './Preview';
import BasicsSection from './sections/BasicsSection';
import IngredientsSection from './sections/IngredientsSection';
import MediaSection from './sections/MediaSection';
import StepsSection from './sections/StepsSection';
import type { ComposerSection, RecipeComposerProps } from './types';
 
export const RecipeComposer = ({
  form,
  handlePublish,
  submitLoading,
  completion,
  lastSavedLabel,
  onSave,
  onReset,
  addIngredient,
  addStep,
  headerTitle,
  submitLabel,
  resetLabel,
  goToSectionRef,
}: Readonly<RecipeComposerProps>) => {
  const router = useRouter();
  const scrollRef = useRef<HTMLDivElement>(null);
  const [activeSection, setActiveSection] = useState<ComposerSection>('basics');
  const [previewOpen, setPreviewOpen] = useState(false);
 
  /* Metadata */
  const {
    categories,
    levels,
    labels,
    unitOptions,
    cuisines,
    servingUnits,
    dietaryFlags,
    allergens,
    equipment,
    costLevels,
    metadataLoading,
    metadataLoaded,
  } = useRecipeMetadata();
 
  const goToSection = useCallback((section: ComposerSection) => {
    setActiveSection(section);
    scrollRef.current?.scrollTo({ top: 0, behavior: 'smooth' });
  }, []);
 
  /* Expose goToSection to parent via ref */
  useEffect(() => {
    if (goToSectionRef) {
      goToSectionRef.current = goToSection;
    }
  }, [goToSection, goToSectionRef]);
 
  /* Debounced values for Preview – avoids heavy re-render on every keystroke */
  const [debouncedPreviewValues] = useDebouncedValue(form.values, 300);
 
  /* Stable navigation callbacks */
  const handleBack = useCallback(() => router.back(), [router]);
  const handleOpenPreview = useCallback(() => setPreviewOpen(true), []);
  const handleClosePreview = useCallback(() => setPreviewOpen(false), []);
  const goToBasics = useCallback(() => goToSection('basics'), [goToSection]);
  const goToMedia = useCallback(() => goToSection('media'), [goToSection]);
  const goToIngredients = useCallback(
    () => goToSection('ingredients'),
    [goToSection],
  );
  const goToSteps = useCallback(() => goToSection('steps'), [goToSection]);
 
  /* Loading gate */
  if (!metadataLoaded && metadataLoading) {
    return (
      <LoadingOverlay
        visible
        zIndex={1000}
        overlayProps={{ blur: 2, radius: 'sm' }}
      />
    );
  }
 
  return (
    <RecipeFormProvider form={form}>
      <Box
        style={{ height: '100vh', display: 'flex', flexDirection: 'column' }}
      >
        <LoadingOverlay
          visible={submitLoading || metadataLoading}
          zIndex={1000}
          overlayProps={{ blur: 2, radius: 'sm' }}
        />
 
        {/* ═══ HEADER ═══ */}
        <ComposerHeader
          title={headerTitle}
          onBack={handleBack}
          completion={completion}
          lastSavedLabel={lastSavedLabel}
          onSave={onSave}
          onPreview={handleOpenPreview}
          onPublish={form.onSubmit(handlePublish)}
          publishLoading={submitLoading}
          submitLabel={submitLabel}
        />
 
        {/* ═══ WORKSPACE ═══ */}
        <Group
          align="stretch"
          gap={0}
          style={{ flex: 1, minHeight: 0, overflow: 'hidden' }}
        >
          {/* ── SIDEBAR NAV (desktop) ── */}
          <ComposerSidebar
            activeSection={activeSection}
            onSectionChange={goToSection}
            values={form.values}
            completion={completion}
            onAddIngredient={addIngredient}
            onAddStep={addStep}
            onReset={onReset}
            resetLabel={resetLabel}
          />
 
          {/* ── EDITOR (center) ── */}
          <ScrollArea
            viewportRef={scrollRef}
            style={{ flex: 1, height: '100%' }}
            type="always"
            bg="gray.0"
          >
            <Box
              py="xl"
              px={{ base: 'md', sm: 'xl' }}
              maw={860}
              mx="auto"
              style={{
                background:
                  'radial-gradient(1200px 500px at 0% 0%, rgba(99,102,241,0.04), transparent 60%), radial-gradient(900px 400px at 100% 0%, rgba(236,72,153,0.04), transparent 55%)',
              }}
            >
              {activeSection === 'basics' && (
                <BasicsSection
                  categories={categories}
                  levels={levels}
                  labels={labels}
                  cuisines={cuisines}
                  servingUnits={servingUnits}
                  costLevels={costLevels}
                  dietaryFlags={dietaryFlags}
                  allergens={allergens}
                  equipment={equipment}
                  onNext={goToMedia}
                />
              )}
 
              {activeSection === 'media' && (
                <MediaSection onBack={goToBasics} onNext={goToIngredients} />
              )}
 
              {activeSection === 'ingredients' && (
                <IngredientsSection
                  unitOptions={unitOptions}
                  onAdd={addIngredient}
                  onBack={goToMedia}
                  onNext={goToSteps}
                />
              )}
 
              {activeSection === 'steps' && (
                <StepsSection
                  onAdd={addStep}
                  onBack={goToIngredients}
                  onSubmit={form.onSubmit(handlePublish)}
                  isSubmitting={submitLoading}
                  submitLabel={submitLabel}
                />
              )}
            </Box>
          </ScrollArea>
 
          {/* ── PREVIEW (desktop right / drawer on mobile) ── */}
          <Box
            visibleFrom="lg"
            w={400}
            style={{
              borderLeft: '1px solid var(--mantine-color-gray-2)',
              flexShrink: 0,
            }}
          >
            <Preview labels={labels} values={debouncedPreviewValues} />
          </Box>
        </Group>
 
        <Drawer
          opened={previewOpen}
          onClose={handleClosePreview}
          position="right"
          size="lg"
          withCloseButton={false}
          styles={{ body: { height: '100%', padding: 0 } }}
        >
          <Preview labels={labels} values={debouncedPreviewValues} />
          <ActionIcon
            variant="filled"
            color="dark"
            radius="xl"
            size="lg"
            onClick={handleClosePreview}
            style={{
              position: 'absolute',
              top: 16,
              right: 16,
              zIndex: 200,
            }}
          >
            <IconArrowLeft size={18} />
          </ActionIcon>
        </Drawer>
      </Box>
    </RecipeFormProvider>
  );
};