From 37b8b3c5bc228b6e215c68941c813196bcc50561 Mon Sep 17 00:00:00 2001 From: mbidenaio Date: Tue, 15 Aug 2023 17:06:40 +0500 Subject: [PATCH] Add button to copy flip info to clipboard --- pages/flips/edit.js | 43 +++++++++++++++++++++++++++++---------- pages/flips/new.js | 43 +++++++++++++++++++++++++++++---------- shared/utils/clipboard.js | 10 +++++++++ 3 files changed, 74 insertions(+), 22 deletions(-) diff --git a/pages/flips/edit.js b/pages/flips/edit.js index 3947a800..ba98fe11 100644 --- a/pages/flips/edit.js +++ b/pages/flips/edit.js @@ -52,8 +52,9 @@ import {useEpoch} from '../../shared/providers/epoch-context' import {BadFlipDialog} from '../../screens/validation/components' import {InfoIcon, RefreshIcon} from '../../shared/components/icons' import {useTrackTx} from '../../screens/ads/hooks' -import {useFailToast} from '../../shared/hooks/use-toast' +import {useFailToast, useSuccessToast} from '../../shared/hooks/use-toast' import {eitherState} from '../../shared/utils/utils' +import {writeTextToClipboard} from '../../shared/utils/clipboard' export default function EditFlipPage() { const {t, i18n} = useTranslation() @@ -69,6 +70,7 @@ export default function EditFlipPage() { const [, {waitFlipsUpdate}] = useIdentity() const failToast = useFailToast() + const successToast = useSuccessToast() const [didShowShuffleAdversarial, setDidShowShuffleAdversarial] = useState( false @@ -435,16 +437,35 @@ export default function EditFlipPage() { )} {is('submit') && ( - { - publishDrawerDisclosure.onOpen() - }} - > - {t('Submit')} - + <> + + (await writeTextToClipboard( + JSON.stringify({ + originalOrder, + order, + images: protectedImages, + }) + )) + ? successToast({ + title: t('Copied'), + }) + : failToast('Failed to copy') + } + > + {t('Copy to clipboard')} + + { + publishDrawerDisclosure.onOpen() + }} + > + {t('Submit')} + + )} diff --git a/pages/flips/new.js b/pages/flips/new.js index d2347725..b0619ce6 100644 --- a/pages/flips/new.js +++ b/pages/flips/new.js @@ -53,8 +53,9 @@ import {useEpoch} from '../../shared/providers/epoch-context' import {BadFlipDialog} from '../../screens/validation/components' import {InfoIcon, RefreshIcon} from '../../shared/components/icons' import {useTrackTx} from '../../screens/ads/hooks' -import {useFailToast} from '../../shared/hooks/use-toast' +import {useFailToast, useSuccessToast} from '../../shared/hooks/use-toast' import {eitherState} from '../../shared/utils/utils' +import {writeTextToClipboard} from '../../shared/utils/clipboard' export default function NewFlipPage() { const {t, i18n} = useTranslation() @@ -67,6 +68,7 @@ export default function NewFlipPage() { const [, {waitFlipsUpdate}] = useIdentity() const failToast = useFailToast() + const successToast = useSuccessToast() const [didShowShuffleAdversarial, setDidShowShuffleAdversarial] = useState( false @@ -444,16 +446,35 @@ export default function NewFlipPage() { )} {is('submit') && ( - { - publishDrawerDisclosure.onOpen() - }} - > - {t('Submit')} - + <> + + (await writeTextToClipboard( + JSON.stringify({ + originalOrder, + order, + images: protectedImages, + }) + )) + ? successToast({ + title: t('Copied'), + }) + : failToast('Failed to copy') + } + > + {t('Copy to clipboard')} + + { + publishDrawerDisclosure.onOpen() + }} + > + {t('Submit')} + + )} diff --git a/shared/utils/clipboard.js b/shared/utils/clipboard.js index 2aadb2a6..648ada72 100644 --- a/shared/utils/clipboard.js +++ b/shared/utils/clipboard.js @@ -31,3 +31,13 @@ export async function writeImageURLToClipboard(url) { const data = [new ClipboardItem({[blob.type]: blob})] return navigator.clipboard.write(data) } + +export async function writeTextToClipboard(text) { + try { + await navigator.clipboard.writeText(text) + return true + } catch (err) { + console.error('Failed to copy: ', err) + return false + } +}