Skip to content
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

feat: copy shortlink micro interaction #178

Merged
merged 1 commit into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions apps/masterbots.ai/app/globals.css
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@
--link: 208, 100%, 60%;
--mirage: 217, 33%, 17%;
--iron: 240, 6%, 90%;

--mb-gradient: linear-gradient(
21deg,
rgba(16, 171, 255, 0.7),
rgba(27, 234, 189, 0.6)
);
}

.dark {
Expand Down Expand Up @@ -170,11 +176,7 @@
/* Input specific styles */
.gradient-input {
position: relative; /* Set to relative for proper positioning of child elements */
background: linear-gradient(
21deg,
#10abff,
#1beabd
); /* Gradient background */
background: var(--mb-gradient);
padding: 2px; /* Padding around the input for border effect */
display: inline-block; /* Default display to inline for inline fields */
border-radius: 9999em; /* Circular border radius */
Expand Down
41 changes: 29 additions & 12 deletions apps/masterbots.ai/components/shared/copy-shortlink.tsx
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'
Comment on lines +3 to +6
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider grouping imports from the same module to clean up the import statements.

- import { Check, CopyIcon, LoaderCircle } from 'lucide-react'
- import { useAsyncFn } from 'react-use'
+ import { Check, CopyIcon, LoaderCircle } from 'lucide-react';
+ import { useAsyncFn } from 'react-use';

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
import { Check, CopyIcon, LoaderCircle } from 'lucide-react'
import { shorten } from '@/app/actions'
import { useAsyncFn } from 'react-use'
import { cn } from '@/lib/utils'
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)
})
Comment on lines +15 to +20
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ensure proper error handling in the asynchronous function used to fetch and copy the short link.

  const [shortlink, getShortlink] = useAsyncFn(async () => {
    try {
      const formData = new FormData();
      formData.set('url', url);
      const { shortLink } = await shorten({}, formData);
      await navigator.clipboard.writeText(shortLink);
      return shortLink;
    } catch (error) {
      console.error('Failed to fetch and copy shortlink:', error);
      throw error; // or handle error appropriately
    }
  });

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
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 [shortlink, getShortlink] = useAsyncFn(async () => {
try {
const formData = new FormData()
formData.set('url', url)
const { shortLink } = await shorten({}, formData)
await navigator.clipboard.writeText(shortLink)
return shortLink
} catch (error) {
console.error('Failed to fetch and copy shortlink:', error)
throw error; // or handle error appropriately
}
})


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}
/>
Comment on lines +29 to +43
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The dynamic icon switching logic is clear and concise. However, consider adding a tooltip or aria-label for accessibility, providing context about the icon's function.

  <Icon
    className={cn('pointer transition-all', shortlink.loading && 'animate-spin')}
    onClick={handleIconClick}
    size={15}
+   aria-label={shortlink.value ? 'Copy successful' : shortlink.loading ? 'Loading' : 'Copy link'}
  />

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
const Icon = shortlink.value
? Check
: shortlink.loading
? LoaderCircle
: CopyIcon
return (
<Icon
className={cn(
'pointer transition-all',
shortlink.loading && 'animate-spin'
)}
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}
aria-label={shortlink.value ? 'Copy successful' : shortlink.loading ? 'Loading' : 'Copy link'}
/>

)
}
2 changes: 1 addition & 1 deletion apps/masterbots.ai/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"framer-motion": "^10.16.12",
"geist": "^1.1.0",
"lodash": "^4.17.21",
"lucide-react": "^0.297.0",
"lucide-react": "latest",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using the "latest" tag for lucide-react can lead to unexpected behavior if the library introduces breaking changes. Consider using a more specific version range.

- "lucide-react": "latest",
+ "lucide-react": "^current.stable.version",

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation.

Suggested change
"lucide-react": "latest",
"lucide-react": "^current.stable.version",

"nanoid": "^3.0.0",
"next": "latest",
"next-themes": "^0.2.1",
Expand Down
Binary file modified bun.lockb
Binary file not shown.
5 changes: 5 additions & 0 deletions packages/config-tailwind/tailwind.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ const config: Omit<Config, "content"> = {
},
to: { opacity: 0 },
},
spin: {
'0%': { transform: 'rotate(0deg)' },
'100%': { transform: 'rotate(360deg)' },
spin: 'spin 1s linear infinite'
}
},
animation: {
"accordion-down": "accordion-down 0.2s ease-out",
Expand Down
Loading