:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(navigation): copy the scene name breadcrumb on click (#122831)
ruby childsdf2cdcfa6aca
diff --git a/frontend/src/layout/scenes/components/QuillSceneStaticName.test.tsx b/frontend/src/layout/scenes/components/QuillSceneStaticName.test.tsxnew file mode 100644index 000000000000..5f61a86fb772--- /dev/null+++ b/frontend/src/layout/scenes/components/QuillSceneStaticName.test.tsx@@ -0,0 +1,63 @@+import '@testing-library/jest-dom'++import { cleanup, fireEvent, render, screen } from '@testing-library/react'+import userEvent from '@testing-library/user-event'++import { copyToClipboard } from 'lib/utils/copyToClipboard'++import { QuillSceneStaticName } from './QuillSceneStaticName'++jest.mock('lib/utils/copyToClipboard', () => ({ copyToClipboard: jest.fn() }))++describe('QuillSceneStaticName', () => {+ beforeEach(() => {+ jest.mocked(copyToClipboard).mockResolvedValue(true)+ })++ afterEach(() => {+ cleanup()+ jest.mocked(copyToClipboard).mockReset()+ })++ test.each([+ { label: 'with a description', description: 'Tier request rollout' },+ { label: 'without a description', description: undefined },+ ])('copies a text name on click $label, without a toast', ({ description }) => {+ render(<QuillSceneStaticName name="my-flag-key" description={description} />)++ fireEvent.click(screen.getByText('my-flag-key'))++ expect(copyToClipboard).toHaveBeenCalledWith('my-flag-key', 'name', { silent: true })+ })++ test('confirms the copy in the tooltip, hiding the description', async () => {+ render(<QuillSceneStaticName name="my-flag-key" description="Tier request rollout" />)++ await userEvent.tab()+ expect(await screen.findByText('Click to copy')).toBeInTheDocument()+ await userEvent.click(screen.getByText('my-flag-key'))++ expect(await screen.findByText('Copied')).toBeInTheDocument()+ expect(screen.queryByText('Tier request rollout')).not.toBeInTheDocument()+ expect(screen.queryByText('Click to copy')).not.toBeInTheDocument()+ })++ test('does not confirm a copy that failed', async () => {+ jest.mocked(copyToClipboard).mockResolvedValue(false)+ render(<QuillSceneStaticName name="my-flag-key" />)++ await userEvent.tab()+ await userEvent.click(screen.getByText('my-flag-key'))++ expect(await screen.findByText('Click to copy')).toBeInTheDocument()+ expect(screen.queryByText('Copied')).not.toBeInTheDocument()+ })++ test('does not copy a name that is not plain text', () => {+ render(<QuillSceneStaticName name={<span>Custom name</span>} />)++ fireEvent.click(screen.getByText('Custom name'))++ expect(copyToClipboard).not.toHaveBeenCalled()+ })+})diff --git a/frontend/src/layout/scenes/components/QuillSceneStaticName.tsx b/frontend/src/layout/scenes/components/QuillSceneStaticName.tsxindex e45e68f9d2b6..f216efbcd2bf 100644--- a/frontend/src/layout/scenes/components/QuillSceneStaticName.tsx+++ b/frontend/src/layout/scenes/components/QuillSceneStaticName.tsx@@ -1,7 +1,11 @@-import type { ReactNode } from 'react'+import { type ReactNode, useEffect, useRef, useState } from 'react'import { Button, Tooltip, TooltipContent, TooltipTrigger, cn } from '@posthog/quill'+import { copyToClipboard } from 'lib/utils/copyToClipboard'++const COPIED_RESET_MS = 1500+export interface QuillSceneStaticNameProps {name: ReactNodeicon?: ReactNode@@ -12,17 +16,38 @@ export interface QuillSceneStaticNameProps {/*** A scene name nobody can rename, drawn like the trail's crumbs so the whole trail reads as one line. The scene- * description moves into its tooltip. `QuillSceneTrail` renders it as its last crumb and owns its offsets.+ * description moves into its tooltip. A plain text name copies to the clipboard on click, and the tooltip confirms it+ * in place instead of a toast. `QuillSceneTrail` renders it as its last crumb and owns its offsets.*/export function QuillSceneStaticName({ name, icon, description, className }: QuillSceneStaticNameProps): JSX.Element {+ const copyableName = typeof name === 'string' && name ? name : undefined+ const [copied, setCopied] = useState(false)+ // Clicking the trigger dismisses the tooltip, so it is controlled: `copied` holds it open over the dismissal+ // until the confirmation has had its moment.+ const [open, setOpen] = useState(false)+ const copiedTimeout = useRef<ReturnType<typeof setTimeout>>()+ useEffect(() => () => clearTimeout(copiedTimeout.current), [])+const crumb = (<ButtonnativeButton={false}render={<h1 />}- // Focusable only to reach the tooltip, so keyboard users can read the description too.- tabIndex={description ? 0 : undefined}+ // Focusable to copy the name or to reach the tooltip, so keyboard users can do both.+ tabIndex={copyableName || description ? 0 : undefined}+ onClick={+ copyableName+ ? async () => {+ if (await copyToClipboard(copyableName, 'name', { silent: true })) {+ setCopied(true)+ clearTimeout(copiedTimeout.current)+ copiedTimeout.current = setTimeout(() => setCopied(false), COPIED_RESET_MS)+ }+ }+ : undefined+ }className={cn(- 'm-0 min-w-0 cursor-default text-foreground hover:bg-transparent active:[translate:none]',+ 'm-0 min-w-0 text-foreground',+ !copyableName && 'cursor-default hover:bg-transparent active:[translate:none]',className)}data-attr="scene-name"@@ -36,14 +61,23 @@ export function QuillSceneStaticName({ name, icon, description, className }: Qui</Button>)- if (!description) {+ if (!description && !copyableName) {return crumb}return (- <Tooltip disableHoverablePopup>+ <Tooltip disableHoverablePopup open={open || copied} onOpenChange={setOpen}><TooltipTrigger delay={300} render={crumb} />- <TooltipContent className="max-w-80">{description}</TooltipContent>+ <TooltipContent className="max-w-80">+ {copied ? (+ <div role="status">Copied</div>+ ) : (+ <>+ {description && <div>{description}</div>}+ {copyableName && <div className="text-muted-foreground">Click to copy</div>}+ </>+ )}+ </TooltipContent></Tooltip>)}