From 9f084518eed061b370e6d0a0b93aca76e897b9f2 Mon Sep 17 00:00:00 2001 From: Alejandro Gomez Date: Fri, 6 Jan 2023 16:22:02 +0100 Subject: [PATCH] feat: copy npub on profile --- src/pages/ProfilePage.css | 23 +++++++++++++++++++++++ src/pages/ProfilePage.js | 20 +++++++++++++++++--- src/useCopy.js | 20 ++++++++++++++++++++ 3 files changed, 60 insertions(+), 3 deletions(-) create mode 100644 src/useCopy.js diff --git a/src/pages/ProfilePage.css b/src/pages/ProfilePage.css index 81b656bca..5cb0bd44b 100644 --- a/src/pages/ProfilePage.css +++ b/src/pages/ProfilePage.css @@ -10,6 +10,29 @@ white-space: pre-wrap; } +.profile .name { + align-items: flex-start; +} + +.profile .name h2 { + margin: 0; +} + +.profile .npub-container { + user-select: none; + cursor: pointer; +} + +.profile .npub { + font-family: monospace; + font-size: 14px; + margin: 0; + width: 18em; + overflow: hidden; + white-space: nowrap; + text-overflow: ellipsis; +} + .profile .avatar { width: 256px; height: 256px; diff --git a/src/pages/ProfilePage.js b/src/pages/ProfilePage.js index b4f343f66..975d08917 100644 --- a/src/pages/ProfilePage.js +++ b/src/pages/ProfilePage.js @@ -3,7 +3,7 @@ import { useMemo, useRef, useState } from "react"; import { useDispatch, useSelector } from "react-redux"; import { bech32 } from "bech32"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; -import { faQrcode } from "@fortawesome/free-solid-svg-icons"; +import { faQrcode, faCopy, faCheck } from "@fortawesome/free-solid-svg-icons"; import { useParams } from "react-router-dom"; import useProfile from "../feed/ProfileFeed"; @@ -18,6 +18,7 @@ import VoidUpload from "../feed/VoidUpload"; import { openFile, parseId } from "../Util"; import Timeline from "../element/Timeline"; import { extractLinks } from '../Text' +import { useCopy } from '../useCopy' export default function ProfilePage() { const dispatch = useDispatch(); @@ -28,6 +29,7 @@ export default function ProfilePage() { const loginPubKey = useSelector(s => s.login.publicKey); const isMe = loginPubKey === id; const qrRef = useRef(); + const { copy, copied, error } = useCopy() const [name, setName] = useState(""); const [picture, setPicture] = useState(""); @@ -172,8 +174,20 @@ export default function ProfilePage() { function details() { return ( <> -
-

{name}

+
+
+

{name}

+
copy(params.id)}> + +

+ {params.id} +

+
+
diff --git a/src/useCopy.js b/src/useCopy.js new file mode 100644 index 000000000..5883c34f0 --- /dev/null +++ b/src/useCopy.js @@ -0,0 +1,20 @@ +import { useState } from 'react' + +export const useCopy = (timeout = 2000) => { + const [error, setError] = useState(false); + const [copied, setCopied] = useState(false); + + const copy = async (text) => { + try { + await navigator.clipboard.writeText(text); + setCopied(true) + setError(false) + } catch (error) { + setError(true) + } + + setTimeout(() => setCopied(false), timeout); + }; + + return { error, copied, copy }; +};