-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
createAppProviderContext.ts
68 lines (62 loc) · 1.67 KB
/
createAppProviderContext.ts
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import createApp, {
getShopOrigin,
LifecycleHook,
DispatchActionHook,
} from '@shopify/app-bridge';
import {AppProviderProps, Context} from '../../types';
import {StickyManager} from '../withSticky';
import ScrollLockManager from '../ScrollLockManager';
import Intl from '../Intl';
import Link from '../Link';
import {polarisVersion} from '../../../../configure';
export interface CreateAppProviderContext extends AppProviderProps {
stickyManager?: StickyManager;
scrollLockManager?: ScrollLockManager;
subscribe?(callback: () => void): void;
unsubscribe?(callback: () => void): void;
}
export default function createAppProviderContext({
i18n,
linkComponent,
apiKey,
shopOrigin,
forceRedirect,
stickyManager,
scrollLockManager,
subscribe = noop,
unsubscribe = noop,
}: CreateAppProviderContext = {}): Context {
const intl = new Intl(i18n);
const link = new Link(linkComponent);
const appBridge = apiKey
? createApp({
apiKey,
shopOrigin: shopOrigin || getShopOrigin(),
forceRedirect,
})
: undefined;
if (appBridge && appBridge.hooks) {
appBridge.hooks.set(LifecycleHook.DispatchAction, setClientInterfaceHook);
}
return {
polaris: {
intl,
link,
stickyManager: stickyManager || new StickyManager(),
scrollLockManager: scrollLockManager || new ScrollLockManager(),
subscribe,
unsubscribe,
appBridge,
},
};
}
export const setClientInterfaceHook: DispatchActionHook = function(next) {
return function(action) {
action.clientInterface = {
name: '@shopify/polaris',
version: polarisVersion,
};
return next(action);
};
};
function noop() {}