Skip to content

Commit

Permalink
Add alert.alertType. Fixes #665.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed May 19, 2020
1 parent a463e7f commit eb0f9f8
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 7 deletions.
10 changes: 9 additions & 1 deletion src/action-creators/alert.js
@@ -1,10 +1,18 @@
import { store } from '../store'

export default (value, { showCloseLink } = {}) => {
/**
* Dispatch an alert action.
*
* @param value The string or React Component that will be rendered in the alert.
* @param showCloseLink Show a small 'x' in the upper right corner that allows the user to close the alert. Default: true.
* @param type An arbitrary alert type that can be added to the alert. This is useful if specific alerts needs to be detected later on, for example, to determine if the alert should be closed, or if it has been superceded by a different alert type.
*/
export default (value, { showCloseLink, alertType } = {}) => {
if (store.getState().alert !== value) {
store.dispatch({
type: 'alert',
value,
alertType,
showCloseLink: showCloseLink !== false
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/reducers/alert.js
@@ -1,3 +1,3 @@
export default (state, { value, showCloseLink }) => ({
alert: value ? { value, showCloseLink } : null
export default (state, { value, showCloseLink, alertType }) => ({
alert: value ? { value, showCloseLink, alertType } : null
})
15 changes: 11 additions & 4 deletions src/shortcuts.js
Expand Up @@ -84,6 +84,9 @@ const shortcutGestureIndex = globalShortcuts.reduce((accum, shortcut) => shortcu
{}
)

/** Returns true if the current alert is a gestureHint */
const isGestureHint = state => state.alert && state.alert.alertType === 'gestureHint'

let handleGestureSegmentTimeout // eslint-disable-line fp/no-let

export const handleGestureSegment = (g, sequence, e) => {
Expand All @@ -104,11 +107,11 @@ export const handleGestureSegment = (g, sequence, e) => {
() => {
// only show "Invalid gesture" if hint is already being shown
alert(shortcut ? shortcut.name
: state.alert ? '✗ Invalid gesture'
: null, { showCloseLink: false })
: isGestureHint(state) ? '✗ Invalid gesture'
: null, { alertType: 'gestureHint', showCloseLink: false })
},
// if the hint is already being shown, do not wait to change the value
state.alert ? 0 : GESTURE_SEGMENT_HINT_TIMEOUT
isGestureHint(state) ? 0 : GESTURE_SEGMENT_HINT_TIMEOUT
)
}

Expand All @@ -132,7 +135,11 @@ export const handleGestureEnd = (gesture, e) => {
handleGestureSegmentTimeout = null // null the timer to track when it is running for handleGestureSegment

// needs to be delayed until the next tick otherwise there is a re-render which inadvertantly calls the automatic render focus in the Thought component.
setTimeout(() => alert(null))
setTimeout(() => {
if (isGestureHint(store.getState())) {
alert(null)
}
})
}

/** Global keyUp handler */
Expand Down

0 comments on commit eb0f9f8

Please sign in to comment.