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 { RichTextEditor } from "../../../../components/ui/rich-text-editor"; 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"; import { hasRichTextContent } from "../../../../lib/richText"; 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; 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, 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 (e) { notifyApiError(e, "Could not upload story image"); } finally { setUploadingStory(false); } }; const canContinue = Boolean(formData.title?.trim()) && hasRichTextContent(formData.description); const parentsSummary = formData.parents && formData.parents.length > 0 ? formatPracticeParentsSummary(formData.parents) : parentSummary; return (

Practice details

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