import React, { useState } from "react"; import { Card, Stack, Text, Select, Button, TextInput } from "@sanity/ui"; import { PatchEvent, set } from "sanity"; import useProEnabled from "../../hooks/useProEnabled"; import ProGate from "./ProGate"; import { SCHEMA_TYPES, FIELDS_BY_TYPE } from "../../constants/schemaTypes"; /* eslint-disable @typescript-eslint/no-explicit-any */ export default function SchemaWizard({ value, onChange, }: { value: any; onChange: (e: any) => void; }) { const { isPro } = useProEnabled(); const schemaOrg = value?.schemaOrg || {}; const originalType = schemaOrg.schemaType || ""; const [selectedType, setSelectedType] = useState(originalType); const [faqItems, setFaqItems] = useState<{ question: string; answer: string }[]>( schemaOrg.faqItems || [{ question: "", answer: "" }], ); const fields = FIELDS_BY_TYPE[selectedType] || []; const patch = (fieldName: string, fieldValue: any) => { onChange(PatchEvent.from(set({ ...schemaOrg, [fieldName]: fieldValue }, ["schemaOrg"]))); }; const handleTypeChange = (e: React.ChangeEvent) => { const newType = e.target.value; setSelectedType(newType); if (newType !== originalType) { onChange(PatchEvent.from(set({ ...schemaOrg, schemaType: newType }, ["schemaOrg"]))); } if (newType !== "FAQPage") { setFaqItems([{ question: "", answer: "" }]); } else if (!Array.isArray(schemaOrg.faqItems)) { setFaqItems([{ question: "", answer: "" }]); } }; const addFaq = () => setFaqItems((prev) => [...prev, { question: "", answer: "" }]); const updateFaq = (i: number, field: "question" | "answer", val: string) => { const updated = faqItems.map((item, idx) => (idx === i ? { ...item, [field]: val } : item)); setFaqItems(updated); patch("faqItems", updated); }; return ( Schema.org Structured Data Schema Type {selectedType === "FAQPage" && ( FAQ Items {faqItems.map((item, i) => ( Question {i + 1} updateFaq(i, "question", (e.target as HTMLInputElement).value) } placeholder="What is…?" /> Answer updateFaq(i, "answer", (e.target as HTMLInputElement).value)} placeholder="The answer is…" /> ))}