import { notifyApiError } from "../../../../lib/apiErrors" import { useRef, useState, type ChangeEvent } from "react"; import { ArrowRight, ChevronDown, ChevronUp, Loader2, Upload } from "lucide-react"; import { Button } from "../../../../components/ui/button"; import { Card } from "../../../../components/ui/card"; import { Input } from "../../../../components/ui/input"; import { Textarea } from "../../../../components/ui/textarea"; import { toast } from "sonner"; import { uploadImageFile } from "../../../../api/files.api"; import type { PracticeParent, AuthoringProfile } from "../../../../types/course.types"; import { PracticeParentsField } from "./PracticeParentsField"; import { formatPracticeParentsSummary } from "../../../../lib/practiceParents"; interface ContextStepProps { formData: { title?: string; description?: string; storyImageUrl?: string; shuffleQuestions?: boolean; tips?: string; authoringProfile?: AuthoringProfile; parents?: PracticeParent[]; }; setFormData: (data: ContextStepProps["formData"]) => void; nextStep: () => void; onCancel: () => void; /** Lesson-linked practice: no title, story description, or story image on step 1. */ isLessonPractice?: boolean; lessonTitle?: string | null; parentSummary?: string | null; /** Learn English LMS — show multi-parent picker. */ showParentsEditor?: boolean; lockedParentKey?: string | null; /** When true, locations are optional and the picker can stay collapsed. */ parentsOptional?: boolean; /** Use exam-prep parent kinds in the locations editor. */ isExamPrepParents?: boolean; parentsCollapsedDefault?: boolean; /** When editing an existing practice, enable per-parent DELETE unlink. */ practiceId?: number; onParentsUnlinked?: (parents: PracticeParent[]) => void; } /** * Module / lesson entry: fields that map to POST /practices and POST /question-sets. */ export function ContextStep({ formData, setFormData, nextStep, onCancel, isLessonPractice = false, lessonTitle = null, parentSummary = null, showParentsEditor = false, lockedParentKey = null, parentsOptional = false, parentsCollapsedDefault = false, isExamPrepParents = false, practiceId, onParentsUnlinked, }: ContextStepProps) { const storyFileRef = useRef(null); const [uploadingStory, setUploadingStory] = useState(false); const [parentsExpanded, setParentsExpanded] = useState(!parentsCollapsedDefault); const handleStoryImageFile = async (event: ChangeEvent) => { const file = event.target.files?.[0]; event.target.value = ""; if (!file) return; setUploadingStory(true); try { const res = await uploadImageFile(file); const url = res.data?.data?.url?.trim(); if (!url) throw new Error("Missing image URL from upload"); setFormData({ ...formData, storyImageUrl: url }); toast.success("Story image uploaded"); } catch { notifyApiError(err, "Could not upload story image"); } finally { setUploadingStory(false); } }; const canContinue = isLessonPractice ? true : Boolean(formData.title?.trim()) && Boolean(formData.description?.trim()); const parentsSummary = formData.parents && formData.parents.length > 0 ? formatPracticeParentsSummary(formData.parents) : parentSummary; return (

{isLessonPractice ? "Practice options" : "Practice details"}

{isLessonPractice ? ( <> Story fields and question set options used when saving the practice. Linked to{" "} {lessonTitle?.trim() || "the selected lesson"} . ) : ( <> Story fields and question set options used when saving the practice. )}