This repository has been archived by the owner on Jul 9, 2019. It is now read-only.
/
index.js
88 lines (81 loc) · 2.17 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import React, { Component } from 'react'
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import { withRouter } from 'react-router-dom'
import { Shortcuts } from 'react-shortcuts'
import {
selectChannel,
toggleChannelsMenu,
focusComponent,
toggleShortcutHelp,
} from '../../actions'
class GlobalShortcutHandler extends Component {
constructor(props) {
super(props)
this.handleShortcuts = this.handleShortcuts.bind(this)
}
handleShortcuts(action) {
const {
channels,
selectChannel,
history,
focusComponent,
toggleShortcutHelp,
} = this.props
if (action.indexOf('CHANNEL_') === 0) {
// Switch channel
const channelIndex = parseInt(action.replace('CHANNEL_', '')) - 1
if (channels && channels[channelIndex] && channels[channelIndex].uid) {
// Switch to the selected channel
const uid = channels[channelIndex].uid
history.push(`/channel/${uid}`)
selectChannel(uid)
focusComponent(null)
focusComponent('timeline')
}
} else {
// Other actions
switch (action) {
case 'NEW_POST':
history.push('/editor')
break
case 'FOCUS_CHANNEL_LIST':
focusComponent(null)
focusComponent('channels')
break
case 'HELP':
toggleShortcutHelp()
break
case 'KONAMI':
alert('Look at you. You are very clever')
break
default:
// Nothing to do
break
}
}
}
render() {
return (
<Shortcuts name="GLOBAL" handler={this.handleShortcuts} global>
{this.props.children}
</Shortcuts>
)
}
}
const mapStateToProps = state => ({
channels: state.channels
.toJS()
.filter(channel => channel.uid !== 'notifications'),
})
const mapDispatchToProps = dispatch =>
bindActionCreators(
{ selectChannel, toggleChannelsMenu, focusComponent, toggleShortcutHelp },
dispatch
)
export default withRouter(
connect(
mapStateToProps,
mapDispatchToProps
)(GlobalShortcutHandler)
)