-
Notifications
You must be signed in to change notification settings - Fork 43
fix(sessions): Fix not copying URL from link-bubbles #2713
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
mp-hog
wants to merge
3
commits into
main
Choose a base branch
from
posthog-code/fix-conversation-copy-link
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+337
−11
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
34 changes: 34 additions & 0 deletions
34
packages/ui/src/features/editor/components/GithubRefChip.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| import { render, screen } from "@testing-library/react"; | ||
| import { describe, expect, it } from "vitest"; | ||
| import { GITHUB_REF_URL_ATTR, GithubRefChip } from "./GithubRefChip"; | ||
|
|
||
| describe("GithubRefChip", () => { | ||
| it("exposes its URL as a DOM attribute so the context menu can copy it", () => { | ||
| const href = "https://github.com/PostHog/posthog/pull/23985"; | ||
| const { container } = render( | ||
| <GithubRefChip href={href} kind="pr"> | ||
| PostHog/posthog#23985 | ||
| </GithubRefChip>, | ||
| ); | ||
|
|
||
| const carrier = container.querySelector(`[${GITHUB_REF_URL_ATTR}]`); | ||
| expect(carrier).not.toBeNull(); | ||
| expect(carrier?.getAttribute(GITHUB_REF_URL_ATTR)).toBe(href); | ||
| }); | ||
|
|
||
| it("lets a nested right-click target resolve the URL via closest()", () => { | ||
| const href = "https://github.com/PostHog/posthog/issues/42"; | ||
| render( | ||
| <GithubRefChip href={href} kind="issue"> | ||
| PostHog/posthog#42 | ||
| </GithubRefChip>, | ||
| ); | ||
|
|
||
| const label = screen.getByText("PostHog/posthog#42"); | ||
| expect( | ||
| label | ||
| .closest(`[${GITHUB_REF_URL_ATTR}]`) | ||
| ?.getAttribute(GITHUB_REF_URL_ATTR), | ||
| ).toBe(href); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
packages/ui/src/features/sessions/components/copyContextMenu.integration.test.tsx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,85 @@ | ||
| import { GithubRefChip } from "@posthog/ui/features/editor/components/GithubRefChip"; | ||
| import { | ||
| copyFromContextMenu, | ||
| getGithubRefUrlFromEventTarget, | ||
| resolveCopyText, | ||
| } from "@posthog/ui/features/sessions/components/copyContextTarget"; | ||
| import { ContextMenu, Theme } from "@radix-ui/themes"; | ||
| import { fireEvent, render, screen, waitFor } from "@testing-library/react"; | ||
| import userEvent from "@testing-library/user-event"; | ||
| import { useRef } from "react"; | ||
| import { describe, expect, it, vi } from "vitest"; | ||
|
|
||
| const PR_URL = "https://github.com/PostHog/posthog/pull/63995"; | ||
|
|
||
| // Radix's menu content mounts a scroll-area that observes resizes; jsdom lacks it. | ||
| if (typeof globalThis.ResizeObserver === "undefined") { | ||
| globalThis.ResizeObserver = class { | ||
| observe() {} | ||
| unobserve() {} | ||
| disconnect() {} | ||
| } as unknown as typeof ResizeObserver; | ||
| } | ||
|
|
||
| /** | ||
| * Mirrors the exact context-menu wiring in SessionView: a ContextMenu.Trigger | ||
| * whose child captures the right-clicked URL in a ref, and a "Copy" item that | ||
| * copies the captured URL (falling back to the text selection). | ||
| */ | ||
| function Harness() { | ||
| const copyTargetUrlRef = useRef<string | null>(null); | ||
| const handleContextMenu = (e: React.MouseEvent) => { | ||
| copyTargetUrlRef.current = getGithubRefUrlFromEventTarget(e.target); | ||
| }; | ||
| return ( | ||
| <Theme> | ||
| <ContextMenu.Root> | ||
| <ContextMenu.Trigger> | ||
| {/** biome-ignore lint/a11y/noStaticElementInteractions: test harness */} | ||
| <div onContextMenu={handleContextMenu}> | ||
| <span>The draft PR is up: </span> | ||
| <GithubRefChip href={PR_URL} kind="pr"> | ||
| PostHog/posthog#63995 | ||
| </GithubRefChip> | ||
| </div> | ||
| </ContextMenu.Trigger> | ||
| <ContextMenu.Content> | ||
| <ContextMenu.Item | ||
| onSelect={() => { | ||
| const url = copyTargetUrlRef.current; | ||
| const text = resolveCopyText( | ||
| url, | ||
| window.getSelection()?.toString(), | ||
| ); | ||
| if (!text) { | ||
| return; | ||
| } | ||
| copyFromContextMenu(text); | ||
| }} | ||
| > | ||
| Copy | ||
| </ContextMenu.Item> | ||
| </ContextMenu.Content> | ||
| </ContextMenu.Root> | ||
| </Theme> | ||
| ); | ||
| } | ||
|
|
||
| describe("conversation context-menu copy (integration)", () => { | ||
| it("copies the PR URL when right-clicking the chip and choosing Copy", async () => { | ||
| const writeText = vi.fn().mockResolvedValue(undefined); | ||
| Object.assign(navigator, { clipboard: { writeText } }); | ||
|
|
||
| render(<Harness />); | ||
|
|
||
| // Right-click the chip label, exactly as a user would. | ||
| const label = screen.getByText("PostHog/posthog#63995"); | ||
| fireEvent.contextMenu(label); | ||
|
|
||
| const copyItem = await screen.findByText("Copy"); | ||
| await userEvent.click(copyItem); | ||
|
|
||
| // The write is deferred until after the menu closes (focus race), so wait. | ||
| await waitFor(() => expect(writeText).toHaveBeenCalledWith(PR_URL)); | ||
| }); | ||
| }); |
127 changes: 127 additions & 0 deletions
127
packages/ui/src/features/sessions/components/copyContextTarget.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,127 @@ | ||
| import { describe, expect, it, vi } from "vitest"; | ||
| import { | ||
| copyFromContextMenu, | ||
| getGithubRefUrlFromEventTarget, | ||
| resolveCopyText, | ||
| } from "./copyContextTarget"; | ||
|
|
||
| function buildDom(): { | ||
| icon: HTMLElement; | ||
| label: HTMLElement; | ||
| chip: HTMLElement; | ||
| outside: HTMLElement; | ||
| } { | ||
| document.body.innerHTML = ` | ||
| <div id="conversation"> | ||
| <span data-github-ref-url="https://github.com/PostHog/posthog/pull/23985"> | ||
| <button id="chip"><svg id="icon"></svg><span id="label">PostHog/posthog#23985</span></button> | ||
| </span> | ||
| <p id="outside">just some prose</p> | ||
| </div>`; | ||
| return { | ||
| icon: document.getElementById("icon") as HTMLElement, | ||
| label: document.getElementById("label") as HTMLElement, | ||
| chip: document.getElementById("chip") as HTMLElement, | ||
| outside: document.getElementById("outside") as HTMLElement, | ||
| }; | ||
| } | ||
|
|
||
| const CHIP_URL = "https://github.com/PostHog/posthog/pull/23985"; | ||
|
|
||
| describe("getGithubRefUrlFromEventTarget", () => { | ||
| it.each<{ | ||
| name: string; | ||
| pick: (dom: ReturnType<typeof buildDom>) => EventTarget | null; | ||
| expected: string | null; | ||
| }>([ | ||
| { name: "a nested icon", pick: (dom) => dom.icon, expected: CHIP_URL }, | ||
| { name: "the label", pick: (dom) => dom.label, expected: CHIP_URL }, | ||
| { name: "non-chip prose", pick: (dom) => dom.outside, expected: null }, | ||
| { name: "a non-element target", pick: () => null, expected: null }, | ||
| ])("resolves $expected when the target is $name", ({ pick, expected }) => { | ||
| expect(getGithubRefUrlFromEventTarget(pick(buildDom()))).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| describe("resolveCopyText", () => { | ||
| it.each<{ | ||
| name: string; | ||
| url: string | null; | ||
| selection: string | null | undefined; | ||
| expected: string | null; | ||
| }>([ | ||
| { | ||
| name: "prefers a captured chip URL over the text selection", | ||
| url: CHIP_URL, | ||
| selection: "selected", | ||
| expected: CHIP_URL, | ||
| }, | ||
| { | ||
| name: "falls back to the text selection when there is no chip URL", | ||
| url: null, | ||
| selection: "selected words", | ||
| expected: "selected words", | ||
| }, | ||
| { | ||
| name: "returns null for an empty selection and no chip URL", | ||
| url: null, | ||
| selection: "", | ||
| expected: null, | ||
| }, | ||
| { | ||
| name: "returns null for an undefined selection and no chip URL", | ||
| url: null, | ||
| selection: undefined, | ||
| expected: null, | ||
| }, | ||
| ])("$name", ({ url, selection, expected }) => { | ||
| expect(resolveCopyText(url, selection)).toBe(expected); | ||
| }); | ||
| }); | ||
|
|
||
| describe("copyFromContextMenu", () => { | ||
| it("defers the clipboard write until after the current task (focus race)", async () => { | ||
| const writeText = vi.fn().mockResolvedValue(undefined); | ||
| Object.assign(navigator, { clipboard: { writeText } }); | ||
|
|
||
| copyFromContextMenu("https://github.com/PostHog/posthog/pull/1"); | ||
|
|
||
| // Not written synchronously while the menu is still dismissing. | ||
| expect(writeText).not.toHaveBeenCalled(); | ||
| await vi.waitFor(() => | ||
| expect(writeText).toHaveBeenCalledWith( | ||
| "https://github.com/PostHog/posthog/pull/1", | ||
| ), | ||
| ); | ||
| }); | ||
|
|
||
| it("invokes onSuccess after the deferred write resolves", async () => { | ||
| Object.assign(navigator, { | ||
| clipboard: { writeText: vi.fn().mockResolvedValue(undefined) }, | ||
| }); | ||
| const onSuccess = vi.fn(); | ||
| const onError = vi.fn(); | ||
|
|
||
| copyFromContextMenu("text", { onSuccess, onError }); | ||
|
|
||
| await vi.waitFor(() => expect(onSuccess).toHaveBeenCalledTimes(1)); | ||
| expect(onError).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it("invokes onError when the deferred write rejects", async () => { | ||
| Object.assign(navigator, { | ||
| clipboard: { | ||
| writeText: vi | ||
| .fn() | ||
| .mockRejectedValue(new Error("Document is not focused")), | ||
| }, | ||
| }); | ||
| const onSuccess = vi.fn(); | ||
| const onError = vi.fn(); | ||
|
|
||
| copyFromContextMenu("text", { onSuccess, onError }); | ||
|
|
||
| await vi.waitFor(() => expect(onError).toHaveBeenCalledTimes(1)); | ||
| expect(onSuccess).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
52 changes: 52 additions & 0 deletions
52
packages/ui/src/features/sessions/components/copyContextTarget.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| import { GITHUB_REF_URL_ATTR } from "@posthog/ui/features/editor/components/GithubRefChip"; | ||
|
|
||
| /** | ||
| * Resolve the GitHub PR/issue URL the context menu was opened on, if the | ||
| * right-click landed inside a {@link GithubRefChip}. Returns `null` for any | ||
| * other target (prose, file chips, empty space, non-elements). | ||
| */ | ||
| export function getGithubRefUrlFromEventTarget( | ||
| target: EventTarget | null, | ||
| ): string | null { | ||
| // `Element`, not `HTMLElement`: the chip icon renders as an <svg>, whose | ||
| // right-click target is an SVGElement that still supports `closest()`. | ||
| if (!(target instanceof Element)) return null; | ||
| return ( | ||
| target.closest<HTMLElement>(`[${GITHUB_REF_URL_ATTR}]`)?.dataset | ||
| .githubRefUrl ?? null | ||
| ); | ||
| } | ||
|
|
||
| /** | ||
| * Decide what the conversation "Copy" action should put on the clipboard: a | ||
| * captured chip URL wins, otherwise fall back to the current text selection. | ||
| * Returns `null` when there is nothing to copy. | ||
| */ | ||
| export function resolveCopyText( | ||
| capturedUrl: string | null, | ||
| selection: string | null | undefined, | ||
| ): string | null { | ||
| return capturedUrl ?? (selection ? selection : null); | ||
| } | ||
|
|
||
| /** | ||
| * Copy text to the clipboard from a context-menu selection. | ||
| * | ||
| * The write is deferred to a later task on purpose. When a Radix | ||
| * `ContextMenu.Item` is selected, the menu's focus scope is being torn down and | ||
| * the document is momentarily not focused — calling `navigator.clipboard.writeText` | ||
| * synchronously there rejects with "Document is not focused" in Electron/Chromium, | ||
| * so the clipboard is left unchanged. Deferring lets the menu finish closing and | ||
| * focus return to the document before we write. | ||
| */ | ||
| export function copyFromContextMenu( | ||
| text: string, | ||
| callbacks: { onSuccess?: () => void; onError?: () => void } = {}, | ||
| ): void { | ||
| setTimeout(() => { | ||
| navigator.clipboard | ||
| .writeText(text) | ||
| .then(() => callbacks.onSuccess?.()) | ||
| .catch(() => callbacks.onError?.()); | ||
| }, 0); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.