| 1 |
/** |
| 2 |
* WordPress dependencies. |
| 3 |
*/ |
| 4 |
import { SnackbarList } from '@wordpress/components'; |
| 5 |
import { useDispatch, useSelect } from '@wordpress/data'; |
| 6 |
import { createContext, useContext, useMemo, WPElement } from '@wordpress/element'; |
| 7 |
import { store as noticeStore } from '@wordpress/notices'; |
| 8 |
|
| 9 |
/** |
| 10 |
* Internal dependencies. |
| 11 |
*/ |
| 12 |
import './style.css'; |
| 13 |
|
| 14 |
const Context = createContext(); |
| 15 |
|
| 16 |
/** |
| 17 |
* ElasticPress Settings Screen provider component. |
| 18 |
* |
| 19 |
* @param {object} props Component props. |
| 20 |
* @param {WPElement} props.children Component children. |
| 21 |
* @param {string} props.title Page title. |
| 22 |
* @returns {WPElement} Sync page component. |
| 23 |
*/ |
| 24 |
export const SettingsScreenProvider = ({ children, title }) => { |
| 25 |
const { createNotice, removeNotice } = useDispatch(noticeStore); |
| 26 |
|
| 27 |
const { notices } = useSelect((select) => { |
| 28 |
return { |
| 29 |
notices: select(noticeStore).getNotices(), |
| 30 |
}; |
| 31 |
}, []); |
| 32 |
|
| 33 |
const contextValue = useMemo( |
| 34 |
() => ({ |
| 35 |
createNotice, |
| 36 |
removeNotice, |
| 37 |
}), |
| 38 |
[createNotice, removeNotice], |
| 39 |
); |
| 40 |
|
| 41 |
return ( |
| 42 |
<Context.Provider value={contextValue}> |
| 43 |
<div className="ep-settings-page"> |
| 44 |
<div className="ep-settings-page__wrap"> |
| 45 |
<h1 className="ep-settings-page__title">{title}</h1> |
| 46 |
{children} |
| 47 |
</div> |
| 48 |
<SnackbarList |
| 49 |
className="ep-settings-page__snackbar-list" |
| 50 |
notices={notices} |
| 51 |
onRemove={(notice) => removeNotice(notice)} |
| 52 |
/> |
| 53 |
</div> |
| 54 |
</Context.Provider> |
| 55 |
); |
| 56 |
}; |
| 57 |
|
| 58 |
/** |
| 59 |
* Use the Settings Page. |
| 60 |
* |
| 61 |
* @returns {object} Settings Page Context. |
| 62 |
*/ |
| 63 |
export const useSettingsScreen = () => { |
| 64 |
return useContext(Context); |
| 65 |
}; |
| 66 |
|