import { useEffect } from "react"; import { CookieCategory } from "~/application/cookies/CookieCategory"; import { useRootData } from "~/utils/data/useRootData"; import CookieHelper from "~/utils/helpers/CookieHelper"; type ReplayModule = typeof import("~/sentry-replay.client"); let loadedReplayModule: Promise | undefined; export default function ScriptSentryReplay() { const rootData = useRootData(); const hasAnalyticsConsent = CookieHelper.hasConsent(rootData.userSession ?? null, CookieCategory.ANALYTICS); useEffect(() => { let cancelled = false; if (!hasAnalyticsConsent) { if (loadedReplayModule) { void loadedReplayModule.then(({ disableSentryReplay }) => disableSentryReplay()); } return () => { cancelled = true; }; } void import("~/sentry.client").then(({ readClientSentryConfig }) => { const config = readClientSentryConfig(); if (cancelled || !config.dsn || (config.replaysSessionSampleRate <= 0 && config.replaysOnErrorSampleRate <= 0)) { return; } loadedReplayModule ??= import("~/sentry-replay.client"); void loadedReplayModule.then(({ enableSentryReplay }) => { if (!cancelled) { enableSentryReplay(config); } }); }); return () => { cancelled = true; if (loadedReplayModule) { void loadedReplayModule.then(({ disableSentryReplay }) => disableSentryReplay()); } }; }, [hasAnalyticsConsent]); return null; }