Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

918 B
Raw Blame
'use client'

import type { AmplitudeInitializationOptions } from './init'
import { useEffect } from 'react'
import { useAnalyticsConsent } from '@/app/components/base/analytics-consent/consent-store'
import { ensureAmplitudeInitialized, setAmplitudeOptOut } from './init'

export type IAmplitudeProps = AmplitudeInitializationOptions & {
  active?: boolean
}

export function AmplitudeProvider({
  active = true,
  sessionReplaySampleRate = 0.5,
}: IAmplitudeProps) {
  const consent = useAnalyticsConsent()

  useEffect(() => {
    if (!active || consent !== 'granted') {
      setAmplitudeOptOut(true)
      return
    }

    setAmplitudeOptOut(false)
    void ensureAmplitudeInitialized({ sessionReplaySampleRate }).catch((error) => {
      console.error('Amplitude initialization failed:', error)
    })

    return () => setAmplitudeOptOut(true)
  }, [active, consent, sessionReplaySampleRate])

  return null
}