import { DropdownMenuItem, Icon, Window, WindowFrame, WindowTabs, WindowTabsContent, WindowTabsList, WindowTabsTrigger, } from "@prismicio/editor-ui"; import { useState } from "react"; import { createSection, deleteSection, renameSection, } from "@/domain/customType"; import { useCustomTypeState } from "@/features/customTypes/customTypesBuilder/CustomTypeProvider"; import { UIDEditor } from "@/features/customTypes/customTypesBuilder/UIDEditor"; import { CustomTypes } from "@/legacy/lib/models/common/CustomType"; import CreateModal from "./TabModal/create"; import DeleteModal from "./TabModal/delete"; import UpdateModal from "./TabModal/update"; import TabZone from "./TabZone"; type DialogState = | { type: "CREATE_CUSTOM_TYPE_TAB" } | { type: "UPDATE_CUSTOM_TYPE_TAB"; tabKey: string } | { type: "DELETE_CUSTOM_TYPE_TAB"; tabKey: string } | undefined; export const CustomTypeBuilder = () => { const { customType, setCustomType } = useCustomTypeState(); const customTypeSM = CustomTypes.toSM(customType); const [tabValue, setTabValue] = useState(customTypeSM.tabs[0]?.key); const [dialog, setDialog] = useState(); const sliceZoneEmpty = customTypeSM.tabs.find((tab) => tab.key === tabValue)?.sliceZone?.value .length === 0; function isRemoveDisabled(tabKey: string) { if (customTypeSM.tabs.length <= 1) return true; const tabWithUID = customTypeSM.tabs.find((tab) => tab.value.find((field) => field.key === "uid"), ); return ( customType.format === "page" && customType.repeatable && tabWithUID?.key === tabKey ); } return ( <> {customType.format === "page" ? ( : undefined} /> ) : undefined} { setDialog({ type: "CREATE_CUSTOM_TYPE_TAB" }); }} > {customTypeSM.tabs.map((tab) => ( { setDialog({ type: "UPDATE_CUSTOM_TYPE_TAB", tabKey: tab.key, }); }} startIcon={} > Rename { setDialog({ type: "DELETE_CUSTOM_TYPE_TAB", tabKey: tab.key, }); }} startIcon={} > Remove } value={tab.key} > {tab.key} ))} {customTypeSM.tabs.map((tab) => ( ))} {dialog?.type === "CREATE_CUSTOM_TYPE_TAB" ? ( { setDialog(undefined); }} isOpen onSubmit={({ id }) => { const newCustomType = createSection(customType, id); setCustomType({ customType: newCustomType }); setTabValue(id); }} tabIds={customTypeSM.tabs.map((tab) => tab.key.toLowerCase())} /> ) : undefined} {dialog?.type === "UPDATE_CUSTOM_TYPE_TAB" ? ( { setDialog(undefined); }} initialTabKey={dialog.tabKey} isOpen onSubmit={({ id }) => { const newCustomType = renameSection(customType, dialog.tabKey, id); setCustomType({ customType: newCustomType }); if (tabValue === dialog.tabKey) setTabValue(id); }} tabIds={customTypeSM.tabs .filter((tab) => tab.key !== dialog.tabKey) .map((tab) => tab.key.toLowerCase())} /> ) : undefined} {dialog?.type === "DELETE_CUSTOM_TYPE_TAB" ? ( { setDialog(undefined); }} isOpen onSubmit={() => { const newCustomType = deleteSection(customType, dialog.tabKey); setCustomType({ customType: newCustomType }); if (tabValue === dialog.tabKey) { const otherTabValue = customTypeSM.tabs.find( (tab) => tab.key !== dialog.tabKey, )?.key; if (otherTabValue !== undefined) setTabValue(otherTabValue); } }} /> ) : undefined} ); };