/** * WordPress dependencies */ import { useDispatch, useSelect } from '@safe-wordpress/data'; import { useCallback } from '@safe-wordpress/element'; /** * External dependencies */ import { store as NC_DATA } from '@nelio-content/data'; import type { Maybe, Uuid, WebhookSettings } from '@nelio-content/types'; /** * Internal dependencies */ import { store as NC_SOCIAL_EDITOR } from '../store'; import type { State } from '../store/types'; export const useWebhookSettings = (): State[ 'attributes' ][ 'webhookSettingsByProfile' ] => useSelect( ( select ) => select( NC_SOCIAL_EDITOR ).getWebhookSettings(), [] ); export const useWebhookSettingsInProfile = ( profileId: Maybe< Uuid > ): [ Maybe< WebhookSettings >, ( settings: WebhookSettings ) => void ] => { const settings = useSelect( ( select ) => { if ( ! profileId ) { return undefined; } const editor = select( NC_SOCIAL_EDITOR ); const data = select( NC_DATA ); return ( editor.getWebhookSettingsInProfile( profileId ) ?? data.getSocialProfile( profileId )?.webhookSettings ); }, [ profileId ] ); const { setWebhookSettingsInProfile } = useDispatch( NC_SOCIAL_EDITOR ); const setSettings = useCallback( ( updatedSettings: WebhookSettings ) => { if ( ! profileId ) { return; } void setWebhookSettingsInProfile( profileId, updatedSettings ); }, [ profileId, setWebhookSettingsInProfile ] ); return [ settings, setSettings ]; };