@kontextso/sdk-react-native 0.0.4 → 0.0.5-rc.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.
package/dist/index.d.mts CHANGED
@@ -140,8 +140,10 @@ interface AdsProviderProps {
140
140
  logLevel?: any;
141
141
  conversationId?: string;
142
142
  styles?: AdStyles;
143
+ throwError?: boolean;
143
144
  }
144
145
  declare const VISITOR_ID_KEY = "brain-visitor-id";
146
+ declare const AdsProviderWithoutBoundary: ({ children, messages, publisherToken, isLoading, adserverUrl, isDisabled, character, conversationId, userId, logLevel, onAdView, onAdClick, ...props }: AdsProviderProps) => react_jsx_runtime.JSX.Element;
145
147
  declare const AdsProvider: ({ children, messages, publisherToken, isLoading, adserverUrl, isDisabled, character, conversationId, userId, logLevel, onAdView, onAdClick, ...props }: AdsProviderProps) => react_jsx_runtime.JSX.Element;
146
148
 
147
- export { type AdStyles, AdsContext, AdsProvider, type AdsProviderProps, type ContextType, InlineAd, VISITOR_ID_KEY, useAd };
149
+ export { type AdStyles, AdsContext, AdsProvider, type AdsProviderProps, AdsProviderWithoutBoundary, type ContextType, InlineAd, VISITOR_ID_KEY, useAd };
package/dist/index.d.ts CHANGED
@@ -140,8 +140,10 @@ interface AdsProviderProps {
140
140
  logLevel?: any;
141
141
  conversationId?: string;
142
142
  styles?: AdStyles;
143
+ throwError?: boolean;
143
144
  }
144
145
  declare const VISITOR_ID_KEY = "brain-visitor-id";
146
+ declare const AdsProviderWithoutBoundary: ({ children, messages, publisherToken, isLoading, adserverUrl, isDisabled, character, conversationId, userId, logLevel, onAdView, onAdClick, ...props }: AdsProviderProps) => react_jsx_runtime.JSX.Element;
145
147
  declare const AdsProvider: ({ children, messages, publisherToken, isLoading, adserverUrl, isDisabled, character, conversationId, userId, logLevel, onAdView, onAdClick, ...props }: AdsProviderProps) => react_jsx_runtime.JSX.Element;
146
148
 
147
- export { type AdStyles, AdsContext, AdsProvider, type AdsProviderProps, type ContextType, InlineAd, VISITOR_ID_KEY, useAd };
149
+ export { type AdStyles, AdsContext, AdsProvider, type AdsProviderProps, AdsProviderWithoutBoundary, type ContextType, InlineAd, VISITOR_ID_KEY, useAd };
package/dist/index.js CHANGED
@@ -32,6 +32,7 @@ var src_exports = {};
32
32
  __export(src_exports, {
33
33
  AdsContext: () => AdsContext,
34
34
  AdsProvider: () => AdsProvider,
35
+ AdsProviderWithoutBoundary: () => AdsProviderWithoutBoundary,
35
36
  InlineAd: () => InlineAd_default,
36
37
  VISITOR_ID_KEY: () => VISITOR_ID_KEY,
37
38
  useAd: () => useAd
@@ -39,14 +40,14 @@ __export(src_exports, {
39
40
  module.exports = __toCommonJS(src_exports);
40
41
 
41
42
  // src/formats/InlineAd.tsx
42
- var import_react11 = require("react");
43
+ var import_react12 = require("react");
43
44
  var import_react_native5 = require("react-native");
44
45
 
45
46
  // src/hooks/useAdViewed.tsx
46
- var import_react5 = require("react");
47
+ var import_react6 = require("react");
47
48
 
48
49
  // src/context/AdsProvider.tsx
49
- var import_react4 = __toESM(require("react"));
50
+ var import_react5 = __toESM(require("react"));
50
51
 
51
52
  // src/hooks/useInitializeAds.tsx
52
53
  var import_react = require("react");
@@ -201,7 +202,7 @@ var parseMessageText = (text) => {
201
202
  var import_loglevel = __toESM(require("loglevel"));
202
203
 
203
204
  // package.json
204
- var version = "0.0.4";
205
+ var version = "0.0.5-rc.0";
205
206
 
206
207
  // src/hooks/useInitializeAds.tsx
207
208
  async function initialize(adServerUrl, publisherToken, userId, conversationId, legacyVisitorId, character) {
@@ -747,11 +748,83 @@ function useStreamAds({
747
748
 
748
749
  // src/context/AdsProvider.tsx
749
750
  var import_loglevel4 = __toESM(require("loglevel"));
751
+
752
+ // src/components/ErrorBoundary.tsx
753
+ var import_react4 = __toESM(require("react"));
754
+ var captureErrorFn = (adServerUrl, error, componentStack, context) => {
755
+ fetch(`${adServerUrl}/error`, {
756
+ method: "POST",
757
+ body: JSON.stringify({
758
+ error: error.message,
759
+ stack: componentStack,
760
+ context
761
+ })
762
+ }).catch((e) => {
763
+ console.error(e);
764
+ }).finally(() => {
765
+ console.error(error, componentStack);
766
+ });
767
+ };
768
+ var ErrorBoundary = class extends import_react4.default.Component {
769
+ static contextType = AdsContext;
770
+ constructor(props) {
771
+ super(props);
772
+ this.state = {
773
+ hasError: false
774
+ };
775
+ }
776
+ static getDerivedStateFromError() {
777
+ return {
778
+ hasError: true
779
+ };
780
+ }
781
+ componentDidCatch(error, info) {
782
+ const contextValue = this.context;
783
+ const adServerUrl = this.props.adserverUrl;
784
+ const props = [
785
+ "adserverUrl",
786
+ "publisherToken",
787
+ "visitorId",
788
+ "sessionId",
789
+ "ads",
790
+ "enabledPlacements",
791
+ "isInitialised",
792
+ "isLoading",
793
+ "messages"
794
+ ];
795
+ let contextProps = {};
796
+ if (contextValue) {
797
+ contextProps = props.reduce(
798
+ (acc, prop) => {
799
+ acc[prop] = contextValue[prop];
800
+ return acc;
801
+ },
802
+ {}
803
+ );
804
+ } else {
805
+ contextProps = {
806
+ ...this.props.reqBodyParams,
807
+ adServerUrl
808
+ };
809
+ }
810
+ if (adServerUrl) {
811
+ captureErrorFn(adServerUrl, error, info.componentStack, contextProps);
812
+ }
813
+ }
814
+ render() {
815
+ if (this.state.hasError) {
816
+ return this.props.fallback || null;
817
+ }
818
+ return this.props.children;
819
+ }
820
+ };
821
+
822
+ // src/context/AdsProvider.tsx
750
823
  var import_jsx_runtime = require("react/jsx-runtime");
751
- var AdsContext = import_react4.default.createContext(null);
824
+ var AdsContext = import_react5.default.createContext(null);
752
825
  AdsContext.displayName = "AdsContext";
753
826
  var VISITOR_ID_KEY = "brain-visitor-id";
754
- var AdsProvider = ({
827
+ var AdsProviderWithoutBoundary = ({
755
828
  children,
756
829
  messages,
757
830
  publisherToken,
@@ -766,7 +839,7 @@ var AdsProvider = ({
766
839
  onAdClick,
767
840
  ...props
768
841
  }) => {
769
- const [viewedAds, setViewedAds] = (0, import_react4.useState)([]);
842
+ const [viewedAds, setViewedAds] = (0, import_react5.useState)([]);
770
843
  const adServerUrlOrDefault = adserverUrl || "https://server.megabrain.co";
771
844
  import_loglevel4.default.setLevel(logLevel || "ERROR");
772
845
  const {
@@ -812,6 +885,9 @@ var AdsProvider = ({
812
885
  publisherToken
813
886
  });
814
887
  const ads = mergeAds({ initAds, preloadAds, streamAds, viewedAds });
888
+ if (props.throwError && Math.random() < 0.5) {
889
+ throw new Error("Test error");
890
+ }
815
891
  const markAdAsViewed = (ad) => {
816
892
  import_loglevel4.default.debug("[Brain] Calling onAdView");
817
893
  onAdView && onAdView({
@@ -858,13 +934,63 @@ var AdsProvider = ({
858
934
  }
859
935
  );
860
936
  };
937
+ var AdsProvider = ({
938
+ children,
939
+ messages,
940
+ publisherToken,
941
+ isLoading,
942
+ adserverUrl,
943
+ isDisabled,
944
+ character,
945
+ conversationId,
946
+ userId,
947
+ logLevel,
948
+ onAdView,
949
+ onAdClick,
950
+ ...props
951
+ }) => {
952
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
953
+ ErrorBoundary,
954
+ {
955
+ fallback: children,
956
+ adserverUrl,
957
+ reqBodyParams: {
958
+ publisherToken,
959
+ adserverUrl,
960
+ conversationId,
961
+ character,
962
+ userId,
963
+ isLoading,
964
+ messages
965
+ },
966
+ children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
967
+ AdsProviderWithoutBoundary,
968
+ {
969
+ children,
970
+ messages,
971
+ publisherToken,
972
+ isLoading,
973
+ adserverUrl,
974
+ isDisabled,
975
+ character,
976
+ conversationId,
977
+ userId,
978
+ logLevel,
979
+ onAdView,
980
+ onAdClick,
981
+ throwError: props.throwError
982
+ }
983
+ )
984
+ }
985
+ );
986
+ };
861
987
 
862
988
  // src/hooks/useAdViewed.tsx
863
- var import_react6 = require("react");
989
+ var import_react7 = require("react");
864
990
  var import_loglevel5 = __toESM(require("loglevel"));
865
991
  function useAdViewed(ad) {
866
- const context = (0, import_react6.useContext)(AdsContext);
867
- const [stillMounted, setStillMounted] = (0, import_react5.useState)(false);
992
+ const context = (0, import_react7.useContext)(AdsContext);
993
+ const [stillMounted, setStillMounted] = (0, import_react6.useState)(false);
868
994
  const sendRequest = async () => {
869
995
  if (!context?.adserverUrl || !ad?.id) {
870
996
  return;
@@ -886,7 +1012,7 @@ function useAdViewed(ad) {
886
1012
  import_loglevel5.default.error("[BRAIN] Error sending view request", e);
887
1013
  }
888
1014
  };
889
- (0, import_react5.useEffect)(() => {
1015
+ (0, import_react6.useEffect)(() => {
890
1016
  if (!ad || ad.isError || ad.isLoading || ad.isStreaming || ad.viewed || stillMounted) {
891
1017
  return;
892
1018
  }
@@ -896,7 +1022,7 @@ function useAdViewed(ad) {
896
1022
  setStillMounted(true);
897
1023
  }, 1e3);
898
1024
  }, [ad]);
899
- (0, import_react5.useEffect)(() => {
1025
+ (0, import_react6.useEffect)(() => {
900
1026
  if (stillMounted) {
901
1027
  import_loglevel5.default.log("[BRAIN] sending request");
902
1028
  sendRequest();
@@ -909,14 +1035,14 @@ var import_loglevel8 = __toESM(require("loglevel"));
909
1035
 
910
1036
  // src/components/MarkdownText.tsx
911
1037
  var import_loglevel6 = __toESM(require("loglevel"));
912
- var import_react7 = require("react");
1038
+ var import_react8 = require("react");
913
1039
  var import_react_native2 = require("react-native");
914
1040
  var import_jsx_runtime2 = require("react/jsx-runtime");
915
1041
  function MarkdownText({
916
1042
  content,
917
1043
  onLinkClick
918
1044
  }) {
919
- const context = (0, import_react7.useContext)(AdsContext);
1045
+ const context = (0, import_react8.useContext)(AdsContext);
920
1046
  const textParts = parseMessageText(content);
921
1047
  const styles = context?.styles?.markdownText;
922
1048
  const linkClickHandler = (href) => {
@@ -950,17 +1076,17 @@ function MarkdownText({
950
1076
  // src/components/VideoPlayer.tsx
951
1077
  var import_expo_av = require("expo-av");
952
1078
  var import_loglevel7 = __toESM(require("loglevel"));
953
- var import_react9 = require("react");
1079
+ var import_react10 = require("react");
954
1080
  var import_react_native4 = require("react-native");
955
1081
 
956
1082
  // src/components/VideoProgressBar.tsx
957
- var import_react8 = require("react");
1083
+ var import_react9 = require("react");
958
1084
  var import_react_native3 = require("react-native");
959
1085
  var import_jsx_runtime3 = require("react/jsx-runtime");
960
1086
  function VideoProgressBar({ videoRef }) {
961
- const [progress, setProgress] = (0, import_react8.useState)(0);
962
- const styles = (0, import_react8.useContext)(AdsContext)?.styles?.videoPlayer?.videoProgress;
963
- (0, import_react8.useEffect)(() => {
1087
+ const [progress, setProgress] = (0, import_react9.useState)(0);
1088
+ const styles = (0, import_react9.useContext)(AdsContext)?.styles?.videoPlayer?.videoProgress;
1089
+ (0, import_react9.useEffect)(() => {
964
1090
  const interval = setInterval(() => {
965
1091
  if (!videoRef.current) {
966
1092
  return;
@@ -993,14 +1119,14 @@ var VideoPlayer = ({
993
1119
  mediaPlacement,
994
1120
  onLinkClick
995
1121
  }) => {
996
- const videoRef = (0, import_react9.useRef)(null);
997
- const [isPlaying, setIsPlaying] = (0, import_react9.useState)(false);
998
- const [isPaused, setIsPaused] = (0, import_react9.useState)(true);
999
- const [isEnded, setIsEnded] = (0, import_react9.useState)(false);
1000
- const [showDownloadBadge, setShowDownloadBadge] = (0, import_react9.useState)(false);
1001
- const lastCallbackTimeRef = (0, import_react9.useRef)(0);
1002
- const styles = (0, import_react9.useContext)(AdsContext)?.styles?.videoPlayer;
1003
- (0, import_react9.useEffect)(() => {
1122
+ const videoRef = (0, import_react10.useRef)(null);
1123
+ const [isPlaying, setIsPlaying] = (0, import_react10.useState)(false);
1124
+ const [isPaused, setIsPaused] = (0, import_react10.useState)(true);
1125
+ const [isEnded, setIsEnded] = (0, import_react10.useState)(false);
1126
+ const [showDownloadBadge, setShowDownloadBadge] = (0, import_react10.useState)(false);
1127
+ const lastCallbackTimeRef = (0, import_react10.useRef)(0);
1128
+ const styles = (0, import_react10.useContext)(AdsContext)?.styles?.videoPlayer;
1129
+ (0, import_react10.useEffect)(() => {
1004
1130
  const interval = setInterval(() => {
1005
1131
  if (videoRef.current) {
1006
1132
  videoRef.current.getStatusAsync().then((status) => {
@@ -1083,9 +1209,9 @@ var VideoPlayer = ({
1083
1209
  };
1084
1210
 
1085
1211
  // src/hooks/useAd.tsx
1086
- var import_react10 = require("react");
1212
+ var import_react11 = require("react");
1087
1213
  function useAd({ code, messageId }) {
1088
- const context = (0, import_react10.useContext)(AdsContext);
1214
+ const context = (0, import_react11.useContext)(AdsContext);
1089
1215
  if (!context) {
1090
1216
  return null;
1091
1217
  }
@@ -1133,11 +1259,11 @@ function useAd({ code, messageId }) {
1133
1259
  var import_jsx_runtime5 = require("react/jsx-runtime");
1134
1260
  var InlineAd = ({ code, messageId, wrapper }) => {
1135
1261
  const ad = useAd({ code, messageId });
1136
- const [linkIncluded, setLinkIncluded] = (0, import_react11.useState)(false);
1137
- const context = (0, import_react11.useContext)(AdsContext);
1262
+ const [linkIncluded, setLinkIncluded] = (0, import_react12.useState)(false);
1263
+ const context = (0, import_react12.useContext)(AdsContext);
1138
1264
  const styles = context?.styles?.inlineAd;
1139
1265
  useAdViewed(ad);
1140
- (0, import_react11.useEffect)(() => {
1266
+ (0, import_react12.useEffect)(() => {
1141
1267
  if (ad?.content && !ad.isStreaming && !ad.content.match(/\[.*?\]\(.*?\)/) && !linkIncluded) {
1142
1268
  setLinkIncluded(true);
1143
1269
  }
@@ -1223,6 +1349,7 @@ var InlineAd_default = InlineAd;
1223
1349
  0 && (module.exports = {
1224
1350
  AdsContext,
1225
1351
  AdsProvider,
1352
+ AdsProviderWithoutBoundary,
1226
1353
  InlineAd,
1227
1354
  VISITOR_ID_KEY,
1228
1355
  useAd
package/dist/index.mjs CHANGED
@@ -20,7 +20,7 @@ import {
20
20
  import { useEffect as useEffect5, useState as useState5 } from "react";
21
21
 
22
22
  // src/context/AdsProvider.tsx
23
- import React, { useState as useState4 } from "react";
23
+ import React2, { useState as useState4 } from "react";
24
24
 
25
25
  // src/hooks/useInitializeAds.tsx
26
26
  import { useEffect, useState } from "react";
@@ -175,7 +175,7 @@ var parseMessageText = (text) => {
175
175
  import log from "loglevel";
176
176
 
177
177
  // package.json
178
- var version = "0.0.4";
178
+ var version = "0.0.5-rc.0";
179
179
 
180
180
  // src/hooks/useInitializeAds.tsx
181
181
  async function initialize(adServerUrl, publisherToken, userId, conversationId, legacyVisitorId, character) {
@@ -721,11 +721,83 @@ function useStreamAds({
721
721
 
722
722
  // src/context/AdsProvider.tsx
723
723
  import log4 from "loglevel";
724
+
725
+ // src/components/ErrorBoundary.tsx
726
+ import React from "react";
727
+ var captureErrorFn = (adServerUrl, error, componentStack, context) => {
728
+ fetch(`${adServerUrl}/error`, {
729
+ method: "POST",
730
+ body: JSON.stringify({
731
+ error: error.message,
732
+ stack: componentStack,
733
+ context
734
+ })
735
+ }).catch((e) => {
736
+ console.error(e);
737
+ }).finally(() => {
738
+ console.error(error, componentStack);
739
+ });
740
+ };
741
+ var ErrorBoundary = class extends React.Component {
742
+ static contextType = AdsContext;
743
+ constructor(props) {
744
+ super(props);
745
+ this.state = {
746
+ hasError: false
747
+ };
748
+ }
749
+ static getDerivedStateFromError() {
750
+ return {
751
+ hasError: true
752
+ };
753
+ }
754
+ componentDidCatch(error, info) {
755
+ const contextValue = this.context;
756
+ const adServerUrl = this.props.adserverUrl;
757
+ const props = [
758
+ "adserverUrl",
759
+ "publisherToken",
760
+ "visitorId",
761
+ "sessionId",
762
+ "ads",
763
+ "enabledPlacements",
764
+ "isInitialised",
765
+ "isLoading",
766
+ "messages"
767
+ ];
768
+ let contextProps = {};
769
+ if (contextValue) {
770
+ contextProps = props.reduce(
771
+ (acc, prop) => {
772
+ acc[prop] = contextValue[prop];
773
+ return acc;
774
+ },
775
+ {}
776
+ );
777
+ } else {
778
+ contextProps = {
779
+ ...this.props.reqBodyParams,
780
+ adServerUrl
781
+ };
782
+ }
783
+ if (adServerUrl) {
784
+ captureErrorFn(adServerUrl, error, info.componentStack, contextProps);
785
+ }
786
+ }
787
+ render() {
788
+ if (this.state.hasError) {
789
+ return this.props.fallback || null;
790
+ }
791
+ return this.props.children;
792
+ }
793
+ };
794
+
795
+ // src/context/AdsProvider.tsx
724
796
  import { jsx } from "react/jsx-runtime";
725
- var AdsContext = React.createContext(null);
797
+ var AdsContext = React2.createContext(null);
726
798
  AdsContext.displayName = "AdsContext";
727
799
  var VISITOR_ID_KEY = "brain-visitor-id";
728
- var AdsProvider = ({
800
+ var AdsProviderWithoutBoundary = ({
729
801
  children,
730
802
  messages,
731
803
  publisherToken,
@@ -786,6 +858,9 @@ var AdsProvider = ({
786
858
  publisherToken
787
859
  });
788
860
  const ads = mergeAds({ initAds, preloadAds, streamAds, viewedAds });
861
+ if (props.throwError && Math.random() < 0.5) {
862
+ throw new Error("Test error");
863
+ }
789
864
  const markAdAsViewed = (ad) => {
790
865
  log4.debug("[Brain] Calling onAdView");
791
866
  onAdView && onAdView({
@@ -832,6 +907,56 @@ var AdsProvider = ({
832
907
  }
833
908
  );
834
909
  };
910
+ var AdsProvider = ({
911
+ children,
912
+ messages,
913
+ publisherToken,
914
+ isLoading,
915
+ adserverUrl,
916
+ isDisabled,
917
+ character,
918
+ conversationId,
919
+ userId,
920
+ logLevel,
921
+ onAdView,
922
+ onAdClick,
923
+ ...props
924
+ }) => {
925
+ return /* @__PURE__ */ jsx(
926
+ ErrorBoundary,
927
+ {
928
+ fallback: children,
929
+ adserverUrl,
930
+ reqBodyParams: {
931
+ publisherToken,
932
+ adserverUrl,
933
+ conversationId,
934
+ character,
935
+ userId,
936
+ isLoading,
937
+ messages
938
+ },
939
+ children: /* @__PURE__ */ jsx(
940
+ AdsProviderWithoutBoundary,
941
+ {
942
+ children,
943
+ messages,
944
+ publisherToken,
945
+ isLoading,
946
+ adserverUrl,
947
+ isDisabled,
948
+ character,
949
+ conversationId,
950
+ userId,
951
+ logLevel,
952
+ onAdView,
953
+ onAdClick,
954
+ throwError: props.throwError
955
+ }
956
+ )
957
+ }
958
+ );
959
+ };
835
960
 
836
961
  // src/hooks/useAdViewed.tsx
837
962
  import { useContext } from "react";
@@ -1199,6 +1324,7 @@ var InlineAd_default = InlineAd;
1199
1324
  export {
1200
1325
  AdsContext,
1201
1326
  AdsProvider,
1327
+ AdsProviderWithoutBoundary,
1202
1328
  InlineAd_default as InlineAd,
1203
1329
  VISITOR_ID_KEY,
1204
1330
  useAd
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kontextso/sdk-react-native",
3
- "version": "0.0.4",
3
+ "version": "0.0.5-rc.1",
4
4
  "main": "./dist/index.js",
5
5
  "module": "./dist/index.mjs",
6
6
  "types": "./dist/index.d.ts",