@selfcommunity/react-core
Version:
React Core Components useful for integrating UI Community components (react-ui).
45 lines (43 loc) • 1.69 kB
JavaScript
import { jsx as _jsx } from "react/jsx-runtime";
import { createContext, useContext } from 'react';
import useSCWebSocket from '../../../hooks/useSCWebSocket';
import useSCWebPushMessaging from '../../../hooks/useSCWebPushMessaging';
import useSCMobileNativePushMessaging from '../../../hooks/useSCMobileNativePushMessaging';
/**
* Creates Global Context
*
:::tip Context can be consumed in one of the following ways:
```jsx
1. <SCNotificationContext.Consumer>{(wsInstance, subscribe,) => (...)}</SCNotificationContext.Consumer>
```
```jsx
2. const scNotificationContext: SCNotificationContextType = useContext(SCNotificationContext);
```
```jsx
3. const scNotificationContext: SCNotificationContextType = useSCNotification();
````
:::
*/
export const SCNotificationContext = createContext({});
/**
* #### Description:
* This component makes the notification context available down the React tree.
* @param children
* @return
* ```jsx
* <SCNotificationContext.Provider value={{wsInstance}}>{children}</SCNotificationContext.Provider>
* ```
*/
export default function SCNotificationProvider({ children = null }) {
const { wsInstance } = useSCWebSocket();
const { wpSubscription } = useSCWebPushMessaging();
const { mnpmInstance } = useSCMobileNativePushMessaging();
return _jsx(SCNotificationContext.Provider, Object.assign({ value: { wsInstance, wpSubscription, mnpmInstance } }, { children: children }));
}
/**
* Let's only export the `useSCNotification` hook instead of the context.
* We only want to use the hook directly and never the context component.
*/
export function useSCNotification() {
return useContext(SCNotificationContext);
}