File size: 1,087 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { createContext, useContext } from 'react';

export enum SubscriptionsPortal {
	Subscriptions = 'subscriptions',
	Reader = 'reader',
}

export interface SubscriptionManagerContext {
	portal: SubscriptionsPortal;
	isSubscriptionsPortal?: boolean;
	isReaderPortal?: boolean;
}

const SubscriptionManagerContext = createContext< SubscriptionManagerContext | undefined >(
	undefined
);

export const SubscriptionManagerContextProvider: React.FC< {
	portal: SubscriptionsPortal;
	children?: React.ReactNode;
} > = ( { portal, ...props } ) => {
	return (
		<SubscriptionManagerContext.Provider
			value={ {
				portal,
				isSubscriptionsPortal: portal === SubscriptionsPortal.Subscriptions,
				isReaderPortal: portal === SubscriptionsPortal.Reader,
			} }
			{ ...props }
		/>
	);
};

export const useSubscriptionManagerContext = (): SubscriptionManagerContext => {
	const context = useContext( SubscriptionManagerContext );
	if ( ! context ) {
		throw new Error(
			'useSubscriptionManagerContext must be used within a SubscriptionManagerContextProvider'
		);
	}
	return context;
};