import { Button } from '@components/common/ui/Button.js'; import { Dialog, DialogContent, DialogDescription, DialogFooter, DialogHeader, DialogTitle } from '@components/common/ui/Dialog.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import { AlertTriangle, ArrowRight, Layers, Minus, Pencil, Plus, Rocket } from 'lucide-react'; import React, { useMemo } from 'react'; interface ChangesetOperation { entityUrn: string; oldPayload: Record | null; newPayload: Record | null; route?: string | null; } interface PublishDialogProps { open: boolean; onClose: () => void; onConfirm: () => void; isBusy: boolean; operations: ChangesetOperation[]; } interface OpSummary { added: number; updated: number; removed: number; total: number; routes: Array<{ id: string; count: number }>; } function summarize(ops: ChangesetOperation[]): OpSummary { let added = 0; let updated = 0; let removed = 0; const routeCounts = new Map(); for (const op of ops) { const isWidget = op.entityUrn?.includes(':widget_instance:'); const isPlacement = op.entityUrn?.includes(':widget_placement:'); if (!isWidget && !isPlacement) continue; const isInsert = op.oldPayload == null && op.newPayload != null; const isDelete = op.oldPayload != null && op.newPayload == null; const isUpdate = op.oldPayload != null && op.newPayload != null; // Count widget_instance ops only — placement ops are derived (every // INSERT/DELETE widget pairs with a placement op; updates to widget // settings vs placement sort_order are merged into a single "updated" // figure). Showing both was noisy without adding signal. if (isWidget) { if (isInsert) added += 1; else if (isUpdate) updated += 1; else if (isDelete) removed += 1; } else if (isPlacement) { // Placement-only ops (e.g. reorder, cross-route share) count as updates. if (isUpdate) updated += 1; else if (isInsert) added += 0; // already counted via the widget INSERT else if (isDelete) removed += 0; // already counted via the widget DELETE } const r = op.route; if (typeof r === 'string' && r.length > 0) { routeCounts.set(r, (routeCounts.get(r) ?? 0) + 1); } } const routes = Array.from(routeCounts.entries()) .map(([id, count]) => ({ id, count })) .sort((a, b) => b.count - a.count); return { added, updated, removed, total: ops.length, routes }; } function humanizeRoute(id: string): string { if (id === 'all') return _('every page'); // Camel-case route ids → spaced words: `categoryView` → `Category view`. const spaced = id.replace(/([a-z])([A-Z])/g, '$1 $2').toLowerCase(); return spaced.charAt(0).toUpperCase() + spaced.slice(1); } interface StatCardProps { icon: React.ReactNode; label: string; count: number; tone: 'add' | 'update' | 'remove'; } const TONE_CLASSES: Record = { add: 'bg-emerald-50 text-emerald-700 ring-emerald-200/70 dark:bg-emerald-950/40 dark:text-emerald-300 dark:ring-emerald-900/60', update: 'bg-sky-50 text-sky-700 ring-sky-200/70 dark:bg-sky-950/40 dark:text-sky-300 dark:ring-sky-900/60', remove: 'bg-rose-50 text-rose-700 ring-rose-200/70 dark:bg-rose-950/40 dark:text-rose-300 dark:ring-rose-900/60' }; function StatCard({ icon, label, count, tone }: StatCardProps) { const dim = count === 0; return (
{icon} {label}
{count}
); } export function PublishDialog({ open, onClose, onConfirm, isBusy, operations }: PublishDialogProps): React.ReactElement { const summary = useMemo(() => summarize(operations || []), [operations]); const total = summary.total; const hasOps = total > 0; return ( (!o && !isBusy ? onClose() : null)} >
{hasOps ? _('Publish to the live storefront') : _('Nothing to publish')} {hasOps ? ( <> {total === 1 ? _('1 change will go live immediately.') : _('${total} changes will go live immediately.', { total: String(total) })}{' '} {_( 'Visitors will see the new layout on their next page load.' )} ) : ( <>{_('This changeset has no operations yet.')} )}
{hasOps && (
} label={_('Added')} count={summary.added} tone="add" /> } label={_('Updated')} count={summary.updated} tone="update" /> } label={_('Removed')} count={summary.removed} tone="remove" />
{summary.routes.length > 0 && (
)}
)}
); }