Skip to content

Commit

Permalink
Narrow Action type to only defined reducers.
Browse files Browse the repository at this point in the history
  • Loading branch information
raineorshine committed Jan 8, 2025
1 parent 24bc0ad commit db00102
Show file tree
Hide file tree
Showing 6 changed files with 19 additions and 9 deletions.
5 changes: 5 additions & 0 deletions src/@types/ActionType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import * as reducers from '../actions/index'

type ActionType = keyof typeof reducers

export default ActionType
3 changes: 2 additions & 1 deletion src/@types/Patch.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import { GetOperation } from 'fast-json-patch'
import ActionType from './ActionType'

// Extend fast-json-patch Operation type to include actions list
// See fast-json-patch types: https://github.com/Starcounter-Jack/JSON-Patch/blob/89a09e94e0e6500115789e33586a75c8dd1aea13/module/core.d.ts
// TODO: This should allow any Operation, not just GetOperation. But how to extend?
interface ExtendedOperation<T = any> extends GetOperation<T> {
actions: string[]
actions: ActionType[]
}

type Patch = ExtendedOperation[]
Expand Down
3 changes: 2 additions & 1 deletion src/actions/app.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { Action } from 'redux'
import ActionType from '../@types/ActionType'
import Index from '../@types/IndexType'
import State from '../@types/State'
import initialState from '../util/initialState'
Expand All @@ -7,7 +8,7 @@ import * as reducers from './index'
/**
* The main app reducer. Uses action.type to select the reducer with the same name. Otherwise throw an error with unknownAction.
*/
const appReducer = (state: State = initialState(), action: Action<string>): State =>
const appReducer = (state: State = initialState(), action: Action<ActionType>): State =>
((reducers as Index<any>)[action.type] || reducers.unknownAction)(state, action)

export default appReducer
5 changes: 1 addition & 4 deletions src/commands/splitSentences.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import _ from 'lodash'
import { Dispatch } from 'react'
import { Action } from 'redux'
import Command from '../@types/Command'
import Thunk from '../@types/Thunk'
import { alertActionCreator as alert } from '../actions/alert'
import { splitSentencesActionCreator as splitSentences } from '../actions/splitSentences'
import SplitSentencesIcon from '../components/icons/SplitSentencesIcon'
Expand All @@ -20,7 +17,7 @@ const splitSentencesShortcut: Command = {
canExecute: state => {
return !!state.cursor || hasMulticursor(state)
},
exec: (dispatch: Dispatch<Action | Thunk>, getState) => {
exec: (dispatch, getState) => {
const state = getState()
const { cursor } = state
const value = cursor ? headValue(state, cursor) : undefined
Expand Down
8 changes: 7 additions & 1 deletion src/components/LayoutTree.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { useSelector } from 'react-redux'
import { TransitionGroup } from 'react-transition-group'
import { CSSTransitionProps } from 'react-transition-group/CSSTransition'
import { css, cx } from '../../styled-system/css'
import ActionType from '../@types/ActionType'
import Autofocus from '../@types/Autofocus'
import Index from '../@types/IndexType'
import LazyEnv from '../@types/LazyEnv'
Expand Down Expand Up @@ -435,7 +436,12 @@ const TreeNode = ({

// true if the last action is any of archive/delete/collapse
const isLastActionDelete = useSelector(state => {
const deleteActions = ['archiveThought', 'collapseContext', 'deleteThought', 'deleteThoughtWithCursor']
const deleteActions: ActionType[] = [
'archiveThought',
'collapseContext',
'deleteThought',
'deleteThoughtWithCursor',
]
const lastPatches = state.undoPatches[state.undoPatches.length - 1]
return lastPatches?.some(patch => deleteActions.includes(patch.actions[0]))
})
Expand Down
4 changes: 2 additions & 2 deletions src/redux-enhancers/undoRedoEnhancer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,20 @@ import { Operation, applyPatch, compare } from 'fast-json-patch'
import { produce } from 'immer'
import _ from 'lodash'
import { Action, Store, StoreEnhancer, StoreEnhancerStoreCreator } from 'redux'
import ActionType from '../@types/ActionType'
import Index from '../@types/IndexType'
import Lexeme from '../@types/Lexeme'
import Patch from '../@types/Patch'
import State from '../@types/State'
import ThoughtId from '../@types/ThoughtId'
import * as reducers from '../actions'
import updateThoughts from '../actions/updateThoughts'
import getThoughtById from '../selectors/getThoughtById'
import headValue from '../util/headValue'
import reducerFlow from '../util/reducerFlow'

// a map of action types to boolean
type ActionFlags = {
[key in keyof typeof reducers]: boolean
[key in ActionType]: boolean
}

// Actions representing any cursor movements.
Expand Down

0 comments on commit db00102

Please sign in to comment.