command-loader.js
142 lines
| 1 | import { addQueryArgs } from '@wordpress/url'; |
| 2 | import { useCommandLoader } from '@wordpress/commands'; |
| 3 | import { useSelect } from '@wordpress/data'; |
| 4 | import { Dashicon } from '@wordpress/components'; |
| 5 | import { store as coreStore } from '@wordpress/core-data'; |
| 6 | // eslint-disable-next-line import/no-extraneous-dependencies |
| 7 | import { useMemo, createElement } from '@wordpress/element'; |
| 8 | |
| 9 | const taxonomies = [ 'advanced_ads_groups' ]; |
| 10 | const postTypes = [ 'advanced_ads', 'advanced_ads_plcmnt' ]; |
| 11 | const labelsHash = { |
| 12 | advanced_ads: 'Ads', |
| 13 | advanced_ads_plcmnt: 'Placement', |
| 14 | advanced_ads_groups: 'Group', |
| 15 | }; |
| 16 | const iconsHash = { |
| 17 | advanced_ads: 'format-video', |
| 18 | advanced_ads_plcmnt: 'welcome-widgets-menus', |
| 19 | advanced_ads_groups: 'category', |
| 20 | }; |
| 21 | |
| 22 | const getEditUrl = ( type, item ) => { |
| 23 | if ( 'advanced_ads_plcmnt' === type ) { |
| 24 | return addQueryArgs( `edit.php#modal-placement-edit-${ item.id }`, { |
| 25 | post_status: 'all', |
| 26 | s: item.title?.rendered, |
| 27 | post_type: type, |
| 28 | } ); |
| 29 | } |
| 30 | |
| 31 | return addQueryArgs( 'post.php', { |
| 32 | action: 'edit', |
| 33 | post: item.id, |
| 34 | } ); |
| 35 | }; |
| 36 | |
| 37 | function useAdvancedAdsSearchCommandLoader( { search } ) { |
| 38 | const { records, isLoading } = useSelect( |
| 39 | ( select ) => { |
| 40 | const { getEntityRecords, hasFinishedResolution } = |
| 41 | select( coreStore ); |
| 42 | const query = { |
| 43 | search, |
| 44 | per_page: 10, |
| 45 | }; |
| 46 | |
| 47 | const postResults = postTypes.flatMap( ( type ) => { |
| 48 | const r = getEntityRecords( 'postType', type, query ); |
| 49 | return r |
| 50 | ? r.map( ( item ) => ( { |
| 51 | name: `edit-${ type }-${ item.id }`, |
| 52 | label: `${ labelsHash[ type ] }: ${ item.title?.rendered }`, |
| 53 | category: 'edit', |
| 54 | icon: createElement( Dashicon, { |
| 55 | icon: iconsHash[ type ], |
| 56 | } ), |
| 57 | editUrl: getEditUrl( type, item ), |
| 58 | } ) ) |
| 59 | : []; |
| 60 | } ); |
| 61 | |
| 62 | const taxResults = taxonomies.flatMap( ( type ) => { |
| 63 | const r = getEntityRecords( 'taxonomy', type, query ); |
| 64 | return r |
| 65 | ? r.map( ( item ) => ( { |
| 66 | name: `edit-${ type }-${ item.id }`, |
| 67 | label: `${ labelsHash[ type ] }: ${ item.name }`, |
| 68 | category: 'edit', |
| 69 | icon: createElement( Dashicon, { |
| 70 | icon: iconsHash[ type ], |
| 71 | } ), |
| 72 | editUrl: addQueryArgs( |
| 73 | `admin.php#modal-group-edit-${ item.id }`, |
| 74 | { |
| 75 | page: 'advanced-ads-groups', |
| 76 | s: item.name, |
| 77 | } |
| 78 | ), |
| 79 | } ) ) |
| 80 | : []; |
| 81 | } ); |
| 82 | |
| 83 | const finishedPosts = postTypes.every( ( type ) => |
| 84 | hasFinishedResolution( 'getEntityRecords', [ |
| 85 | 'postType', |
| 86 | type, |
| 87 | query, |
| 88 | ] ) |
| 89 | ); |
| 90 | |
| 91 | const finishedTax = taxonomies.every( ( type ) => |
| 92 | hasFinishedResolution( 'getEntityRecords', [ |
| 93 | 'taxonomy', |
| 94 | type, |
| 95 | query, |
| 96 | ] ) |
| 97 | ); |
| 98 | |
| 99 | return { |
| 100 | records: [ ...postResults, ...taxResults ], |
| 101 | isLoading: ! finishedPosts || ! finishedTax, |
| 102 | }; |
| 103 | }, |
| 104 | [ search ] |
| 105 | ); |
| 106 | |
| 107 | // Create the commands. |
| 108 | const commands = useMemo( () => { |
| 109 | return ( records ?? [] ).slice( 0, 10 ).map( ( record ) => { |
| 110 | return { |
| 111 | ...record, |
| 112 | callback: ( { close } ) => { |
| 113 | document.location = record.editUrl; |
| 114 | close(); |
| 115 | }, |
| 116 | }; |
| 117 | } ); |
| 118 | }, [ records ] ); |
| 119 | |
| 120 | return { |
| 121 | commands, |
| 122 | isLoading, |
| 123 | }; |
| 124 | } |
| 125 | |
| 126 | function AdvancedAdsSearch() { |
| 127 | useCommandLoader( { |
| 128 | name: 'advanced-ads/commands/search', |
| 129 | hook: useAdvancedAdsSearchCommandLoader, |
| 130 | } ); |
| 131 | |
| 132 | return null; |
| 133 | } |
| 134 | |
| 135 | export function commandLoader() { |
| 136 | const mountPoint = document.createElement( 'div' ); |
| 137 | mountPoint.id = 'advanced-ads-commands-root'; |
| 138 | document.body.appendChild( mountPoint ); |
| 139 | const root = wp.element.createRoot( mountPoint ); |
| 140 | root.render( createElement( AdvancedAdsSearch ) ); |
| 141 | } |
| 142 |