"use client"; import type React from "react"; import { useState } from "react"; import { cx } from "../lib/utils"; import { OnboardingBadge } from "../primitives/onboarding-badge"; import { OnboardingButton } from "../primitives/onboarding-button"; import { OnboardingCard } from "../primitives/onboarding-card"; import { OnboardingCheckbox } from "../primitives/onboarding-checkbox"; import { OnboardingLabel } from "../primitives/onboarding-label"; export interface ProductCategory { id: string; title: string; subcategories: string[]; } export interface ProductSelectionStepProps { /** Title displayed at the top of the step */ title?: string; /** Description text below the title */ description?: string; /** Array of product categories to display */ categories: ProductCategory[]; /** Initially selected category IDs */ defaultSelected?: string[]; /** Called when selection changes */ onSelectionChange?: (selectedIds: string[]) => void; /** Called when the user submits the form */ onSubmit: (selectedIds: string[]) => void | Promise; /** Text for the submit button */ submitText?: string; /** Text shown while submitting */ loadingText?: string; /** Optional back button config */ backButton?: { text: string; onClick: () => void; }; } interface CategoryItemProps { category: ProductCategory; checked: boolean; onCheckedChange: (categoryId: string, checked: boolean) => void; } function CategoryItem({ category, checked, onCheckedChange, }: CategoryItemProps) { return (
onCheckedChange(category.id, isChecked === true) } /> {category.title}
{category.subcategories.length > 0 && (
    {category.subcategories.map((subcategory) => (
  • {subcategory}
  • ))}
)}
); } export function ProductSelectionStep({ title = "Which products are you interested in?", description = "You can choose multiple. This will help us customize the experience.", categories, defaultSelected = [], onSelectionChange, onSubmit, submitText = "Continue", loadingText = "Submitting...", backButton, }: ProductSelectionStepProps) { const [checkedItems, setCheckedItems] = useState>( () => { const initial: Record = {}; defaultSelected.forEach((id) => { initial[id] = true; }); return initial; }, ); const [loading, setLoading] = useState(false); const handleCheckedChange = (categoryId: string, isChecked: boolean) => { const newCheckedItems = { ...checkedItems, [categoryId]: isChecked, }; setCheckedItems(newCheckedItems); if (onSelectionChange) { const selectedIds = Object.entries(newCheckedItems) .filter(([, checked]) => checked) .map(([id]) => id); onSelectionChange(selectedIds); } }; const selectedIds = Object.entries(checkedItems) .filter(([, checked]) => checked) .map(([id]) => id); const isAnyItemChecked = selectedIds.length > 0; const handleSubmit = async (e: React.FormEvent) => { e.preventDefault(); setLoading(true); try { await onSubmit(selectedIds); } finally { setLoading(false); } }; return (

{title}

{description}

{title}
{categories.map((category, index) => (
))}
{backButton && ( {backButton.text} )} {loading ? loadingText : submitText}
); }