MCPcopy Create free account
hub / github.com/DavidHDev/react-bits / useHistory

Function useHistory

src/tools/shape-magic/ShapeMagic.jsx:9–39  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

7import { createInitialState, createShape, PRESETS } from './types';
8
9const useHistory = initialState => {
10 const [history, setHistory] = useState([initialState]);
11 const [historyIndex, setHistoryIndex] = useState(0);
12
13 const current = history[historyIndex];
14
15 const push = useCallback(
16 newState => {
17 setHistory(prev => [...prev.slice(0, historyIndex + 1), newState]);
18 setHistoryIndex(prev => prev + 1);
19 },
20 [historyIndex]
21 );
22
23 const undo = useCallback(() => {
24 if (historyIndex > 0) {
25 setHistoryIndex(prev => prev - 1);
26 }
27 }, [historyIndex]);
28
29 const redo = useCallback(() => {
30 if (historyIndex < history.length - 1) {
31 setHistoryIndex(prev => prev + 1);
32 }
33 }, [historyIndex, history.length]);
34
35 const canUndo = historyIndex > 0;
36 const canRedo = historyIndex < history.length - 1;
37
38 return { current, push, undo, redo, canUndo, canRedo };
39};
40
41export default function ShapeMagic({ toolSelector }) {
42 const isMobile = useBreakpointValue({ base: true, lg: false });

Callers 1

ShapeMagicFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected