-
Notifications
You must be signed in to change notification settings - Fork 491
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
License: MIT Signed-off-by: Chris Waring <chris@wwaves.co> Signed-off-by: Henrique Dias <hacdias@gmail.com>
- Loading branch information
Showing
9 changed files
with
263 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { createSelector } from 'redux-bundler' | ||
|
||
export const ACTIONS = { | ||
EXP_TOGGLE_STARTED: 'EXPERIMENTS_TOGGLE_STARTED', | ||
EXP_TOGGLE_FINISHED: 'EXPERIMENTS_TOGGLE_FINISHED', | ||
EXP_TOGGLE_FAILED: 'EXPERIMENTS_TOGGLE_FAILED', | ||
EXP_UPDATE_STATE: 'EXPERIMENTS_UPDATE_STATE' | ||
} | ||
|
||
const EXPERIMENTS = [ | ||
{ | ||
key: 'npmOnIpfs', | ||
actionUrls: [ | ||
{ | ||
url: 'https://github.com/ipfs-shipyard/npm-on-ipfs', | ||
key: 'readMoreUrl' | ||
}, | ||
{ | ||
url: 'https://github.com/ipfs-shipyard/npm-on-ipfs/issues', | ||
key: 'issueUrl' | ||
}, | ||
{ | ||
url: 'https://github.com/ipfs-shipyard/ipfs-desktop/issues/957', | ||
key: 'feedbackUrl' | ||
} | ||
], | ||
desktopOnly: true | ||
} | ||
] | ||
|
||
const mergeState = (state, payload) => | ||
Object.keys(payload).reduce( | ||
(all, key) => ({ | ||
...all, | ||
[key]: { | ||
...state[key], | ||
...payload[key] | ||
} | ||
}), | ||
state | ||
) | ||
|
||
const toggleEnabled = (state, key) => { | ||
return unblock( | ||
{ | ||
...state, | ||
[key]: { | ||
...state[key], | ||
enabled: !(state && state[key] && state[key].enabled) | ||
} | ||
}, | ||
key | ||
) | ||
} | ||
|
||
const unblock = (state, key) => { | ||
return { | ||
...state, | ||
[key]: { | ||
...state[key], | ||
blocked: false | ||
} | ||
} | ||
} | ||
|
||
const block = (state, key) => { | ||
return { | ||
...state, | ||
[key]: { | ||
...state[key], | ||
blocked: true | ||
} | ||
} | ||
} | ||
|
||
export default { | ||
name: 'experiments', | ||
|
||
persistActions: [ | ||
ACTIONS.EXP_TOGGLE_FINISHED, | ||
ACTIONS.EXP_TOGGLE_FAILED, | ||
ACTIONS.EXP_UPDATE_STATE | ||
], | ||
|
||
reducer: (state = {}, action) => { | ||
if (action.type === ACTIONS.EXP_TOGGLE_STARTED) { | ||
return block(state, action.payload.key) | ||
} | ||
|
||
if (action.type === ACTIONS.EXP_UPDATE_STATE) { | ||
return mergeState(state, action.payload) | ||
} | ||
|
||
if (action.type === ACTIONS.EXP_TOGGLE_FINISHED) { | ||
return toggleEnabled(state, action.payload.key) | ||
} | ||
|
||
if (action.type === ACTIONS.EXP_TOGGLE_FAILED) { | ||
return unblock(state, action.payload.key) | ||
} | ||
|
||
return state | ||
}, | ||
|
||
doExpToggleAction: key => ({ dispatch }) => { | ||
if (!key) return | ||
|
||
dispatch({ | ||
type: ACTIONS.EXP_TOGGLE_STARTED, | ||
payload: { key } | ||
}) | ||
}, | ||
|
||
selectExperimentsState: state => state.experiments, | ||
|
||
selectExperiments: createSelector( | ||
'selectIsIpfsDesktop', | ||
'selectExperimentsState', | ||
(isDesktop, state) => | ||
EXPERIMENTS.filter(e => !!e.desktopOnly === isDesktop).map(e => ({ | ||
...e, | ||
...state[e.key] | ||
})) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React from 'react' | ||
import { connect } from 'redux-bundler-react' | ||
import Checkbox from '../checkbox/Checkbox' | ||
import Box from '../box/Box' | ||
import Title from '../../settings/Title' | ||
|
||
const Experiments = ({ doExpToggleAction, experiments, t }) => { | ||
// if there are no experiments to show don't render | ||
if (experiments && experiments.length > 0) { | ||
const tkey = (selector, key) => | ||
t(`Experiments.${key ? `${key}.${selector}` : `${selector}`}`) | ||
return ( | ||
<Box className='mb3 pa4 lh-copy'> | ||
<Title>{t('experiments')}</Title> | ||
<p className='db mv4 f6 charcoal mw7'>{tkey('description')}</p> | ||
<div className='flex flex-wrap pb3'> | ||
{experiments.map(({ key, actionUrls, enabled, blocked }) => { | ||
return ( | ||
<div | ||
key={key} | ||
className='pa3 mr3 mb3 mw6 br3 bg-white dib f6 ba b1 b--light-gray' | ||
> | ||
<h3>{tkey('title', key)}</h3> | ||
<p className='charcoal'>{tkey('description', key)}</p> | ||
<Checkbox | ||
className='dib' | ||
disabled={blocked} | ||
onChange={() => doExpToggleAction(key, enabled)} | ||
checked={enabled} | ||
label={<span className='fw5 f6'>{tkey('label', key)}</span>} | ||
/> | ||
{actionUrls && ( | ||
<div className='mv3'> | ||
{actionUrls.map((action, i) => ( | ||
<a | ||
target='_blank' | ||
rel='noopener noreferrer' | ||
key={action.key} | ||
className={`link blue pr2 ${i > 0 && | ||
'bl b1 pl2 b--light-gray'}`} | ||
href={action.url} | ||
> | ||
{tkey(action.key)} | ||
</a> | ||
))} | ||
</div> | ||
)} | ||
</div> | ||
) | ||
})} | ||
</div> | ||
</Box> | ||
) | ||
} else { | ||
return null | ||
} | ||
} | ||
|
||
export default connect( | ||
'selectExperiments', | ||
'doExpToggleAction', | ||
Experiments | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters