This repository has been archived by the owner on May 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 28
/
createBrowserApp.js
172 lines (162 loc) · 4.79 KB
/
createBrowserApp.js
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
/* eslint-env browser */
import {
createBrowserHistory,
createHashHistory,
createMemoryHistory,
} from 'history';
import React from 'react';
import {
NavigationActions,
getNavigation,
NavigationProvider,
} from '@react-navigation/core';
import { SafeAreaProvider } from 'react-native-safe-area-context';
/* eslint-disable import/no-commonjs */
const queryString = require('query-string');
const getPathAndParamsFromLocation = location => {
const path = encodeURI(location.pathname.substr(1));
const params = queryString.parse(location.search);
return { path, params };
};
const matchPathAndParams = (a, b) => {
if (a.path !== b.path) {
return false;
}
if (queryString.stringify(a.params) !== queryString.stringify(b.params)) {
return false;
}
return true;
};
function getHistory(history) {
if (typeof history === 'string') {
switch (history) {
case 'browser':
return createBrowserHistory();
case 'hash':
return createHashHistory();
case 'memory':
return createMemoryHistory();
default:
throw new Error(
'@react-navigation/web: createBrowserApp() Invalid value for options.history ' +
history
);
}
}
return history || createBrowserHistory();
}
export default function createBrowserApp(App, { history: historyOption } = {}) {
const history = getHistory(historyOption);
let currentPathAndParams = getPathAndParamsFromLocation(history.location);
const initAction =
App.router.getActionForPathAndParams(
currentPathAndParams.path,
currentPathAndParams.params
) || NavigationActions.init();
const setHistoryListener = dispatch => {
history.listen(location => {
const pathAndParams = getPathAndParamsFromLocation(location);
if (matchPathAndParams(pathAndParams, currentPathAndParams)) {
return;
}
currentPathAndParams = pathAndParams;
const action = App.router.getActionForPathAndParams(
pathAndParams.path,
pathAndParams.params
);
if (action) {
dispatch(action);
} else {
dispatch(initAction);
}
});
};
class WebApp extends React.Component {
state = { nav: App.router.getStateForAction(initAction) };
_title = document.title;
_actionEventSubscribers = new Set();
componentDidMount() {
setHistoryListener(this.dispatch);
this.updateTitle();
this._actionEventSubscribers.forEach(subscriber =>
subscriber({
type: 'action',
action: initAction,
state: this.state.nav,
lastState: null,
})
);
}
componentDidUpdate() {
this.updateTitle();
}
updateTitle() {
const { state } = this._navigation;
const childKey = state.routes[state.index].key;
const activeNav = this._navigation.getChildNavigation(childKey);
const opts = App.router.getScreenOptions(activeNav);
this._title = opts.title || opts.headerTitle;
if (this._title) {
document.title = this._title;
}
}
_onNavigationStateChange(prevNav, nav, action) {
if (typeof this.props.onNavigationStateChange === 'function') {
this.props.onNavigationStateChange(prevNav, nav, action);
}
}
render() {
this._navigation = getNavigation(
App.router,
this.state.nav,
this.dispatch,
this._actionEventSubscribers,
() => this.props.screenProps,
() => this._navigation
);
return (
<SafeAreaProvider>
<NavigationProvider value={this._navigation}>
<App {...this.props} navigation={this._navigation} />
</NavigationProvider>
</SafeAreaProvider>
);
}
dispatch = action => {
const lastState = this.state.nav;
const newState = App.router.getStateForAction(action, lastState);
const dispatchEvents = () =>
this._actionEventSubscribers.forEach(subscriber =>
subscriber({
type: 'action',
action,
state: newState,
lastState,
})
);
if (newState && newState !== lastState) {
this.setState({ nav: newState }, () => {
this._onNavigationStateChange(lastState, newState, action);
dispatchEvents();
});
const pathAndParams =
App.router.getPathAndParamsForState &&
App.router.getPathAndParamsForState(newState);
if (
pathAndParams &&
!matchPathAndParams(pathAndParams, currentPathAndParams)
) {
currentPathAndParams = pathAndParams;
history.push(
`/${pathAndParams.path}?${queryString.stringify(
pathAndParams.params
)}`
);
}
} else {
dispatchEvents();
}
};
}
return WebApp;
}