@adview/react 1.0.13 → 1.0.15

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.
Files changed (107) hide show
  1. package/dist/AdViewUnit/AdViewUnit.cjs +273 -243
  2. package/dist/AdViewUnit/AdViewUnit.cjs.map +1 -1
  3. package/dist/AdViewUnit/AdViewUnit.client.cjs +234 -198
  4. package/dist/AdViewUnit/AdViewUnit.client.cjs.map +1 -1
  5. package/dist/AdViewUnit/AdViewUnit.client.d.cts +2 -2
  6. package/dist/AdViewUnit/AdViewUnit.client.d.ts +2 -2
  7. package/dist/AdViewUnit/AdViewUnit.client.js +226 -190
  8. package/dist/AdViewUnit/AdViewUnit.client.js.map +1 -1
  9. package/dist/AdViewUnit/AdViewUnit.d.cts +1 -1
  10. package/dist/AdViewUnit/AdViewUnit.d.ts +1 -1
  11. package/dist/AdViewUnit/AdViewUnit.js +261 -231
  12. package/dist/AdViewUnit/AdViewUnit.js.map +1 -1
  13. package/dist/AdViewUnit/AdViewUnit.server.cjs +229 -157
  14. package/dist/AdViewUnit/AdViewUnit.server.cjs.map +1 -1
  15. package/dist/AdViewUnit/AdViewUnit.server.d.cts +3 -3
  16. package/dist/AdViewUnit/AdViewUnit.server.d.ts +3 -3
  17. package/dist/AdViewUnit/AdViewUnit.server.js +227 -155
  18. package/dist/AdViewUnit/AdViewUnit.server.js.map +1 -1
  19. package/dist/AdViewUnit/{AdViewUnitBanner.cjs → AdViewUnitBannerTemplate.cjs} +36 -14
  20. package/dist/AdViewUnit/AdViewUnitBannerTemplate.cjs.map +1 -0
  21. package/dist/AdViewUnit/AdViewUnitBannerTemplate.d.cts +17 -0
  22. package/dist/AdViewUnit/AdViewUnitBannerTemplate.d.ts +17 -0
  23. package/dist/AdViewUnit/AdViewUnitBannerTemplate.js +51 -0
  24. package/dist/AdViewUnit/AdViewUnitBannerTemplate.js.map +1 -0
  25. package/dist/AdViewUnit/AdViewUnitDefaultTemplate.cjs +66 -0
  26. package/dist/AdViewUnit/AdViewUnitDefaultTemplate.cjs.map +1 -0
  27. package/dist/AdViewUnit/AdViewUnitDefaultTemplate.d.cts +16 -0
  28. package/dist/AdViewUnit/AdViewUnitDefaultTemplate.d.ts +16 -0
  29. package/dist/AdViewUnit/AdViewUnitDefaultTemplate.js +35 -0
  30. package/dist/AdViewUnit/AdViewUnitDefaultTemplate.js.map +1 -0
  31. package/dist/AdViewUnit/{AdViewUnitNative.cjs → AdViewUnitNativeTemplate.cjs} +31 -13
  32. package/dist/AdViewUnit/AdViewUnitNativeTemplate.cjs.map +1 -0
  33. package/dist/AdViewUnit/AdViewUnitNativeTemplate.d.cts +16 -0
  34. package/dist/AdViewUnit/AdViewUnitNativeTemplate.d.ts +16 -0
  35. package/dist/AdViewUnit/{AdViewUnitNative.js → AdViewUnitNativeTemplate.js} +28 -10
  36. package/dist/AdViewUnit/AdViewUnitNativeTemplate.js.map +1 -0
  37. package/dist/AdViewUnit/{AdViewUnitProxy.cjs → AdViewUnitProxyTemplate.cjs} +33 -13
  38. package/dist/AdViewUnit/AdViewUnitProxyTemplate.cjs.map +1 -0
  39. package/dist/AdViewUnit/AdViewUnitProxyTemplate.d.cts +17 -0
  40. package/dist/AdViewUnit/AdViewUnitProxyTemplate.d.ts +17 -0
  41. package/dist/AdViewUnit/AdViewUnitProxyTemplate.js +52 -0
  42. package/dist/AdViewUnit/AdViewUnitProxyTemplate.js.map +1 -0
  43. package/dist/AdViewUnit/AdViewUnitTemplate.cjs +132 -0
  44. package/dist/AdViewUnit/AdViewUnitTemplate.cjs.map +1 -1
  45. package/dist/AdViewUnit/AdViewUnitTemplate.d.cts +14 -1
  46. package/dist/AdViewUnit/AdViewUnitTemplate.d.ts +14 -1
  47. package/dist/AdViewUnit/AdViewUnitTemplate.js +95 -0
  48. package/dist/AdViewUnit/AdViewUnitTemplate.js.map +1 -1
  49. package/dist/AdViewUnit/useAdViewController.cjs +5 -3
  50. package/dist/AdViewUnit/useAdViewController.cjs.map +1 -1
  51. package/dist/AdViewUnit/useAdViewController.d.cts +2 -2
  52. package/dist/AdViewUnit/useAdViewController.d.ts +2 -2
  53. package/dist/AdViewUnit/useAdViewController.js +5 -3
  54. package/dist/AdViewUnit/useAdViewController.js.map +1 -1
  55. package/dist/AdViewUnit/utils.cjs +33 -0
  56. package/dist/AdViewUnit/utils.cjs.map +1 -0
  57. package/dist/AdViewUnit/utils.d.cts +7 -0
  58. package/dist/AdViewUnit/utils.d.ts +7 -0
  59. package/dist/AdViewUnit/utils.js +8 -0
  60. package/dist/AdViewUnit/utils.js.map +1 -0
  61. package/dist/index.cjs +305 -204
  62. package/dist/index.cjs.map +1 -1
  63. package/dist/index.d.cts +8 -7
  64. package/dist/index.d.ts +8 -7
  65. package/dist/index.js +286 -193
  66. package/dist/index.js.map +1 -1
  67. package/dist/server.cjs +302 -167
  68. package/dist/server.cjs.map +1 -1
  69. package/dist/server.d.cts +7 -6
  70. package/dist/server.d.ts +7 -6
  71. package/dist/server.js +288 -160
  72. package/dist/server.js.map +1 -1
  73. package/dist/{types-BaV07oMT.d.cts → types-DHjNgnRW.d.cts} +35 -43
  74. package/dist/{types-BaV07oMT.d.ts → types-DHjNgnRW.d.ts} +35 -43
  75. package/package.json +1 -1
  76. package/dist/AdViewUnit/AdViewUnitBanner.cjs.map +0 -1
  77. package/dist/AdViewUnit/AdViewUnitBanner.d.cts +0 -10
  78. package/dist/AdViewUnit/AdViewUnitBanner.d.ts +0 -10
  79. package/dist/AdViewUnit/AdViewUnitBanner.js +0 -29
  80. package/dist/AdViewUnit/AdViewUnitBanner.js.map +0 -1
  81. package/dist/AdViewUnit/AdViewUnitNative.cjs.map +0 -1
  82. package/dist/AdViewUnit/AdViewUnitNative.d.cts +0 -10
  83. package/dist/AdViewUnit/AdViewUnitNative.d.ts +0 -10
  84. package/dist/AdViewUnit/AdViewUnitNative.js.map +0 -1
  85. package/dist/AdViewUnit/AdViewUnitProxy.cjs.map +0 -1
  86. package/dist/AdViewUnit/AdViewUnitProxy.d.cts +0 -10
  87. package/dist/AdViewUnit/AdViewUnitProxy.d.ts +0 -10
  88. package/dist/AdViewUnit/AdViewUnitProxy.js +0 -32
  89. package/dist/AdViewUnit/AdViewUnitProxy.js.map +0 -1
  90. package/dist/AdViewUnit/AdViewUnitTypeSwitch.cjs +0 -203
  91. package/dist/AdViewUnit/AdViewUnitTypeSwitch.cjs.map +0 -1
  92. package/dist/AdViewUnit/AdViewUnitTypeSwitch.d.cts +0 -12
  93. package/dist/AdViewUnit/AdViewUnitTypeSwitch.d.ts +0 -12
  94. package/dist/AdViewUnit/AdViewUnitTypeSwitch.js +0 -170
  95. package/dist/AdViewUnit/AdViewUnitTypeSwitch.js.map +0 -1
  96. package/dist/AdViewUnit/AdViewUnitWrapper.client.cjs +0 -250
  97. package/dist/AdViewUnit/AdViewUnitWrapper.client.cjs.map +0 -1
  98. package/dist/AdViewUnit/AdViewUnitWrapper.client.d.cts +0 -11
  99. package/dist/AdViewUnit/AdViewUnitWrapper.client.d.ts +0 -11
  100. package/dist/AdViewUnit/AdViewUnitWrapper.client.js +0 -220
  101. package/dist/AdViewUnit/AdViewUnitWrapper.client.js.map +0 -1
  102. package/dist/AdViewUnit/AdViewUnitWrapper.server.cjs +0 -232
  103. package/dist/AdViewUnit/AdViewUnitWrapper.server.cjs.map +0 -1
  104. package/dist/AdViewUnit/AdViewUnitWrapper.server.d.cts +0 -11
  105. package/dist/AdViewUnit/AdViewUnitWrapper.server.d.ts +0 -11
  106. package/dist/AdViewUnit/AdViewUnitWrapper.server.js +0 -201
  107. package/dist/AdViewUnit/AdViewUnitWrapper.server.js.map +0 -1
package/dist/server.js CHANGED
@@ -1,77 +1,75 @@
1
1
  'use client';
2
2
 
3
3
  // src/AdViewUnit/AdViewUnit.server.tsx
4
- import React7 from "react";
4
+ import React6 from "react";
5
5
  import { adViewFetcher, getAdRequestUrl, getResolveConfig } from "@adview/core/utils";
6
6
 
7
- // src/AdViewUnit/AdViewUnitTracking.tsx
8
- import { executeImpressionsTracking } from "@adview/core/utils";
9
- import React, { useCallback, useEffect, useMemo, useRef } from "react";
10
- function AdViewUnitTracking({
11
- impressions,
12
- views,
13
- clicks,
14
- children
15
- }) {
16
- const isServer = typeof window === "undefined";
17
- const wrapperRef = useRef(null);
18
- const executeImpressionsTrackingHandler = useCallback(
19
- () => executeImpressionsTracking(impressions),
20
- [impressions]
21
- );
22
- const trackingObserverInstanceHandler = useCallback(
23
- (entries) => {
24
- entries.forEach((entry) => {
25
- if (entry.isIntersecting) {
26
- executeImpressionsTracking(views);
27
- trackingObserverInstance?.unobserve(wrapperRef.current);
28
- }
29
- });
7
+ // src/AdViewUnit/AdViewUnitBannerTemplate.tsx
8
+ import { getAssetByName, getPrepareURL, getSrcSetCSSThumbs } from "@adview/core/utils";
9
+ import React from "react";
10
+
11
+ // src/AdViewUnit/utils.ts
12
+ var matchExpectedState = (expectState, state) => {
13
+ return !expectState || (expectState.isInitial === void 0 || expectState.isInitial === state?.isInitial) && (expectState.isLoading === void 0 || expectState.isLoading === state?.isLoading) && (expectState.isError === void 0 || expectState.isError === state?.isError) && (expectState.isComplete === void 0 || expectState.isComplete === state?.isComplete);
14
+ };
15
+
16
+ // src/AdViewUnit/AdViewUnitBannerTemplate.tsx
17
+ function AdViewUnitBannerTemplate({ className = "banner", style, data, state, ...props }) {
18
+ const expectState = props?.isInitial || props?.isLoading || props?.isError || props?.isComplete ? {
19
+ isInitial: props?.isInitial,
20
+ isLoading: props?.isLoading,
21
+ isError: props?.isError,
22
+ isComplete: props?.isComplete
23
+ } : { isComplete: true };
24
+ if (!data || !data.assets || !data.assets.length) {
25
+ return null;
26
+ }
27
+ if (!matchExpectedState(expectState, state)) {
28
+ return null;
29
+ }
30
+ const asset = getAssetByName("main", data.assets);
31
+ return /* @__PURE__ */ React.createElement(
32
+ "a",
33
+ {
34
+ target: "_blank",
35
+ rel: "noopener noreferrer",
36
+ href: getPrepareURL(data.url),
37
+ className,
38
+ style: { fontSize: 0, ...style }
30
39
  },
31
- [views]
40
+ asset && /* @__PURE__ */ React.createElement(
41
+ "img",
42
+ {
43
+ alt: "main",
44
+ src: asset.path,
45
+ srcSet: asset.thumbs ? getSrcSetCSSThumbs(asset.thumbs) : ""
46
+ }
47
+ )
32
48
  );
33
- const trackingObserverInstance = useMemo(() => {
34
- return !isServer ? new IntersectionObserver(trackingObserverInstanceHandler) : null;
35
- }, [isServer, trackingObserverInstanceHandler]);
36
- const clickTrackerHandler = useCallback(() => {
37
- executeImpressionsTracking(clicks);
38
- wrapperRef.current?.removeEventListener("click", clickTrackerHandler);
39
- }, [clicks]);
40
- useEffect(() => {
41
- if (wrapperRef.current && trackingObserverInstance) {
42
- executeImpressionsTrackingHandler();
43
- wrapperRef.current?.addEventListener("click", clickTrackerHandler);
44
- trackingObserverInstance?.observe(wrapperRef.current);
45
- return () => {
46
- wrapperRef.current?.removeEventListener("click", clickTrackerHandler);
47
- if (wrapperRef.current)
48
- trackingObserverInstance?.unobserve(wrapperRef.current);
49
- };
50
- }
51
- return () => {
52
- };
53
- }, [
54
- trackingObserverInstance,
55
- executeImpressionsTrackingHandler,
56
- clickTrackerHandler
57
- ]);
58
- return /* @__PURE__ */ React.createElement("div", { ref: wrapperRef }, children);
59
49
  }
60
- var AdViewUnitTracking_default = AdViewUnitTracking;
61
-
62
- // src/AdViewUnit/AdViewUnitWrapper.server.tsx
63
- import React6 from "react";
50
+ AdViewUnitBannerTemplate.defaults = {
51
+ type: "banner"
52
+ };
53
+ var AdViewUnitBannerTemplate_default = AdViewUnitBannerTemplate;
64
54
 
65
- // src/AdViewUnit/AdViewUnitNative.tsx
66
- import { getAssetByName, getPrepareURL, getSrcSetCSSThumbs } from "@adview/core/utils";
55
+ // src/AdViewUnit/AdViewUnitNativeTemplate.tsx
56
+ import { getAssetByName as getAssetByName2, getPrepareURL as getPrepareURL2, getSrcSetCSSThumbs as getSrcSetCSSThumbs2 } from "@adview/core/utils";
67
57
  import React2 from "react";
68
- function AdViewUnitNative({
69
- assets,
70
- fields,
71
- url,
72
- classNames
73
- }) {
74
- const asset = getAssetByName("main", assets);
58
+ function AdViewUnitNativeTemplate({ classNames, data, state, ...props }) {
59
+ if (!data || !data.assets || !data.assets.length) {
60
+ return null;
61
+ }
62
+ const expectState = props?.isInitial || props?.isLoading || props?.isError || props?.isComplete ? {
63
+ isInitial: props?.isInitial,
64
+ isLoading: props?.isLoading,
65
+ isError: props?.isError,
66
+ isComplete: props?.isComplete
67
+ } : { isComplete: true };
68
+ if (!matchExpectedState(expectState, state)) {
69
+ return null;
70
+ }
71
+ const { assets, url, fields } = data;
72
+ const asset = getAssetByName2("main", assets);
75
73
  if (!url) {
76
74
  return null;
77
75
  }
@@ -80,7 +78,7 @@ function AdViewUnitNative({
80
78
  {
81
79
  target: "_blank",
82
80
  rel: "noopener noreferrer",
83
- href: getPrepareURL(url),
81
+ href: getPrepareURL2(url),
84
82
  className: classNames?.imageLink
85
83
  },
86
84
  asset && /* @__PURE__ */ React2.createElement(
@@ -89,7 +87,7 @@ function AdViewUnitNative({
89
87
  alt: fields?.title,
90
88
  title: fields?.title,
91
89
  src: asset.path,
92
- srcSet: asset.thumbs ? getSrcSetCSSThumbs(asset.thumbs) : void 0,
90
+ srcSet: asset.thumbs ? getSrcSetCSSThumbs2(asset.thumbs) : void 0,
93
91
  className: classNames?.image,
94
92
  style: { objectFit: "cover", width: "100%", height: "100%" }
95
93
  }
@@ -99,7 +97,7 @@ function AdViewUnitNative({
99
97
  {
100
98
  target: "_blank",
101
99
  rel: "noopener noreferrer",
102
- href: getPrepareURL(url),
100
+ href: getPrepareURL2(url),
103
101
  "data-class": "titleLink",
104
102
  className: classNames?.titleLink
105
103
  },
@@ -109,7 +107,7 @@ function AdViewUnitNative({
109
107
  {
110
108
  target: "_blank",
111
109
  rel: "noopener noreferrer",
112
- href: getPrepareURL(url),
110
+ href: getPrepareURL2(url),
113
111
  "data-class": "descriptionLink",
114
112
  className: classNames?.descriptionLink
115
113
  },
@@ -119,7 +117,7 @@ function AdViewUnitNative({
119
117
  {
120
118
  target: "_blank",
121
119
  rel: "noopener noreferrer",
122
- href: getPrepareURL(url),
120
+ href: getPrepareURL2(url),
123
121
  "data-class": "brandNameLink",
124
122
  className: classNames?.brandNameLink
125
123
  },
@@ -129,7 +127,7 @@ function AdViewUnitNative({
129
127
  {
130
128
  target: "_blank",
131
129
  rel: "noopener noreferrer",
132
- href: getPrepareURL(url),
130
+ href: getPrepareURL2(url),
133
131
  "data-class": "phoneLink",
134
132
  className: classNames?.phoneLink
135
133
  },
@@ -139,22 +137,34 @@ function AdViewUnitNative({
139
137
  {
140
138
  target: "_blank",
141
139
  rel: "noopener noreferrer",
142
- href: getPrepareURL(url),
140
+ href: getPrepareURL2(url),
143
141
  "data-class": "urlLink",
144
142
  className: classNames?.urlLink
145
143
  },
146
144
  fields?.url
147
145
  )));
148
146
  }
149
- var AdViewUnitNative_default = AdViewUnitNative;
147
+ AdViewUnitNativeTemplate.defaults = {
148
+ type: "native"
149
+ };
150
+ var AdViewUnitNativeTemplate_default = AdViewUnitNativeTemplate;
150
151
 
151
- // src/AdViewUnit/AdViewUnitProxy.tsx
152
+ // src/AdViewUnit/AdViewUnitProxyTemplate.tsx
152
153
  import React3 from "react";
153
- function AdViewUnitProxy({
154
- // assets,
155
- fields,
156
- url
157
- }) {
154
+ function AdViewUnitProxyTemplate({ className, style, data, state, ...props }) {
155
+ if (!data) {
156
+ return null;
157
+ }
158
+ const expectState = props?.isInitial || props?.isLoading || props?.isError || props?.isComplete ? {
159
+ isInitial: props?.isInitial,
160
+ isLoading: props?.isLoading,
161
+ isError: props?.isError,
162
+ isComplete: props?.isComplete
163
+ } : { isComplete: true };
164
+ if (!matchExpectedState(expectState, state)) {
165
+ return null;
166
+ }
167
+ const { url, fields } = data;
158
168
  const iframeSrc = fields?.url || url;
159
169
  if (!iframeSrc) {
160
170
  return null;
@@ -162,6 +172,7 @@ function AdViewUnitProxy({
162
172
  return /* @__PURE__ */ React3.createElement(
163
173
  "iframe",
164
174
  {
175
+ className,
165
176
  width: "100%",
166
177
  height: "100%",
167
178
  frameBorder: "0",
@@ -170,122 +181,239 @@ function AdViewUnitProxy({
170
181
  allowTransparency: true,
171
182
  scrolling: "no",
172
183
  allowFullScreen: true,
173
- style: { width: "100%", height: "100%" },
184
+ style: { width: "100%", height: "100%", border: "none", ...style },
174
185
  src: iframeSrc
175
186
  }
176
187
  );
177
188
  }
178
- var AdViewUnitProxy_default = AdViewUnitProxy;
189
+ AdViewUnitProxyTemplate.defaults = {
190
+ type: "proxy"
191
+ };
192
+ var AdViewUnitProxyTemplate_default = AdViewUnitProxyTemplate;
179
193
 
180
- // src/AdViewUnit/AdViewUnitBanner.tsx
181
- import { getAssetByName as getAssetByName2, getPrepareURL as getPrepareURL2, getSrcSetCSSThumbs as getSrcSetCSSThumbs2 } from "@adview/core/utils";
194
+ // src/AdViewUnit/AdViewUnitTemplate.tsx
182
195
  import React4 from "react";
183
- function AdViewUnitBanner({ assets, url }) {
184
- const asset = getAssetByName2("main", assets);
185
- return /* @__PURE__ */ React4.createElement("center", null, /* @__PURE__ */ React4.createElement(
186
- "a",
187
- {
188
- target: "_blank",
189
- rel: "noopener noreferrer",
190
- href: getPrepareURL2(url),
191
- className: "banner",
192
- style: { fontSize: 0 }
193
- },
194
- asset && /* @__PURE__ */ React4.createElement(
195
- "img",
196
- {
197
- alt: "main",
198
- src: asset.path,
199
- srcSet: asset.thumbs ? getSrcSetCSSThumbs2(asset.thumbs) : ""
200
- }
201
- )
202
- ));
203
- }
204
- var AdViewUnitBanner_default = AdViewUnitBanner;
205
-
206
- // src/AdViewUnit/AdViewUnitTypeSwitch.tsx
207
- import React5 from "react";
208
- function AdViewUnitTypeSwitch({
209
- data,
210
- classNames,
211
- onDefault
212
- }) {
213
- switch (data?.type) {
214
- case "native":
215
- return /* @__PURE__ */ React5.createElement(AdViewUnitNative_default, { ...data, classNames: classNames?.native });
216
- case "proxy":
217
- return /* @__PURE__ */ React5.createElement(AdViewUnitProxy_default, { ...data, classNames: classNames?.proxy });
218
- case "banner":
219
- return /* @__PURE__ */ React5.createElement(AdViewUnitBanner_default, { ...data, classNames: classNames?.banner });
220
- default:
221
- if (React5.isValidElement(onDefault)) {
222
- return React5.cloneElement(onDefault);
223
- }
224
- if (typeof onDefault === "function") {
225
- return onDefault();
226
- }
196
+ var templateFromListRender = (tmpls, data, wrap) => {
197
+ let renderedRemplates = tmpls.map((tmpl, index) => {
198
+ let tmp = renderTemplate(tmpl, data);
199
+ return !!tmp && React4.isValidElement(tmp) ? /* @__PURE__ */ React4.createElement(React4.Fragment, { key: index }, !!wrap ? wrap(tmp) : tmp) : null;
200
+ }).filter((it) => !!it);
201
+ if (!renderedRemplates.length) {
202
+ renderedRemplates = tmpls.map((tmpl, index) => {
203
+ let tmp = renderTemplate(tmpl, { ...data, type: "default" });
204
+ return !!tmp && React4.isValidElement(tmp) ? /* @__PURE__ */ React4.createElement(React4.Fragment, { key: index }, !!wrap ? wrap(tmp) : tmp) : null;
205
+ }).filter((it) => !!it);
206
+ }
207
+ return /* @__PURE__ */ React4.createElement(React4.Fragment, null, renderedRemplates);
208
+ };
209
+ var renderTemplate = (tmpl, data) => {
210
+ const isFunction = typeof tmpl === "function";
211
+ const isReactElement = React4.isValidElement(tmpl);
212
+ if (isReactElement) {
213
+ let { type, children } = tmpl.props;
214
+ if (!type && typeof tmpl.type !== "string") {
215
+ type = tmpl.type?.defaults?.type;
216
+ }
217
+ if (data.type !== type) {
227
218
  return null;
219
+ }
220
+ if (typeof children === "function") {
221
+ return children(data);
222
+ }
223
+ return React4.cloneElement(tmpl, {
224
+ ...data,
225
+ children
226
+ });
228
227
  }
229
- }
230
- var AdViewUnitTypeSwitch_default = AdViewUnitTypeSwitch;
231
-
232
- // src/AdViewUnit/AdViewUnitWrapper.server.tsx
233
- async function AdViewUnitWrapperServer({
234
- data,
235
- children,
236
- onDefault
237
- }) {
238
- const isChildrenReactNode = React6.isValidElement(children);
239
- const isChildrenFunction = typeof children === "function";
240
- const isLoadingError = data instanceof Error;
241
- if (isChildrenReactNode) {
242
- return React6.cloneElement(children, { data, onDefault });
228
+ if (isFunction) {
229
+ return tmpl(data);
243
230
  }
244
- if (isChildrenFunction) {
245
- return children({ data, onDefault });
231
+ return null;
232
+ };
233
+ var renderTemplates = (tmpls, data, listRender) => {
234
+ return listRender ? listRender(tmpls) : templateFromListRender(tmpls, data);
235
+ };
236
+ var renderAnyTemplates = (tmpls, data, listRender) => {
237
+ if (!tmpls) {
238
+ return null;
246
239
  }
247
- if (!children && !isLoadingError) {
248
- return /* @__PURE__ */ React6.createElement(AdViewUnitTypeSwitch_default, { data, onDefault });
240
+ if (Array.isArray(tmpls)) {
241
+ return renderTemplates(tmpls, data, listRender);
249
242
  }
250
- if (React6.isValidElement(onDefault)) {
251
- return React6.cloneElement(onDefault);
243
+ if (typeof tmpls === "function") {
244
+ return tmpls(data);
252
245
  }
253
- if (typeof onDefault === "function") {
254
- return onDefault();
246
+ if (React4.isValidElement(tmpls)) {
247
+ return templateFromListRender([tmpls], data);
255
248
  }
256
249
  return null;
250
+ };
251
+ var AdViewUnitTemplate = ({ type, state, children, ...props }) => {
252
+ const tmplProps = {
253
+ type,
254
+ state,
255
+ ...props
256
+ };
257
+ const expectState = props?.isInitial || props?.isLoading || props?.isError || props?.isComplete ? {
258
+ isInitial: props?.isInitial,
259
+ isLoading: props?.isLoading,
260
+ isError: props?.isError,
261
+ isComplete: props?.isComplete
262
+ } : { isComplete: true };
263
+ if (!matchExpectedState(expectState, state)) {
264
+ return null;
265
+ }
266
+ if (typeof children === "function") {
267
+ return children(tmplProps);
268
+ }
269
+ if (React4.isValidElement(children)) {
270
+ return React4.cloneElement(children, tmplProps);
271
+ }
272
+ return /* @__PURE__ */ React4.createElement(React4.Fragment, null);
273
+ };
274
+ var AdViewUnitTemplate_default = AdViewUnitTemplate;
275
+
276
+ // src/AdViewUnit/AdViewUnitTracking.tsx
277
+ import { executeImpressionsTracking } from "@adview/core/utils";
278
+ import React5, { useCallback, useEffect, useMemo, useRef } from "react";
279
+ function AdViewUnitTracking({
280
+ impressions,
281
+ views,
282
+ clicks,
283
+ children
284
+ }) {
285
+ const isServer = typeof window === "undefined";
286
+ const wrapperRef = useRef(null);
287
+ const executeImpressionsTrackingHandler = useCallback(
288
+ () => executeImpressionsTracking(impressions),
289
+ [impressions]
290
+ );
291
+ const trackingObserverInstanceHandler = useCallback(
292
+ (entries) => {
293
+ entries.forEach((entry) => {
294
+ if (entry.isIntersecting) {
295
+ executeImpressionsTracking(views);
296
+ trackingObserverInstance?.unobserve(wrapperRef.current);
297
+ }
298
+ });
299
+ },
300
+ [views]
301
+ );
302
+ const trackingObserverInstance = useMemo(() => {
303
+ return !isServer ? new IntersectionObserver(trackingObserverInstanceHandler) : null;
304
+ }, [isServer, trackingObserverInstanceHandler]);
305
+ const clickTrackerHandler = useCallback(() => {
306
+ executeImpressionsTracking(clicks);
307
+ wrapperRef.current?.removeEventListener("click", clickTrackerHandler);
308
+ }, [clicks]);
309
+ useEffect(() => {
310
+ if (wrapperRef.current && trackingObserverInstance) {
311
+ executeImpressionsTrackingHandler();
312
+ wrapperRef.current?.addEventListener("click", clickTrackerHandler);
313
+ trackingObserverInstance?.observe(wrapperRef.current);
314
+ return () => {
315
+ wrapperRef.current?.removeEventListener("click", clickTrackerHandler);
316
+ if (wrapperRef.current)
317
+ trackingObserverInstance?.unobserve(wrapperRef.current);
318
+ };
319
+ }
320
+ return () => {
321
+ };
322
+ }, [
323
+ trackingObserverInstance,
324
+ executeImpressionsTrackingHandler,
325
+ clickTrackerHandler
326
+ ]);
327
+ return /* @__PURE__ */ React5.createElement("div", { ref: wrapperRef }, children);
257
328
  }
258
- var AdViewUnitWrapper_server_default = AdViewUnitWrapperServer;
329
+ var AdViewUnitTracking_default = AdViewUnitTracking;
259
330
 
260
331
  // src/AdViewUnit/AdViewUnit.server.tsx
261
332
  async function AdViewUnitServer({
262
333
  unitId,
263
334
  format,
264
335
  children,
265
- onDefault = () => null,
266
336
  ...config
267
337
  }) {
338
+ const checkFormat = (f) => {
339
+ if (!format) {
340
+ return true;
341
+ }
342
+ return Array.isArray(format) ? format.includes(f) : f === format;
343
+ };
268
344
  const baseConfig = getResolveConfig(config);
269
345
  const requestUrl = getAdRequestUrl(baseConfig, unitId, format);
270
346
  const response = await adViewFetcher(requestUrl);
271
347
  const isLoadingError = response instanceof Error;
272
348
  const error = isLoadingError ? response : void 0;
273
- const responseGroup = response instanceof Error ? null : response?.groups?.[0];
274
- const customTracker = responseGroup?.custom_tracker ?? {};
275
- const groupItems = responseGroup?.items;
349
+ const state = {
350
+ isLoading: true,
351
+ isComplete: true,
352
+ isInitial: false,
353
+ isError: isLoadingError
354
+ };
355
+ const { responseGroup: _, customTracker, groupItems } = error ? { responseGroup: null, customTracker: {}, groupItems: [] } : (() => {
356
+ for (let responseGroup of response?.groups || []) {
357
+ const customTracker2 = responseGroup?.custom_tracker ?? {};
358
+ const groupItems2 = responseGroup?.items.map((it) => checkFormat(it.type) ? it : null).filter(Boolean);
359
+ if (groupItems2 && groupItems2.length > 0) {
360
+ return { responseGroup, customTracker: customTracker2, groupItems: groupItems2 };
361
+ }
362
+ }
363
+ return { responseGroup: null, customTracker: {}, groupItems: [] };
364
+ })();
365
+ if (!children) {
366
+ children = [
367
+ /* @__PURE__ */ React6.createElement(AdViewUnitBannerTemplate_default, null),
368
+ /* @__PURE__ */ React6.createElement(AdViewUnitNativeTemplate_default, null),
369
+ /* @__PURE__ */ React6.createElement(AdViewUnitProxyTemplate_default, null)
370
+ ];
371
+ }
276
372
  if (groupItems && groupItems.length) {
277
373
  return groupItems.map(({ tracker, ...data }) => {
278
- return /* @__PURE__ */ React7.createElement(AdViewUnitTracking_default, { key: data.id, ...tracker }, /* @__PURE__ */ React7.createElement(AdViewUnitWrapper_server_default, { data, onDefault }, children));
374
+ return /* @__PURE__ */ React6.createElement(AdViewUnitTracking_default, { key: data.id, ...tracker }, renderAnyTemplates(children, { data, type: data.type || "default", error, state }));
279
375
  });
280
376
  }
281
- return /* @__PURE__ */ React7.createElement(AdViewUnitTracking_default, { ...customTracker }, /* @__PURE__ */ React7.createElement(AdViewUnitWrapper_server_default, { data: error, onDefault }, children));
377
+ return /* @__PURE__ */ React6.createElement(AdViewUnitTracking_default, { ...customTracker }, renderAnyTemplates(children, { data: null, type: "default", error, state }));
282
378
  }
283
379
  var AdViewUnit_server_default = AdViewUnitServer;
380
+
381
+ // src/AdViewUnit/AdViewUnitDefaultTemplate.tsx
382
+ import React7 from "react";
383
+ function AdViewUnitDefaultTemplate({ type = "default", data, state, children, ...props }) {
384
+ if (data) {
385
+ return null;
386
+ }
387
+ const expectState = props?.isInitial || props?.isLoading || props?.isError || props?.isComplete ? {
388
+ isInitial: props?.isInitial,
389
+ isLoading: props?.isLoading,
390
+ isError: props?.isError,
391
+ isComplete: props?.isComplete
392
+ } : { isComplete: true };
393
+ if (!matchExpectedState(expectState, state)) {
394
+ return null;
395
+ }
396
+ if (typeof children === "function") {
397
+ return /* @__PURE__ */ React7.createElement(React7.Fragment, null, children({ data, state, ...props }));
398
+ }
399
+ return children ? /* @__PURE__ */ React7.createElement(React7.Fragment, null, children) : /* @__PURE__ */ React7.createElement("div", { style: { padding: "1rem", textAlign: "center", color: "#666" } }, /* @__PURE__ */ React7.createElement("p", null, "\u{1F504} Default content for ad unit"), /* @__PURE__ */ React7.createElement("p", null, "Data: ", JSON.stringify(data)), /* @__PURE__ */ React7.createElement("p", null, "State: ", JSON.stringify(state)));
400
+ }
401
+ AdViewUnitDefaultTemplate.defaults = {
402
+ type: "default"
403
+ };
404
+ var AdViewUnitDefaultTemplate_default = AdViewUnitDefaultTemplate;
284
405
  export {
285
- AdViewUnitBanner_default as Banner,
286
- AdViewUnitNative_default as Native,
287
- AdViewUnitProxy_default as Proxy,
288
- AdViewUnit_server_default as Server,
289
- AdViewUnitTypeSwitch_default as TypeSwitch
406
+ AdViewUnitBannerTemplate_default as AdViewUnitBannerTemplate,
407
+ AdViewUnitDefaultTemplate_default as AdViewUnitDefaultTemplate,
408
+ AdViewUnitNativeTemplate_default as AdViewUnitNativeTemplate,
409
+ AdViewUnitProxyTemplate_default as AdViewUnitProxyTemplate,
410
+ AdViewUnit_server_default as AdViewUnitServer,
411
+ AdViewUnitTemplate_default as AdViewUnitTemplate,
412
+ AdViewUnitBannerTemplate_default as BannerTemplate,
413
+ AdViewUnitDefaultTemplate_default as DefaultTemplate,
414
+ AdViewUnitNativeTemplate_default as NativeTemplate,
415
+ AdViewUnitProxyTemplate_default as ProxyTemplate,
416
+ AdViewUnitTemplate_default as Template,
417
+ AdViewUnit_server_default as Unit
290
418
  };
291
419
  //# sourceMappingURL=server.js.map