@visitorquery/react 0.0.9 → 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,17 +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
- type State = {
12
- Started: boolean;
13
- Ended: boolean;
14
- Errored?: boolean;
15
- };
16
- export declare function useVisitorQuery(p: Params): State;
17
- export {};
1
+ export * from "./provider.tsx";
package/dist/index.js CHANGED
@@ -1,5 +1,5 @@
1
- // src/index.ts
2
- import { useState as useState2, 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,8 +49,15 @@ 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
@@ -59,12 +66,17 @@ function useVisitorQuery(p) {
59
66
  Started: false,
60
67
  Ended: false
61
68
  });
69
+ const params = useMemo(() => ({
70
+ ApiKey: apiKey,
71
+ SessionId: sessionId,
72
+ Endpoint: endpoint || defaultEndpoint
73
+ }), [apiKey, sessionId, endpoint, defaultEndpoint]);
62
74
  useEffect2(() => {
63
75
  if (typeof window.VisitorQuery !== "undefined") {
64
76
  window.VisitorQuery.run({
65
- ApiKey: p.ApiKey,
66
- Endpoint: p.Endpoint || defaultEndpoint,
67
- SessionId: p.SessionId,
77
+ ApiKey: params.ApiKey,
78
+ Endpoint: params.Endpoint,
79
+ SessionId: params.SessionId,
68
80
  onOpen: () => {
69
81
  setState({
70
82
  Started: true,
@@ -87,9 +99,24 @@ function useVisitorQuery(p) {
87
99
  }
88
100
  });
89
101
  }
90
- }, [p, clientScript]);
91
- return state;
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;
92
118
  }
93
119
  export {
94
- useVisitorQuery
120
+ useVisitorQuery,
121
+ VisitorQueryProvider
95
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.9",
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
  }