| 1 |
/** |
| 2 |
* WordPress dependencies. |
| 3 |
*/ |
| 4 |
import { useDispatch } from '@wordpress/data'; |
| 5 |
import { createContext, useContext, useMemo, useState, WPElement } from '@wordpress/element'; |
| 6 |
import { store as noticeStore } from '@wordpress/notices'; |
| 7 |
|
| 8 |
/** |
| 9 |
* Sync context. |
| 10 |
*/ |
| 11 |
const Context = createContext(); |
| 12 |
|
| 13 |
/** |
| 14 |
* Sync settings provider. |
| 15 |
* |
| 16 |
* @param {object} props Component props. |
| 17 |
* @param {boolean} props.autoIndex Whether to start an index automatically. |
| 18 |
* @param {Function} props.children Component children |
| 19 |
* @param {Array} props.indexables Indexables and their labels. |
| 20 |
* @param {Array} props.postTypes Post types and their labels. |
| 21 |
* @returns {WPElement} |
| 22 |
*/ |
| 23 |
export const SyncSettingsProvider = ({ autoIndex, children, indexables, postTypes }) => { |
| 24 |
const { createNotice } = useDispatch(noticeStore); |
| 25 |
|
| 26 |
const [args, setArgs] = useState({ |
| 27 |
include: [], |
| 28 |
indexables: [], |
| 29 |
lower_limit_object_id: null, |
| 30 |
offset: 0, |
| 31 |
put_mapping: false, |
| 32 |
post_type: [], |
| 33 |
upper_limit_object_id: null, |
| 34 |
}); |
| 35 |
|
| 36 |
const [showLog, setShowLog] = useState(false); |
| 37 |
|
| 38 |
const value = useMemo( |
| 39 |
() => ({ |
| 40 |
args, |
| 41 |
autoIndex, |
| 42 |
createNotice, |
| 43 |
indexables, |
| 44 |
postTypes, |
| 45 |
setArgs, |
| 46 |
showLog, |
| 47 |
setShowLog, |
| 48 |
}), |
| 49 |
[args, autoIndex, createNotice, indexables, postTypes, setArgs, showLog, setShowLog], |
| 50 |
); |
| 51 |
|
| 52 |
return <Context.Provider value={value}>{children}</Context.Provider>; |
| 53 |
}; |
| 54 |
|
| 55 |
/** |
| 56 |
* Use the API Search context. |
| 57 |
* |
| 58 |
* @returns {object} API Search Context. |
| 59 |
*/ |
| 60 |
export const useSyncSettings = () => { |
| 61 |
return useContext(Context); |
| 62 |
}; |
| 63 |
|