import * as React from 'react' import { useContext, useState, } from '@wordpress/element' import { __experimentalVStack as VStack, __experimentalHStack as HStack, __experimentalGrid as Grid, __experimentalView as View, Button, } from '@wordpress/components' import { store as coreDataStore, useEntityRecord, } from '@wordpress/core-data' import { useSelect, useDispatch, } from '@wordpress/data' import { ImportExportStringsContext, } from '@ska/components' import { ImportContext, } from './ImportModal' import PostTypeLabel from './PostTypeLabel' import { EXPORTABLES, } from './util' interface TemplateImportButtonProps { post: any postType?: 'wp_template' | 'wp_template_part' /** Slug of the stylesheet that the data was exported from. */ stylesheet?: string /** Slug of the stylesheet that is currently in use. */ currentStylesheet?: string } const TemplateImportButton: React.FC = ({ post, postType = 'wp_template', stylesheet = 'ska-theme', currentStylesheet = 'ska-theme', }) => { const { id, title = '', content = '', } = post const { importLabel, importedLabel, createLabel, createdLabel, overwriteLabel, } = useContext(ImportExportStringsContext) const [creating, setCreating] = useState(false) const [created, setCreated] = useState(false) const { saveEntityRecord, } = useDispatch(coreDataStore) const getPost = EXPORTABLES.find(({value}) => value === postType)?.getPost || ((...args: any) => false) const processedPost = getPost(post, postType, {stylesheet, currentStylesheet}) || {} const { id: ID = -1, ...postToImport } = processedPost const { hasResolved, editedRecord, edit, status, } = useEntityRecord('postType', postType, ID) if(!hasResolved) { return null } if(status !== 'SUCCESS') { return (