Files
supabase/apps/studio/hooks/misc/useDataApiRevokeOnCreateDefault.ts
Sean Oliver fdceb29260 fix(telemetry): exposure event captures dataApiDefaultPrivileges + drop race-fix hook (#46085)
## I have read the
[CONTRIBUTING.md](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
file.

YES

## What kind of change does this PR introduce?

Bug fix.

## What is the current behavior?

This PR addresses three issues with the implementation of the
`dataApiRevokeOnCreateDefault` experiment
([GROWTH-858](https://linear.app/supabase/issue/GROWTH-858)) on the
frontend side:

1. The `project_creation_default_privileges_exposed` event payload
captures `dataApiEnabled`, which is the parent "Enable Data API" toggle.
That value defaults to `true` for everyone in both arms, which doesn't
help understand how people are interacting with the form.

2. The hook itself was gating on PostHog JS SDK values rather than our
backend server values being sent from `/telemetry/feature-flags`.

3. The form on `/new/[slug]` captures `dataApiDefaultPrivileges`
defaults once at mount via react-hook-form's `defaultValues`. If the
flag is still loading when the page mounts,
`useDataApiRevokeOnCreateDefaultEnabled()` returns `false` (coerced from
undefined), and the form locks the field to the legacy default of
`true`. The flag later resolving has no effect, and treatment users get
the legacy default visually and in the exposure event.

## What is the new behavior?

1. Main-surface payload now sends `dataApiDefaultPrivileges` — the form
field the experiment actually controls (`true` = legacy grants kept,
`false` = revoked on create). Post-fix data will let us read out whether
treatment users actually got the new default.

2. Hook is simplified: drop `orgCountReady`, drop the `onFeatureFlags`
subscription, drop the `posthogClient` import. It now fires once when
the flag resolves, period. Vercel surface is unchanged (still no
`dataApiDefaultPrivileges` since there's no user-facing toggle there).
Tests updated.

3. New useEffect in `/new/[slug]` watches the raw flag value and syncs
`dataApiDefaultPrivileges` to the correct experiment-driven default when
the flag resolves, gated on `getFieldState(...).isDirty` so we don't
clobber intentional user input.

## Additional context

Backend half of this fix is at supabase/platform#32933 (passes
`org_count` and `signup_timestamp` in `personProperties` so the audience
filter actually evaluates correctly). Both PRs are needed for the
experiment to bucket at 5% and be measurable.

<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->
## Summary by CodeRabbit

* **Changes**
* Telemetry now records the selected default-privileges setting
(dataApiDefaultPrivileges) in project-creation events; the previous
dataApiEnabled field was removed.
* Project-creation flows apply the experiment-driven default for that
setting once the experiment resolves, but they do not overwrite
user-edited choices. Vercel new-project flow syncs with the experiment
until the user changes the checkbox.

* **Tests**
* Updated tests to validate tracking, deduplication, and sync/timing
behaviors for dataApiDefaultPrivileges.

<!-- review_stack_entry_start -->

[![Review Change
Stack](https://storage.googleapis.com/coderabbit_public_assets/review-stack-in-coderabbit-ui.svg)](https://app.coderabbit.ai/change-stack/supabase/supabase/pull/46085?utm_source=github_walkthrough&utm_medium=github&utm_campaign=change_stack)

<!-- review_stack_entry_end -->
<!-- end of auto-generated comment: release notes by coderabbit.ai -->
2026-05-19 12:54:36 -07:00

75 lines
2.7 KiB
TypeScript

import { useEffect, useRef } from 'react'
import { usePHFlag } from '../ui/useFlag'
import { IS_TEST_ENV } from '@/lib/constants'
import { useTrack } from '@/lib/telemetry/track'
/**
* Controls the default state of the "Automatically expose new tables"
* checkbox at project creation. When the flag is on, the checkbox defaults
* to unchecked (i.e. revoke SQL runs). When off/absent, the checkbox defaults
* to checked (current behaviour — default grants remain).
*/
export const useDataApiRevokeOnCreateDefaultEnabled = (): boolean => {
const flag = usePHFlag<boolean>('dataApiRevokeOnCreateDefault')
// Preserve current behaviour (default grants remain) in tests so existing
// E2E flows don't change silently. Tests that need the revoke-default path
// should opt in explicitly.
if (IS_TEST_ENV) {
return false
}
return !!flag
}
type DefaultPrivilegesExposureOptions =
| {
surface: 'main'
dataApiDefaultPrivileges: boolean
hasUserModified: boolean
}
| {
surface: 'vercel'
orgSlug: string | undefined
dataApiDefaultPrivileges: boolean
hasUserModified: boolean
}
/**
* Fires `project_creation_default_privileges_exposed` once per mount, once the
* flag has resolved AND the form value is consistent with the flag's expected
* default. The convergence gate avoids a render-ordering race: caller-side
* sync effects (in /new/[slug] and the Vercel deploy flow) update the form
* value when the flag resolves late, but child effects fire before parent
* effects in the same commit, so without the gate the exposure would capture
* the stale pre-sync value. If the user has dirtied the field, fire
* immediately with their explicit value. Deduplicated via ref.
*/
export const useTrackDefaultPrivilegesExposure = (options: DefaultPrivilegesExposureOptions) => {
const track = useTrack()
const flag = usePHFlag<boolean>('dataApiRevokeOnCreateDefault')
const hasTracked = useRef(false)
const { surface, dataApiDefaultPrivileges, hasUserModified } = options
const orgSlug = options.surface === 'vercel' ? options.orgSlug : undefined
useEffect(() => {
if (hasTracked.current) return
if (flag === undefined) return
if (surface === 'vercel' && !orgSlug) return
// Gate on form-flag convergence unless the user explicitly dirtied the field.
if (!hasUserModified && dataApiDefaultPrivileges !== !flag) return
hasTracked.current = true
track(
'project_creation_default_privileges_exposed',
{
surface,
dataApiDefaultPrivileges,
dataApiRevokeOnCreateDefaultEnabled: flag,
},
surface === 'vercel' ? { organization: orgSlug } : undefined
)
}, [flag, track, surface, dataApiDefaultPrivileges, hasUserModified, orgSlug])
}