Skip to content

Commit 8723d5d

Browse files
committed
chore(project): refactor things
1 parent bc08f99 commit 8723d5d

File tree

5 files changed

+22
-262
lines changed

5 files changed

+22
-262
lines changed

package-lock.json

Lines changed: 0 additions & 215 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,16 @@
3131
]
3232
},
3333
"scripts": {
34-
"test": "cross-env CI=1 react-scripts test --env=jsdom || true",
34+
"test": "CI=true react-scripts test --env=jsdom || true",
3535
"test:watch": "react-scripts test --env=jsdom",
3636
"build": "rollup -c",
3737
"start": "rollup -c -w",
3838
"prepare": "npm run build",
39-
"predeploy": "cd example && npm install && npm run build",
40-
"deploy": "gh-pages -d example/build",
4139
"format": "prettier --write src/**/*.{js,jsx,ts,tsx}",
4240
"lint": "eslint --fix src/**/*.{js,jsx,ts,tsx}",
4341
"make-badges": "echo ok",
4442
"release": "standard-version"
4543
},
46-
"dependencies": {},
4744
"peerDependencies": {
4845
"react": "^16.13.1"
4946
},
@@ -55,7 +52,6 @@
5552
"@types/react": "^16.9.46",
5653
"@typescript-eslint/eslint-plugin": "^3.9.0",
5754
"@typescript-eslint/parser": "^3.9.0",
58-
"cross-env": "^7.0.2",
5955
"eslint": "^7.6.0",
6056
"eslint-config-airbnb": "^18.2.0",
6157
"eslint-config-airbnb-typescript-prettier": "^3.1.0",
@@ -65,7 +61,6 @@
6561
"eslint-plugin-prettier": "^3.1.4",
6662
"eslint-plugin-react": "^7.20.5",
6763
"eslint-plugin-react-hooks": "^4.0.8",
68-
"gh-pages": "^3.1.0",
6964
"husky": "^4.2.5",
7065
"prettier": "^2.0.5",
7166
"react": "^16.13.1",
@@ -76,7 +71,6 @@
7671
"rollup-plugin-node-resolve": "^5.2.0",
7772
"rollup-plugin-peer-deps-external": "^2.2.3",
7873
"rollup-plugin-typescript2": "^0.27.2",
79-
"rollup-plugin-url": "^3.0.1",
8074
"standard-version": "^8.0.2",
8175
"typescript": "^3.9.7"
8276
}

src/index.tsx

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,46 @@
11
import * as React from 'react';
2-
import { useAppendHeadScript } from './useAppendHeadScript';
2+
import useAppendHeadScript from './useAppendHeadScript';
33

44
type TUseHotjar = {
55
initHotjar: (
66
hotjarId: string,
77
hotjarVersion: string,
8-
shouldLog?: boolean
8+
logCallback?: () => void
99
) => boolean;
1010
identityHotjar: (
1111
userId: string,
1212
userInfo: string,
13-
shouldLog?: boolean
13+
logCallback?: () => void
1414
) => boolean;
1515
};
1616

1717
export function useHotjar(): TUseHotjar {
1818
const { appendHeadScript } = useAppendHeadScript();
1919

2020
const initHotjar = React.useCallback(
21-
(hotjarId: string, hotjarVersion: string, shouldLog = true): boolean => {
21+
(hotjarId: string, hotjarVersion: string, loggerFunction): boolean => {
2222
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({
24-
scriptText: hotjarScript,
25-
scriptId: 'hotjar-script',
26-
});
23+
const isHotjarAppended = appendHeadScript(hotjarScript, 'hotjar-script');
2724

28-
if (shouldLog) console.info(`Hotjar ready: ${isHotjarAppended}`);
25+
if (loggerFunction && typeof loggerFunction === 'function')
26+
loggerFunction(`Hotjar ready: ${isHotjarAppended}`);
2927

3028
return isHotjarAppended;
3129
},
3230
[appendHeadScript]
3331
);
3432

3533
const identityHotjar = React.useCallback(
36-
(userId: string, userInfo: string, shouldLog = true): boolean => {
34+
(userId: string, userInfo: string, loggerFunction): boolean => {
3735
try {
3836
const hotjarIdentityScript = `var userId="${userId}" || null;window.hj("identify",userId,${userInfo});`;
39-
const isIdentified = appendHeadScript({
40-
scriptText: hotjarIdentityScript,
41-
scriptId: 'identity-script',
42-
});
37+
const isIdentified = appendHeadScript(
38+
hotjarIdentityScript,
39+
'identity-script'
40+
);
4341

44-
if (shouldLog) console.info(`Hotjar identified: ${isIdentified}`);
42+
if (loggerFunction && typeof loggerFunction === 'function')
43+
loggerFunction(`Hotjar identified: ${isIdentified}`);
4544

4645
return isIdentified;
4746
} catch (error) {
@@ -54,7 +53,7 @@ export function useHotjar(): TUseHotjar {
5453
);
5554

5655
return React.useMemo(() => ({ initHotjar, identityHotjar }), [
57-
identityHotjar,
5856
initHotjar,
57+
identityHotjar,
5958
]);
6059
}

0 commit comments

Comments
 (0)