Skip to content

Commit 30ba4e0

Browse files
committed
refactor(project): update project
1 parent 88103ab commit 30ba4e0

File tree

7 files changed

+128
-122
lines changed

7 files changed

+128
-122
lines changed

.eslintrc.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,6 @@ module.exports = {
4646
],
4747
'react/state-in-constructor': 'off',
4848
'no-console': 'off',
49+
'no-restricted-syntax': 'off',
4950
},
5051
};

CHANGELOG.md

Lines changed: 0 additions & 30 deletions
This file was deleted.

README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
# react-use-hotjar
1+
# react-use-script
22

3-
> Adds [Hotjar](https://www.hotjar.com/) capabilities as custom hooks to your project
3+
> Appends script tags to the document with ease
44
5-
[![NPM](https://img.shields.io/npm/v/react-use-hotjar.svg)](https://www.npmjs.com/package/react-use-hotjar)
5+
[![NPM](https://img.shields.io/npm/v/react-use-script.svg)](https://www.npmjs.com/package/react-use-script)
66

77
---
88

package-lock.json

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
{
2-
"name": "react-use-hotjar",
2+
"name": "react-use-script",
33
"version": "1.0.6",
4-
"description": "Add Hotjar capabilities as custom hooks",
4+
"description": "Appends script tags to the document",
55
"author": "Olavo Parno",
66
"license": "MIT",
77
"repository": {
88
"type": "git",
9-
"url": "git://github.com/olavoparno/react-use-hotjar.git"
9+
"url": "git://github.com/olavoparno/react-use-script.git"
1010
},
1111
"main": "dist/index.js",
1212
"module": "dist/index.es.js",

src/index.tsx

Lines changed: 120 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -1,59 +1,124 @@
11
import * as React from 'react';
2-
import useAppendHeadScript from './useAppendHeadScript';
3-
4-
type TUseHotjar = {
5-
initHotjar: (
6-
hotjarId: string,
7-
hotjarVersion: string,
8-
logCallback?: () => void
9-
) => boolean;
10-
identifyHotjar: (
11-
userId: string,
12-
userInfo: string,
13-
logCallback?: () => void
14-
) => boolean;
2+
3+
type TScriptLoader = {
4+
onCreate?: (() => null) | undefined;
5+
onLoad?: (() => null) | undefined;
6+
onError?: ((e: any) => never) | undefined;
7+
delayMs?: number | undefined;
8+
htmlPart?: string | undefined;
9+
src: string;
10+
otherProps?: Record<string, unknown> | undefined;
11+
};
12+
13+
type TAppendScript = {
14+
id: string;
15+
scriptText: string;
16+
optionalCallback?: (() => null) | undefined;
17+
htmlPart: string;
18+
otherProps?: Record<string, unknown> | undefined;
19+
};
20+
21+
type TUseScript = {
22+
ScriptLoader: {
23+
({
24+
onCreate,
25+
onLoad,
26+
onError,
27+
delayMs,
28+
htmlPart,
29+
src,
30+
...otherProps
31+
}: TScriptLoader): null;
32+
};
33+
appendScript: {
34+
({
35+
id,
36+
scriptText,
37+
optionalCallback,
38+
htmlPart,
39+
otherProps,
40+
}: TAppendScript): boolean;
41+
};
42+
};
43+
44+
const handleScriptAttributes = (
45+
script: HTMLScriptElement,
46+
otherProps: Record<string, unknown>
47+
) => {
48+
for (const [attr, value] of Object.entries(otherProps)) {
49+
script.setAttribute(attr, value as string);
50+
}
51+
};
52+
53+
const ScriptLoader = ({
54+
onCreate = () => null,
55+
onLoad = () => null,
56+
onError = (e) => {
57+
throw new URIError(`The script ${e.target.src} is not accessible`);
58+
},
59+
delayMs = 0,
60+
htmlPart = 'head',
61+
src,
62+
...otherProps
63+
}: TScriptLoader): null => {
64+
const appendScript = React.useCallback(() => {
65+
const script = global.document.createElement('script');
66+
67+
script.src = src;
68+
69+
if (otherProps) {
70+
handleScriptAttributes(script, otherProps as Record<string, unknown>);
71+
}
72+
73+
script.onload = onLoad;
74+
script.onerror = onError;
75+
76+
global.document[htmlPart].appendChild(script);
77+
78+
onCreate();
79+
}, [onCreate, onError, onLoad, otherProps, src, htmlPart]);
80+
81+
React.useEffect(() => {
82+
const timeout = setTimeout(() => appendScript(), delayMs);
83+
84+
return () => {
85+
clearTimeout(timeout);
86+
};
87+
}, [appendScript, delayMs]);
88+
89+
return null;
90+
};
91+
92+
const appendScript = ({
93+
id,
94+
scriptText,
95+
optionalCallback = () => null,
96+
htmlPart = 'head',
97+
otherProps = {},
98+
}: TAppendScript): boolean => {
99+
try {
100+
const existentScript = global.document.getElementById(
101+
id
102+
) as HTMLScriptElement;
103+
const script = existentScript || global.document.createElement('script');
104+
105+
script.id = id;
106+
script.innerText = scriptText.toString();
107+
108+
handleScriptAttributes(script, otherProps);
109+
110+
global.document[htmlPart].appendChild(script);
111+
112+
optionalCallback();
113+
114+
return true;
115+
} catch (error) {
116+
console.error('Must be a string!', error);
117+
118+
return false;
119+
}
15120
};
16121

17-
export function useHotjar(): TUseHotjar {
18-
const { appendHeadScript } = useAppendHeadScript();
19-
20-
const initHotjar = React.useCallback(
21-
(hotjarId: string, hotjarVersion: string, loggerFunction): boolean => {
22-
const hotjarScript = `(function(h,o,t,j,a,r){h.hj=h.hj||function(){(h.hj.q=h.hj.q||[]).push(arguments)};h._hjSettings={hjid:${hotjarId},hjsv:${hotjarVersion}};a=o.getElementsByTagName('head')[0];r=o.createElement('script');r.async=1;r.src=t+h._hjSettings.hjid+j+h._hjSettings.hjsv;a.appendChild(r);})(window,document,'https://static.hotjar.com/c/hotjar-','.js?sv=');`;
23-
const isHotjarAppended = appendHeadScript(hotjarScript, 'hotjar-script');
24-
25-
if (loggerFunction && typeof loggerFunction === 'function')
26-
loggerFunction(`Hotjar ready: ${isHotjarAppended}`);
27-
28-
return isHotjarAppended;
29-
},
30-
[appendHeadScript]
31-
);
32-
33-
const identifyHotjar = React.useCallback(
34-
(userId: string, userInfo: string, loggerFunction): boolean => {
35-
try {
36-
const hotjarIdentifyScript = `var userId="${userId}" || null;window.hj("identify",userId,${userInfo});`;
37-
const isIdentified = appendHeadScript(
38-
hotjarIdentifyScript,
39-
'identify-script'
40-
);
41-
42-
if (loggerFunction && typeof loggerFunction === 'function')
43-
loggerFunction(`Hotjar identified: ${isIdentified}`);
44-
45-
return isIdentified;
46-
} catch (error) {
47-
console.error('Hotjar error:', error);
48-
49-
return false;
50-
}
51-
},
52-
[appendHeadScript]
53-
);
54-
55-
return React.useMemo(() => ({ initHotjar, identifyHotjar }), [
56-
initHotjar,
57-
identifyHotjar,
58-
]);
122+
export function useScript(): TUseScript {
123+
return React.useMemo(() => ({ ScriptLoader, appendScript }), []);
59124
}

src/useAppendHeadScript.ts

Lines changed: 0 additions & 30 deletions
This file was deleted.

0 commit comments

Comments
 (0)