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

Commit

Permalink
Merge branch 'react' into add/edit-dat-title
Browse files Browse the repository at this point in the history
  • Loading branch information
juliangruber committed Feb 9, 2018
2 parents afd5689 + 1a2790c commit 9bb3548
Show file tree
Hide file tree
Showing 6 changed files with 108 additions and 40 deletions.
91 changes: 80 additions & 11 deletions app/actions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import fs from 'fs'
import promisify from 'util-promisify'
import { basename } from 'path'

const dats = new Map()
const dats = {}

const stat = promisify(fs.stat)
const readFile = promisify(fs.readFile)
const writeFile = promisify(fs.writeFile)
const mkdir = promisify(fs.mkdir)
const { Notification } = window

export const shareDat = key => ({ type: 'DIALOGS_LINK_OPEN', key })
Expand All @@ -32,20 +33,28 @@ export const createDat = () => dispatch => {
addDat({ path })(dispatch)
}

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

if (key) dispatch({ type: 'ADD_DAT', key, path, paused })
opts = {
watch: true,
resume: true,
ignoreHidden: true,
compareFileContent: true,
...opts
}

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 })
dispatch({ type: 'ADD_DAT', key, path, paused })
}

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

dispatch({
type: 'DAT_METADATA',
Expand All @@ -56,7 +65,6 @@ export const addDat = ({ key, path }) => dispatch => {
}
})

dats.set(key, dat)
dispatch({ type: 'ADD_DAT_SUCCESS', key })
dispatch({ type: 'DAT_WRITABLE', key, writable: dat.writable })

Expand Down Expand Up @@ -137,8 +145,10 @@ export const addDat = ({ key, path }) => dispatch => {
}
updateProgress()

joinNetwork(dat)(dispatch)
updateConnections(dat)(dispatch)
if (!paused) {
joinNetwork(dat)(dispatch)
updateConnections(dat)(dispatch)
}

let prevNetworkStats
dat.updateInterval = setInterval(() => {
Expand All @@ -154,6 +164,9 @@ export const addDat = ({ key, path }) => dispatch => {
}
})
}, 1000)

dats[key] = { dat, path, opts }
storeOnDisk()
})
}

Expand Down Expand Up @@ -187,7 +200,7 @@ const updateState = dat => {

export const deleteDat = key => ({ type: 'DIALOGS_DELETE_OPEN', key })
export const confirmDeleteDat = key => dispatch => {
const dat = dats.get(key)
const { dat } = dats[key]

for (const con of dat.network.connections) {
con.removeAllListeners()
Expand All @@ -196,19 +209,21 @@ export const confirmDeleteDat = key => dispatch => {
clearInterval(dat.updateInterval)

dat.close()
dats.delete(key)
delete dats[key]
storeOnDisk()
dispatch({ type: 'REMOVE_DAT', key })
dispatch({ type: 'DIALOGS_DELETE_CLOSE' })
}
export const cancelDeleteDat = () => ({ type: 'DIALOGS_DELETE_CLOSE' })

export const togglePause = ({ key, paused }) => dispatch => {
const dat = dats.get(key)
const { dat } = dats[key]
if (paused) {
joinNetwork(dat)(dispatch)
} else {
dat.leaveNetwork()
}
storeOnDisk()
if (paused) {
dispatch({ type: 'RESUME_DAT', key: key })
} else {
Expand Down Expand Up @@ -254,3 +269,57 @@ export const updateTitle = (key, path, editValue) => {
export const deactivateTitleEditing = () => ({
type: 'DEACTIVATE_TITLE_EDITING'
})

export const loadFromDisk = () => async dispatch => {
try {
await mkdir(`${homedir()}/.dat-desktop`)
} catch (_) {}

let blob
try {
blob = await readFile(`${homedir()}/.dat-desktop/dats.json`, 'utf8')
} catch (_) {
return
}
const datOpts = JSON.parse(blob)

blob = {}
try {
blob = await readFile(`${homedir()}/.dat-desktop/paused.json`, 'utf8')
} catch (_) {}
const paused = JSON.parse(blob)

for (const key of Object.keys(datOpts)) {
const opts = JSON.parse(datOpts[key])
addDat({
key: key,
path: opts.dir,
paused: paused[key],
...opts
})(dispatch)
}
}

const storeOnDisk = async () => {
const dir = `${homedir()}/.dat-desktop`
const datsState = Object.keys(dats).reduce(
(acc, key) => ({
...acc,
[key]: JSON.stringify({
dir: dats[key].path,
opts: dats[key].opts
})
}),
{}
)
const pausedState = Object.keys(dats).reduce(
(acc, key) => ({
...acc,
[key]: !dats[key].dat.network
}),
{}
)

await writeFile(`${dir}/dats.json`, JSON.stringify(datsState))
await writeFile(`${dir}/paused.json`, JSON.stringify(pausedState))
}
39 changes: 22 additions & 17 deletions app/components/status.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,28 @@ const Status = ({ dat }) => {
const netStats = dat.stats.network

let progressText
switch (dat.state) {
case 'complete':
progressText = `Complete. ↑ ${speed(netStats.up)}`
break
case 'loading':
progressText = (
<span>
<span className='arrow'></span> {speed(netStats.down)}
<span className='arrow ml2'></span> {speed(netStats.up)}
</span>
)
break
case 'stale':
progressText = 'waiting for peers…'
break
default:
progressText = 'Paused.'

if (dat.paused) {
progressText = 'Paused.'
} else {
switch (dat.state) {
case 'complete':
progressText = `Complete. ↑ ${speed(netStats.up)}`
break
case 'loading':
progressText = (
<span>
<span className='arrow'></span> {speed(netStats.down)}
<span className='arrow ml2'></span> {speed(netStats.up)}
</span>
)
break
case 'stale':
progressText = 'waiting for peers…'
break
default:
progressText = 'Paused.'
}
}

return (
Expand Down
7 changes: 4 additions & 3 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ import { render } from 'react-dom'
import { Provider } from 'react-redux'
import { createStore, applyMiddleware, compose } from 'redux'
import datDesktopApp from './reducers'
import { addDat } from './actions'
import { addDat, loadFromDisk } from './actions'
import App from './components/app'
import logger from 'redux-logger'
// import persistState from 'redux-localstorage'
import thunk from 'redux-thunk'
import { ipcRenderer as ipc } from 'electron'

const store = createStore(
datDesktopApp,
compose(/* persistState(), */ applyMiddleware(thunk, logger))
compose(applyMiddleware(thunk, logger))
)

render(
Expand All @@ -24,5 +23,7 @@ render(
document.querySelector('div')
)

store.dispatch(loadFromDisk())

ipc.on('log', (_, str) => console.log(str))
ipc.on('link', key => store.dispatch(addDat({ key })))
5 changes: 2 additions & 3 deletions app/reducers/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ const redatApp = (state = defaultState, action) => {
key: action.key,
path: action.path,
loading: true,
paused: false,
paused: action.paused,
metadata: {},
stats: {
network: {
Expand Down Expand Up @@ -288,8 +288,7 @@ const redatApp = (state = defaultState, action) => {
...state.dats,
[action.key]: {
...state.dats[action.key],
paused: false,
state: 'stale'
paused: false
}
}
}
Expand Down
5 changes: 0 additions & 5 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@
"react-redux": "^5.0.6",
"react-swap": "^2.0.2",
"redux": "^3.7.2",
"redux-localstorage": "^0.4.1",
"redux-logger": "^3.0.6",
"redux-thunk": "^2.2.0",
"styled-components": "^2.4.0",
Expand Down

0 comments on commit 9bb3548

Please sign in to comment.