import { notifyApiError } from "../../../../lib/apiErrors" import { useCallback, useEffect, useMemo, useState } from "react" import { ArrowRight, ChevronLeft, ChevronRight, Search } from "lucide-react" import { toast } from "sonner" import { Button } from "../../../../components/ui/button" import { Card } from "../../../../components/ui/card" import { Input } from "../../../../components/ui/input" import { SpinnerIcon } from "../../../../components/ui/spinner-icon" import { practiceAlreadyLinkedToParent } from "../../../../lib/attachPracticeToParent" import { fetchAllPractices } from "../../../../lib/fetchAllPractices" import { formatPracticeParentsSummary } from "../../../../lib/practiceParents" import { DEFAULT_TABLE_PAGE_SIZE, TABLE_PAGE_SIZE_OPTIONS } from "../../../../lib/tablePagination" import type { ParentContextPractice, PracticeParent } from "../../../../types/course.types" import { cn } from "../../../../lib/utils" import { SearchHighlight, getSearchTokens } from "../../../../components/SearchHighlight" interface SelectPracticeToAttachStepProps { targetParent: PracticeParent targetSummary: string selectedPracticeId: number | null onSelect: (practice: ParentContextPractice) => void nextStep: () => void onCancel: () => void variant?: "page" | "dialog" isExamPrep?: boolean } function publishStatusClass(status?: string | null): string { const s = (status || "").toUpperCase() if (s === "PUBLISHED") return "bg-[#F0FDF4] text-[#16A34A]" if (s === "DRAFT") return "bg-amber-50 text-amber-800" return "bg-grayScale-50 text-grayScale-600" } function matchesPracticeSearch( practice: ParentContextPractice, query: string, ): boolean { const tokens = getSearchTokens(query) if (tokens.length === 0) return true const haystack = [ practice.title ?? "", practice.story_description ?? "", String(practice.id), String(practice.question_set_id), formatPracticeParentsSummary(practice.parents ?? [], { isExamPrep }), ] .join(" ") .toLowerCase() return tokens.every((token) => haystack.includes(token.toLowerCase())) } export function SelectPracticeToAttachStep({ targetParent, targetSummary, selectedPracticeId, onSelect, nextStep, onCancel, variant = "page", isExamPrep = false, }: SelectPracticeToAttachStepProps) { const isDialog = variant === "dialog" const [loading, setLoading] = useState(true) const [allPractices, setAllPractices] = useState([]) const [pageSize, setPageSize] = useState(DEFAULT_TABLE_PAGE_SIZE) const [offset, setOffset] = useState(0) const [searchInput, setSearchInput] = useState("") const [unlinkedOnly, setUnlinkedOnly] = useState(false) const load = useCallback(async () => { setLoading(true) try { const practices = await fetchAllPractices(unlinkedOnly, { isExamPrep }) setAllPractices(practices) } catch (e) { notifyApiError(e, "Could not load practices") setAllPractices([]) } finally { setLoading(false) } }, [unlinkedOnly, isExamPrep]) useEffect(() => { void load() }, [load]) useEffect(() => { setOffset(0) }, [searchInput, unlinkedOnly, pageSize]) const filteredPractices = useMemo( () => allPractices.filter((practice) => matchesPracticeSearch(practice, searchInput)), [allPractices, searchInput], ) const totalCount = filteredPractices.length const practices = useMemo( () => filteredPractices.slice(offset, offset + pageSize), [filteredPractices, offset, pageSize], ) const pageStart = totalCount === 0 ? 0 : offset + 1 const pageEnd = Math.min(offset + practices.length, totalCount) const canPrev = offset > 0 const canNext = offset + pageSize < totalCount const canContinue = selectedPracticeId != null const header = (

Select a practice

Link an existing practice to{" "} {targetSummary}.

) const filters = (
setSearchInput(e.target.value)} placeholder="Search by title, id, or location…" className="h-10 rounded-[8px] border-grayScale-200 pl-9" />
Show:
) const listBody = (
{loading ? (

Loading practices…

) : practices.length === 0 ? (

No practices found

{unlinkedOnly ? isExamPrep ? "No unlinked exam-prep practices match your search. Try all practices or create a new shell first." : "No unlinked practices match your search. Try all practices or create a new shell from Question Types." : "Try a different search, switch to unlinked only, or create a new practice instead."}

) : ( )}
) const pagination = !loading && totalCount > 0 ? (
Showing {pageStart}–{pageEnd} of {totalCount}
) : null const footer = (
) if (isDialog) { return (
{header} {filters}
{listBody}
{pagination} {footer}
) } return ( {header}