'use client'; import React, { useEffect, useState } from 'react'; import { useParams } from 'next/navigation'; import { getShopPolicy, type ShopPolicy } from '@/hooks/use-shopify-policies'; export interface PolicyBodyProps { /** * Pins the block to one policy. Left empty, it reads the `[handle]` segment, * which is what the `/policies/[handle]` template does. */ handle?: string; /** Overrides the policy's own title. Empty falls back to Shopify's. */ title?: string; notFoundMessage?: string; } /** * Policy copy is authored in the Shopify admin, not here — the editor only * chooses which policy to show and can override the heading. */ const PolicyBody: React.FC = ({ handle: handleProp, title: titleProp, notFoundMessage = 'This policy has not been published yet.', }) => { const params = useParams(); const handle = handleProp || (params?.handle as string | undefined); const [policy, setPolicy] = useState(null); const [loading, setLoading] = useState(true); useEffect(() => { if (!handle) { setLoading(false); return; } let cancelled = false; setLoading(true); getShopPolicy(handle) .then((result) => { if (!cancelled) setPolicy(result); }) .catch(() => { if (!cancelled) setPolicy(null); }) .finally(() => { if (!cancelled) setLoading(false); }); return () => { cancelled = true; }; }, [handle]); return (

{titleProp || policy?.title || 'Policy'}

{loading ? (
{Array.from({ length: 6 }).map((_, index) => (
))}
) : policy ? (
) : (

{notFoundMessage}

)}
); }; export default PolicyBody;