mirror of
https://github.com/supabase/supabase.git
synced 2026-10-12 21:05:06 +03:00
* clean circular deps in telemetry * clean circular deps in command * fix commandmenu circular deps * rm deps.json * fix consent circular dep * fix TOC circular deps * fix wrong import path * fix test types * fix missing fn * undo let change * fix consent component * empty
44 lines
1.4 KiB
TypeScript
44 lines
1.4 KiB
TypeScript
// hooks/useTelemetryCookie.ts
|
|
import { useRouter } from 'next/compat/router'
|
|
import { useEffect } from 'react'
|
|
import { IS_PROD, LOCAL_STORAGE_KEYS } from '../constants'
|
|
import { getSharedTelemetryData } from '../telemetry-utils'
|
|
|
|
interface UseTelemetryCookieProps {
|
|
hasAcceptedConsent: boolean
|
|
title: string
|
|
referrer: string
|
|
}
|
|
|
|
/**
|
|
* This hook saves the telemetry data to a cookie. The cookie will be sent when the user consents to telemetry. If they
|
|
* don't consent, the cookie will never be sent.
|
|
*/
|
|
export function useTelemetryCookie({
|
|
hasAcceptedConsent,
|
|
title,
|
|
referrer,
|
|
}: UseTelemetryCookieProps) {
|
|
const router = useRouter()
|
|
const telemetryStorageKey = LOCAL_STORAGE_KEYS.TELEMETRY_DATA
|
|
|
|
useEffect(() => {
|
|
if (!router?.isReady) return
|
|
|
|
const cookies = document.cookie.split(';')
|
|
const cookieOptions = IS_PROD ? 'path=/; domain=supabase.com' : 'path=/'
|
|
|
|
const telemetryCookie = cookies.find((cookie) => cookie.trim().startsWith(telemetryStorageKey))
|
|
if (telemetryCookie) return
|
|
|
|
const telemetryData = getSharedTelemetryData(router.pathname)
|
|
|
|
if (!hasAcceptedConsent) {
|
|
const encodedData = encodeURIComponent(JSON.stringify(telemetryData))
|
|
document.cookie = `${telemetryStorageKey}=${encodedData}; ${cookieOptions}`
|
|
}
|
|
}, [hasAcceptedConsent, router?.isReady, telemetryStorageKey, title, router?.pathname, referrer])
|
|
}
|
|
|
|
export default useTelemetryCookie
|