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: add connection #969

Merged
merged 7 commits into from
Mar 19, 2019
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@
npm-debug.log*
yarn-debug.log*
yarn-error.log*

.vscode
4 changes: 3 additions & 1 deletion public/locales/en/notify.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,7 @@
"windowIpfsRequestFailed": "IPFS request failed. Please check your IPFS Companion settings.",
"ipfsIsBack": "Normal IPFS service has resumed. Enjoy!",
"filesEventFailed": "Failed to add files to IPFS. Please try again.",
"folderExists": "A folder with that name already exists. Please choose another."
"folderExists": "A folder with that name already exists. Please choose another.",
"couldntConnectToSwarm": "Could not connect to the swarm.",
lidel marked this conversation as resolved.
Show resolved Hide resolved
"connectedToSwarm": "Successfully connected to the swarm."
}
6 changes: 5 additions & 1 deletion public/locales/en/peers.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,9 @@
"peers": "Peers",
"address": "Address",
"location": "Location",
"unknownLocation": "Unknown"
"unknownLocation": "Unknown",
"addConnection": "Add Connection",
"insertSwarmAddress": "Insert the swarm address you want to connect to.",
"add": "Add",
"example": "Example:"
}
17 changes: 17 additions & 0 deletions src/bundles/notify.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ const notify = {
}
}

if (action.type === 'SWARM_CONNECT_FAILED' || action.type === 'SWARM_CONNECT_FINISHED') {
return {
...state,
show: true,
error: action.type === 'SWARM_CONNECT_FAILED',
eventId: action.type
}
}

return state
},

Expand All @@ -73,6 +82,14 @@ const notify = {
return 'ipfsIsBack'
}

if (eventId === 'SWARM_CONNECT_FAILED') {
return 'couldntConnectToSwarm'
}

if (eventId === 'SWARM_CONNECT_FINISHED') {
return 'connectedToSwarm'
}

return eventId
}
),
Expand Down
16 changes: 16 additions & 0 deletions src/bundles/peers.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ bundle.selectPeersCount = createSelector(
}
)

bundle.doConnectSwarm = addr => async ({ dispatch, getIpfs }) => {
dispatch({ type: 'SWARM_CONNECT_STARTED', payload: { addr } })
const ipfs = getIpfs()

try {
await ipfs.swarm.connect(addr)
} catch (err) {
return dispatch({
type: 'SWARM_CONNECT_FAILED',
payload: { addr, error: err }
})
}

dispatch({ type: 'SWARM_CONNECT_FINISHED', payload: { addr } })
}

// Update the peers if they are stale (appTime - lastSuccess > staleAfter)
bundle.reactPeersFetchWhenIdle = createSelector(
'selectPeersShouldUpdate',
Expand Down
73 changes: 73 additions & 0 deletions src/peers/AddConnection/AddConnection.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import React from 'react'
import { translate } from 'react-i18next'
import { connect } from 'redux-bundler-react'
import toUri from 'multiaddr-to-uri'

import Icon from '../../icons/StrokeDecentralization'
import Button from '../../components/button/Button'
import Overlay from '../../components/overlay/Overlay'
import TextInputModal from '../../components/text-input-modal/TextInputModal'

class AddConnection extends React.Component {
state = {
open: false
}

toggleModal = () => {
this.setState({
open: !this.state.open
})
}

addConnection = (maddr) => {
this.props.doConnectSwarm(maddr)
this.toggleModal()
}

validate = (maddr) => {
hacdias marked this conversation as resolved.
Show resolved Hide resolved
try {
toUri(maddr)
return true
} catch (_) {
return false
}
}

getDescription = () => {
const { t } = this.props
const codeClass = 'w-90 mb1 pa1 bg-snow f7 charcoal-muted truncate'

return (
<div className='mb3 flex flex-column items-center'>
<p className='gray w-80'>{t('insertSwarmAddress')}</p>
<span className='w-80 mv2 f7 charcoal-muted'>{t('example')}</span>
<code className={codeClass}>/ip4/76.176.168.65/tcp/4001/ipfs/QmbBHw1Xx9pUpAbrVZUKTPL5Rsph5Q9GQhRvcWVBPFgGtC</code>
</div>
)
}

render () {
const { open } = this.state
const { t } = this.props

return (
<div>
<Button onClick={this.toggleModal} minWidth='160px' className='pointer bg-green white f6'>+ {t('addConnection')}</Button>

<Overlay show={open} onLeave={this.toggleModal}>
<TextInputModal
validate={this.validate}
onSubmit={this.addConnection}
onCancel={this.toggleModal}
submitText={t('add')}
icon={Icon}
title={t('addConnection')}
description={this.getDescription()}
/>
</Overlay>
</div>
)
}
}

export default connect('doConnectSwarm', translate('peers')(AddConnection))
6 changes: 6 additions & 0 deletions src/peers/PeersPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,18 @@ import { translate } from 'react-i18next'
import Box from '../components/box/Box'
import WorldMap from './WorldMap/WorldMap'
import PeersTable from './PeersTable/PeersTable'
import AddConnection from './AddConnection/AddConnection'

const PeersPage = ({ t }) => (
<div data-id='PeersPage'>
<Helmet>
<title>{t('title')} - IPFS</title>
</Helmet>

<div className='flex justify-end mb3'>
<AddConnection />
</div>

<Box className='pt3 ph3 pb4'>
<WorldMap />
<PeersTable />
Expand Down