An open source design system that's fully customizable and agent ready
TypeScript
refactor(Comments): the copy press owns the link-copied reset, through useClipboard, not an Effect (#9864)
Cindy Zhang830f87528184
diff --git a/packages/lab/src/Comments/CommentActions.tsx b/packages/lab/src/Comments/CommentActions.tsxindex dcd53c118b..d29209004c 100644--- a/packages/lab/src/Comments/CommentActions.tsx+++ b/packages/lab/src/Comments/CommentActions.tsx@@ -4,15 +4,15 @@/*** @file CommentActions.tsx- * @input Uses React, React DOM flushSync, StyleX, IconButton, Button, Tooltip,- * ToggleButton, OverflowList, DropdownMenu, Popover, Text+ * @input Uses React, React DOM flushSync, StyleX, useClipboard, IconButton,+ * Button, Tooltip, ToggleButton, OverflowList, DropdownMenu, Popover, Text* @output Exports CommentActions (a comment's header actions), ReactionSummary* (its reaction toggles), and the action-lookup helpers rows use for focus* hand-offs* @position Internal parts of a comment row in the Comments family*/-import {useCallback, useEffect, useRef, useState, type ReactNode} from 'react';+import {useCallback, useRef, useState, type ReactNode} from 'react';import {flushSync} from 'react-dom';import * as stylex from '@stylexjs/stylex';import {Button} from '@astryxdesign/core/Button';@@ -20,6 +20,7 @@ import {DropdownMenu,type DropdownMenuOption,} from '@astryxdesign/core/DropdownMenu';+import {useClipboard} from '@astryxdesign/core/hooks';import {Icon} from '@astryxdesign/core/Icon';import {IconButton} from '@astryxdesign/core/IconButton';import {HStack, VStack} from '@astryxdesign/core/Layout';@@ -35,7 +36,6 @@ import {CommentIcon} from './icons';import {commentAnchorId, type AnyComment} from './utils';import type {CommentReaction, CommentReactionOption} from './types';-const LINK_COPIED_RESET_MS = 2_000;const MORE_MENU_MIN_WIDTH = `calc(${spacingVars['--spacing-8']} * 5)`;const styles = stylex.create({@@ -256,7 +256,7 @@ export function CommentActions({resolveAction?: ResolveAction;}) {const t = useTranslator();- const [isLinkCopied, setIsLinkCopied] = useState(false);+ const {copy: copyToClipboard, isCopied: isLinkCopied} = useClipboard();const [isLinkTooltipOpen, setIsLinkTooltipOpen] = useState(false);const [isPickerOpen, setIsPickerOpen] = useState(false);const [collapsed, setCollapsed] = useState(NO_COLLAPSED_ACTIONS);@@ -276,30 +276,8 @@ export function CommentActions({<CommentIcon name="copyLink" />);- const copyCommentLink = async () => {- if (navigator.clipboard == null) {- return;- }- try {- await navigator.clipboard.writeText(- new URL(href, window.location.href).href,- );- setIsLinkCopied(true);- } catch {- setIsLinkCopied(false);- }- };-- useEffect(() => {- if (!isLinkCopied) {- return;- }- const timer = window.setTimeout(- () => setIsLinkCopied(false),- LINK_COPIED_RESET_MS,- );- return () => window.clearTimeout(timer);- }, [isLinkCopied]);+ const copyCommentLink = () =>+ copyToClipboard(new URL(href, window.location.href).href);const changePicker = (isOpen: boolean) => {setIsPickerOpen(isOpen);diff --git a/packages/lab/src/Comments/Comments.test.tsx b/packages/lab/src/Comments/Comments.test.tsxindex bc943d5c69..1d7b7584e9 100644--- a/packages/lab/src/Comments/Comments.test.tsx+++ b/packages/lab/src/Comments/Comments.test.tsx@@ -957,6 +957,38 @@ describe('Comments threads', () => {).toBeTruthy();});+ it('keeps the copied label for the full time after a second copy', async () => {+ vi.useFakeTimers({toFake: ['setTimeout', 'clearTimeout']});+ const wait = (ms: number) => act(() => vi.advanceTimersByTimeAsync(ms));+ const copyButton = (name: string) => screen.getByRole('button', {name});+ try {+ render(+ <Sized>+ <Comments+ comments={[{...COMMENTS[0], replies: []}]}+ currentUser={OTHER}+ />+ </Sized>,+ );+ await click(copyButton('Copy comment link'));+ await wait(0);+ expect(copyButton('Comment link copied')).toBeTruthy();++ await wait(1500);+ await click(copyButton('Comment link copied'));+ await wait(0);+ // 2.5 s after the first copy and 1 s after the second one.+ await wait(1000);+ expect(writeText).toHaveBeenCalledTimes(2);+ expect(copyButton('Comment link copied')).toBeTruthy();++ await wait(1000);+ expect(copyButton('Copy comment link')).toBeTruthy();+ } finally {+ vi.useRealTimers();+ }+ });+it('collapses header actions into the more menu when space runs low', async () => {overflowControl.collapsed = new Set(['reply', 'link']);const onSubmit = vi.fn();