Skip to content

fix: resolve circular dependency in Analytics context #1686

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
15 changes: 9 additions & 6 deletions apps/web/contexts/Analytics.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
'use client';

import { ReactNode, createContext, useCallback, useContext, useMemo } from 'react';
import logEvent, {
ActionType,
AnalyticsEventImportance,
CCAEventData,
AnalyticsContextType,
} from 'libs/base-ui/utils/logEvent';
import { ReactNode, createContext, useCallback, useContext, useMemo } from 'react';

export type AnalyticsContextProps = {
logEventWithContext: (eventName: string, action: ActionType, eventData?: CCAEventData) => void;
Expand All @@ -29,19 +30,21 @@ export function useAnalytics() {

type AnalyticsProviderProps = {
children?: ReactNode;
context: string; // TODO: This could be an enum in CCAEventData
context: AnalyticsContextType;
parentContext?: string;
};

export default function AnalyticsProvider({ children, context }: AnalyticsProviderProps) {
const { fullContext: previousContext } = useAnalytics();
export default function AnalyticsProvider({ children, context, parentContext = '' }: AnalyticsProviderProps) {
const fullContext = useMemo(() => {
return [parentContext, context].filter((c) => !!c).join('_');
}, [parentContext, context]);

const fullContext = [previousContext, context].filter((c) => !!c).join('_');
const logEventWithContext = useCallback(
(eventName: string, action: ActionType, eventData?: CCAEventData) => {
const sanitizedEventName = eventName.toLocaleLowerCase();
if (typeof window === 'undefined') return;
logEvent(
sanitizedEventName, // TODO: Do we want context here?
sanitizedEventName,
{
action: action,
context: fullContext,
Expand Down