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

refactor: Replace useClipboardCopy from dcl-gatsby #1051

Merged
merged 3 commits into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
1 change: 1 addition & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
"autoprefixer": "^10.4.4",
"chart.js": "^3.8.2",
"classnames": "^2.3.2",
"clipboard-copy": "^4.0.1",
"core-js": "^3.21.1",
"decentraland-gatsby": "^5.67.2",
"decentraland-ui": "^3.102.0",
Expand Down
10 changes: 5 additions & 5 deletions src/components/Error/ErrorMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ import React, { useCallback, useState } from 'react'
import classNames from 'classnames'
import Link from 'decentraland-gatsby/dist/components/Text/Link'
import Markdown from 'decentraland-gatsby/dist/components/Text/Markdown'
import useClipboardCopy from 'decentraland-gatsby/dist/hooks/useClipboardCopy'
import useFormatMessage from 'decentraland-gatsby/dist/hooks/useFormatMessage'
import { Button } from 'decentraland-ui/dist/components/Button/Button'

import useClipboardCopy from '../../hooks/useClipboardCopy'
import Time from '../../utils/date/Time'
import ErrorNotice from '../Icon/ErrorNotice'

Expand All @@ -19,16 +19,16 @@ interface Props {

export default function ErrorMessage({ label, errorMessage }: Props) {
const t = useFormatMessage()
const [copied, state] = useClipboardCopy(Time.Second)
const { copiedValue, copy } = useClipboardCopy(Time.Second)
const [open, setOpen] = useState(false)

const toggleHandler = () => {
setOpen(!open)
}

const handleCopy = useCallback(() => {
state.copy(errorMessage)
}, [errorMessage, state])
copy(errorMessage)
}, [errorMessage, copy])

return (
<div className="ErrorMessage__Container">
Expand All @@ -43,7 +43,7 @@ export default function ErrorMessage({ label, errorMessage }: Props) {
<div className="ErrorMessage__Message">
<pre>{errorMessage}</pre>
<Button className={classNames('Button', 'ErrorMessage__Copy')} primary size="small" onClick={handleCopy}>
<span>{copied ? t('error.message.copied') : t('error.message.copy')}</span>
<span>{copiedValue ? t('error.message.copied') : t('error.message.copy')}</span>
</Button>
</div>
<Markdown className="ErrorMessage__CallForAction">{t('error.message.call_for_action')}</Markdown>
Expand Down
10 changes: 5 additions & 5 deletions src/components/Modal/SuccessModal.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React, { useCallback } from 'react'

import classNames from 'classnames'
import useClipboardCopy from 'decentraland-gatsby/dist/hooks/useClipboardCopy'
import useFormatMessage from 'decentraland-gatsby/dist/hooks/useFormatMessage'
import { Button } from 'decentraland-ui/dist/components/Button/Button'
import { Close } from 'decentraland-ui/dist/components/Close/Close'
import { Header } from 'decentraland-ui/dist/components/Header/Header'
import { Modal, ModalProps } from 'decentraland-ui/dist/components/Modal/Modal'

import { JOIN_DISCORD_URL } from '../../entities/Proposal/utils'
import useClipboardCopy from '../../hooks/useClipboardCopy'
import Time from '../../utils/date/Time'
import Text from '../Common/Text/Text'

Expand All @@ -33,12 +33,12 @@ export function SuccessModal({
...props
}: SuccessModalProps) {
const t = useFormatMessage()
const [copied, state] = useClipboardCopy(Time.Second)
const { copiedValue, copy } = useClipboardCopy(Time.Second)
const handleCopy = useCallback(() => {
if (linkToCopy) {
state.copy(linkToCopy)
copy(linkToCopy)
}
}, [linkToCopy, state])
}, [linkToCopy, copy])

return (
<Modal
Expand Down Expand Up @@ -110,7 +110,7 @@ export function SuccessModal({
primary
size="small"
>
{copied ? t('modal.success.link_copied_label') : t('modal.success.copy_link_label')}
{copiedValue ? t('modal.success.link_copied_label') : t('modal.success.copy_link_label')}
</Button>
</div>
)}
Expand Down
34 changes: 34 additions & 0 deletions src/hooks/useClipboardCopy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { useCallback, useEffect, useState } from 'react'

import clipboardCopy from 'clipboard-copy'

export default function useClipboardCopy(timeout?: number) {
const [copiedValue, setCopiedValue] = useState<string | null>(null)

const copy = useCallback((value: unknown) => {
const copiedValue = String(value ?? '')
clipboardCopy(copiedValue)
setCopiedValue(copiedValue)
}, [])

const clear = useCallback(() => setCopiedValue(null), [])

useEffect(() => {
let copyTimeout: null | ReturnType<typeof setTimeout> = null
if (copiedValue && timeout && timeout > 0) {
copyTimeout = setTimeout(() => clear(), timeout)
}

return () => {
if (copyTimeout) {
clearTimeout(copyTimeout)
}
}
}, [clear, copiedValue, timeout])

return {
copiedValue,
copy,
clear,
}
}
8 changes: 4 additions & 4 deletions src/hooks/useForumConnect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { useCallback, useEffect, useState } from 'react'

import useAuthContext from 'decentraland-gatsby/dist/context/Auth/useAuthContext'
import useTrackContext from 'decentraland-gatsby/dist/context/Track/useTrackContext'
import useClipboardCopy from 'decentraland-gatsby/dist/hooks/useClipboardCopy'
import useSign from 'decentraland-gatsby/dist/hooks/useSign'

import { Governance } from '../clients/Governance'
Expand All @@ -13,6 +12,7 @@ import { DISCOURSE_API } from '../entities/User/utils'
import { openUrl } from '../helpers'
import Time from '../utils/date/Time'

import useClipboardCopy from './useClipboardCopy'
import useTimer from './useTimer'

export const THREAD_URL = `${DISCOURSE_API}${
Expand All @@ -25,7 +25,7 @@ export default function useForumConnect() {
const [user, userState] = useAuthContext()
const track = useTrackContext()
const [sign, signState] = useSign(user, userState.provider)
const [copied, clipboardState] = useClipboardCopy(Time.Second)
const { copy } = useClipboardCopy(Time.Second)
const [signatureResolution, setSignatureResolution] = useState<{
resolve: (value: unknown) => void
reject: (reason?: unknown) => void
Expand Down Expand Up @@ -80,9 +80,9 @@ export default function useForumConnect() {
const copyMessageToClipboard = useCallback(() => {
const { message, signature } = sign
if (message && signature) {
clipboardState.copy(`${message}\nSignature: ${signature}`)
copy(`${message}\nSignature: ${signature}`)
}
}, [clipboardState, sign])
}, [copy, sign])

const openThread = () => {
openUrl(THREAD_URL)
Expand Down