Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 48 additions & 16 deletions src/providers/WidgetTrackingProvider.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { useUrlParams } from '@/hooks/useUrlParams';
import type { JumperEventData } from '@/utils/tracking/jumperTracking';
import type {
ChainTokenSelected,
Expand All @@ -7,7 +8,9 @@ import type {
RouteHighValueLossUpdate,
SettingUpdated,
} from '@lifi/widget';
import { useWidgetEvents } from '@lifi/widget';
import { formatTokenPrice, useWidgetEvents } from '@lifi/widget';
import type { Address } from 'viem';
import { useTokens } from 'src/hooks/useTokens';
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion | 🟠 Major | ⚡ Quick win

Use @/ path alias instead of src/.

As per coding guidelines, prefer @/ over src/ path aliases in new code.

Proposed fix
-import { useTokens } from 'src/hooks/useTokens';
+import { useTokens } from '@/hooks/useTokens';
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import { useTokens } from 'src/hooks/useTokens';
import { useTokens } from '@/hooks/useTokens';
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@src/providers/WidgetTrackingProvider.tsx` at line 13, Update the import in
WidgetTrackingProvider.tsx to use the project path alias: change the import of
the hook useTokens from "src/hooks/useTokens" to use "@/hooks/useTokens" (i.e.
replace the "src/" prefix with "@/") so the file follows the coding guideline;
ensure any other occurrences in this file or adjacent imports are updated
similarly to remain consistent with the alias convention.

import { isEqual, omit } from 'lodash';
import type { FC, PropsWithChildren } from 'react';
import {
Expand Down Expand Up @@ -122,6 +125,12 @@ export const WidgetTrackingProvider: FC<WidgetTrackingProviderProps> = ({
const trackedRoutesData = useRef<Record<string, TrackTransactionDataProps>>(
{},
);
const urlParams = useUrlParams();
const urlParamsRef = useRef(urlParams);
urlParamsRef.current = urlParams;

const { getToken } = useTokens();

const posthogTracker = useMemo(() => {
return makePosthogTracker({ trackTransaction, trackEvent });
}, [trackTransaction, trackEvent]);
Expand Down Expand Up @@ -155,8 +164,37 @@ export const WidgetTrackingProvider: FC<WidgetTrackingProviderProps> = ({

const availableRoutes = useCallback(
(availableRoutes: Route[]) => {
if (currentFromAmount.current !== availableRoutes[0]?.fromAmount) {
currentFromAmount.current = availableRoutes[0]?.fromAmount;
const firstRoute = availableRoutes[0];

const fromToken =
sourceChainToken.current?.tokenAddress ??
urlParamsRef.current.sourceChainToken.token;
const fromChainId =
sourceChainToken.current?.chainId ??
urlParamsRef.current.sourceChainToken.chainId;
const toToken =
destinationChainToken.current?.tokenAddress ??
urlParamsRef.current.destinationChainToken.token;
const toChainId =
destinationChainToken.current?.chainId ??
urlParamsRef.current.destinationChainToken.chainId;

const fromAmount =
firstRoute?.fromAmount ?? urlParamsRef.current.fromAmount;

const fallbackToken =
fromChainId && fromToken
? getToken(fromChainId, fromToken as Address)
: undefined;

const fromAmountUSD = firstRoute
? Number(firstRoute.fromAmountUSD)
: fallbackToken?.priceUSD && fromAmount
? Number(formatTokenPrice(fromAmount, fallbackToken.priceUSD))
: 0;

if (currentFromAmount.current !== fromAmount) {
currentFromAmount.current = fromAmount;
isRoutesForCurrentFromAmountTracked.current = false;
}

Expand Down Expand Up @@ -201,18 +239,12 @@ export const WidgetTrackingProvider: FC<WidgetTrackingProviderProps> = ({
label: `routes_available`,
enableAddressable: true,
data: {
[TrackingEventParameter.FromToken]:
sourceChainToken.current?.tokenAddress || '',
[TrackingEventParameter.FromChainId]:
sourceChainToken.current?.chainId || '',
[TrackingEventParameter.ToToken]:
destinationChainToken.current?.tokenAddress || '',
[TrackingEventParameter.ToChainId]:
destinationChainToken.current?.chainId || '',
[TrackingEventParameter.FromAmountUSD]: Number(
availableRoutes?.[0]?.fromAmountUSD,
),
[TrackingEventParameter.FromAmount]: availableRoutes?.[0]?.fromAmount,
[TrackingEventParameter.FromToken]: fromToken || '',
[TrackingEventParameter.FromChainId]: fromChainId || '',
[TrackingEventParameter.ToToken]: toToken || '',
[TrackingEventParameter.ToChainId]: toChainId || '',
[TrackingEventParameter.FromAmountUSD]: fromAmountUSD,
[TrackingEventParameter.FromAmount]: fromAmount || '',
[TrackingEventParameter.NbOfSteps]: availableRoutes.length,
[TrackingEventParameter.Routes]: transformedRoutes,
},
Expand All @@ -222,7 +254,7 @@ export const WidgetTrackingProvider: FC<WidgetTrackingProviderProps> = ({
isRoutesForCurrentDestinationTokenTracked.current = true;
isRoutesForCurrentFromAmountTracked.current = true;
},
[trackEvent, trackingActionKeys.availableRoutes],
[trackEvent, trackingActionKeys.availableRoutes, getToken],
);

const routeExecutionStarted = useCallback(
Expand Down
Loading