import { Head, Link, router } from '@inertiajs/react'; import { PencilIcon, TrashIcon } from 'lucide-react'; import { PlusIcon } from 'lucide-react'; import { useState } from 'react'; import { CopyButton } from '@/components/copy-button'; import { ActivityTimeline, DetailList, DetailRow, isBlank, RecordCard, RecordHeader, RecordMeta, RecordPage, } from '@/components/crud/record'; import type { ActivityEntry } from '@/components/crud/record'; import { EmbedPreview } from '@/components/embed'; import { KeyValueList } from '@/components/key-value-input'; import { RichText } from '@/components/rich-text'; import { ConfirmDialog } from '@/components/table/confirm-dialog'; import { Button, buttonVariants } from '@/components/ui/button'; import { enumLabel, formatDate, formatDateTime, formatMoney, } from '@/lib/format'; import { storedFileUrl } from '@/lib/stored-file'; import { moduleLabel, t } from '@/lib/ui-text'; import { ProductVariantFormModal } from '@/pages/product-variant/form-modal'; import type { Options as ProductVariantFormOptions } from '@/pages/product-variant/form-modal'; import { ReviewFormModal } from '@/pages/review/form-modal'; import type { Options as ReviewFormOptions } from '@/pages/review/form-modal'; import { index as indexCoupons, show as showCoupons } from '@/routes/coupon'; import { destroy, index } from '@/routes/product'; import { index as indexProductVariants, show as showProductVariants, } from '@/routes/product-variant'; import { index as indexReviews, show as showReviews } from '@/routes/review'; import { show as showTags } from '@/routes/tag'; import { ProductFormModal } from './form-modal'; import type { ProductDetail, ProductVariantsRow, ReviewsRow } from './types'; const statusBadge: Record = { draft: 'border-zinc-300/60 text-zinc-600 dark:border-zinc-700 dark:text-zinc-400', active: 'border-emerald-300/60 text-emerald-700 dark:border-emerald-800 dark:text-emerald-400', archived: 'border-violet-300/60 text-violet-700 dark:border-violet-800 dark:text-violet-400', }; function ProductVariantsPanel({ parentId, rows, auth, options, }: { parentId: number; rows: ProductVariantsRow[]; auth: { permissions: string[]; isSuperAdmin: boolean }; options: ProductVariantFormOptions; }) { const can = (perm: string) => auth.isSuperAdmin || auth.permissions.includes(perm); const [adding, setAdding] = useState(false); const [deleteId, setDeleteId] = useState(null); const [deleting, setDeleting] = useState(false); function confirmDelete() { if (deleteId === null) { return; } setDeleting(true); router.delete(`/product/${parentId}/product-variant/${deleteId}`, { preserveScroll: true, onFinish: () => { setDeleting(false); setDeleteId(null); }, }); } return (

{moduleLabel('product-variant', 'Product Variants', true)} ( {rows.length})

{can('product-variant.view') && rows.length > 0 && ( {t('viewAll')} )} {can('product-variant.create') && ( )}
{/* The panel scrolls inside itself. Without this the table's minimum width holds the whole page open — measured at +351px on a 375px phone, and still +222px at 768px. `contain-paint` is required for the same reason it is on the index scrollport: clipping alone still counts the full width toward the document. The comment sits here, in children position, not inside the ternary below: a braced JSX comment in expression position parses as an object literal, and the file silently stops being valid TSX — prettier and eslint then skip it without failing the generate. */} {rows.length > 0 ? (
{rows.map((row) => ( ))}
Variant SKU Size Color Price override Stock
{can('product-variant.view') ? ( {row.name} ) : ( row.name )} {row.sku ?? '—'} {row.size ? enumLabel(row.size, { xs: 'XS', s: 'S', m: 'M', l: 'L', xl: 'XL', }) : '—'} {row.color ?? '—'} {row.price ? formatMoney(row.price) : '—'} {row.stock ?? '—'} {can('product-variant.delete') && ( )}
) : (

{t('noneYet', { label: 'product variants' })}

)} {adding && ( !next && setAdding(false)} target={{ mode: 'create', prefill: { product_id: parentId }, fixed: ['product_id'], }} options={options} /> )} !open && setDeleteId(null)} title={t('deleteOf', { label: 'product variants' })} description={t('deleteAsk', { name: 'product variants' })} confirmLabel={t('delete')} onConfirm={confirmDelete} pending={deleting} />
); } function ReviewsPanel({ parentId, rows, auth, options, }: { parentId: number; rows: ReviewsRow[]; auth: { permissions: string[]; isSuperAdmin: boolean }; options: ReviewFormOptions; }) { const can = (perm: string) => auth.isSuperAdmin || auth.permissions.includes(perm); const [adding, setAdding] = useState(false); const [deleteId, setDeleteId] = useState(null); const [deleting, setDeleting] = useState(false); function confirmDelete() { if (deleteId === null) { return; } setDeleting(true); router.delete(`/product/${parentId}/review/${deleteId}`, { preserveScroll: true, onFinish: () => { setDeleting(false); setDeleteId(null); }, }); } return (

{moduleLabel('review', 'Reviews', true)} ({rows.length})

{can('review.view') && rows.length > 0 && ( {t('viewAll')} )} {can('review.create') && ( )}
{/* The panel scrolls inside itself. Without this the table's minimum width holds the whole page open — measured at +351px on a 375px phone, and still +222px at 768px. `contain-paint` is required for the same reason it is on the index scrollport: clipping alone still counts the full width toward the document. The comment sits here, in children position, not inside the ternary below: a braced JSX comment in expression position parses as an object literal, and the file silently stops being valid TSX — prettier and eslint then skip it without failing the generate. */} {rows.length > 0 ? (
{rows.map((row) => ( ))}
Title Rating Status Customer
{can('review.view') ? ( {row.title} ) : ( row.title )} {row.rating ?? '—'} {row.status ? enumLabel(row.status) : '—'} {row.customer ?? '—'} {can('review.delete') && ( )}
) : (

{t('noneYet', { label: 'reviews' })}

)} {adding && ( !next && setAdding(false)} target={{ mode: 'create', prefill: { product_id: parentId }, fixed: ['product_id'], }} options={options} /> )} !open && setDeleteId(null)} title={t('deleteOf', { label: 'reviews' })} description={t('deleteAsk', { name: 'reviews' })} confirmLabel={t('delete')} onConfirm={confirmDelete} pending={deleting} />
); } export default function ProductShow({ item, auth, timeline, abilities, statusOptions, fkOptionsUrl, tagsOptions, productVariantsFormOptions, reviewsFormOptions, }: { item: ProductDetail; auth: { permissions: string[]; isSuperAdmin: boolean }; /** The audit trail, or null for a viewer not allowed to read it. */ timeline: ActivityEntry[] | null; /** What this viewer may do to this record, decided by the module's policy on the server. */ abilities: { update: boolean; updateRestricted: boolean; delete: boolean }; statusOptions: string[]; fkOptionsUrl: string; tagsOptions: { id: number; name: string }[]; productVariantsFormOptions: ProductVariantFormOptions; reviewsFormOptions: ReviewFormOptions; }) { // Editing happens in a dialog here too, as on the list. const [editing, setEditing] = useState(false); // This module's name, in the app's language — the `:label` the chrome // strings carry. Same lookup as the index page. const label = moduleLabel('product', 'Product'); const labels = moduleLabel('product', 'Products', true); // Another module's links and panels answer to that module's permissions. const can = (perm: string) => auth.isSuperAdmin || auth.permissions.includes(perm); const canEdit = abilities.update; const canDelete = abilities.delete; const canEditRestricted = abilities.updateRestricted; const [showDelete, setShowDelete] = useState(false); const [deleting, setDeleting] = useState(false); function handleDelete() { router.delete(destroy(item.id), { preserveScroll: true, onStart: () => setDeleting(true), onFinish: () => setDeleting(false), }); } return ( <> {enumLabel(item.status)} } subtitle={ <> SKU · {item.sku} {item.sku ? ( ) : null} } actions={ <> {canEdit && ( )} {canDelete && ( )} } /> 0, )} value={ item.photos && item.photos.length > 0 ? ( {item.photos.map((p) => ( ))} ) : ( '—' ) } /> ) : ( '—' ) } /> ) : ( '—' ) } />