|
| 1 | +/** |
| 2 | + * WordPress dependencies |
| 3 | + */ |
| 4 | +import { __ } from '@wordpress/i18n'; |
| 5 | +import { ComboboxControl } from '@wordpress/components'; |
| 6 | +import { useSelect } from '@wordpress/data'; |
| 7 | +import { store as coreDataStore } from '@wordpress/core-data'; |
| 8 | +import { useState } from '@wordpress/element'; |
| 9 | + |
| 10 | +/** |
| 11 | + * Get formatted label with snippet type |
| 12 | + * |
| 13 | + * @param {Object} snippet Snippet object with title and meta |
| 14 | + * @return {string} Formatted label |
| 15 | + */ |
| 16 | +const getFormattedLabel = (snippet) => { |
| 17 | + const title = snippet.title.rendered || __('(No title)', 'add-to-all'); |
| 18 | + const type = snippet.meta._ata_snippet_type || 'html'; |
| 19 | + return `${title} (${type.toUpperCase()})`; |
| 20 | +}; |
| 21 | + |
| 22 | +/** |
| 23 | + * SnippetSelector component for selecting a snippet from the available snippets. |
| 24 | + * |
| 25 | + * @param {Object} props Component props. |
| 26 | + * @param {number} props.value Currently selected snippet ID. |
| 27 | + * @param {Function} props.onChange Callback function when selection changes. |
| 28 | + * @return {Element} Component to render. |
| 29 | + */ |
| 30 | +export default function SnippetSelector({ value, onChange }) { |
| 31 | + const [searchInput, setSearchInput] = useState(''); |
| 32 | + |
| 33 | + const { snippets, selectedSnippet, isLoading } = useSelect( |
| 34 | + (select) => { |
| 35 | + const { getEntityRecords, getEntityRecord } = select(coreDataStore); |
| 36 | + const query = { |
| 37 | + per_page: 20, |
| 38 | + _fields: ['id', 'title', 'meta'], |
| 39 | + orderby: 'title', |
| 40 | + order: 'asc', |
| 41 | + search: searchInput || undefined, |
| 42 | + }; |
| 43 | + |
| 44 | + return { |
| 45 | + snippets: getEntityRecords('postType', 'ata_snippets', query), |
| 46 | + selectedSnippet: value |
| 47 | + ? getEntityRecord('postType', 'ata_snippets', value, { |
| 48 | + _fields: ['id', 'title', 'meta'], |
| 49 | + }) |
| 50 | + : null, |
| 51 | + isLoading: select(coreDataStore).isResolving( |
| 52 | + 'getEntityRecords', |
| 53 | + ['postType', 'ata_snippets', query] |
| 54 | + ), |
| 55 | + }; |
| 56 | + }, |
| 57 | + [searchInput, value] |
| 58 | + ); |
| 59 | + |
| 60 | + const options = (snippets || []).map((snippet) => ({ |
| 61 | + value: snippet.id, |
| 62 | + label: getFormattedLabel(snippet), |
| 63 | + })); |
| 64 | + |
| 65 | + // If we have a selected value but it's not in the current options, |
| 66 | + // add it to the options list |
| 67 | + if (selectedSnippet && !options.find((option) => option.value === value)) { |
| 68 | + options.unshift({ |
| 69 | + value: selectedSnippet.id, |
| 70 | + label: getFormattedLabel(selectedSnippet), |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + return ( |
| 75 | + <ComboboxControl |
| 76 | + __next40pxDefaultSize |
| 77 | + __nextHasNoMarginBottom |
| 78 | + label={__('Choose Snippet', 'add-to-all')} |
| 79 | + value={value || 0} |
| 80 | + onChange={(newValue) => |
| 81 | + onChange(newValue ? parseInt(newValue, 10) : 0) |
| 82 | + } |
| 83 | + options={options} |
| 84 | + onFilterValueChange={(inputValue) => setSearchInput(inputValue)} |
| 85 | + /> |
| 86 | + ); |
| 87 | +} |
0 commit comments