Skip to content

SigNoz is an open-source, OpenTelemetry-native observability platform for your team and their AI agents. Get logs, metrics, and traces in one tool with features like APM, distributed tracing, log management, infra monitoring, etc. Combined with SigNoz MCP and a native AI teammate (in SigNoz Cloud) it helps you build more resilient apps.

1.5 KB
Raw Blame
import { ReactNode, useEffect } from 'react';
import { useMutation } from 'react-query';
import updateUserPreferenceAPI from 'api/v1/user/preferences/name/update';
import { useAppContext } from 'providers/App/App';

import {
	setTraceStoreCallbacks,
	setTraceStoreUserPreferences,
} from './traceStore';

interface TraceStoreSyncProps {
	children: ReactNode;
}

/**
 * Bridges React-managed inputs (`userPreferences` from AppContext and the
 * user-pref mutation hook) into the Zustand store. `availableColorByFields`
 * is pushed by TraceDetailsOverview, which owns the span data.
 *
 * Renders nothing until `userPreferences` resolves so the flamegraph never
 * paints with the default colour first and then swaps to the user's
 * persisted choice. AppProvider fires the prefs query as soon as the user
 * is logged in, so this gate is usually already settled by mount time.
 */
function TraceStoreSync({ children }: TraceStoreSyncProps): JSX.Element | null {
	const { userPreferences, updateUserPreferenceInContext } = useAppContext();
	const { mutate: mutateUserPreference } = useMutation(updateUserPreferenceAPI);

	useEffect(() => {
		setTraceStoreUserPreferences(userPreferences ?? null);
	}, [userPreferences]);

	useEffect(() => {
		setTraceStoreCallbacks({
			updateUserPreferenceInContext,
			mutateUserPreference,
		});
	}, [updateUserPreferenceInContext, mutateUserPreference]);

	if (!userPreferences) {
		return null;
	}

	return <>{children}</>;
}

export default TraceStoreSync;