The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Update PH consent banner (#29891)
Joshen Limcdb07346bcc1
diff --git a/apps/studio/components/interfaces/Account/Preferences/AnalyticsSettings.tsx b/apps/studio/components/interfaces/Account/Preferences/AnalyticsSettings.tsxindex 779e48e5c07..16f566b15a7 100644--- a/apps/studio/components/interfaces/Account/Preferences/AnalyticsSettings.tsx+++ b/apps/studio/components/interfaces/Account/Preferences/AnalyticsSettings.tsx@@ -1,16 +1,23 @@import { Toggle } from 'ui'import Panel from 'components/ui/Panel'-import { useAppStateSnapshot } from 'state/app-state'import { useSendResetMutation } from 'data/telemetry/send-reset-mutation'+import { useFlag } from 'hooks/ui/useFlag'+import { LOCAL_STORAGE_KEYS } from 'lib/constants'+import { useAppStateSnapshot } from 'state/app-state'const AnalyticsSettings = () => {const snap = useAppStateSnapshot()+ const enablePostHogTelemetry = useFlag('enablePosthogChanges')const { mutate: sendReset } = useSendResetMutation()+ const consentFlag = enablePostHogTelemetry+ ? LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT_PH+ : LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT+const onToggleOptIn = () => {const value = !snap.isOptedInTelemetry ? 'true' : 'false'- snap.setIsOptedInTelemetry(value === 'true')+ snap.setIsOptedInTelemetry(value === 'true', consentFlag)if (value === 'false') sendReset()}diff --git a/apps/studio/components/ui/PageTelemetry.tsx b/apps/studio/components/ui/PageTelemetry.tsxindex 88e10f0beab..ad98b7e96ac 100644--- a/apps/studio/components/ui/PageTelemetry.tsx+++ b/apps/studio/components/ui/PageTelemetry.tsx@@ -1,7 +1,8 @@import { Sha256 } from '@aws-crypto/sha256-browser'import * as Sentry from '@sentry/nextjs'import { useRouter } from 'next/router'-import { PropsWithChildren, useEffect, useState } from 'react'+import { PropsWithChildren, useEffect, useRef } from 'react'+import { toast } from 'sonner'import { useParams, useUser } from 'common'import { useSendGroupsIdentifyMutation } from 'data/telemetry/send-groups-identify-mutation'@@ -13,6 +14,7 @@ import { useSelectedOrganization } from 'hooks/misc/useSelectedOrganization'import { useFlag } from 'hooks/ui/useFlag'import { IS_PLATFORM, LOCAL_STORAGE_KEYS } from 'lib/constants'import { useAppStateSnapshot } from 'state/app-state'+import { ConsentToast } from 'ui-patterns/ConsentToast'const getAnonId = async (id: string) => {const hash = new Sha256()@@ -30,12 +32,14 @@ const PageTelemetry = ({ children }: PropsWithChildren<{}>) => {const snap = useAppStateSnapshot()const organization = useSelectedOrganization()+ const consentToastId = useRef<string | number>()const previousPathname = usePrevious(router.pathname)const enablePostHogTelemetry = useFlag('enablePosthogChanges')- const consent =- typeof window !== 'undefined'- ? localStorage.getItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT)- : null++ const consentFlag = enablePostHogTelemetry+ ? LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT_PH+ : LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT+ const consent = typeof window !== 'undefined' ? localStorage.getItem(consentFlag) : nullconst trackTelemetryPH = enablePostHogTelemetry && consent === 'true'const { mutate: sendPage } = useSendPageMutation()@@ -44,13 +48,45 @@ const PageTelemetry = ({ children }: PropsWithChildren<{}>) => {const { mutate: sendGroupsReset } = useSendGroupsResetMutation()useEffect(() => {- const consent =- typeof window !== 'undefined'- ? localStorage.getItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT)- : null- if (consent !== null) snap.setIsOptedInTelemetry(consent === 'true')+ if (typeof window !== 'undefined' && enablePostHogTelemetry !== undefined) {+ const onAcceptConsent = () => {+ snap.setIsOptedInTelemetry(true, consentFlag)+ if (consentToastId.current) toast.dismiss(consentToastId.current)+ }++ const onOptOut = () => {+ snap.setIsOptedInTelemetry(false, consentFlag)+ if (consentToastId.current) toast.dismiss(consentToastId.current)+ }++ const hasAcknowledgedConsent = localStorage.getItem(+ enablePostHogTelemetry+ ? LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT_PH+ : LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT+ )+ snap.setIsOptedInTelemetry(+ typeof hasAcknowledgedConsent === 'string'+ ? hasAcknowledgedConsent === 'true'+ : hasAcknowledgedConsent,+ consentFlag+ )++ if (IS_PLATFORM && hasAcknowledgedConsent === null) {+ setTimeout(() => {+ consentToastId.current = toast(+ <ConsentToast onAccept={onAcceptConsent} onOptOut={onOptOut} />,+ {+ id: 'consent-toast',+ position: 'bottom-right',+ duration: Infinity,+ }+ )+ }, 300)+ }+ }+// eslint-disable-next-line react-hooks/exhaustive-deps- }, [])+ }, [enablePostHogTelemetry])useEffect(() => {function handleRouteChange() {diff --git a/apps/studio/data/telemetry/send-event-mutation.ts b/apps/studio/data/telemetry/send-event-mutation.tsindex 34add0c887d..c2a22289c48 100644--- a/apps/studio/data/telemetry/send-event-mutation.ts+++ b/apps/studio/data/telemetry/send-event-mutation.ts@@ -20,12 +20,16 @@ export type SendEventVariables = {type SendEventPayload = any-export async function sendEvent(type: 'GA' | 'PH', body: SendEventPayload) {- const consent =- typeof window !== 'undefined'- ? localStorage.getItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT)- : null- if (consent !== 'true') return+export async function sendEvent({+ consent,+ type,+ body,+}: {+ consent: boolean+ type: 'GA' | 'PH'+ body: SendEventPayload+}) {+ if (!consent) return undefinedconst headers = type === 'PH' ? { Version: '2' } : undefinedconst { data, error } = await post(`/platform/telemetry/event`, { body, headers })@@ -46,6 +50,15 @@ export const useSendEventMutation = ({const router = useRouter()const usePostHogParameters = useFlag('enablePosthogChanges')+ const consent =+ (typeof window !== 'undefined'+ ? localStorage.getItem(+ usePostHogParameters+ ? LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT_PH+ : LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT+ )+ : null) === 'true'+const title = typeof document !== 'undefined' ? document?.title : ''const referrer = typeof document !== 'undefined' ? document?.referrer : ''@@ -85,7 +98,7 @@ export const useSendEventMutation = ({custom_properties: otherVars,} as unknown as SendEventPH): ({ ...vars, ...payload } as SendEventGA)- return sendEvent(type, body)+ return sendEvent({ consent, type, body })},{async onSuccess(data, variables, context) {diff --git a/apps/studio/data/telemetry/send-groups-identify-mutation.ts b/apps/studio/data/telemetry/send-groups-identify-mutation.tsindex 1b3d241d4a4..04bd2673623 100644--- a/apps/studio/data/telemetry/send-groups-identify-mutation.ts+++ b/apps/studio/data/telemetry/send-groups-identify-mutation.ts@@ -2,12 +2,24 @@ import { useMutation, UseMutationOptions } from '@tanstack/react-query'import { components } from 'api-types'import { handleError, post } from 'data/fetchers'+import { LOCAL_STORAGE_KEYS } from 'lib/constants'import type { ResponseError } from 'types'export type SendGroupsIdentifyVariables = components['schemas']['TelemetryGroupsIdentityBody']-export async function sendGroupsIdentify(body: SendGroupsIdentifyVariables) {- const { data, error } = await post(`/platform/telemetry/groups/identify`, { body })+export async function sendGroupsIdentify({+ consent,+ body,+}: {+ consent: boolean+ body: SendGroupsIdentifyVariables+}) {+ if (!consent) return undefined++ const { data, error } = await post(`/platform/telemetry/groups/identify`, {+ body,+ credentials: 'include',+ })if (error) handleError(error)return data}@@ -22,8 +34,13 @@ export const useSendGroupsIdentifyMutation = ({UseMutationOptions<SendGroupsIdentifyData, ResponseError, SendGroupsIdentifyVariables>,'mutationFn'> = {}) => {+ const consent =+ (typeof window !== 'undefined'+ ? localStorage.getItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT_PH)+ : null) === 'true'+return useMutation<SendGroupsIdentifyData, ResponseError, SendGroupsIdentifyVariables>(- (vars) => sendGroupsIdentify(vars),+ (vars) => sendGroupsIdentify({ consent, body: vars }),{async onSuccess(data, variables, context) {await onSuccess?.(data, variables, context)diff --git a/apps/studio/data/telemetry/send-groups-reset-mutation.ts b/apps/studio/data/telemetry/send-groups-reset-mutation.tsindex 4d6e214d481..3523235426c 100644--- a/apps/studio/data/telemetry/send-groups-reset-mutation.ts+++ b/apps/studio/data/telemetry/send-groups-reset-mutation.ts@@ -2,12 +2,24 @@ import { useMutation, UseMutationOptions } from '@tanstack/react-query'import { components } from 'api-types'import { handleError, post } from 'data/fetchers'+import { LOCAL_STORAGE_KEYS } from 'lib/constants'import type { ResponseError } from 'types'export type SendGroupsResetVariables = components['schemas']['TelemetryGroupsResetBody']-export async function sendGroupsReset(body: SendGroupsResetVariables) {- const { data, error } = await post(`/platform/telemetry/groups/reset`, { body })+export async function sendGroupsReset({+ consent,+ body,+}: {+ consent: boolean+ body: SendGroupsResetVariables+}) {+ if (!consent) return undefined++ const { data, error } = await post(`/platform/telemetry/groups/reset`, {+ body,+ credentials: 'include',+ })if (error) handleError(error)return data}@@ -22,8 +34,13 @@ export const useSendGroupsResetMutation = ({UseMutationOptions<SendGroupsResetData, ResponseError, SendGroupsResetVariables>,'mutationFn'> = {}) => {+ const consent =+ (typeof window !== 'undefined'+ ? localStorage.getItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT_PH)+ : null) === 'true'+return useMutation<SendGroupsResetData, ResponseError, SendGroupsResetVariables>(- (vars) => sendGroupsReset(vars),+ (vars) => sendGroupsReset({ consent, body: vars }),{async onSuccess(data, variables, context) {await onSuccess?.(data, variables, context)diff --git a/apps/studio/data/telemetry/send-identify-mutation.ts b/apps/studio/data/telemetry/send-identify-mutation.tsindex 76d9eb5ab3d..ad63eb75d6f 100644--- a/apps/studio/data/telemetry/send-identify-mutation.ts+++ b/apps/studio/data/telemetry/send-identify-mutation.ts@@ -1,4 +1,5 @@import { useMutation, UseMutationOptions } from '@tanstack/react-query'+import { useRouter } from 'next/router'import { components } from 'api-types'import { isBrowser } from 'common'@@ -6,7 +7,6 @@ import { handleError, post } from 'data/fetchers'import { Profile } from 'data/profile/types'import { useFlag } from 'hooks/ui/useFlag'import { LOCAL_STORAGE_KEYS } from 'lib/constants'-import { useRouter } from 'next/router'import type { ResponseError } from 'types'type SendIdentifyGA = components['schemas']['TelemetryIdentifyBody']@@ -20,12 +20,16 @@ export type SendIdentifyVariables = {type SendIdentifyPayload = any-export async function sendIdentify(type: 'GA' | 'PH', body: SendIdentifyPayload) {- const consent =- typeof window !== 'undefined'- ? localStorage.getItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT)- : null- if (consent !== 'true') return+export async function sendIdentify({+ consent,+ type,+ body,+}: {+ consent: boolean+ type: 'GA' | 'PH'+ body: SendIdentifyPayload+}) {+ if (!consent) return undefinedconst headers = type === 'PH' ? { Version: '2' } : undefinedconst { data, error } = await post(`/platform/telemetry/identify`, { body, headers })@@ -46,6 +50,15 @@ export const useSendIdentifyMutation = ({const router = useRouter()const usePostHogParameters = useFlag('enablePosthogChanges')+ const consent =+ (typeof window !== 'undefined'+ ? localStorage.getItem(+ usePostHogParameters+ ? LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT_PH+ : LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT+ )+ : null) === 'true'+const payload = usePostHogParameters? ({} as SendIdentifyPH): ({@@ -67,7 +80,7 @@ export const useSendIdentifyMutation = ({project_ref: ref,} as SendIdentifyPH): ({ user: userInfo, ...payload } as SendIdentifyGA)- return sendIdentify(type, body)+ return sendIdentify({ consent, type, body })},{async onSuccess(data, variables, context) {diff --git a/apps/studio/data/telemetry/send-page-leave-mutation.ts b/apps/studio/data/telemetry/send-page-leave-mutation.tsindex fc458c2f449..ad187b4ea07 100644--- a/apps/studio/data/telemetry/send-page-leave-mutation.ts+++ b/apps/studio/data/telemetry/send-page-leave-mutation.ts@@ -2,13 +2,26 @@ import { useMutation, UseMutationOptions } from '@tanstack/react-query'import { components } from 'api-types'import { handleError, post } from 'data/fetchers'+import { useFlag } from 'hooks/ui/useFlag'+import { LOCAL_STORAGE_KEYS } from 'lib/constants'import { useRouter } from 'next/router'import type { ResponseError } from 'types'type SendPageLeaveBody = components['schemas']['TelemetryPageLeaveBody']-export async function sendPageLeave(body: SendPageLeaveBody) {- const { data, error } = await post(`/platform/telemetry/page-leave`, { body })+export async function sendPageLeave({+ consent,+ body,+}: {+ consent: boolean+ body: SendPageLeaveBody+}) {+ if (!consent) return undefined++ const { data, error } = await post(`/platform/telemetry/page-leave`, {+ body,+ credentials: 'include',+ })if (error) handleError(error)return data}@@ -21,6 +34,11 @@ export const useSendPageLeaveMutation = ({...options}: Omit<UseMutationOptions<SendPageLeaveData, ResponseError>, 'mutationFn'> = {}) => {const router = useRouter()+ const consent =+ (typeof window !== 'undefined'+ ? localStorage.getItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT_PH)+ : null) === 'true'+const url = typeof window !== 'undefined' ? window.location.href : ''const title = typeof document !== 'undefined' ? document?.title : ''@@ -30,7 +48,7 @@ export const useSendPageLeaveMutation = ({pathname: router.pathname,} as SendPageLeaveBody- return useMutation<SendPageLeaveData, ResponseError>((vars) => sendPageLeave(body), {+ return useMutation<SendPageLeaveData, ResponseError>((vars) => sendPageLeave({ consent, body }), {async onSuccess(data, variables, context) {await onSuccess?.(data, variables, context)},diff --git a/apps/studio/data/telemetry/send-page-mutation.ts b/apps/studio/data/telemetry/send-page-mutation.tsindex 4d357c1be2a..6d3d7a67499 100644--- a/apps/studio/data/telemetry/send-page-mutation.ts+++ b/apps/studio/data/telemetry/send-page-mutation.ts@@ -4,6 +4,7 @@ import { components } from 'api-types'import { isBrowser } from 'common'import { handleError, post } from 'data/fetchers'import { useFlag } from 'hooks/ui/useFlag'+import { LOCAL_STORAGE_KEYS } from 'lib/constants'import { useRouter } from 'next/router'import type { ResponseError } from 'types'@@ -16,7 +17,17 @@ export type SendPageVariables = {type SendPagePayload = any-export async function sendPage(type: 'GA' | 'PH', body: SendPagePayload) {+export async function sendPage({+ consent,+ type,+ body,+}: {+ consent: boolean+ type: 'GA' | 'PH'+ body: SendPagePayload+}) {+ if (!consent) return undefined+const headers = type === 'PH' ? { Version: '2' } : undefinedconst { data, error } = await post(`/platform/telemetry/page`, { body, headers })if (error) handleError(error)@@ -33,6 +44,15 @@ export const useSendPageMutation = ({const router = useRouter()const usePostHogParameters = useFlag('enablePosthogChanges')+ const consent =+ (typeof window !== 'undefined'+ ? localStorage.getItem(+ usePostHogParameters+ ? LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT_PH+ : LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT+ )+ : null) === 'true'+const title = typeof document !== 'undefined' ? document?.title : ''const referrer = typeof document !== 'undefined' ? document?.referrer : ''@@ -65,7 +85,7 @@ export const useSendPageMutation = ({const body = usePostHogParameters? ({ page_url: url, ...payload } as SendPagePH): ({ route: url, ...payload } as SendPageGA)- return sendPage(type, body)+ return sendPage({ consent, type, body })},{async onSuccess(data, variables, context) {diff --git a/apps/studio/data/telemetry/send-reset-mutation.ts b/apps/studio/data/telemetry/send-reset-mutation.tsindex c1056d8bf8f..070824b9f67 100644--- a/apps/studio/data/telemetry/send-reset-mutation.ts+++ b/apps/studio/data/telemetry/send-reset-mutation.ts@@ -16,7 +16,7 @@ export const useSendResetMutation = ({onError,...options}: Omit<UseMutationOptions<SendResetData, ResponseError>, 'mutationFn'> = {}) => {- return useMutation<SendResetData, ResponseError>((vars) => sendReset(), {+ return useMutation<SendResetData, ResponseError>(() => sendReset(), {async onSuccess(data, variables, context) {await onSuccess?.(data, variables, context)},diff --git a/apps/studio/lib/constants/index.ts b/apps/studio/lib/constants/index.tsindex 3fe036d1d63..da484ef951d 100644--- a/apps/studio/lib/constants/index.ts+++ b/apps/studio/lib/constants/index.ts@@ -30,6 +30,8 @@ export const USAGE_APPROACHING_THRESHOLD = 0.75export const LOCAL_STORAGE_KEYS = {RECENTLY_VISITED_ORGANIZATION: 'supabase-organization',TELEMETRY_CONSENT: 'supabase-consent',+ // Used for when we enable PH for telemetry instead of GA+ TELEMETRY_CONSENT_PH: 'supabase-consent-ph',UI_PREVIEW_NAVIGATION_LAYOUT: 'supabase-ui-preview-nav-layout',UI_PREVIEW_API_SIDE_PANEL: 'supabase-ui-api-side-panel',diff --git a/apps/studio/next-env.d.ts b/apps/studio/next-env.d.tsindex 4f11a03dc6c..a4a7b3f5cfa 100644--- a/apps/studio/next-env.d.ts+++ b/apps/studio/next-env.d.ts@@ -2,4 +2,4 @@/// <reference types="next/image-types/global" />// NOTE: This file should not be edited-// see https://nextjs.org/docs/basic-features/typescript for more information.+// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information.diff --git a/apps/studio/pages/_app.tsx b/apps/studio/pages/_app.tsxindex 8e5989df167..b1c6e7fff68 100644--- a/apps/studio/pages/_app.tsx+++ b/apps/studio/pages/_app.tsx@@ -54,7 +54,6 @@ import HCaptchaLoadedStore from 'stores/hcaptcha-loaded-store'import { AppPropsWithLayout } from 'types'import { SonnerToaster } from 'ui'import { CommandProvider } from 'ui-patterns/CommandMenu'-import { ConsentToast } from 'ui-patterns/ConsentToast'dayjs.extend(customParseFormat)dayjs.extend(utc)@@ -119,36 +118,6 @@ function CustomApp({ Component, pageProps }: AppPropsWithLayout) {console.error(error.stack)}- useEffect(() => {- // Check for telemetry consent- if (typeof window !== 'undefined') {- const onAcceptConsent = () => {- snap.setIsOptedInTelemetry(true)- if (consentToastId.current) toast.dismiss(consentToastId.current)- }-- const onOptOut = () => {- snap.setIsOptedInTelemetry(false)- if (consentToastId.current) toast.dismiss(consentToastId.current)- }-- const hasAcknowledgedConsent = localStorage.getItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT)- if (IS_PLATFORM && hasAcknowledgedConsent === null) {- setTimeout(() => {- consentToastId.current = toast(- <ConsentToast onAccept={onAcceptConsent} onOptOut={onOptOut} />,- {- id: 'consent-toast',- position: 'bottom-right',- duration: Infinity,- }- )- }, 300)- }- }- // eslint-disable-next-line react-hooks/exhaustive-deps- }, [])-useThemeSandbox()const isTestEnv = process.env.NEXT_PUBLIC_NODE_ENV === 'test'diff --git a/apps/studio/state/app-state.ts b/apps/studio/state/app-state.tsindex 4aa575e3702..7f4246a26dc 100644--- a/apps/studio/state/app-state.ts+++ b/apps/studio/state/app-state.ts@@ -36,11 +36,16 @@ export const appState = proxy({},isOptedInTelemetry: false,- setIsOptedInTelemetry: (value: boolean) => {- appState.isOptedInTelemetry = value- if (typeof window !== 'undefined') {- localStorage.setItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT, value.toString())+ setIsOptedInTelemetry: (value: boolean | null, flag = LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT) => {+ appState.isOptedInTelemetry = value === null ? false : value+ if (typeof window !== 'undefined' && value !== null) {+ localStorage.setItem(flag, value.toString())}+ // [Joshen] Eventually once we completely move to PH, we should just set local storage+ // directly here, but since currently it's dependent on a feature flag, will need to pass in as a prop+ // if (typeof window !== 'undefined') {+ // localStorage.setItem(LOCAL_STORAGE_KEYS.TELEMETRY_CONSENT, value.toString())+ // }},showEnableBranchingModal: false,setShowEnableBranchingModal: (value: boolean) => {diff --git a/packages/ui-patterns/ConsentToast/index.tsx b/packages/ui-patterns/ConsentToast/index.tsxindex 64011845e3d..8a45ede2242 100644--- a/packages/ui-patterns/ConsentToast/index.tsx+++ b/packages/ui-patterns/ConsentToast/index.tsx@@ -22,10 +22,10 @@ export const ConsentToast = ({ onAccept = noop, onOptOut = noop }: ConsentToastPconst isMobile = useBreakpoint(639)return (- <div className="space-y-3 py-1 flex flex-col w-full">+ <div className="py-1 flex flex-col gap-y-3 w-full"><div>- <p className="text-foreground">- We only collect analytics essential to ensuring smooth operation of our services.{' '}+ <p className="text-sm text-foreground">+ We use first-party cookies to improve our services.<LinkclassName="inline sm:hidden underline text-light"target="_blank"