-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
102 additions
and
4 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,98 @@ | ||
import { useReducer, useCallback } from 'react'; | ||
|
||
// Initial state that we pass into useReducer | ||
const initialState = { | ||
// Array of previous state values updated each time we push a new state | ||
past: [], | ||
// Current state value | ||
present: null, | ||
// Will contain "future" state values if we undo (so we can redo) | ||
future: [] | ||
}; | ||
|
||
// Our reducer function to handle state changes based on action | ||
const reducer = (state, action) => { | ||
const { past, present, future } = state; | ||
switch (action.type) { | ||
case 'UNDO': | ||
const previous = past[past.length - 1]; | ||
const newPast = past.slice(0, past.length - 1); | ||
|
||
return { | ||
past: newPast, | ||
present: previous, | ||
future: [present, ...future] | ||
}; | ||
case 'REDO': | ||
const next = future[0]; | ||
const newFuture = future.slice(1); | ||
|
||
return { | ||
past: [...past, present], | ||
present: next, | ||
future: newFuture | ||
}; | ||
case 'SET': | ||
const { newPresent } = action; | ||
|
||
if (newPresent === present) { | ||
return state; | ||
} | ||
return { | ||
past: [...past, present], | ||
present: newPresent, | ||
future: [] | ||
}; | ||
case 'CLEAR': | ||
const { initialPresent } = action; | ||
|
||
return { | ||
...initialState, | ||
present: initialPresent | ||
}; | ||
} | ||
}; | ||
|
||
const useHistory = initialPresent => { | ||
const [state, dispatch] = useReducer(reducer, { | ||
...initialState, | ||
present: initialPresent | ||
}); | ||
|
||
const canUndo = state.past.length !== 0; | ||
const canRedo = state.future.length !== 0; | ||
|
||
// Setup our callback functions | ||
// We memoize with useCallback to prevent unecessary re-renders | ||
|
||
const undo = useCallback( | ||
() => { | ||
if (canUndo) { | ||
dispatch({ type: 'UNDO' }); | ||
} | ||
}, | ||
[canUndo, dispatch] | ||
); | ||
|
||
const redo = useCallback( | ||
() => { | ||
if (canRedo) { | ||
dispatch({ type: 'REDO' }); | ||
} | ||
}, | ||
[canRedo, dispatch] | ||
); | ||
|
||
const set = useCallback(newPresent => dispatch({ type: 'SET', newPresent }), [ | ||
dispatch | ||
]); | ||
|
||
const clear = useCallback(() => dispatch({ type: 'CLEAR', initialPresent }), [ | ||
dispatch | ||
]); | ||
|
||
// If needed we could also return past and future state | ||
return { state: state.present, set, undo, redo, clear, canUndo, canRedo }; | ||
}; | ||
|
||
export default useHistory; |