import { computeDefinitionStatus, type DefinitionStatus } from '@components/common/metafield/definitionStatus.js'; import axios from 'axios'; import React, { useCallback, useEffect, useState } from 'react'; /** Payload of the iframe's `metafield-select` bridge message. */ export interface MetafieldSelection { ownerType: string; ownerUuid?: string; namespace: string; fieldKey: string; value?: unknown; declared?: { type: string; isList?: boolean; visibleToCustomer?: boolean; name?: string; description?: string; placeholder?: string; } | null; resolutionKind?: string; } interface ExistingDefinition { uuid: string; namespace: string; key: string; name: string; type: string; isList?: boolean; visibleToCustomer?: boolean; provisionedByTheme?: string; } /** Admin edit-surface deep links per owner (route.json paths, /admin prefix). */ const EDIT_SURFACES: Record< string, { label: string; url: (uuid?: string) => string | null } > = { product: { label: 'the product edit form', url: (uuid) => (uuid ? `/admin/products/edit/${uuid}` : null) }, category: { label: 'the category edit form', url: (uuid) => (uuid ? `/admin/categories/edit/${uuid}` : null) }, collection: { label: 'the collection edit form', url: (uuid) => (uuid ? `/admin/collections/edit/${uuid}` : null) }, customer: { label: 'the customer view', url: (uuid) => (uuid ? `/admin/customers/edit/${uuid}` : null) }, order: { label: 'the order view', url: (uuid) => (uuid ? `/admin/order/edit/${uuid}` : null) }, blog_post: { label: 'the blog post edit form', url: (uuid) => (uuid ? `/admin/blog/posts/edit/${uuid}` : null) }, blog_category: { label: 'the blog category edit form', url: (uuid) => (uuid ? `/admin/blog/categories/edit/${uuid}` : null) }, shop: { label: 'Store Settings', url: () => '/admin/setting/store' } }; /** * The guideline drawer (theme-metafields design § V1.1) — informational by * design. Metafield values are LIVE entity data (owner decision: the builder * is route-level, values are entity-level — a value editor here would write * to whatever sample entity the preview happens to show). The drawer * explains the field, reports definition status (create-if-missing + * immutable-conflict banner), and deep-links to the real editing surface. */ export default function MetafieldGuideDrawer({ selection, onClose, containerRef }: { selection: MetafieldSelection; onClose: () => void; containerRef: React.RefObject; }) { const { ownerType, ownerUuid, namespace, fieldKey, declared } = selection; const [existing, setExisting] = useState(null); const [status, setStatus] = useState(null); const [busy, setBusy] = useState(false); const [error, setError] = useState(null); const refresh = useCallback(async () => { setError(null); try { const res = await axios.get( `/api/metafield-definitions?ownerType=${encodeURIComponent(ownerType)}` ); const defs: ExistingDefinition[] = res.data?.data ?? []; const found = defs.find((d) => d.namespace === namespace && d.key === fieldKey) ?? null; setExisting(found); setStatus(computeDefinitionStatus(declared ?? null, found)); } catch (e) { setError((e as Error).message); setStatus(null); } }, [ownerType, namespace, fieldKey, declared]); useEffect(() => { refresh(); }, [refresh]); const createDefinition = async () => { if (!declared) return; setBusy(true); setError(null); try { await axios.post('/api/metafield-definitions', { ownerType, namespace, fieldKey, name: declared.name ?? fieldKey, fieldType: declared.type, ...(declared.isList ? { isList: true } : {}), ...(declared.visibleToCustomer === false ? { visibleToCustomer: false } : {}), ...(declared.description ? { description: declared.description } : {}), ...(declared.placeholder ? { appearance: { placeholder: declared.placeholder } } : {}), // Attribute to the active theme (resolved server-side) — same // ownership a boot/activate provisioning run would have stamped. provisionedByActiveTheme: true }); } catch (e: any) { // 409 = a concurrent create (boot provisioning, another tab) won the // race — fall through to refresh and re-diff against the winner. if (e?.response?.status !== 409) { setError(e?.response?.data?.error?.message ?? (e as Error).message); } } finally { setBusy(false); await refresh(); } }; const surface = EDIT_SURFACES[ownerType]; const editUrl = surface?.url(ownerUuid) ?? null; const displayName = existing?.name ?? declared?.name ?? fieldKey; const valuePreview = selection.value === null || selection.value === undefined ? null : JSON.stringify(selection.value, null, 1); return ( ); }