@visitorquery/react 0.0.7 → 0.0.8

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 (2) hide show
  1. package/dist/index.js +64 -20
  2. package/package.json +3 -2
package/dist/index.js CHANGED
@@ -1,25 +1,69 @@
1
1
  // src/index.ts
2
- import { useState, useEffect } from "react";
3
- function useVisitorQuery(publicApiKey, endpoint = "https://main.check.visitorquery.com/") {
4
- const [visitorQuery, setVisitorQuery] = useState({
5
- is: false,
6
- confidence: 0
2
+ import { 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
7
57
  });
8
- useEffect(() => {
9
- const fetchData = async () => {
10
- try {
11
- const response = await fetch(endpoint, {
12
- headers: { "X-Api-Key": publicApiKey }
13
- });
14
- const data = await response.json();
15
- setVisitorQuery(data);
16
- } catch (error) {
17
- console.error(`Error fetching visitorquery data: ${error}`);
18
- }
19
- };
20
- fetchData().catch(console.error);
21
- }, [publicApiKey, endpoint]);
22
- return visitorQuery;
58
+ useEffect2(() => {
59
+ if (typeof window.VisitorQuery !== "undefined") {
60
+ window.VisitorQuery.run({
61
+ ApiKey: p.ApiKey,
62
+ Endpoint: p.Endpoint || defaultEndpoint,
63
+ SessionId: p.SessionId
64
+ });
65
+ }
66
+ }, [p, clientScript]);
23
67
  }
24
68
  export {
25
69
  useVisitorQuery
package/package.json CHANGED
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "0.0.7",
2
+ "version": "0.0.8",
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
- "release": "git fetch --tags -f && standard-version && git push --follow-tags origin master && bun publish --access public"
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",