Skip to content

Commit

Permalink
feat(PostActions): use tooltip to denote copy action
Browse files Browse the repository at this point in the history
  • Loading branch information
karrui committed Jul 7, 2023
1 parent dc5c59d commit d4d1391
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 26 deletions.
38 changes: 38 additions & 0 deletions src/features/posts/components/PostActions/CopyAction.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { Tooltip } from '@chakra-ui/react'
import { IconButton } from '@opengovsg/design-system-react'
import { useState, type MouseEventHandler, useEffect } from 'react'
import { BiLink } from 'react-icons/bi'

interface CopyActionProps {
postId: string
}

export const CopyAction = ({ postId }: CopyActionProps): JSX.Element => {
const [isOpen, setIsOpen] = useState(false)

useEffect(() => {
const timer = setTimeout(() => {
setIsOpen(false)
}, 2000)
return () => clearTimeout(timer)
}, [isOpen])

const handleCopyLink: MouseEventHandler = async (e) => {
setIsOpen(true)
e.stopPropagation()
const url = `${window.location.origin}/thread/${postId}`
await navigator.clipboard.writeText(url)
}

return (
<Tooltip label="Link copied!" hasArrow isOpen={isOpen}>
<IconButton
onMouseLeave={() => setIsOpen(false)}
data-value="post-action"
aria-label="Link to post"
icon={<BiLink fontSize="1.25rem" />}
onClick={handleCopyLink}
/>
</Tooltip>
)
}
32 changes: 6 additions & 26 deletions src/features/posts/components/PostActions/PostActions.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { Box, ButtonGroup } from '@chakra-ui/react'
import {
Button,
BxsHeart,
IconButton,
useToast,
} from '@opengovsg/design-system-react'
import { type MouseEventHandler, useState } from 'react'
import { BiHeart, BiLink } from 'react-icons/bi'
import { Button, BxsHeart } from '@opengovsg/design-system-react'
import { useState, type MouseEventHandler } from 'react'
import { BiHeart } from 'react-icons/bi'
import { useMe } from '~/features/me/api'
import { type RouterOutput, trpc } from '~/utils/trpc'
import { trpc, type RouterOutput } from '~/utils/trpc'
import { AddCommentAction } from './AddCommentAction'
import { CopyAction } from './CopyAction'
import { DeletePostAction } from './DeletePostAction'

export interface PostActionsProps {
Expand All @@ -21,10 +17,6 @@ export const PostActions = ({
}: PostActionsProps): JSX.Element => {
const { me } = useMe()

const toast = useToast({
status: 'info',
})

// Use local state to update the UI immediately on like/unlike
const [post, setPost] = useState(postProp)
const isOwnPost = me?.username === post.author.username
Expand Down Expand Up @@ -67,13 +59,6 @@ export const PostActions = ({
})
}

const handleCopyLink: MouseEventHandler = async (e) => {
e.stopPropagation()
const url = `${window.location.origin}/thread/${post.id}`
await navigator.clipboard.writeText(url)
toast({ description: 'Link copied', icon: <BiLink /> })
}

return (
<ButtonGroup
variant="clear"
Expand All @@ -99,12 +84,7 @@ export const PostActions = ({
{post._count.likes}
</Button>
<AddCommentAction post={post} onSuccess={incrementReplyCount} />
<IconButton
data-value="post-action"
aria-label="Link to post"
icon={<BiLink fontSize="1.25rem" />}
onClick={handleCopyLink}
/>
<CopyAction postId={post.id} />
{isOwnPost ? (
<DeletePostAction postId={post.id} />
) : (
Expand Down

0 comments on commit d4d1391

Please sign in to comment.