Skip to content

Commit

Permalink
feat: log connection errors & add spinner (#107)
Browse files Browse the repository at this point in the history
* chore: connection errors and spinner

* chore: dialing -> dialling

* chore: connecting text during dial

* chore: move spinner to new component

* chore: fmt svg props
  • Loading branch information
dozyio authored Dec 11, 2023
1 parent 28ee7ba commit 5c2d93e
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
28 changes: 28 additions & 0 deletions js-peer/src/components/spinner.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
export default function Spinner() {
return (
<svg
className="inline-block text-gray-300 animate-spin"
viewBox="0 0 64 64"
fill="none"
xmlns="http://www.w3.org/2000/svg"
width="20"
height="20"
>
<path
d="M32 3C35.8083 3 39.5794 3.75011 43.0978 5.20749C46.6163 6.66488 49.8132 8.80101 52.5061 11.4939C55.199 14.1868 57.3351 17.3837 58.7925 20.9022C60.2499 24.4206 61 28.1917 61 32C61 35.8083 60.2499 39.5794 58.7925 43.0978C57.3351 46.6163 55.199 49.8132 52.5061 52.5061C49.8132 55.199 46.6163 57.3351 43.0978 58.7925C39.5794 60.2499 35.8083 61 32 61C28.1917 61 24.4206 60.2499 20.9022 58.7925C17.3837 57.3351 14.1868 55.199 11.4939 52.5061C8.801 49.8132 6.66487 46.6163 5.20749 43.0978C3.7501 39.5794 3 35.8083 3 32C3 28.1917 3.75011 24.4206 5.2075 20.9022C6.66489 17.3837 8.80101 14.1868 11.4939 11.4939C14.1868 8.80099 17.3838 6.66487 20.9022 5.20749C24.4206 3.7501 28.1917 3 32 3L32 3Z"
stroke="white"
strokeWidth="5"
strokeLinecap="round"
strokeLinejoin="round"
></path>
<path
d="M32 3C36.5778 3 41.0906 4.08374 45.1692 6.16256C49.2477 8.24138 52.7762 11.2562 55.466 14.9605C58.1558 18.6647 59.9304 22.9531 60.6448 27.4748C61.3591 31.9965 60.9928 36.6232 59.5759 40.9762"
stroke="currentColor"
strokeWidth="5"
strokeLinecap="round"
strokeLinejoin="round"
className="text-indigo-500"
></path>
</svg>
)
}
21 changes: 18 additions & 3 deletions js-peer/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ import Image from 'next/image'
import { multiaddr } from '@multiformats/multiaddr'
import { connectToMultiaddr } from '../lib/libp2p'
import { useListenAddressesContext } from '../context/listen-addresses-ctx'
import Spinner from '@/components/spinner'

export default function Home() {
const { libp2p } = useLibp2pContext()
const { peerStats, setPeerStats } = usePeerContext()
const { listenAddresses, setListenAddresses } = useListenAddressesContext()
const [maddr, setMultiaddr] = useState('')
const [dialling, setDialling] = useState(false)
const [err, setErr] = useState('')

useEffect(() => {
const interval = setInterval(() => {
Expand Down Expand Up @@ -78,17 +81,26 @@ export default function Home() {

const handleConnectToMultiaddr = useCallback(
async (e: React.MouseEvent<HTMLButtonElement>) => {
setErr('')

if (!maddr) {
return
}

setDialling(true)

try {
const connection = await connectToMultiaddr(libp2p)(multiaddr(maddr))
console.log('connection: ', connection)

return connection
} catch (e) {
} catch (e: any) {
if (e && e.message) {
setErr(e.message)
}
console.error(e)
} finally {
setDialling(false)
}
},
[libp2p, maddr],
Expand Down Expand Up @@ -163,11 +175,14 @@ export default function Home() {
</div>
<button
type="button"
className="rounded-md bg-indigo-600 my-2 py-2 px-3 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
className={"rounded-md bg-indigo-600 my-2 py-2 px-3 text-sm font-semibold text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600" + (dialling ? ' cursor-not-allowed' : '')}
onClick={handleConnectToMultiaddr}
disabled={dialling}
>
Connect to multiaddr
{dialling && <Spinner />}{' '}
Connect{dialling && 'ing'} to multiaddr
</button>
{err && <p className="text-red-500">{err}</p>}
</div>

<div className="my-4 inline-flex items-center text-xl">
Expand Down

1 comment on commit 5c2d93e

@vercel
Copy link

@vercel vercel bot commented on 5c2d93e Dec 11, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.