Skip to content
This repository has been archived by the owner on Jan 6, 2022. It is now read-only.

Commit

Permalink
add dat drag/drop. #472
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber committed Jan 30, 2018
1 parent 688575e commit 404bc7d
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 8 deletions.
33 changes: 29 additions & 4 deletions app/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,45 @@ import Dat from 'dat-node'
import { encode } from 'dat-encoding'
import { homedir } from 'os'
import { clipboard } from 'electron'
import fs from 'fs'
import promisify from 'util-promisify'
import { basename } from 'path'

const dats = new Map()

const stat = promisify(fs.stat)

export const shareDat = key => ({ type: 'DIALOGS_LINK_OPEN', key })
export const copyLink = link => {
clipboard.writeText(link)
return { type: 'DIALOGS_LINK_COPY' }
}
export const closeShareDat = () => ({ type: 'DIALOGS_LINK_CLOSE' })

export const addDat = key => dispatch => {
key = encode(key)
const path = `${homedir()}/Downloads/${key}`
dispatch({ type: 'ADD_DAT', key, path })
export const addDat = ({ key, path }) => dispatch => {
if (key) key = encode(key)
if (!path) path = `${homedir()}/Downloads/${key}`
if (key) dispatch({ type: 'ADD_DAT', key, path })

Dat(path, { key }, (error, dat) => {
if (error) return dispatch({ type: 'ADD_DAT_ERROR', key, error })
if (!key) {
key = encode(dat.key)
dispatch({ type: 'ADD_DAT', key, path })
}

dat.joinNetwork()
dat.trackStats()
if (dat.writable) dat.importFiles()

dispatch({
type: 'DAT_METADATA',
key,
metadata: {
title: basename(path),
author: 'Anonymous'
}
})

dats.set(key, dat)
dispatch({ type: 'ADD_DAT_SUCCESS', key })
Expand Down Expand Up @@ -111,3 +130,9 @@ export const deleteDat = key => dispatch => {

dats.delete(key)
}

export const dropFolder = folder => async dispatch => {
const isDirectory = (await stat(folder.path)).isDirectory()
if (!isDirectory) return
addDat({ path: folder.path })(dispatch)
}
2 changes: 2 additions & 0 deletions app/components/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ import Sprite from './sprite'
import Header from './header'
import TableContainer from '../containers/table'
import LinkContainer from '../containers/dialog'
import DragDropContainer from '../containers/drag-drop'

const App = () => (
<Fragment>
<Sprite />
<Header />
<TableContainer />
<LinkContainer />
<DragDropContainer />
</Fragment>
)

Expand Down
2 changes: 1 addition & 1 deletion app/containers/dat-import.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const mapStateToProps = state => state

const mapDispatchToProps = dispatch => {
return {
onAddDat: key => dispatch(addDat(key))
onAddDat: key => dispatch(addDat({ key }))
}
}

Expand Down
16 changes: 16 additions & 0 deletions app/containers/drag-drop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import FileDrop from 'react-file-drop'
import { connect } from 'react-redux'
import { dropFolder } from '../actions'

const mapStateToProps = state => ({})

const mapDispatchToProps = dispatch => ({
onDrop: list => dispatch(dropFolder(list[0]))
})

const DragDropContainer = connect(
mapStateToProps,
mapDispatchToProps
)(FileDrop)

export default DragDropContainer
101 changes: 99 additions & 2 deletions package-lock.json

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

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,15 @@
"prettier-bytes": "^1.0.4",
"react": "^16.2.0",
"react-dom": "^16.2.0",
"react-file-drop": "^0.1.9",
"react-redux": "^5.0.6",
"redux": "^3.7.2",
"redux-localstorage": "^0.4.1",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.2.0",
"styled-components": "^2.4.0",
"tachyons": "^4.9.1"
"tachyons": "^4.9.1",
"util-promisify": "^2.1.0"
},
"build": {
"appId": "com.datproject.dat",
Expand Down

0 comments on commit 404bc7d

Please sign in to comment.