-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: copy shortlink micro interaction (#178)
- Loading branch information
1 parent
b5cd5af
commit f179424
Showing
5 changed files
with
42 additions
and
18 deletions.
There are no files selected for viewing
This file contains 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 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 |
---|---|---|
@@ -1,28 +1,45 @@ | ||
'use client' | ||
|
||
import { CopyIcon } from 'lucide-react' | ||
import { Check, CopyIcon, LoaderCircle } from 'lucide-react' | ||
import { shorten } from '@/app/actions' | ||
import { useAsyncFn } from 'react-use' | ||
import { cn } from '@/lib/utils' | ||
|
||
export default function Shortlink() { | ||
// for local dev | ||
const url = window.location.href.replace( | ||
'http://localhost:3000', | ||
'https://dev.masterbots.ai' | ||
'https://alpha.masterbots.ai' | ||
) | ||
|
||
const [shortlink, getShortlink] = useAsyncFn(async () => { | ||
const formData = new FormData() | ||
formData.set('url', url) | ||
const { shortLink } = await shorten({}, formData) | ||
return navigator.clipboard.writeText(shortLink).then(() => shortLink) | ||
}) | ||
|
||
const handleIconClick = async (e: any) => { | ||
// Stop propagation to prevent form submission when clicking on the icon | ||
e.preventDefault() | ||
e.stopPropagation() | ||
const formData = new FormData() | ||
formData.set('url', url) | ||
const { shortLink } = await shorten({}, formData) | ||
navigator.clipboard | ||
.writeText(shortLink) | ||
.then(() => { console.log('Shortlink copied to clipboard'); }) | ||
.catch(error => | ||
{ console.error('Error copying shortlink to clipboard: ', error); } | ||
) | ||
getShortlink() | ||
} | ||
|
||
return <CopyIcon className="pointer" onClick={handleIconClick} size={15} /> | ||
const Icon = shortlink.value | ||
? Check | ||
: shortlink.loading | ||
? LoaderCircle | ||
: CopyIcon | ||
|
||
return ( | ||
<Icon | ||
className={cn( | ||
'pointer transition-all', | ||
shortlink.loading && 'animate-spin' | ||
)} | ||
onClick={handleIconClick} | ||
size={15} | ||
/> | ||
) | ||
} |
This file contains 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 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