@24i/bigscreen-sdk 1.0.18 → 1.0.19-alpha.2343

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@24i/bigscreen-sdk",
3
- "version": "1.0.18",
3
+ "version": "1.0.19-alpha.2343",
4
4
  "description": "SmartApps BIGscreen SDK monorepo",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -41,7 +41,7 @@
41
41
  "@24i/appstage-shared-events-manager": "1.0.7",
42
42
  "@24i/appstage-shared-perf-utils": "1.0.5",
43
43
  "@24i/player-base": "6.10.0",
44
- "@24i/smartapps-datalayer": "3.0.7-alpha.998",
44
+ "@24i/smartapps-datalayer": "3.0.8-alpha.1006",
45
45
  "@sentry/browser": "7.35.0",
46
46
  "@sentry/types": "7.35.0",
47
47
  "@types/gtag.js": "^0.0.12",
@@ -55,11 +55,11 @@
55
55
  "@24i/bigscreen-sdk": "file:.",
56
56
  "@24i/eslint-config-smartapps-bigscreen-linters": "file:./libs/linters",
57
57
  "@24i/smartapps-bigscreen-changelog-generator": "0.17.0",
58
- "@babel/core": "^7.21.0",
59
- "@babel/preset-env": "^7.20.2",
58
+ "@babel/core": "^7.22.1",
59
+ "@babel/preset-env": "^7.22.4",
60
60
  "@types/fs-extra": "^11.0.1",
61
61
  "@types/inquirer": "^9.0.3",
62
- "@types/jest": "^29.2.3",
62
+ "@types/jest": "^29.5.2",
63
63
  "@types/minimist": "^1.2.2",
64
64
  "@types/node": "^18.7.18",
65
65
  "@types/react": "^18.0.33",
@@ -70,7 +70,7 @@
70
70
  "fs-extra": "^11.1.0",
71
71
  "full-icu": "^1.5.0",
72
72
  "identity-obj-proxy": "^3.0.0",
73
- "inquirer": "^9.1.4",
73
+ "inquirer": "^9.2.6",
74
74
  "jest": "^29.3.1",
75
75
  "jest-canvas-mock": "^2.4.0",
76
76
  "jest-environment-jsdom": "^29.0.3",
@@ -3,6 +3,7 @@ import {
3
3
  Component,
4
4
  createRef,
5
5
  detachNode,
6
+ Reference,
6
7
  replaceNode,
7
8
  } from '@24i/bigscreen-sdk/jsx';
8
9
  import { filter, forEach } from '@24i/bigscreen-sdk/perf-utils/array';
@@ -13,9 +14,9 @@ import { IRoute, RouteParams } from './types';
13
14
 
14
15
  type Props = {
15
16
  path: string,
17
+ domParent?: Reference<HTMLElement>;
16
18
  regexpPath?: RegExp,
17
19
  paramNames?: string[],
18
- isDetachable?: boolean;
19
20
  Component: Function,
20
21
  };
21
22
 
@@ -26,18 +27,10 @@ export class Route extends Component<Props> {
26
27
 
27
28
  isDetached = false;
28
29
 
29
- domParent: HTMLElement | null = null;
30
-
31
30
  domRef = createRef<HTMLDivElement>();
32
31
 
33
32
  component = createRef<IRoute | null>();
34
33
 
35
- componentDidMount() {
36
- const { isDetachable } = this.props;
37
- this.domParent = this.domRef.current?.parentElement!;
38
- if (isDetachable) this.detach();
39
- }
40
-
41
34
  getParams(): RouteParams {
42
35
  const { regexpPath, paramNames } = this.props;
43
36
  if (!paramNames || !regexpPath) return {};
@@ -63,7 +56,6 @@ export class Route extends Component<Props> {
63
56
 
64
57
  show() {
65
58
  menuService.addEventListener('visibilitychange', this.onMenuVisibilityChange);
66
- this.showRoute();
67
59
  if (!this.wasCreated) {
68
60
  this.mountRoute();
69
61
  this.component.current?.activate(this.getParams());
@@ -74,10 +66,7 @@ export class Route extends Component<Props> {
74
66
 
75
67
  hide() {
76
68
  menuService.removeEventListener('visibilitychange', this.onMenuVisibilityChange);
77
- if (this.wasCreated) {
78
- this.component.current?.deactivate();
79
- this.hideRoute();
80
- }
69
+ this.deactivate();
81
70
  }
82
71
 
83
72
  replace(newRoute: Route) {
@@ -89,8 +78,9 @@ export class Route extends Component<Props> {
89
78
  }
90
79
 
91
80
  attach() {
81
+ const { domParent } = this.props;
92
82
  if (this.isDetached) {
93
- attachNode(this.domRef, this.domParent!);
83
+ attachNode(this.domRef, domParent!);
94
84
  this.isDetached = false;
95
85
  }
96
86
  }
@@ -114,14 +104,12 @@ export class Route extends Component<Props> {
114
104
  }
115
105
  }
116
106
 
117
- private showRoute() {
118
- const { isDetachable } = this.props;
119
- if (!isDetachable) this.domRef.current!.style.display = 'block';
107
+ showRoute() {
108
+ this.domRef.current!.style.display = 'block';
120
109
  }
121
110
 
122
- private hideRoute() {
123
- const { isDetachable } = this.props;
124
- if (!isDetachable) this.domRef.current!.style.display = 'none';
111
+ hideRoute() {
112
+ this.domRef.current!.style.display = 'none';
125
113
  }
126
114
 
127
115
  private mountRoute() {
@@ -134,13 +122,8 @@ export class Route extends Component<Props> {
134
122
  }
135
123
 
136
124
  render() {
137
- const { isDetachable } = this.props;
138
125
  return (
139
- <div
140
- className="route"
141
- style={!isDetachable ? { display: 'none' } : {}}
142
- ref={this.domRef}
143
- />
126
+ <div className="route" ref={this.domRef} />
144
127
  );
145
128
  }
146
129
  }
@@ -28,6 +28,8 @@ const filterNulls = (allChildren: (JSX.Element | null)[]) => (
28
28
  export class Router extends Component<Props> {
29
29
  paths: { [key: string]: RoutePath } = {};
30
30
 
31
+ routerDomRef = createRef<HTMLDivElement>();
32
+
31
33
  static defaultProps = {
32
34
  defaultPath: '/',
33
35
  detachInactiveRoutes: true,
@@ -38,12 +40,14 @@ export class Router extends Component<Props> {
38
40
  constructor(props: Props) {
39
41
  super(props);
40
42
  routerRef.current = this;
41
- this.createRefs();
43
+ const { children } = this.props;
44
+ this.createRefs(children);
42
45
  }
43
46
 
44
47
  componentDidMount() {
45
48
  window.document.addEventListener('keydown', this.onKeyDown, false);
46
49
  window.addEventListener('hashchange', this.onHashChange, false);
50
+ this.hideAllRoutes();
47
51
  this.goToDefaultPath();
48
52
  }
49
53
 
@@ -91,6 +95,8 @@ export class Router extends Component<Props> {
91
95
  return activeRoutePath?.ref.current || null;
92
96
  }
93
97
 
98
+ // TODO: refactor to reduce complexity
99
+ // eslint-disable-next-line sonarjs/cognitive-complexity
94
100
  switchRoutes(
95
101
  routePathToHide: RoutePath | null,
96
102
  routePathToShow: RoutePath | null,
@@ -104,18 +110,31 @@ export class Router extends Component<Props> {
104
110
  } else if (routePathToShow) {
105
111
  routeToHide!.hide();
106
112
  routePathToHide.isVisible = false;
107
- if (detachInactiveRoutes) routeToHide!.replace(routeToShow!);
113
+ if (detachInactiveRoutes) {
114
+ routeToHide!.replace(routeToShow!);
115
+ } else {
116
+ routeToHide!.hideRoute();
117
+ routeToShow!.showRoute();
118
+ }
108
119
  routeToShow!.show();
109
120
  routePathToShow.isVisible = true;
110
121
  return;
111
122
  } else {
112
123
  routeToHide!.hide();
113
124
  routePathToHide.isVisible = false;
114
- if (detachInactiveRoutes) routeToHide!.detach();
125
+ if (detachInactiveRoutes) {
126
+ routeToHide!.detach();
127
+ } else {
128
+ routeToHide!.hideRoute();
129
+ }
115
130
  }
116
131
  }
117
132
  if (routePathToShow) {
118
- if (detachInactiveRoutes) routeToShow!.attach();
133
+ if (detachInactiveRoutes) {
134
+ routeToShow!.attach();
135
+ } else {
136
+ routeToShow!.showRoute();
137
+ }
119
138
  routeToShow!.show();
120
139
  routePathToShow.isVisible = true;
121
140
  }
@@ -125,11 +144,10 @@ export class Router extends Component<Props> {
125
144
  this.getActiveRoute()?.focus();
126
145
  }
127
146
 
128
- createRefs() {
129
- const { children } = this.props;
130
- forEach(children, (child: JSX.Element) => {
131
- if (child.type === Route) {
132
- const { path } = child.props;
147
+ createRefs(routes: JSX.Element[]) {
148
+ forEach(routes, (route: JSX.Element) => {
149
+ if (route.type === Route) {
150
+ const { path } = route.props;
133
151
  const { regexpPath, paramNames } = pathToRegExp(path);
134
152
  this.paths[path] = {
135
153
  ref: createRef<Route>(),
@@ -151,9 +169,28 @@ export class Router extends Component<Props> {
151
169
  }
152
170
  }
153
171
 
154
- render() {
155
- const { children, detachInactiveRoutes } = this.props;
156
- return filterNulls(map(children, (child: JSX.Element) => {
172
+ hideAllRoutes() {
173
+ const { detachInactiveRoutes } = this.props;
174
+ forEach(Object.keys(this.paths), (key: string) => {
175
+ const route = this.paths[key];
176
+ if (!route.isVisible) {
177
+ if (detachInactiveRoutes) {
178
+ route.ref.current!.detach();
179
+ } else {
180
+ route.ref.current!.hideRoute();
181
+ }
182
+ }
183
+ });
184
+ }
185
+
186
+ appendRoutes(routes: JSX.Element[]) {
187
+ this.createRefs(routes);
188
+ this.appendMount(this.renderRoutes(routes), this.routerDomRef);
189
+ this.hideAllRoutes();
190
+ }
191
+
192
+ renderRoutes(routes: JSX.Element[]) {
193
+ return filterNulls(map(routes, (child: JSX.Element) => {
157
194
  if (child.type === Route) {
158
195
  const { path, Component: RouteComponent } = child.props;
159
196
  const route = this.paths[path];
@@ -161,14 +198,23 @@ export class Router extends Component<Props> {
161
198
  <Route
162
199
  ref={route.ref}
163
200
  path={path}
201
+ domParent={this.routerDomRef}
164
202
  regexpPath={route.regexpPath}
165
203
  Component={RouteComponent}
166
204
  paramNames={route.paramNames}
167
- isDetachable={detachInactiveRoutes}
168
205
  />
169
206
  );
170
207
  }
171
208
  return null;
172
209
  }));
173
210
  }
211
+
212
+ render() {
213
+ const { children } = this.props;
214
+ return (
215
+ <div ref={this.routerDomRef} className="router">
216
+ {this.renderRoutes(children)}
217
+ </div>
218
+ );
219
+ }
174
220
  }