@visitorquery/react 0.0.8 → 0.0.10

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.ts CHANGED
@@ -1,12 +1 @@
1
- declare global {
2
- interface Window {
3
- VisitorQuery: any;
4
- }
5
- }
6
- type Params = {
7
- ApiKey: string;
8
- SessionId: string;
9
- Endpoint?: string;
10
- };
11
- export declare function useVisitorQuery(p: Params): void;
12
- export {};
1
+ export * from "./provider.tsx";
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- // src/index.ts
2
- import { useEffect as useEffect2 } from "react";
1
+ // src/provider.tsx
2
+ import { createContext, useContext, useState as useState2, useEffect as useEffect2, useMemo } from "react";
3
3
 
4
4
  // src/hooks.ts
5
5
  import * as React from "react";
@@ -49,22 +49,74 @@ function useScript(src, options = {}) {
49
49
  return status;
50
50
  }
51
51
 
52
- // src/index.ts
53
- function useVisitorQuery(p) {
52
+ // src/provider.tsx
53
+ import { jsxDEV } from "react/jsx-dev-runtime";
54
+ var VisitorQueryContext = createContext(null);
55
+ function VisitorQueryProvider({
56
+ apiKey,
57
+ sessionId,
58
+ endpoint,
59
+ children
60
+ }) {
54
61
  const defaultEndpoint = "main.check.visitorquery.com";
55
62
  const clientScript = useScript("https://cdn.visitorquery.com/visitorquery.js", {
56
63
  removeOnUnmount: false
57
64
  });
65
+ const [state, setState] = useState2({
66
+ Started: false,
67
+ Ended: false
68
+ });
69
+ const params = useMemo(() => ({
70
+ ApiKey: apiKey,
71
+ SessionId: sessionId,
72
+ Endpoint: endpoint || defaultEndpoint
73
+ }), [apiKey, sessionId, endpoint, defaultEndpoint]);
58
74
  useEffect2(() => {
59
75
  if (typeof window.VisitorQuery !== "undefined") {
60
76
  window.VisitorQuery.run({
61
- ApiKey: p.ApiKey,
62
- Endpoint: p.Endpoint || defaultEndpoint,
63
- SessionId: p.SessionId
77
+ ApiKey: params.ApiKey,
78
+ Endpoint: params.Endpoint,
79
+ SessionId: params.SessionId,
80
+ onOpen: () => {
81
+ setState({
82
+ Started: true,
83
+ Ended: false
84
+ });
85
+ },
86
+ onClose: () => {
87
+ setState({
88
+ Started: true,
89
+ Ended: true
90
+ });
91
+ },
92
+ onError: (err) => {
93
+ setState({
94
+ Started: true,
95
+ Ended: true,
96
+ Errored: true
97
+ });
98
+ console.error(err);
99
+ }
64
100
  });
65
101
  }
66
- }, [p, clientScript]);
102
+ }, [params, clientScript]);
103
+ const contextValue = useMemo(() => ({
104
+ ...state,
105
+ isLoading: clientScript === "loading"
106
+ }), [state, clientScript]);
107
+ return /* @__PURE__ */ jsxDEV(VisitorQueryContext.Provider, {
108
+ value: contextValue,
109
+ children
110
+ }, undefined, false, undefined, this);
111
+ }
112
+ function useVisitorQuery() {
113
+ const context = useContext(VisitorQueryContext);
114
+ if (!context) {
115
+ throw new Error("useVisitorQuery must be used within a VisitorQueryProvider");
116
+ }
117
+ return context;
67
118
  }
68
119
  export {
69
- useVisitorQuery
120
+ useVisitorQuery,
121
+ VisitorQueryProvider
70
122
  };
@@ -0,0 +1,23 @@
1
+ import React from 'react';
2
+ declare global {
3
+ interface Window {
4
+ VisitorQuery: any;
5
+ }
6
+ }
7
+ type VisitorQueryState = {
8
+ Started: boolean;
9
+ Ended: boolean;
10
+ Errored?: boolean;
11
+ };
12
+ type VisitorQueryContextType = VisitorQueryState & {
13
+ isLoading: boolean;
14
+ };
15
+ type VisitorQueryProviderProps = {
16
+ apiKey: string;
17
+ sessionId: string;
18
+ endpoint?: string;
19
+ children: React.ReactNode;
20
+ };
21
+ export declare function VisitorQueryProvider({ apiKey, sessionId, endpoint, children }: VisitorQueryProviderProps): import("react/jsx-runtime").JSX.Element;
22
+ export declare function useVisitorQuery(): VisitorQueryContextType;
23
+ export {};
package/package.json CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
- "version": "0.0.8",
2
+ "version": "0.0.10",
3
3
  "name": "@visitorquery/react",
4
- "module": "src/index.ts",
4
+ "module": "src/index.tsx",
5
5
  "type": "module",
6
6
  "devDependencies": {
7
7
  "@types/bun": "latest"
8
8
  },
9
9
  "access": "public",
10
- "main": "dist/index.js",
10
+ "main": "dist/index.ts",
11
11
  "types": "dist/index.d.ts",
12
12
  "scripts": {
13
13
  "build": "bun build --target=node ./src/index.ts --outfile=dist/index.js --external react --external react-dom && bun run build:declaration",
@@ -39,6 +39,7 @@
39
39
  "license": "MIT",
40
40
  "files": [
41
41
  "dist/*.js",
42
+ "dist/*.jsx",
42
43
  "dist/*.d.ts"
43
44
  ]
44
45
  }