-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathuseImageHistory.ts
More file actions
124 lines (113 loc) · 2.71 KB
/
useImageHistory.ts
File metadata and controls
124 lines (113 loc) · 2.71 KB
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/**
* Custom hook for managing image generation history.
*/
/**
* WordPress dependencies
*/
import { useReducer, useCallback } from '@wordpress/element';
/**
* Internal dependencies
*/
import type { GeneratedImageData, HistoryEntry } from '../types';
interface UseImageHistoryReturn {
history: HistoryEntry[];
historyIndex: number;
activeEntry: HistoryEntry | null;
canGoBack: boolean;
canGoForward: boolean;
addToHistory: (
data: GeneratedImageData,
referenceSrc?: string,
isRefinement?: boolean,
referenceHistoryIndex?: number
) => void;
goBack: () => void;
goForward: () => void;
resetHistory: () => void;
}
type HistoryState = {
history: HistoryEntry[];
historyIndex: number;
};
type HistoryAction =
| { type: 'ADD'; entry: HistoryEntry }
| { type: 'GO_BACK' }
| { type: 'GO_FORWARD' }
| { type: 'RESET' };
function historyReducer(
state: HistoryState,
action: HistoryAction
): HistoryState {
switch ( action.type ) {
case 'ADD':
return {
history: [ ...state.history, action.entry ],
historyIndex: state.history.length,
};
case 'GO_BACK':
return {
...state,
historyIndex: Math.max( 0, state.historyIndex - 1 ),
};
case 'GO_FORWARD':
return {
...state,
historyIndex: Math.min(
state.history.length - 1,
state.historyIndex + 1
),
};
case 'RESET':
return { history: [], historyIndex: -1 };
}
}
/**
* Manages image generation history with navigation support.
*
* @return {UseImageHistoryReturn} History state and navigation helpers.
*/
export function useImageHistory(): UseImageHistoryReturn {
const [ { history, historyIndex }, dispatch ] = useReducer(
historyReducer,
{ history: [], historyIndex: -1 }
);
const activeEntry =
historyIndex >= 0 ? history[ historyIndex ] ?? null : null;
const canGoBack = historyIndex > 0;
const canGoForward = historyIndex < history.length - 1;
const addToHistory = useCallback(
(
data: GeneratedImageData,
referenceSrc?: string,
isRefinement: boolean = false,
referenceHistoryIndex?: number
) => {
const entry: HistoryEntry = { generatedData: data, isRefinement };
if ( referenceSrc !== undefined ) {
entry.referenceSrc = referenceSrc;
}
if ( referenceHistoryIndex !== undefined ) {
entry.referenceHistoryIndex = referenceHistoryIndex;
}
dispatch( { type: 'ADD', entry } );
},
[]
);
const goBack = useCallback( () => dispatch( { type: 'GO_BACK' } ), [] );
const goForward = useCallback(
() => dispatch( { type: 'GO_FORWARD' } ),
[]
);
const resetHistory = useCallback( () => dispatch( { type: 'RESET' } ), [] );
return {
history,
historyIndex,
activeEntry,
canGoBack,
canGoForward,
addToHistory,
goBack,
goForward,
resetHistory,
};
}