Skip to content

Commit f6cdbdd

Browse files
committed
round 1
1 parent 2846ab1 commit f6cdbdd

2 files changed

Lines changed: 56 additions & 9 deletions

File tree

src/frontend/src/app/AppMain.tsx

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,22 @@ import { BrowserRouter } from 'react-router-dom';
77
import AppContext from './AppContext';
88
import AppPublic from './AppPublic';
99
import { ToastProvider } from '../components/Toast/ToastProvider';
10+
import ClarityProvider from './ClarityProvider';
1011
import AppOAuthProvider from './AppOauthProvider';
1112

1213
const AppMain: React.FC = () => {
1314
return (
14-
<AppContext>
15-
<ToastProvider>
16-
<BrowserRouter>
17-
<AppOAuthProvider>
18-
<AppPublic />
19-
</AppOAuthProvider>
20-
</BrowserRouter>
21-
</ToastProvider>
22-
</AppContext>
15+
<ClarityProvider>
16+
<AppContext>
17+
<ToastProvider>
18+
<BrowserRouter>
19+
<AppOAuthProvider>
20+
<AppPublic />
21+
</AppOAuthProvider>
22+
</BrowserRouter>
23+
</ToastProvider>
24+
</AppContext>
25+
</ClarityProvider>
2326
);
2427
};
2528

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import React, { useEffect, createContext, useContext, useCallback } from 'react';
2+
3+
declare global {
4+
interface Window {
5+
clarity?: (...args: any[]) => void;
6+
}
7+
}
8+
9+
const CLARITY_PROJECT_ID = import.meta.env.VITE_REACT_APP_CLARITY_PROJECT_ID as string;
10+
type ClarityFn = (...args: any[]) => void;
11+
export const ClarityContext = createContext<ClarityFn | undefined>(undefined);
12+
13+
export const useClarity = () => useContext(ClarityContext);
14+
15+
const ClarityProvider: React.FC<{ children: React.ReactNode }> = ({ children }) => {
16+
useEffect(() => {
17+
if (typeof window !== 'undefined' && !window.clarity) {
18+
(function (c: any, l: Document, a: string, r: string, i: string) {
19+
c[a] =
20+
c[a] ||
21+
function (...args: any[]) {
22+
(c[a].q = c[a].q || []).push(args);
23+
};
24+
const t = l.createElement(r) as HTMLScriptElement;
25+
t.async = true;
26+
t.src = 'https://www.clarity.ms/tag/' + i;
27+
const [y] = l.getElementsByTagName(r);
28+
if (y && y.parentNode) {
29+
y.parentNode.insertBefore(t, y);
30+
}
31+
})(window, document, 'clarity', 'script', CLARITY_PROJECT_ID);
32+
}
33+
}, []);
34+
35+
const clarity = useCallback<ClarityFn>((...args) => {
36+
if (typeof window !== 'undefined' && typeof window.clarity === 'function') {
37+
window.clarity(...args);
38+
}
39+
}, []);
40+
41+
return <ClarityContext.Provider value={clarity}>{children}</ClarityContext.Provider>;
42+
};
43+
44+
export default ClarityProvider;

0 commit comments

Comments
 (0)