mirror of
https://github.com/supabase/supabase.git
synced 2026-10-11 04:15:04 +03:00
Sorted all imports in all packages, `cms`, `design-system` and `ui-library` apps by running `pnpm format` on them. All changes in this PR are done by the script.
38 lines
1.2 KiB
TypeScript
38 lines
1.2 KiB
TypeScript
import { useEffect } from 'react'
|
|
|
|
import { LOCAL_STORAGE_KEYS } from '../constants'
|
|
import { getSharedTelemetryData, getTelemetryCookieOptions } from '../telemetry-utils'
|
|
|
|
interface UseTelemetryCookieProps {
|
|
enabled: boolean
|
|
}
|
|
|
|
/**
|
|
* This hook saves the initial telemetry data to a cookie so we can preserve first-touch attribution
|
|
* (e.g. `document.referrer`) until the initial pageview is emitted. If the user never
|
|
* consents, this cookie is never used.
|
|
*/
|
|
export function useTelemetryCookie({ enabled }: UseTelemetryCookieProps) {
|
|
const telemetryStorageKey = LOCAL_STORAGE_KEYS.TELEMETRY_DATA
|
|
|
|
useEffect(() => {
|
|
if (typeof window === 'undefined') return
|
|
if (!enabled) return
|
|
|
|
const cookies = document.cookie.split(';')
|
|
const cookieOptions = getTelemetryCookieOptions()
|
|
|
|
const telemetryCookie = cookies.find((cookie) =>
|
|
cookie.trim().startsWith(`${telemetryStorageKey}=`)
|
|
)
|
|
if (telemetryCookie) return
|
|
|
|
const telemetryData = getSharedTelemetryData(window.location.pathname)
|
|
|
|
const encodedData = encodeURIComponent(JSON.stringify(telemetryData))
|
|
document.cookie = `${telemetryStorageKey}=${encodedData}; ${cookieOptions}`
|
|
}, [enabled, telemetryStorageKey])
|
|
}
|
|
|
|
export default useTelemetryCookie
|