@visitorquery/react 0.0.7 → 0.0.9
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 +6 -1
- package/dist/index.js +89 -20
- package/package.json +3 -2
package/dist/index.d.ts
CHANGED
|
@@ -8,5 +8,10 @@ type Params = {
|
|
|
8
8
|
SessionId: string;
|
|
9
9
|
Endpoint?: string;
|
|
10
10
|
};
|
|
11
|
-
|
|
11
|
+
type State = {
|
|
12
|
+
Started: boolean;
|
|
13
|
+
Ended: boolean;
|
|
14
|
+
Errored?: boolean;
|
|
15
|
+
};
|
|
16
|
+
export declare function useVisitorQuery(p: Params): State;
|
|
12
17
|
export {};
|
package/dist/index.js
CHANGED
|
@@ -1,25 +1,94 @@
|
|
|
1
1
|
// src/index.ts
|
|
2
|
-
import { useState, useEffect } from "react";
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
import { useState as useState2, useEffect as useEffect2 } from "react";
|
|
3
|
+
|
|
4
|
+
// src/hooks.ts
|
|
5
|
+
import * as React from "react";
|
|
6
|
+
function useScript(src, options = {}) {
|
|
7
|
+
const [status, setStatus] = React.useState("loading");
|
|
8
|
+
const optionsRef = React.useRef(options);
|
|
9
|
+
React.useEffect(() => {
|
|
10
|
+
let script = document.querySelector(`script[src="${src}"]`);
|
|
11
|
+
const domStatus = script?.getAttribute("data-status");
|
|
12
|
+
if (domStatus) {
|
|
13
|
+
setStatus(domStatus);
|
|
14
|
+
return;
|
|
15
|
+
}
|
|
16
|
+
if (script === null) {
|
|
17
|
+
script = document.createElement("script");
|
|
18
|
+
script.src = src;
|
|
19
|
+
script.async = true;
|
|
20
|
+
script.setAttribute("data-status", "loading");
|
|
21
|
+
document.body.appendChild(script);
|
|
22
|
+
const handleScriptLoad = () => {
|
|
23
|
+
script?.setAttribute("data-status", "ready");
|
|
24
|
+
setStatus("ready");
|
|
25
|
+
removeEventListeners();
|
|
26
|
+
};
|
|
27
|
+
const handleScriptError = () => {
|
|
28
|
+
script?.setAttribute("data-status", "error");
|
|
29
|
+
setStatus("error");
|
|
30
|
+
removeEventListeners();
|
|
31
|
+
};
|
|
32
|
+
const removeEventListeners = () => {
|
|
33
|
+
script?.removeEventListener("load", handleScriptLoad);
|
|
34
|
+
script?.removeEventListener("error", handleScriptError);
|
|
35
|
+
};
|
|
36
|
+
script.addEventListener("load", handleScriptLoad);
|
|
37
|
+
script.addEventListener("error", handleScriptError);
|
|
38
|
+
const removeOnUnmount = optionsRef.current.removeOnUnmount;
|
|
39
|
+
return () => {
|
|
40
|
+
if (removeOnUnmount === true) {
|
|
41
|
+
script?.remove();
|
|
42
|
+
removeEventListeners();
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
} else {
|
|
46
|
+
setStatus("unknown");
|
|
47
|
+
}
|
|
48
|
+
}, [src]);
|
|
49
|
+
return status;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// src/index.ts
|
|
53
|
+
function useVisitorQuery(p) {
|
|
54
|
+
const defaultEndpoint = "main.check.visitorquery.com";
|
|
55
|
+
const clientScript = useScript("https://cdn.visitorquery.com/visitorquery.js", {
|
|
56
|
+
removeOnUnmount: false
|
|
57
|
+
});
|
|
58
|
+
const [state, setState] = useState2({
|
|
59
|
+
Started: false,
|
|
60
|
+
Ended: false
|
|
7
61
|
});
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
62
|
+
useEffect2(() => {
|
|
63
|
+
if (typeof window.VisitorQuery !== "undefined") {
|
|
64
|
+
window.VisitorQuery.run({
|
|
65
|
+
ApiKey: p.ApiKey,
|
|
66
|
+
Endpoint: p.Endpoint || defaultEndpoint,
|
|
67
|
+
SessionId: p.SessionId,
|
|
68
|
+
onOpen: () => {
|
|
69
|
+
setState({
|
|
70
|
+
Started: true,
|
|
71
|
+
Ended: false
|
|
72
|
+
});
|
|
73
|
+
},
|
|
74
|
+
onClose: () => {
|
|
75
|
+
setState({
|
|
76
|
+
Started: true,
|
|
77
|
+
Ended: true
|
|
78
|
+
});
|
|
79
|
+
},
|
|
80
|
+
onError: (err) => {
|
|
81
|
+
setState({
|
|
82
|
+
Started: true,
|
|
83
|
+
Ended: true,
|
|
84
|
+
Errored: true
|
|
85
|
+
});
|
|
86
|
+
console.error(err);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
}
|
|
90
|
+
}, [p, clientScript]);
|
|
91
|
+
return state;
|
|
23
92
|
}
|
|
24
93
|
export {
|
|
25
94
|
useVisitorQuery
|
package/package.json
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
{
|
|
2
|
-
"version": "0.0.
|
|
2
|
+
"version": "0.0.9",
|
|
3
3
|
"name": "@visitorquery/react",
|
|
4
4
|
"module": "src/index.ts",
|
|
5
5
|
"type": "module",
|
|
@@ -13,7 +13,8 @@
|
|
|
13
13
|
"build": "bun build --target=node ./src/index.ts --outfile=dist/index.js --external react --external react-dom && bun run build:declaration",
|
|
14
14
|
"build:declaration": "tsc --emitDeclarationOnly --project tsconfig.types.json",
|
|
15
15
|
"postbuild": "rimraf tsconfig.types.tsbuildinfo",
|
|
16
|
-
"
|
|
16
|
+
"push": "git fetch --tags -f && standard-version && git push --follow-tags origin master && bun publish --access public",
|
|
17
|
+
"release": "pnpm run build && pnpm run build:declaration && pnpm run push"
|
|
17
18
|
},
|
|
18
19
|
"peerDependencies": {
|
|
19
20
|
"@types/react": "^19.0.10",
|