import { InspectorControls } from "@wordpress/block-editor"; import { PanelBody, TextControl, RangeControl, SelectControl, ToggleControl, } from "@wordpress/components"; import { __ } from "@wordpress/i18n"; import { useEffect, useRef } from "@wordpress/element"; import ServerSideRender from "@wordpress/server-side-render"; import { ClassificationMultiSelect } from "../components/ClassificationMultiSelect"; import { migrateNumericCsvPairToIds } from "../utils/migrateTaxonomyIds"; interface ActivityBlockAttributes { order: "asc" | "desc"; columns: number; per_page: number; title: string; show_pagination: boolean; activityIds: number[]; activity_ids?: string; activity?: string; show_trip_count: boolean; show_description: boolean; show_image: boolean; hide_empty: boolean; } interface EditProps { attributes: ActivityBlockAttributes; setAttributes: (attrs: Partial) => void; } function normalizeIds(arr: number[] | undefined): number[] { return Array.isArray(arr) ? [...new Set(arr.map((n) => parseInt(String(n), 10)).filter((n) => n > 0))] : []; } export default function Edit({ attributes, setAttributes }: EditProps) { const migratedOnce = useRef(false); useEffect(() => { if (migratedOnce.current) { return; } migratedOnce.current = true; let ids = normalizeIds(attributes.activityIds); if (ids.length === 0) { ids = migrateNumericCsvPairToIds( attributes.activity_ids, attributes.activity, ); } if (ids.length === 0) { return; } setAttributes({ activityIds: ids, activity: "", activity_ids: "", }); // eslint-disable-next-line react-hooks/exhaustive-deps }, [setAttributes]); const activityIds = normalizeIds(attributes.activityIds); return ( <> setAttributes({ title: value })} /> setAttributes({ order: value as "asc" | "desc" }) } /> setAttributes({ per_page: value !== undefined && value !== null ? value : 10, }) } min={1} max={50} /> setAttributes({ columns: value !== undefined && value !== null ? value : 3, }) } min={1} max={6} /> setAttributes({ show_pagination: value }) } /> setAttributes({ activityIds: ids })} /> setAttributes({ show_trip_count: value }) } /> setAttributes({ show_description: value }) } /> setAttributes({ show_image: value })} /> setAttributes({ hide_empty: value })} />
); }