Skip to content

Commit 5490a77

Browse files
authored
fix: use crypto-js lib instead of browser lib (#1924)
1 parent f4d5daa commit 5490a77

File tree

1 file changed

+12
-33
lines changed

1 file changed

+12
-33
lines changed

src/hooks/useAnalytics.ts

Lines changed: 12 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { useEffect, useRef, useState } from 'react';
22

3+
import { SHA256 } from 'crypto-js';
34
import { useLocation } from 'react-router-dom';
45

56
import {
@@ -19,7 +20,6 @@ import { getSelectedLocale } from '@/state/localizationSelectors';
1920
import { getTradeFormValues } from '@/state/tradeFormSelectors';
2021

2122
import { identify, track } from '@/lib/analytics/analytics';
22-
import { runFn } from '@/lib/do';
2323

2424
import { useAccounts } from './useAccounts';
2525
import { useApiState } from './useApiState';
@@ -108,41 +108,20 @@ export const useAnalytics = () => {
108108
const [analyticsUserId, setAnalyticsUserId] = useState<string | null>(null);
109109

110110
useEffect(() => {
111-
let dead = false;
112-
113-
runFn(async () => {
114-
if (dead) return;
115-
if (sourceAccount.walletInfo?.connectorType === ConnectorType.Test) {
116-
setAnalyticsUserId(null);
117-
}
111+
if (sourceAccount.walletInfo?.connectorType === ConnectorType.Test) {
112+
setAnalyticsUserId(null);
113+
}
118114

119-
if (sourceAccount.walletInfo?.connectorType === ConnectorType.Turnkey) {
120-
if (sourceAccount.walletInfo.userEmail && typeof globalThis.crypto !== 'undefined') {
121-
const normalizedEmail = sourceAccount.walletInfo.userEmail.trim().toLowerCase();
122-
const hashedEmail = await globalThis.crypto.subtle.digest(
123-
'SHA-256',
124-
new TextEncoder().encode(normalizedEmail)
125-
);
126-
127-
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
128-
if (dead) return;
129-
130-
setAnalyticsUserId(
131-
Array.from(new Uint8Array(hashedEmail))
132-
.map((b) => b.toString(16).padStart(2, '0'))
133-
.join('')
134-
);
135-
136-
return;
137-
}
115+
if (sourceAccount.walletInfo?.connectorType === ConnectorType.Turnkey) {
116+
if (sourceAccount.walletInfo.userEmail) {
117+
const normalizedEmail = sourceAccount.walletInfo.userEmail.trim().toLowerCase();
118+
const hashedEmail = SHA256(normalizedEmail).toString();
119+
setAnalyticsUserId(hashedEmail);
120+
return;
138121
}
122+
}
139123

140-
setAnalyticsUserId(sourceAccount.address ?? null);
141-
});
142-
143-
return () => {
144-
dead = true;
145-
};
124+
setAnalyticsUserId(sourceAccount.address ?? null);
146125
}, [sourceAccount.address, sourceAccount.walletInfo]);
147126

148127
useEffect(() => {

0 commit comments

Comments
 (0)