Files
supabase/apps/studio/components/interfaces/Settings/General/TransferProjectPanel/TransferProjectButton.tsx
supabase-vercel-tedd[bot]andIvan Vasilov e3684c034c feat(studio): track usage on Project Settings: General (#54081)
## Problem

Project Settings → General has almost no usage tracking. We can't tell
how often users restart, pause, resume, or transfer projects from here.
We also can't tell how often they copy the project ID or region, use the
custom domain flow, or toggle high compliance mode.

## Solution

Adds six PostHog events to `packages/common/telemetry-constants.ts`:
`project_name_updated`, `project_settings_value_copied`,
`project_lifecycle_action_submitted`, `project_compliance_mode_updated`,
`custom_domain_step_submitted`, and `custom_domain_dns_record_copied`.
Each event fires from the user's submit, confirm, copy, or toggle
handler, never on render. Properties are only enums and booleans. No
project names, domains, DNS record contents, or other typed values are
sent.

`ResumeProjectButton` is shared between settings and the paused-project
home state, so it now takes a required `source` prop. The two call sites
pass `settings_general` and `project_home`. Restart fires from the
confirmation modal, with `restart_project` or `restart_database`
depending on the chosen restart type. `DNSRecord` now types its `type`
prop as `'CNAME' | 'TXT'`, so the copy event sends the record type as an
enum. No `data-attr` attributes are added, because PostHog autocapture
is off and they would not be collected.

## How to verify

1. On Project Settings → General, save a new project name. One
`project_name_updated` event fires.
2. Copy the project ID, then the region. Each copy fires one
`project_settings_value_copied` event, with `field` set to `projectRef`
and then `region`.
3. Open the restart modal, then cancel it. No event fires. Confirm a
restart (project or fast database reboot). One
`project_lifecycle_action_submitted` fires with `action:
restart_project` or `restart_database` and `source: settings_general`.
4. Confirm a pause, a resume, and a transfer. Each fires
`project_lifecycle_action_submitted` with the matching `action`. A
resume from settings sends `source: settings_general`; a resume from the
paused project home page sends `source: project_home`.
5. Toggle high compliance mode. One `project_compliance_mode_updated`
fires, with `isSensitive` set to the new value.
6. In the custom domain flow, Add, Verify, confirm Activate, and confirm
Delete each fire `custom_domain_step_submitted` with `step` set to
`configure`, `verify`, `activate`, and `remove`.
7. In the custom domain verification step, copy a DNS record's name,
then its value. Each copy fires one `custom_domain_dns_record_copied`
event, with `recordType` set to `CNAME` or `TXT` and `field` set to
`name` and then `value`.

## Checks

Ran `tsc --noEmit` in `apps/studio` and `packages/common`. Ran `eslint`
(default config and `eslint.type-checks.config.cjs`) on the changed
Studio files: no errors, only warnings that were already there. Ran
`oxfmt --check` on all changed files. Ran `vitest run` on
`General.test.tsx`, `Project.test.tsx`, `PauseProjectButton.test.tsx`,
and `RestartServerButton.test.tsx`: all pass.

## Checklist

- [x] I have read the [Contributing
Guidelines](https://github.com/supabase/supabase/blob/master/CONTRIBUTING.md)
- [ ] I have checked that the change works in the preview deployment

## Feedback for Tedd

To ask Tedd for a change to this PR, reply in Tedd's session on
[FE-5029](https://linear.app/supabase/issue/FE-5029/track-usage-on-project-settings-general).
Tedd doesn't read comments on this PR.

---------

Co-authored-by: supabase-vercel-tedd[bot] <336548405+supabase-vercel-tedd[bot]@users.noreply.github.com>
Co-authored-by: Ivan Vasilov <vasilov.ivan@gmail.com>
2026-11-30 20:38:20 +01:00

317 lines
12 KiB
TypeScript

import { useQueryClient } from '@tanstack/react-query'
import { useFlag } from 'common'
import { LoaderIcon, ShieldIcon, WrenchIcon } from 'lucide-react'
import { useEffect, useState } from 'react'
import { toast } from 'sonner'
import {
Button,
Dialog,
DialogContent,
DialogFooter,
DialogHeader,
DialogSection,
DialogSectionSeparator,
DialogTitle,
DialogTrigger,
InfoIcon,
Loading,
Select,
SelectContent,
SelectItem,
SelectTrigger,
SelectValue,
WarningIcon,
} from 'ui'
import { Admonition } from 'ui-patterns/Admonition'
import { FormItemLayout } from 'ui-patterns/form/FormItemLayout/FormItemLayout'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { DocsButton } from '@/components/ui/DocsButton'
import { useOrganizationsQuery } from '@/data/organizations/organizations-query'
import { projectKeys } from '@/data/projects/keys'
import { useProjectTransferMutation } from '@/data/projects/project-transfer-mutation'
import { useProjectTransferPreviewQuery } from '@/data/projects/project-transfer-preview-query'
import { FGA_PERMISSIONS, useAsyncCheckPermissionsV2 } from '@/hooks/misc/useCheckPermissionsV2'
import { useSelectedProjectQuery } from '@/hooks/misc/useSelectedProject'
import { DOCS_URL } from '@/lib/constants'
import { useTrack } from '@/lib/telemetry/track'
export const TransferProjectButton = () => {
const track = useTrack()
const { data: project } = useSelectedProjectQuery()
const projectRef = project?.ref
const projectOrgId = project?.organization_id
const [isOpen, setIsOpen] = useState(false)
const { data: allOrganizations } = useOrganizationsQuery({ enabled: isOpen })
const disableProjectTransfer = useFlag('disableProjectTransfer')
const organizations = (allOrganizations || []).filter((it) => it.id !== projectOrgId)
const [selectedOrg, setSelectedOrg] = useState<string>()
const {
mutate: transferProject,
error: transferError,
isPending: isTransferring,
} = useProjectTransferMutation({
onSuccess: () => {
toast.success(`Successfully transferred project ${project?.name}.`)
setIsOpen(false)
},
})
const {
data: transferPreviewData,
error: transferPreviewError,
isPending: transferPreviewIsLoading,
} = useProjectTransferPreviewQuery(
{ projectRef, targetOrganizationSlug: selectedOrg },
{ enabled: !isTransferring && isOpen }
)
const queryClient = useQueryClient()
useEffect(() => {
if (transferPreviewData === undefined) return
const membersExceedingLimitCount =
transferPreviewData.members_exceeding_free_project_limit.length
const isBlockedNoticeShown =
transferPreviewData.errors.length > 0 && membersExceedingLimitCount > 0
if (!isBlockedNoticeShown) return
track('project_transfer_preview_failed', {
sourcePlan: transferPreviewData.source_subscription_plan,
targetPlan: transferPreviewData.target_subscription_plan,
membersExceedingLimitCount,
})
}, [transferPreviewData, track])
useEffect(() => {
if (isOpen) {
// reset state
setSelectedOrg(undefined)
} else {
// Invalidate cache
queryClient.removeQueries({
queryKey: projectKeys.projectTransferPreview(projectRef, selectedOrg),
})
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [isOpen])
const { can: canTransferProject } = useAsyncCheckPermissionsV2([
FGA_PERMISSIONS.PROJECT.ADMIN_WRITE,
FGA_PERMISSIONS.ORGANIZATION.ADMIN_WRITE,
])
async function handleTransferProject() {
if (project === undefined) return
if (selectedOrg === undefined) return
track('project_lifecycle_action_submitted', { action: 'transfer', source: 'settings_general' })
transferProject({ projectRef, targetOrganizationSlug: selectedOrg })
}
return (
<Dialog onOpenChange={(open) => setIsOpen(open)} open={isOpen}>
<DialogTrigger asChild>
<ButtonTooltip
disabled={!canTransferProject || disableProjectTransfer}
tooltip={{
content: {
side: 'bottom',
text: !canTransferProject
? 'You need additional permissions to transfer this project'
: disableProjectTransfer
? 'Project transfers are temporarily disabled, please try again later.'
: undefined,
},
}}
>
Transfer project
</ButtonTooltip>
</DialogTrigger>
<DialogContent size="xlarge">
<DialogHeader>
<DialogTitle>{`Transfer project ${project?.name}`}</DialogTitle>
</DialogHeader>
<DialogSectionSeparator />
<DialogSection className="text-foreground-light">
<p className="text-sm">
To transfer projects, the owner must be a member of both the source and target
organizations. Consider the following before transferring your project:
</p>
<ul className="mt-4 space-y-5 text-sm">
<li className="flex gap-4">
<span className="shrink-0 mt-1">
<LoaderIcon />
</span>
<div>
<p className="font-bold">Possible downtime</p>
<p>
There might be a short downtime when transferring projects from a paid to a free
organization.
</p>
</div>
</li>
<li className="flex gap-4">
<span className="shrink-0 mt-1">
<ShieldIcon />
</span>
<div>
<p className="font-bold">Permissions</p>
<p>
Depending on your role in the target organization, your level of permissions may
change after transfer.
</p>
</div>
</li>
<li className="flex gap-4">
<span className="shrink-0 mt-1">
<WrenchIcon size={24} className="shrink-0" />
</span>
<div>
<p className="font-bold">Features</p>
<p>
Moving your project to an organization with a smaller subscription plan may result
in the loss of certain features (i.e. image transformations).
</p>
</div>
</li>
</ul>
<DocsButton
abbrev={false}
className="mt-6"
href={`${DOCS_URL}/guides/platform/project-transfer`}
/>
</DialogSection>
<DialogSectionSeparator />
<DialogSection>
{organizations && (
<div className="space-y-2">
{organizations.length === 0 ? (
<div className="flex items-center gap-3 bg-surface-200 p-3 text-sm rounded-md border">
<InfoIcon /> You do not have any organizations you can transfer your project to.
</div>
) : (
<FormItemLayout
id="organization"
isReactForm={false}
layout="vertical"
label="Select Target Organization"
className="gap-[2px]"
size="tiny"
>
<Select onValueChange={(slug) => setSelectedOrg(slug)} value={selectedOrg}>
<SelectTrigger id="organization">
<SelectValue placeholder="Select Organization" />
</SelectTrigger>
<SelectContent>
{organizations.map((x) => (
<SelectItem key={x.id} value={x.slug}>
{x.name}
</SelectItem>
))}
</SelectContent>
</Select>
</FormItemLayout>
)}
</div>
)}
</DialogSection>
{selectedOrg !== undefined && (
<Loading active={selectedOrg !== undefined && transferPreviewIsLoading}>
<DialogSection>
<div className="space-y-2">
{transferPreviewData && transferPreviewData.errors.length > 0 && (
<Admonition type="danger" title="Project cannot be transferred">
<div className="space-y-1">
{transferPreviewData.errors.map((error) => (
<p key={error.key}>{error.message}</p>
))}
</div>
{transferPreviewData.members_exceeding_free_project_limit.length > 0 && (
<div className="space-y-2">
<p className="text-sm text-foreground-light">
These members have reached their maximum limits for the number of active
Free plan projects within organizations where they are an administrator or
owner:
</p>
<ul className="pl-5 text-sm list-disc text-foreground-light">
{(transferPreviewData.members_exceeding_free_project_limit || []).map(
(member, idx: number) => (
<li key={`member-${idx}`}>
{member.name} (Limit: {member.limit} free projects)
</li>
)
)}
</ul>
<p className="text-sm text-foreground-light">
These members will need to either delete, pause, or upgrade one or more of
their projects before you can transfer this project.
</p>
</div>
)}
</Admonition>
)}
{transferPreviewData &&
(transferPreviewData.warnings.length > 0 ||
transferPreviewData.info.length > 0) && (
<Admonition type="caution" showIcon={false} className="mt-3">
<div className="flex flex-col gap-y-2">
{transferPreviewData.warnings.map((warning) => (
<div key={warning.key} className="flex items-center gap-2">
<WarningIcon className="shrink-0" />
<p className="mb-0.5">{warning.message}</p>
</div>
))}
{transferPreviewData.info.map((info) => (
<div key={info.key} className="flex items-start gap-2">
<InfoIcon className="shrink-0 mt-0.5" />
<p className="mb-0.5">{info.message}</p>
</div>
))}
</div>
</Admonition>
)}
{transferPreviewError && !transferError && (
<Admonition
type="danger"
title="Project cannot be transferred"
description={transferPreviewError.message}
/>
)}
{transferError && (
<Admonition
type="danger"
title="Project cannot be transferred"
description={transferError.message}
/>
)}
</div>
</DialogSection>
</Loading>
)}
<DialogFooter>
<Button onClick={() => setIsOpen(false)}>Cancel</Button>
<Button
variant="primary"
onClick={() => handleTransferProject()}
disabled={
!transferPreviewData || !transferPreviewData.valid || isTransferring || !selectedOrg
}
>
Transfer Project
</Button>
</DialogFooter>
</DialogContent>
</Dialog>
)
}