import { SharedSlice } from "@prismicio/types-internal/lib/customtypes"; import { FC, useState } from "react"; import Select from "react-select"; import { Box, Label } from "theme-ui"; import { getState } from "@/apiClient"; import { useOnboarding } from "@/features/onboarding/useOnboarding"; import { createSlice } from "@/features/slices/actions/createSlice"; import { useAutoSync } from "@/features/sync/AutoSyncProvider"; import ModalFormCard from "@/legacy/components/ModalFormCard"; import { LibraryUI } from "@/legacy/lib/models/common/LibraryUI"; import { SliceSM } from "@/legacy/lib/models/common/Slice"; import useSliceMachineActions from "@/modules/useSliceMachineActions"; import { InputBox } from "../components/InputBox"; import { validateSliceModalValues } from "../formsValidator"; type CreateSliceModalProps = { onClose: () => void; onSuccess: (newSlice: SharedSlice, libraryName: string) => void; localLibraries: readonly LibraryUI[]; location: "custom_type" | "page_type" | "slices"; remoteSlices: ReadonlyArray; }; type FormValues = { sliceName: string; from: string }; export const CreateSliceModal: FC = ({ onClose, onSuccess, localLibraries, location, remoteSlices, }) => { const { createSliceSuccess } = useSliceMachineActions(); const [isCreatingSlice, setIsCreatingSlice] = useState(false); const { syncChanges } = useAutoSync(); const { completeStep } = useOnboarding(); const onSubmit = async (values: FormValues) => { const sliceName = values.sliceName; const libraryName = values.from; setIsCreatingSlice(true); await createSlice({ sliceName, libraryName, location, onSuccess: async (newSlice) => { // TODO(DT-1453): Remove the need of the global getState const serverState = await getState(); // Update Redux store createSliceSuccess(serverState.libraries); onSuccess(newSlice, libraryName); syncChanges(); void completeStep("createSlice"); }, }); }; return ( { void onSubmit(values); }} initialValues={{ sliceName: "", from: localLibraries[0].name, }} validate={(values) => validateSliceModalValues(values, localLibraries, remoteSlices) } content={{ title: "Create a new slice", }} > {({ touched, values, setFieldValue, errors }) => (