| 1 |
/** |
| 2 |
* WordPress dependencies. |
| 3 |
*/ |
| 4 |
import { createSlotFill, SlotFillProvider, 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 |
const { Fill, Slot } = createSlotFill('SettingsPageAction'); |
| 16 |
|
| 17 |
/** |
| 18 |
* ElasticPress Settings Screen provider component. |
| 19 |
* |
| 20 |
* @param {object} props Component props. |
| 21 |
* @param {WPElement} props.children Component children. |
| 22 |
* @param {string} props.title Page title. |
| 23 |
* @returns {WPElement} Sync page component. |
| 24 |
*/ |
| 25 |
export const SettingsScreenProvider = ({ children, title }) => { |
| 26 |
const { createNotice, removeNotice } = useDispatch(noticeStore); |
| 27 |
|
| 28 |
const { notices } = useSelect((select) => { |
| 29 |
return { |
| 30 |
notices: select(noticeStore).getNotices(), |
| 31 |
}; |
| 32 |
}, []); |
| 33 |
|
| 34 |
const contextValue = useMemo( |
| 35 |
() => ({ |
| 36 |
ActionSlot: Fill, |
| 37 |
createNotice, |
| 38 |
removeNotice, |
| 39 |
}), |
| 40 |
[createNotice, removeNotice], |
| 41 |
); |
| 42 |
|
| 43 |
return ( |
| 44 |
<SlotFillProvider> |
| 45 |
<Context.Provider value={contextValue}> |
| 46 |
<div className="ep-settings-page"> |
| 47 |
<div className="ep-settings-page__wrap"> |
| 48 |
<header className="ep-settings-page__header"> |
| 49 |
<h1 className="ep-settings-page__title">{title}</h1> |
| 50 |
<Slot /> |
| 51 |
</header> |
| 52 |
{children} |
| 53 |
</div> |
| 54 |
<SnackbarList |
| 55 |
className="ep-settings-page__snackbar-list" |
| 56 |
notices={notices} |
| 57 |
onRemove={(notice) => removeNotice(notice)} |
| 58 |
/> |
| 59 |
</div> |
| 60 |
</Context.Provider> |
| 61 |
</SlotFillProvider> |
| 62 |
); |
| 63 |
}; |
| 64 |
|
| 65 |
/** |
| 66 |
* Use the Settings Page. |
| 67 |
* |
| 68 |
* @returns {object} Settings Page Context. |
| 69 |
*/ |
| 70 |
export const useSettingsScreen = () => { |
| 71 |
return useContext(Context); |
| 72 |
}; |
| 73 |
|