import { InputField } from '@components/common/form/InputField.js'; import { NumberField } from '@components/common/form/NumberField.js'; import { RadioGroupField } from '@components/common/form/RadioGroupField.js'; import { SelectField } from '@components/common/form/SelectField.js'; import { Button } from '@components/common/ui/Button.js'; import { Card, CardContent } from '@components/common/ui/Card.js'; import { Table, TableBody, TableCell, TableFooter, TableHead, TableHeader, TableRow } from '@components/common/ui/Table.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import { Trash2 } from 'lucide-react'; import React from 'react'; import { useFieldArray, useFormContext } from 'react-hook-form'; interface Placement { route: string; area: string; sortOrder?: number; entityUrn?: string | null; } interface GeneralProps { widget?: { name?: string; status?: number; placements?: Placement[]; }; routes: Array<{ value: string; label: string; isApi: boolean; isAdmin: boolean; methods: string[]; }>; } export default function General({ widget, routes }: GeneralProps) { const { control } = useFormContext(); const { fields, append, remove } = useFieldArray({ control, name: 'placements' }); // Pages a widget can be placed on: storefront GET routes (single method, // not API / admin), plus an "All pages" pseudo-route. const pageOptions = [ { value: 'all', label: _('All pages') }, ...routes .filter( (r) => r.isApi === false && r.isAdmin === false && r.methods.includes('GET') && r.methods.length === 1 ) .map((r) => ({ value: r.value, label: r.label })) ]; // Placements scoped to a specific page (a landing page body, a homepage // backup, a CMS page override) are managed in the page builder: they are // never shown here and never touched on save. const entityScopedCount = (widget?.placements ?? []).filter( (p) => p.entityUrn != null ).length; // Seed the rows from the widget's existing route-level placements. New // widgets have no placements yet — start with one blank row. A widget that // lives ONLY inside pages gets no blank row: the area field is required, so // a blank row would make the form unsaveable, and filling it in would put // the widget on every page by accident. React.useEffect(() => { if (fields.length > 0) return; const existing = (widget?.placements ?? []).filter( (p) => p.entityUrn == null ); if (existing.length > 0) { existing.forEach((p) => append( { route: p.route, area: p.area, sort_order: p.sortOrder ?? 0 }, { shouldFocus: false } ) ); } else if (entityScopedCount === 0) { append({ route: 'all', area: '', sort_order: 0 }, { shouldFocus: false }); } }, [widget, fields.length, append, entityScopedCount]); return (
{_('Placements')}

{_( 'Each row places this widget on one page, in one area, with its own sort order. Lower numbers appear first.' )}

{entityScopedCount > 0 && (

{_( 'This widget is also placed on ${count} specific page(s), managed in the page builder. Those placements are not listed here and are not changed when you save.', { count: String(entityScopedCount) } )}

)} {_('Page')} {_('Area')} {_('Sort')} {fields.map((field, index) => ( {fields.length > 1 && ( )} ))}
); } export const layout = { areaId: 'rightSide', sortOrder: 15 }; export const query = ` query Query { widget(id: getContextValue("widgetId", null)) { name status placements { route area sortOrder entityUrn } } routes { value: id label: name isApi isAdmin methods } } `;