import { Accordion, AccordionContent, AccordionItem, AccordionTrigger } from '@components/common/ui/Accordion.js'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@components/common/ui/Card.js'; import { Checkbox } from '@components/common/ui/Checkbox.js'; import { _ } from '@evershop/evershop/lib/locale/translate/_'; import React from 'react'; import { Controller, useFormContext } from 'react-hook-form'; interface Topic { name: string; label: string; description: string; group: string; } interface TopicsProps { webhookTopics: Topic[]; webhook?: { topics?: string[] } | null; } /** Accordion value of the "No longer available" group. */ const GONE_GROUP = '__no_longer_available__'; /** Topics grouped by their `group`, in the order they were registered. */ function groupTopics(topics: Topic[]): Array<[string, Topic[]]> { const groups = new Map(); topics.forEach((topic) => { groups.set(topic.group, [...(groups.get(topic.group) ?? []), topic]); }); return [...groups.entries()]; } function summary(selected: number, total: number): string { if (selected === 0) { return _('None selected'); } return _('${selected} of ${total} selected', { selected: String(selected), total: String(total) }); } function GroupHeader({ title, summaryText }: { title: string; summaryText: string; }) { return ( {title} {summaryText} ); } export default function Topics({ webhookTopics, webhook }: TopicsProps) { const { control } = useFormContext(); const saved = webhook?.topics ?? []; const known = new Set(webhookTopics.map((t) => t.name)); // A saved topic whose event is no longer registered (an extension was // removed): show it by its code so the admin can untick it. const gone = saved.filter((name) => !known.has(name)); return ( {_('Events')} {_( 'Choose the events to send to this URL. Order, shipment and customer events include personal data such as names, emails and addresses.' )} (Array.isArray(value) && value.length > 0) || _('Select at least one event') }} render={({ field, fieldState }) => { const selected: string[] = Array.isArray(field.value) ? field.value : []; const toggle = (name: string, checked: boolean) => field.onChange( checked ? [...selected, name] : selected.filter((item) => item !== name) ); return (
{/* Collapsed by default: the list is long. The selection lives in the form state, not in the checkboxes, so a collapsed group keeps (and saves) what was ticked. The "no longer available" group starts open because it needs a decision. */} 0 ? [GONE_GROUP] : []} className="border rounded border-border divide-y divide-border" > {groupTopics(webhookTopics).map(([group, topics]) => ( selected.includes(t.name)) .length, topics.length )} /> {topics.map((topic) => ( ))} ))} {gone.length > 0 && ( selected.includes(name)) .length, gone.length )} /> {gone.map((name) => ( ))} )} {fieldState.error && (

{fieldState.error.message}

)}
); }} />
); } export const layout = { areaId: 'wideScreen', sortOrder: 20 }; export const query = ` query Query { webhookTopics { name label description group } webhook(id: getContextValue("webhookUuid", null)) { topics } } `;