@surfside/ads-react 0.1.0 → 0.1.1

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 (72) hide show
  1. package/README.md +127 -127
  2. package/lib/cjs/components/SurfsideBanner.js +71 -0
  3. package/lib/cjs/components/SurfsideBanner.js.map +1 -0
  4. package/lib/cjs/components/SurfsideCarousel.js +103 -0
  5. package/lib/cjs/components/SurfsideCarousel.js.map +1 -0
  6. package/lib/cjs/components/SurfsideDynamicZone.js +131 -0
  7. package/lib/cjs/components/SurfsideDynamicZone.js.map +1 -0
  8. package/lib/cjs/components/SurfsideProduct.js +3 -0
  9. package/lib/cjs/components/SurfsideProduct.js.map +1 -0
  10. package/lib/cjs/components/SurfsideVideo.js +82 -0
  11. package/lib/cjs/components/SurfsideVideo.js.map +1 -0
  12. package/lib/cjs/components/index.js +23 -0
  13. package/lib/cjs/components/index.js.map +1 -0
  14. package/lib/cjs/hooks/index.js +22 -0
  15. package/lib/cjs/hooks/index.js.map +1 -0
  16. package/lib/cjs/hooks/useSurfsideBanner.js +215 -0
  17. package/lib/cjs/hooks/useSurfsideBanner.js.map +1 -0
  18. package/lib/cjs/hooks/useSurfsideDynamicZone.js +202 -0
  19. package/lib/cjs/hooks/useSurfsideDynamicZone.js.map +1 -0
  20. package/lib/cjs/hooks/useSurfsideProducts.js +366 -0
  21. package/lib/cjs/hooks/useSurfsideProducts.js.map +1 -0
  22. package/lib/cjs/hooks/useSurfsideVideo.js +295 -0
  23. package/lib/cjs/hooks/useSurfsideVideo.js.map +1 -0
  24. package/lib/cjs/index.js +20 -16325
  25. package/lib/cjs/index.js.map +1 -0
  26. package/lib/cjs/provider/SurfsideContextProvider.js +158 -0
  27. package/lib/cjs/provider/SurfsideContextProvider.js.map +1 -0
  28. package/lib/cjs/provider/index.js +19 -0
  29. package/lib/cjs/provider/index.js.map +1 -0
  30. package/lib/cjs/utils/Analytics.js +181 -0
  31. package/lib/cjs/utils/Analytics.js.map +1 -0
  32. package/lib/cjs/utils/index.js +18 -0
  33. package/lib/cjs/utils/index.js.map +1 -0
  34. package/lib/esm/components/SurfsideBanner.js +67 -0
  35. package/lib/esm/components/SurfsideBanner.js.map +1 -0
  36. package/lib/esm/components/SurfsideCarousel.js +99 -0
  37. package/lib/esm/components/SurfsideCarousel.js.map +1 -0
  38. package/lib/esm/components/SurfsideDynamicZone.js +126 -0
  39. package/lib/esm/components/SurfsideDynamicZone.js.map +1 -0
  40. package/lib/esm/components/SurfsideProduct.js +2 -0
  41. package/lib/esm/components/SurfsideProduct.js.map +1 -0
  42. package/lib/esm/components/SurfsideVideo.js +78 -0
  43. package/lib/esm/components/SurfsideVideo.js.map +1 -0
  44. package/lib/esm/components/index.js +7 -0
  45. package/lib/esm/components/index.js.map +1 -0
  46. package/lib/esm/hooks/index.js +6 -0
  47. package/lib/esm/hooks/index.js.map +1 -0
  48. package/lib/esm/hooks/useSurfsideBanner.js +209 -0
  49. package/lib/esm/hooks/useSurfsideBanner.js.map +1 -0
  50. package/lib/esm/hooks/useSurfsideDynamicZone.js +197 -0
  51. package/lib/esm/hooks/useSurfsideDynamicZone.js.map +1 -0
  52. package/lib/esm/hooks/useSurfsideProducts.js +359 -0
  53. package/lib/esm/hooks/useSurfsideProducts.js.map +1 -0
  54. package/lib/esm/hooks/useSurfsideVideo.js +289 -0
  55. package/lib/esm/hooks/useSurfsideVideo.js.map +1 -0
  56. package/lib/esm/index.js +4 -16299
  57. package/lib/esm/index.js.map +1 -0
  58. package/lib/esm/provider/SurfsideContextProvider.js +151 -0
  59. package/lib/esm/provider/SurfsideContextProvider.js.map +1 -0
  60. package/lib/esm/provider/index.js +3 -0
  61. package/lib/esm/provider/index.js.map +1 -0
  62. package/lib/esm/utils/Analytics.js +171 -0
  63. package/lib/esm/utils/Analytics.js.map +1 -0
  64. package/lib/esm/utils/index.js +2 -0
  65. package/lib/esm/utils/index.js.map +1 -0
  66. package/lib/types/components/SurfsideBanner.d.ts +1 -2
  67. package/lib/types/components/SurfsideVideo.d.ts +0 -1
  68. package/lib/types/hooks/useSurfsideBanner.d.ts +1 -1
  69. package/package.json +41 -34
  70. package/lib/types/hooks/useSurfDynamicZone.d.ts +0 -7
  71. package/lib/types/types.d.ts +0 -33
  72. package/lib/types/utils/analytics.d.ts +0 -58
@@ -0,0 +1,126 @@
1
+ 'use client';
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { AsyncPipe, Ctx, Err } from '@surfside/ads-core';
4
+ import { useEffect, useMemo, useRef, useState } from 'react';
5
+ import { useSurfsideContext } from '../provider';
6
+ export const SurfsideDynamicZone = ({ zoneId, carousel, enabled = true, onError }) => {
7
+ const { isReady: apiIsReady, hasError: apiHasError, error: apiError, getDynamicZone, buildDynamicZoneRequest, buildDynamicResponse, renderBanner, renderVideo, renderCarousel, logger } = useSurfsideContext();
8
+ const log = useMemo(() => Ctx(logger)?.with('SurfsideDynamicZone'), [logger]);
9
+ log?.info('Rendering SurfsideDynamicZone component');
10
+ const [error, setError] = useState();
11
+ const [dynamicResponse, setDynamicResponse] = useState();
12
+ const ref = useRef(null);
13
+ useEffect(() => {
14
+ let cancelled = false;
15
+ if (!apiIsReady || !enabled) {
16
+ if (apiHasError) {
17
+ setErrorState(apiError);
18
+ }
19
+ return;
20
+ }
21
+ AsyncPipe(getDynamicZone(zoneId))
22
+ .then(buildDynamicZoneRequest.bind(undefined, zoneId))
23
+ .then(buildDynamicResponse)
24
+ .result()
25
+ .then(dynamicResponse => {
26
+ if (cancelled) {
27
+ return;
28
+ }
29
+ if (!dynamicResponse.ok) {
30
+ setErrorState(dynamicResponse);
31
+ return;
32
+ }
33
+ setSuccessState(dynamicResponse.value);
34
+ });
35
+ return () => void (cancelled = true);
36
+ }, [
37
+ apiIsReady,
38
+ apiError,
39
+ apiHasError,
40
+ getDynamicZone,
41
+ buildDynamicZoneRequest,
42
+ buildDynamicResponse,
43
+ log,
44
+ zoneId,
45
+ enabled
46
+ ]);
47
+ useEffect(() => {
48
+ let cancelled = false;
49
+ if (!apiIsReady || !enabled ||
50
+ dynamicResponse === undefined ||
51
+ ref.current === null) {
52
+ return;
53
+ }
54
+ switch (dynamicResponse.type) {
55
+ case 'banner': {
56
+ const bannerResult = renderBanner(dynamicResponse);
57
+ if (!bannerResult.ok) {
58
+ setErrorState(bannerResult);
59
+ return;
60
+ }
61
+ ref.current.appendChild(bannerResult.value);
62
+ break;
63
+ }
64
+ case 'video': {
65
+ const videoResult = renderVideo(dynamicResponse);
66
+ if (!videoResult.ok) {
67
+ setErrorState(videoResult);
68
+ return;
69
+ }
70
+ ref.current.appendChild(videoResult.value);
71
+ break;
72
+ }
73
+ case 'carousel': {
74
+ renderCarousel(dynamicResponse, carousel.cardMinWidth, carousel.carouselLimits, carousel.nextType).then(carouselResponse => {
75
+ if (cancelled) {
76
+ return;
77
+ }
78
+ if (!carouselResponse.ok) {
79
+ setErrorState(carouselResponse);
80
+ return;
81
+ }
82
+ // Have to check ref again since we're in a promise.then
83
+ if (ref.current === null) {
84
+ setErrorState(Err(new DynamicZoneUnmountedError));
85
+ return;
86
+ }
87
+ ref.current.appendChild(carouselResponse.value.element);
88
+ carouselResponse.value.init()
89
+ .then(initResult => {
90
+ if (!initResult.ok) {
91
+ setErrorState(initResult);
92
+ if (ref.current !== null) {
93
+ ref.current.innerHTML = '';
94
+ }
95
+ }
96
+ });
97
+ });
98
+ }
99
+ }
100
+ return () => {
101
+ cancelled = true;
102
+ if (ref.current !== null) {
103
+ ref.current.innerHTML = '';
104
+ }
105
+ };
106
+ }, [dynamicResponse, renderBanner, renderVideo, renderCarousel, enabled]);
107
+ const setErrorState = (error) => {
108
+ setError(error);
109
+ setDynamicResponse(undefined);
110
+ };
111
+ const setSuccessState = (response) => {
112
+ setError(undefined);
113
+ setDynamicResponse(response);
114
+ };
115
+ if (error !== undefined) {
116
+ log?.error(error.error);
117
+ onError?.(error);
118
+ }
119
+ return _jsx("div", { ref: ref });
120
+ };
121
+ export class DynamicZoneUnmountedError extends Error {
122
+ constructor() {
123
+ super('Dynamic zone container unmounted while processing carousel');
124
+ }
125
+ }
126
+ //# sourceMappingURL=SurfsideDynamicZone.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SurfsideDynamicZone.js","sourceRoot":"src/","sources":["components/SurfsideDynamicZone.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAEb,OAAO,EAAE,SAAS,EAAE,GAAG,EAAE,GAAG,EAAkB,MAAM,oBAAoB,CAAC;AAEzE,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AAUjD,MAAM,CAAC,MAAM,mBAAmB,GAAG,CAAC,EAChC,MAAM,EACN,QAAQ,EACR,OAAO,GAAG,IAAI,EACd,OAAO,EAC0B,EAAE,EAAE;IACrC,MAAM,EACF,OAAO,EAAE,UAAU,EACnB,QAAQ,EAAE,WAAW,EACrB,KAAK,EAAE,QAAQ,EAEf,cAAc,EACd,uBAAuB,EACvB,oBAAoB,EAEpB,YAAY,EACZ,WAAW,EACX,cAAc,EAEd,MAAM,EACT,GAAG,kBAAkB,EAAE,CAAC;IAEzB,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,IAAI,CAAC,qBAAqB,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;IAC9E,GAAG,EAAE,IAAI,CAAC,yCAAyC,CAAC,CAAC;IAErD,MAAM,CAAC,KAAK,EAAE,QAAQ,CAAC,GAAG,QAAQ,EAAa,CAAC;IAChD,MAAM,CAAC,eAAe,EAAE,kBAAkB,CAAC,GAAG,QAAQ,EAAyB,CAAC;IAChF,MAAM,GAAG,GAAG,MAAM,CAAiB,IAAI,CAAC,CAAC;IAEzC,SAAS,CAAC,GAAG,EAAE;QACX,IAAI,SAAS,GAAG,KAAK,CAAC;QACt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AI,EAAE,CAAC;gCACvB,GAAG,CAAC,OAAO,CAAC,SAAS,GAAG,EAAE,CAAC;4BAC/B,CAAC;wBACL,CAAC;oBACL,CAAC,CAAC,CAAC;gBACX,CAAC,CAAC,CAAC;YACP,CAAC;QACL,CAAC;QAED,OAAO,GAAG,EAAE;YACR,SAAS,GAAG,IAAI,CAAC;YACjB,IAAI,GAAG,CAAC,OAAO,KAAK,IAAI,EAAE,CAAC;gBACvB,GAAG,CAAC,OAAO,CAAC,SAAS,GAAG,EAAE,CAAC;YAC/B,CAAC;QACL,CAAC,CAAC;IACN,CAAC,EAAE,CAAC,eAAe,EAAE,YAAY,EAAE,WAAW,EAAE,cAAc,EAAE,OAAO,CAAC,CAAC,CAAC;IAE1E,MAAM,aAAa,GAAG,CAAC,KAAgB,EAAE,EAAE;QACvC,QAAQ,CAAC,KAAK,CAAC,CAAC;QAChB,kBAAkB,CAAC,SAAS,CAAC,CAAC;IAClC,CAAC,CAAC;IAEF,MAAM,eAAe,GAAG,CAAC,QAA+B,EAAE,EAAE;QACxD,QAAQ,CAAC,SAAS,CAAC,CAAC;QACpB,kBAAkB,CAAC,QAAQ,CAAC,CAAC;IACjC,CAAC,CAAC;IAEF,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;QACtB,GAAG,EAAE,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QACxB,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC;IACrB,CAAC;IAED,OAAO,cAAK,GAAG,EAAE,GAAG,GAAQ,CAAC;AACjC,CAAC,CAAC;AAEF,MAAM,OAAO,yBAA0B,SAAQ,KAAK;IAChD;QACI,KAAK,CAAC,4DAA4D,CAAC,CAAC;IACxE,CAAC;CACJ"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=SurfsideProduct.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SurfsideProduct.js","sourceRoot":"src/","sources":["components/SurfsideProduct.tsx"],"names":[],"mappings":""}
@@ -0,0 +1,78 @@
1
+ 'use client';
2
+ import { jsx as _jsx } from "react/jsx-runtime";
3
+ import { useEffect, useMemo, useRef, useState } from 'react';
4
+ import { useSurfsideContext } from '../provider/SurfsideContextProvider';
5
+ import { Ctx, AsyncPipe, WrapValue } from '@surfside/ads-core-client';
6
+ /**
7
+ * SurfsideVideo renders a video ad using the `useSurfsideVideo` hook. It automatically
8
+ * manages video fetching, win tracking, and optional UI fallback states.
9
+ *
10
+ * Customize the video lifecycle using callbacks and optionally inject custom styles
11
+ * or loading/error content.
12
+ *
13
+ * @param {ISurfsideVideoProps} props Props used to configure the video rendering behavior
14
+ * @returns A responsive ad container that manages display and state transitions
15
+ */
16
+ export const SurfsideVideo = ({ zoneId, size, minDuration = 0, maxDuration, mimes, enabled = true, requestBuilder, onError }) => {
17
+ const { isReady: apiIsReady, error: apiError, hasError: apiHasError, startRequest, requestBids, buildVideoResponse, renderVideo, logger } = useSurfsideContext();
18
+ const log = useMemo(() => Ctx(logger)?.with('SurfsideVideo'), [logger]);
19
+ const [error, setError] = useState();
20
+ const ref = useRef(null);
21
+ useEffect(() => {
22
+ log?.info('Requesting video');
23
+ let cancelled = false;
24
+ if (!enabled || !apiIsReady) {
25
+ if (apiHasError) {
26
+ log?.info('Surfside provider failed to initialize');
27
+ return setError(apiError);
28
+ }
29
+ log?.info('Not ready yet');
30
+ return;
31
+ }
32
+ const request = (requestBuilder ?? startRequest('zone'))
33
+ .withVideo(size[0], size[1], minDuration, maxDuration, mimes)
34
+ .build();
35
+ log?.debug('request', request);
36
+ AsyncPipe(requestBids(request))
37
+ .then(response => WrapValue(response.seatbid?.[0]?.bid?.[0]))
38
+ .then(buildVideoResponse)
39
+ .then(renderVideo)
40
+ .result()
41
+ .then(video => {
42
+ log?.debug('Got video response', video);
43
+ if (cancelled) {
44
+ log?.info('Cancelled');
45
+ return;
46
+ }
47
+ if (!video.ok) {
48
+ log?.debug('Got error');
49
+ return setError(video);
50
+ }
51
+ log?.info('Successful response. Appending video player');
52
+ ref.current.appendChild(video.value);
53
+ });
54
+ return () => void (cancelled = true);
55
+ }, [
56
+ apiIsReady,
57
+ apiError,
58
+ apiHasError,
59
+ zoneId,
60
+ size,
61
+ minDuration,
62
+ maxDuration,
63
+ mimes,
64
+ enabled,
65
+ size[0],
66
+ size[1],
67
+ requestBuilder,
68
+ startRequest
69
+ ]);
70
+ useEffect(() => {
71
+ if (error !== undefined) {
72
+ log?.error(error.error);
73
+ onError?.(error);
74
+ }
75
+ }, [error]);
76
+ return _jsx("div", { ref: ref });
77
+ };
78
+ //# sourceMappingURL=SurfsideVideo.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"SurfsideVideo.js","sourceRoot":"src/","sources":["components/SurfsideVideo.tsx"],"names":[],"mappings":"AAAA,YAAY,CAAC;;AAGb,OAAO,EAAE,SAAS,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,OAAO,CAAC;AAC7D,OAAO,EAAE,kBAAkB,EAAE,MAAM,qCAAqC,CAAC;AACzE,OAAO,EACH,GAAG,EAGH,SAAS,EACT,SAAS,EACZ,MAAM,2BAA2B,CAAC;AAqEnC;;;;;;;;;GASG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,CAAC,EAC1B,MAAM,EACN,IAAI,EACJ,WAAW,GAAG,CAAC,EACf,WAAW,EACX,KAAK,EACL,OAAO,GAAG,IAAI,EACd,cAAc,EACd,OAAO,EACW,EAAE,EAAE;IACtB,MAAM,EACF,OAAO,EAAE,UAAU,EACnB,KAAK,EAAE,QAAQ,EACf,QAAQ,EAAE,WAAW,EAErB,YAAY,EACZ,WAAW,EACX,kBAAkB,EAClB,WAAW,EACX,MAAM,EACT,GAAG,kBAAkB,EAAE,CAAC;IAEzB,MAAM,GAAG,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,IAAI,CAAC,eAAe,CAAC,EAAE,CAAC,MAAM,CAAC,CAAC,CAAC;IAExE,MAA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AO;YACX,CAAC;YACD,IAAI,CAAC,KAAK,CAAC,EAAE,EAAE,CAAC;gBACZ,GAAG,EAAE,KAAK,CAAC,WAAW,CAAC,CAAC;gBACxB,OAAO,QAAQ,CAAC,KAAK,CAAC,CAAC;YAC3B,CAAC;YACD,GAAG,EAAE,IAAI,CAAC,6CAA6C,CAAC,CAAC;YACzD,GAAG,CAAC,OAAQ,CAAC,WAAW,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;QAC1C,CAAC,CAAC,CAAC;QAEP,OAAO,GAAG,EAAE,CAAC,KAAI,CAAC,SAAS,GAAG,IAAI,CAAC,CAAC;IACxC,CAAC,EAAE;QACC,UAAU;QACV,QAAQ;QACR,WAAW;QACX,MAAM;QACN,IAAI;QACJ,WAAW;QACX,WAAW;QACX,KAAK;QACL,OAAO;QACP,IAAI,CAAC,CAAC,CAAC;QACP,IAAI,CAAC,CAAC,CAAC;QACP,cAAc;QACd,YAAY;KACf,CAAC,CAAC;IAEH,SAAS,CAAC,GAAG,EAAE;QACX,IAAI,KAAK,KAAK,SAAS,EAAE,CAAC;YACtB,GAAG,EAAE,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC;YACxB,OAAO,EAAE,CAAC,KAAK,CAAC,CAAC;QACrB,CAAC;IACL,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC;IAEZ,OAAO,cAAK,GAAG,EAAE,GAAG,GAAQ,CAAC;AACjC,CAAC,CAAC"}
@@ -0,0 +1,7 @@
1
+ 'use client';
2
+ export * from './SurfsideBanner';
3
+ export * from './SurfsideCarousel';
4
+ export * from './SurfsideDynamicZone';
5
+ export * from './SurfsideProduct';
6
+ export * from './SurfsideVideo';
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"src/","sources":["components/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AACb,cAAc,kBAAkB,CAAC;AACjC,cAAc,oBAAoB,CAAC;AACnC,cAAc,uBAAuB,CAAC;AACtC,cAAc,mBAAmB,CAAC;AAClC,cAAc,iBAAiB,CAAC"}
@@ -0,0 +1,6 @@
1
+ 'use client';
2
+ export * from './useSurfsideBanner';
3
+ export * from './useSurfsideDynamicZone';
4
+ export * from './useSurfsideProducts';
5
+ export * from './useSurfsideVideo';
6
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"src/","sources":["hooks/index.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AACb,cAAc,qBAAqB,CAAC;AACpC,cAAc,0BAA0B,CAAC;AACzC,cAAc,uBAAuB,CAAC;AACtC,cAAc,oBAAoB,CAAC"}
@@ -0,0 +1,209 @@
1
+ 'use client';
2
+ import { useState, useEffect, useCallback, useMemo } from 'react';
3
+ import { useSurfsideContext } from '../provider';
4
+ import { AsyncPipe, Err, WrapValue, Ctx } from '@surfside/ads-core-client';
5
+ /**
6
+ * Requests and returns a banner creative. Accepts a size argument and returns an object wrapping the creative markup
7
+ * (called the `adm`) and some analytics callbacks for tracking auction events, as well as metadata such as the original
8
+ * bid response which creatived the banner.
9
+ *
10
+ * For documentation on the return values and possible states of this hook, see {@link UseSurfsideBannerReturn}.
11
+ *
12
+ * @example
13
+ * To request a banner and display it, pass a desired creative aspect ratio and check the status of the hook, then
14
+ * render when the banner has returned successfully
15
+ *
16
+ * ```tsx
17
+ * const { banner, loading, error, analytics } = useSurfsideBanner({
18
+ * zoneId: 'my-zone',
19
+ * size: [4, 1] // 4x1 banner
20
+ * })
21
+ *
22
+ * // Track win event
23
+ * useEffect(() => analytics?.trackWin(banner), [analytics]);
24
+ *
25
+ * if (loading) {
26
+ * return <div>Loading...</div>
27
+ * }
28
+ *
29
+ * if (error !== undefined) {
30
+ * throw error.error; // error.error is the Javascript "Error" type which can be safely thrown
31
+ * }
32
+ *
33
+ * return <div dangerouslySetInnerHTML={{ __html: banner.adm }}></div>;
34
+ * ```
35
+ *
36
+ * @expandType IUseSurfsideBannerOptions
37
+ * @expandType UseSurfsideBannerReturn
38
+ *
39
+ * @param {IUseSurfsideBannerOptions} params
40
+ */
41
+ export const useSurfsideBanner = ({ zoneId, size, enabled = true }) => {
42
+ const respProps = useMemo(() => ({
43
+ banner: undefined,
44
+ zoneId,
45
+ size,
46
+ enabled
47
+ }), [zoneId, size?.[0], size?.[1], enabled]);
48
+ const internalState = useSurfsideBannerResponse(respProps);
49
+ useEffect(() => {
50
+ internalState.refetch?.();
51
+ }, [internalState.refetch]);
52
+ return internalState;
53
+ };
54
+ export const useSurfsideBannerResponse = (props) => {
55
+ const { startRequest, requestBids, buildBannerResponse, logger, error: apiError, hasError: apiHasError, isReady: apiIsReady } = useSurfsideContext();
56
+ const [internalBanner, setInternalBanner] = useState(props?.banner);
57
+ const [banner, setBanner] = useState();
58
+ const [loading, setLoading] = useState(true);
59
+ const [bannerError, setBannerError] = useState();
60
+ const log = Ctx(logger)?.with('SurfsideBanner');
61
+ log?.info('Fetching Surfside Banner');
62
+ useEffect(() => {
63
+ if (internalBanner?.ok === true) {
64
+ setSuccessState({
65
+ ...internalBanner.value,
66
+ id: internalBanner.value.bid.id
67
+ });
68
+ }
69
+ else if (internalBanner?.ok === false) {
70
+ setErrorState(internalBanner);
71
+ }
72
+ }, [internalBanner]);
73
+ useEffect(() => {
74
+ if (props?.banner === undefined) {
75
+ setLoadingState();
76
+ }
77
+ else {
78
+ setInternalBanner(props?.banner);
79
+ }
80
+ }, [props?.banner]);
81
+ const setErrorState = (err) => {
82
+ log?.error('Setting banner error state', err);
83
+ setBannerError(err);
84
+ setBanner(undefined);
85
+ setLoading(false);
86
+ };
87
+ const setLoadingState = () => {
88
+ log?.info('Setting banner loading state');
89
+ setBannerError(undefined);
90
+ setBanner(undefined);
91
+ setLoading(true);
92
+ };
93
+ const setSuccessState = (banner) => {
94
+ log?.info('Setting banner success state', banner);
95
+ setBannerError(undefined);
96
+ setLoading(false);
97
+ setBanner({ ...banner });
98
+ };
99
+ const refetch = useCallback(async () => {
100
+ log?.info('Running banner refetch');
101
+ if (props === undefined || !apiIsReady || props.enabled === false) {
102
+ if (apiHasError) {
103
+ log?.warn('Banner unable to render do to api error');
104
+ setErrorState(apiError);
105
+ }
106
+ log?.warn('Banner not fetching, either enabled is false or incorrect initialization');
107
+ return;
108
+ }
109
+ setLoadingState();
110
+ const request = startRequest(props.zoneId)
111
+ .withBanner(props.size[0], props.size[1])
112
+ .build();
113
+ const bannerResult = await AsyncPipe(requestBids(request))
114
+ .then(bidResponse => WrapValue(bidResponse.seatbid?.[0]?.bid?.[0]))
115
+ .then(buildBannerResponse)
116
+ .result();
117
+ log?.info('Setting banner result');
118
+ setInternalBanner(bannerResult);
119
+ }, [
120
+ apiIsReady,
121
+ apiError,
122
+ startRequest,
123
+ buildBannerResponse,
124
+ props?.enabled,
125
+ props?.zoneId,
126
+ props?.size?.[0],
127
+ props?.size?.[1]
128
+ ]);
129
+ // memoize this for rerender edge cases
130
+ const analytics = useMemo(() => {
131
+ if (banner === undefined) {
132
+ return undefined;
133
+ }
134
+ return {
135
+ trackWin: trackWin(banner, logger)
136
+ };
137
+ }, [banner?.id]);
138
+ if (!loading && bannerError === undefined && banner !== undefined) { /* Handle Success State */
139
+ // console.log('BannerState: ----- Success!', banner);
140
+ return {
141
+ banner,
142
+ error: bannerError,
143
+ loading,
144
+ isReady: true,
145
+ analytics,
146
+ refetch: refetch
147
+ };
148
+ }
149
+ else if (!loading && bannerError !== undefined && banner === undefined) { /* Handle Error State */
150
+ // console.log('BannerState: error');
151
+ return {
152
+ banner,
153
+ error: bannerError,
154
+ loading: loading,
155
+ isReady: false,
156
+ analytics: undefined,
157
+ refetch: refetch
158
+ };
159
+ }
160
+ else if (loading && bannerError === undefined && banner === undefined) { /* Handle Loading State */
161
+ // console.log('BannerState: loading');
162
+ return {
163
+ banner,
164
+ error: bannerError,
165
+ loading,
166
+ isReady: false,
167
+ analytics: undefined,
168
+ refetch: refetch
169
+ };
170
+ }
171
+ else { /* Handle Invalid State */
172
+ // console.log('BannerState: invalid');
173
+ return {
174
+ banner: undefined,
175
+ error: Err(new InvalidBannerStateError(banner, bannerError, loading)),
176
+ loading: false,
177
+ isReady: false,
178
+ analytics: undefined,
179
+ refetch: refetch,
180
+ };
181
+ }
182
+ };
183
+ export class InvalidBannerStateError extends Error {
184
+ banner;
185
+ error;
186
+ loading;
187
+ constructor(banner, error, loading) {
188
+ super('useSurfsideBanner resulted in an invalid state');
189
+ this.banner = banner;
190
+ this.error = error;
191
+ this.loading = loading;
192
+ }
193
+ }
194
+ ;
195
+ const trackWin = (banner, logger) => () => {
196
+ if (banner === undefined) {
197
+ return;
198
+ }
199
+ const log = Ctx(logger)?.with('trackWin');
200
+ if (banner.bid.nurl !== undefined) {
201
+ const url = new URL(banner.bid.nurl);
202
+ url.searchParams.delete('url');
203
+ fetch(url.toString()).then(() => log?.debug('Won auction', banner));
204
+ }
205
+ else {
206
+ log?.error('Missing nurl on banner bid response! This should never happen', banner);
207
+ }
208
+ };
209
+ //# sourceMappingURL=useSurfsideBanner.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"useSurfsideBanner.js","sourceRoot":"src/","sources":["hooks/useSurfsideBanner.ts"],"names":[],"mappings":"AAAA,YAAY,CAAC;AAEb,OAAO,EAAE,QAAQ,EAAE,SAAS,EAAE,WAAW,EAAE,OAAO,EAAE,MAAM,OAAO,CAAC;AAClE,OAAO,EAAE,kBAAkB,EAAE,MAAM,aAAa,CAAC;AACjD,OAAO,EAKH,SAAS,EACT,GAAG,EACH,SAAS,EACT,GAAG,EACN,MAAM,2BAA2B,CAAC;AAuOnC;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAmCG;AAEH,MAAM,CAAC,MAAM,iBAAiB,GAAG,CAAC,EAC9B,MAAM,EACN,IAAI,EACJ,OAAO,GAAG,IAAI,EACU,EAA2B,EAAE;IAErD,MAAM,SAAS,GAAG,OAAO,CAAC,GAAG,EAAE,CAAC,CAAC;QAC7B,MAA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AAS,EAAE,SAAS;YACpB,OAAO,EAAE,OAAO;SACc,CAAC;IACvC,CAAC;AAEL,CAAC,CAAC;AAEF,MAAM,OAAO,uBAAwB,SAAQ,KAAK;IAE1B;IACA;IACA;IAHpB,YACoB,MAA2B,EAC3B,KAA4B,EAC5B,OAAgB;QAEhC,KAAK,CAAC,gDAAgD,CAAC,CAAC;QAJxC,WAAM,GAAN,MAAM,CAAqB;QAC3B,UAAK,GAAL,KAAK,CAAuB;QAC5B,YAAO,GAAP,OAAO,CAAS;IAGpC,CAAC;CACJ;AAAA,CAAC;AAEF,MAAM,QAAQ,GAAG,CAAC,MAA2B,EAAE,MAA2B,EAAE,EAAE,CAAC,GAAG,EAAE;IAChF,IAAI,MAAM,KAAK,SAAS,EAAE,CAAC;QACvB,OAAO;IACX,CAAC;IACD,MAAM,GAAG,GAAG,GAAG,CAAC,MAAM,CAAC,EAAE,IAAI,CAAC,UAAU,CAAC,CAAC;IAC1C,IAAI,MAAM,CAAC,GAAG,CAAC,IAAI,KAAK,SAAS,EAAE,CAAC;QAChC,MAAM,GAAG,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,IAAI,CAAC,CAAC;QACrC,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,KAAK,CAAC,CAAC;QAC/B,KAAK,CAAC,GAAG,CAAC,QAAQ,EAAE,CAAC,CAAC,IAAI,CAAC,GAAG,EAAE,CAAC,GAAG,EAAE,KAAK,CAAC,aAAa,EAAE,MAAM,CAAC,CAAC,CAAC;IACxE,CAAC;SAAM,CAAC;QACJ,GAAG,EAAE,KAAK,CACN,+DAA+D,EAC/D,MAAM,CACT,CAAC;IACN,CAAC;AACL,CAAC,CAAC"}
@@ -0,0 +1,197 @@
1
+ 'use client';
2
+ import { useState, useEffect, useCallback } from 'react';
3
+ import { useSurfsideContext } from '../provider';
4
+ import { AsyncPipe, Err, Ok, } from '@surfside/ads-core-client';
5
+ import { useSurfsideProductsResponse, useSurfsideBannerResponse } from '../hooks';
6
+ ;
7
+ ;
8
+ ;
9
+ ;
10
+ ;
11
+ ;
12
+ /**
13
+ * @overload
14
+ *
15
+ * Returns a typed version of the `UseSurfsideDynamicZoneReturn` populated with full product data, if product data is
16
+ * available. If you pass `withProductInfo: true` to props (or pass nothing at all), this overload will be used.
17
+ * @expandType UseSurfDynamicZoneOptionsWithProductInfo
18
+ * @param {UseSurfDynamicZoneOptionsWithProductInfo} props
19
+ */ ;
20
+ /**
21
+ * React hook for surfside dynamic zones. A dynamic zone is a creative which determines which type of creative to
22
+ * request based on the dimensions of the screen (i.e, it can request different creatives for desktop vs mobile).
23
+ * Dynamic zones can also define an order of priority for backup creatives. This requires you to have configured a
24
+ * dynamic zone ahead of time within the Surfside platform
25
+ *
26
+ * For example, a dynamic zone might be configured to request A carousel and a 4x1 banner on Desktop, or a 1x4 banner
27
+ * and 1x1 video on Mobile. Once it has selected the screen size, it will send a request for all configured creative
28
+ * types. It will then return the first available creative in order of priority. In our example, the dynamic zone might
29
+ * detect that we are on a Desktop view and then request the carousel and 4x1 banner at the same time. It will then try
30
+ * to render the carousel, if it is available. If it is not, it will render the banner. If neither are available,
31
+ * `useSurfsideDynamicZone` will return an error of type {@link NoBidsError}.
32
+ *
33
+ * @example
34
+ * To handle the output of each creative, check the `type` field after checking the value of `loading` and `error`.
35
+ * It is recommended that you implement renderer components which you can pass creative responses to, rather than trying
36
+ * to render each creative in the same component as your `useSurfsideDynamicZone` call.
37
+ *
38
+ * ```tsx
39
+ * const { data, error, loading, type } = useSurfsideDynamicZone({
40
+ * zoneId: 'my-zone'
41
+ * });
42
+ *
43
+ * if (loading) {
44
+ * return <div>Loading...</div>
45
+ * }
46
+ *
47
+ * if (error !== undefined) {
48
+ * throw error.error; // error.error is a Javascript Error type and can be safely thrown.
49
+ * }
50
+ *
51
+ * switch(type) {
52
+ * case 'banner': return <MyRenderBannerComponent banner={data} />;
53
+ * case 'video': return <MyRenderVideoComponent video={data} />;
54
+ * case 'products': return <MyRenderCarouselComponent products={data} />;
55
+ * }
56
+ * ```
57
+ *
58
+ * This pattern will allow you to re-use the renderer component across dynamic zones or pages where you call the other
59
+ * use hooks (i.e, `useSurfsideBanner`, `useSurfsideProducts`) directly.
60
+ * @param {UseSurfDynamicZoneOptions} props
61
+ */
62
+ export function useSurfsideDynamicZone({ zoneId, pageSize, enabled = true, withProductInfo = true }) {
63
+ const { getDynamicZone, buildDynamicZoneRequest, buildDynamicResponse, isReady: apiIsReady, hasError: apiHasError, error: apiError } = useSurfsideContext();
64
+ const [type, setType] = useState();
65
+ /* Products */
66
+ const [productsProps, setProductsProps] = useState();
67
+ const productsResponseState = useSurfsideProductsResponse(productsProps);
68
+ /* Banner */
69
+ const [bannerProps, setBannerProps] = useState();
70
+ const bannerResponseState = useSurfsideBannerResponse(bannerProps);
71
+ const [loading, setLoading] = useState(true);
72
+ const [error, setError] = useState();
73
+ const fetchDynamicZone = useCallback(async () => {
74
+ if (!enabled || !apiIsReady) {
75
+ if (apiHasError) {
76
+ setErrorState(apiError);
77
+ }
78
+ return;
79
+ }
80
+ setLoadingState();
81
+ // Get dynamic response
82
+ const result = await AsyncPipe(getDynamicZone(zoneId))
83
+ .then(buildDynamicZoneRequest.bind(undefined, zoneId))
84
+ .then(buildDynamicResponse)
85
+ .result();
86
+ /* If no creatives are available, this may return an error that should be handled explicitly */
87
+ if (!result.ok) {
88
+ setErrorState(result);
89
+ return;
90
+ }
91
+ const dynamicZone = result.value;
92
+ if (dynamicZone.type === 'carousel') {
93
+ setProductsSuccessState(Ok(dynamicZone)); // re-box for type purposes
94
+ }
95
+ else if (dynamicZone.type === 'banner') {
96
+ setBannerSuccessState(Ok(dynamicZone));
97
+ }
98
+ }, [enabled, apiError, getDynamicZone, zoneId]);
99
+ // Fetch dynamic zone when dependencies change
100
+ useEffect(() => {
101
+ console.log('Fetching dynamic zone', apiIsReady);
102
+ fetchDynamicZone();
103
+ }, [fetchDynamicZone, apiIsReady, zoneId]);
104
+ const setLoadingState = () => {
105
+ setLoading(true);
106
+ setError(undefined);
107
+ setBannerProps(undefined);
108
+ setProductsProps(undefined);
109
+ setType(undefined);
110
+ };
111
+ const setErrorState = (error) => {
112
+ setLoading(false);
113
+ setError(error);
114
+ };
115
+ const setProductsSuccessState = (products) => {
116
+ setLoading(false);
117
+ setError(undefined);
118
+ setType('products');
119
+ setProductsProps({
120
+ zoneId,
121
+ carousel: products,
122
+ pageSize,
123
+ strategy: products.value.dynamicCarouselRequest.strategy,
124
+ withProductInfo
125
+ });
126
+ };
127
+ const setBannerSuccessState = (banner) => {
128
+ setLoading(false);
129
+ setError(undefined);
130
+ setType('banner');
131
+ const { width, height } = banner.value.dynamicBannerRequest;
132
+ setBannerProps({
133
+ banner,
134
+ zoneId,
135
+ size: [width, height]
136
+ });
137
+ };
138
+ if (error !== undefined) {
139
+ return {
140
+ type: undefined,
141
+ loading: false,
142
+ refetchDynamicZone: fetchDynamicZone,
143
+ data: undefined,
144
+ error
145
+ };
146
+ }
147
+ else if (loading || !enabled) {
148
+ return {
149
+ type: undefined,
150
+ loading: true,
151
+ refetchDynamicZone: fetchDynamicZone,
152
+ data: undefined,
153
+ error
154
+ };
155
+ }
156
+ else if (type === 'banner' &&
157
+ bannerResponseState !== undefined &&
158
+ bannerResponseState.loading === false &&
159
+ bannerResponseState.error === undefined) {
160
+ console.log('====== ', bannerResponseState);
161
+ return {
162
+ type: 'banner',
163
+ loading,
164
+ error,
165
+ data: bannerResponseState,
166
+ refetchDynamicZone: fetchDynamicZone
167
+ };
168
+ }
169
+ else if (type === 'products' && productsResponseState !== undefined) {
170
+ return {
171
+ type: 'products',
172
+ loading,
173
+ error,
174
+ data: productsResponseState,
175
+ refetchDynamicZone: fetchDynamicZone
176
+ };
177
+ }
178
+ else {
179
+ return {
180
+ error: Err(new InvalidDynamicZoneState(zoneId)),
181
+ loading: false,
182
+ data: undefined,
183
+ refetchDynamicZone: fetchDynamicZone,
184
+ type: undefined
185
+ };
186
+ }
187
+ }
188
+ ;
189
+ export class InvalidDynamicZoneState extends Error {
190
+ zoneId;
191
+ constructor(zoneId) {
192
+ super('useSurfsideDynamicZone was left in an invalid state. This is an internal error, please ' +
193
+ 'contact Surfside');
194
+ this.zoneId = zoneId;
195
+ }
196
+ }
197
+ //# sourceMappingURL=useSurfsideDynamicZone.js.map