Files
supabase/apps/studio/components/interfaces/ConnectSheet/CopyPromptAdmonition.tsx
Mert YEREKAPAN 206430d9b0 feat(studio): restore connect sheet snippet copy tracking (#54398)
The Connect sheet only reports a copy for the Postgres connection string
and one MCP setup step. The Connect dialog it replaced also reported
framework, mobile framework and ORM snippet copies and every MCP
client's copy (#40611); that tracking was not carried over (#44327) and
the dialog was later removed (#47122). I restored it with the existing
`connection_string_copied` event, and added the sheet's command copies
and Copy prompt so the whole sheet is covered.

**Changed:**
- **Framework, mobile framework and ORM snippets**: copying any file tab
in the configure step sends `connection_string_copied` with
`connectionTab` (`App Frameworks`, `Mobile Frameworks` or `ORMs`),
`selectedItem` (the framework or ORM label) and `source: studio`. A
framework added to the sheet later is covered without touching its
content file.
- **Install commands**: the framework, ORM, shadcn and skills install
commands send the same event with `connectionType: Install command`, so
they can be told apart from snippet copies.
- **Claude Code and Codex MCP**: the add-server, authenticate and verify
commands send the event with `connectionTab: MCP`, the client label and
`connectionType: Command Line`.
- **Copy prompt**: sends the existing `ai_prompt_copied` event with a
new `source` value, `connect_sheet`.

**Note:** `lang` carries the client library key (for example
`supabasejs`), as the old dialog sent it, so the restored series lines
up with the data from before March. Filter on `connectionType` to count
snippet copies only: `Framework snippet` for frameworks, absent for ORM
snippets. In Direct mode the install and skills install commands send
the event with `connectionTab: Connection String` and `lang: bash`; the
Direct file examples and Server mode are unchanged. In MCP mode the
skills install command sends nothing, so the `MCP` series only counts
MCP setup copies.

## To test

Tested on the PR preview, with the network tab filtered to the telemetry
event request:
- [x] Open a project, open Connect, keep Framework on Next.js and click
Copy on a file tab in the configure step; expect one
`connection_string_copied` with `connectionTab: App Frameworks`,
`selectedItem: Next.js`, `connectionType: Framework snippet`, `source:
studio`
- [x] Still on Next.js, switch to the `.env.local` tab and copy it;
expect the same event again (one per click)
- [x] Switch the framework to Flutter and copy a file tab; expect
`connectionTab: Mobile Frameworks`, `selectedItem: Flutter`
- [x] Switch to ORM, pick Prisma and copy a file tab; expect
`connectionTab: ORMs`, `selectedItem: Prisma` and no `connectionType`
- [x] Switch to MCP, pick Claude Code and copy the add-server command;
expect `connectionTab: MCP`, `selectedItem: Claude Code`,
`connectionType: Command Line`
- [x] Still on Claude Code, copy the authenticate command; expect the
same properties. Pick Codex and copy add-server, authenticate and
verify; expect `selectedItem: Codex` on each
- [x] In Framework mode copy the install command and the skills install
command; expect `connection_string_copied` with `connectionType: Install
command` and `connectionTab: App Frameworks` for each. In ORM mode copy
an install command; expect `connectionTab: ORMs`, `connectionType:
Install command`
- [ ] In Direct mode copy the skills install command; expect
`connection_string_copied` with `connectionTab: Connection String`,
`connectionType: Install command`, `lang: bash`
- [ ] In MCP mode copy the skills install command; expect no
`connection_string_copied` request
- [x] Click Copy prompt; expect one `ai_prompt_copied` with `source:
connect_sheet` and no `connection_string_copied`
- [x] Switch to Server and copy the env block; expect no
`connection_string_copied` request
- [x] Switch to Direct and copy the connection string; expect exactly
one `connection_string_copied` with `connectionTab: Connection String`
(unchanged behavior, no duplicate)


## Linear
- fixes GROWTH-1532
2026-12-04 10:46:11 +10:00

166 lines
5.5 KiB
TypeScript

import { CheckIcon, CopyIcon } from 'lucide-react'
import { useEffect, useState, type RefObject } from 'react'
import { copyToClipboard } from 'ui'
import { ButtonTooltip } from '@/components/ui/ButtonTooltip'
import { useTrack } from '@/lib/telemetry/track'
interface CopyPromptButtonProps {
stepsContainerRef: RefObject<HTMLDivElement | null>
/** When set, the Copy prompt button uses this verbatim instead of scraping the steps. */
customPrompt?: string
}
const normalizeTextLines = (value: string) => {
return value
.split('\n')
.map((line) => line.trim())
.filter(Boolean)
.join('\n')
}
const getStepTextContent = (contentElement: HTMLElement) => {
const clone = contentElement.cloneNode(true) as HTMLElement
clone
.querySelectorAll(
'pre, button, svg, input, textarea, select, [aria-hidden="true"], [data-connect-prompt-ignore]'
)
.forEach((element) => {
element.remove()
})
clone.querySelectorAll('p, div').forEach((el) => {
el.appendChild(document.createTextNode('\n'))
})
const text = clone.textContent ?? ''
return normalizeTextLines(text)
}
const getStepCodeSnippets = (contentElement: HTMLElement) => {
const snippets: Array<{ label: string; snippet: string }> = []
const seen = new Set<string>()
const addSnippet = (label: string, snippet: string) => {
if (!snippet || seen.has(snippet)) return
seen.add(snippet)
snippets.push({ label, snippet })
}
const getSnippet = (element: Element) => {
if (element.closest('[data-connect-prompt-ignore]')) return undefined
const copyValueElement = element.closest('[data-connect-copy-value]') as HTMLElement | null
return copyValueElement?.dataset.connectCopyValue?.trim() || element.textContent?.trim()
}
const tabContents = Array.from(
contentElement.querySelectorAll('[data-connect-tab-content]')
) as HTMLElement[]
tabContents.forEach((tabContent) => {
const label = tabContent.getAttribute('data-tab-label') || 'Code'
const tabSnippets = Array.from(tabContent.querySelectorAll('pre'))
.map(getSnippet)
.filter((snippet): snippet is string => Boolean(snippet))
if (tabSnippets.length === 0) {
const inlineSnippets = Array.from(tabContent.querySelectorAll('code'))
.filter((code) => !code.closest('pre') && code.closest('.font-mono'))
.map(getSnippet)
.filter((snippet): snippet is string => Boolean(snippet))
inlineSnippets.forEach((snippet, index) => {
const inlineLabel = inlineSnippets.length > 1 ? `${label} (part ${index + 1})` : label
addSnippet(inlineLabel, snippet)
})
return
}
tabSnippets.forEach((snippet, index) => {
const tabLabel = tabSnippets.length > 1 ? `${label} (part ${index + 1})` : label
addSnippet(tabLabel, snippet)
})
})
contentElement.querySelectorAll('pre').forEach((pre) => {
if (pre.closest('[data-connect-tab-content]')) return
const snippet = getSnippet(pre)
if (snippet) addSnippet('Code', snippet)
})
contentElement.querySelectorAll('code').forEach((code) => {
if (code.closest('pre')) return
if (code.closest('[data-connect-tab-content]')) return
if (!code.closest('.font-mono')) return
const snippet = getSnippet(code)
if (snippet) addSnippet('Code', snippet)
})
return snippets
}
export const buildConnectPrompt = (stepsContainer: HTMLElement | null) => {
const stepElements = stepsContainer?.querySelectorAll('[data-connect-step]')
if (!stepElements?.length) return ''
const promptContent = Array.from(stepElements)
.map((stepElement, index) => {
const title = stepElement.getAttribute('data-step-title') ?? `Step ${index + 1}`
const description = stepElement.getAttribute('data-step-description') ?? ''
const contentElement = stepElement.querySelector('[data-step-content]') as HTMLElement | null
const details = contentElement ? getStepTextContent(contentElement) : ''
const codeSnippets = contentElement ? getStepCodeSnippets(contentElement) : []
const sections = [
`${index + 1}. ${title}`,
description,
details ? `Details:\n${details}` : null,
codeSnippets.length
? `Code:\n${codeSnippets
.map(({ label, snippet }) => `File: ${label}\n\`\`\`\n${snippet}\n\`\`\``)
.join('\n\n')}`
: null,
].filter(Boolean)
return sections.join('\n')
})
.join('\n\n')
return promptContent
}
export function CopyPromptButton({ stepsContainerRef, customPrompt }: CopyPromptButtonProps) {
const [showCopied, setShowCopied] = useState(false)
const track = useTrack()
useEffect(() => {
if (!showCopied) return
const timer = setTimeout(() => setShowCopied(false), 2000)
return () => clearTimeout(timer)
}, [showCopied])
return (
<>
<ButtonTooltip
icon={showCopied ? <CheckIcon strokeWidth={2} className="text-primary" /> : <CopyIcon />}
onClick={() => {
const textToCopy = customPrompt ?? buildConnectPrompt(stepsContainerRef.current)
copyToClipboard(textToCopy, () => setShowCopied(true))
track('ai_prompt_copied', { source: 'connect_sheet' })
}}
tooltip={{
content: {
side: 'left',
text: 'Copy these steps for your coding agent',
},
}}
>
{showCopied ? 'Copied' : 'Copy prompt'}
</ButtonTooltip>
<span className="sr-only" role="status" aria-live="polite">
{showCopied ? 'Copied' : ''}
</span>
</>
)
}