|
1 | 1 | import { HistoryStore, QueryStoreItem, StorageAPI } from '@graphiql/toolkit';
|
2 |
| -import { ReactNode, useCallback, useMemo, useRef, useState } from 'react'; |
| 2 | +import { ReactNode, useMemo, useState } from 'react'; |
3 | 3 |
|
4 | 4 | import { useStorageContext } from '../storage';
|
5 | 5 | import { createContextHook, createNullableContext } from '../utility/context';
|
@@ -87,77 +87,44 @@ export type HistoryContextProviderProps = {
|
87 | 87 | * any additional props they added for their needs (i.e., build their own functions that may save
|
88 | 88 | * to a backend instead of localStorage and might need an id property added to the QueryStoreItem)
|
89 | 89 | */
|
90 |
| -export function HistoryContextProvider(props: HistoryContextProviderProps) { |
| 90 | +export function HistoryContextProvider({ |
| 91 | + maxHistoryLength = DEFAULT_HISTORY_LENGTH, |
| 92 | + children, |
| 93 | +}: HistoryContextProviderProps) { |
91 | 94 | const storage = useStorageContext();
|
92 |
| - const historyStore = useRef( |
93 |
| - new HistoryStore( |
| 95 | + const [historyStore] = useState( |
| 96 | + () => |
94 | 97 | // Fall back to a noop storage when the StorageContext is empty
|
95 |
| - storage || new StorageAPI(null), |
96 |
| - props.maxHistoryLength || DEFAULT_HISTORY_LENGTH, |
97 |
| - ), |
| 98 | + new HistoryStore(storage || new StorageAPI(null), maxHistoryLength), |
98 | 99 | );
|
99 |
| - const [items, setItems] = useState(historyStore.current?.queries || []); |
100 |
| - |
101 |
| - const addToHistory: HistoryContextType['addToHistory'] = useCallback( |
102 |
| - (operation: QueryStoreItem) => { |
103 |
| - historyStore.current?.updateHistory(operation); |
104 |
| - setItems(historyStore.current.queries); |
105 |
| - }, |
106 |
| - [], |
107 |
| - ); |
108 |
| - |
109 |
| - const editLabel: HistoryContextType['editLabel'] = useCallback( |
110 |
| - (operation: QueryStoreItem, index?: number) => { |
111 |
| - historyStore.current.editLabel(operation, index); |
112 |
| - setItems(historyStore.current.queries); |
113 |
| - }, |
114 |
| - [], |
115 |
| - ); |
116 |
| - |
117 |
| - const toggleFavorite: HistoryContextType['toggleFavorite'] = useCallback( |
118 |
| - (operation: QueryStoreItem) => { |
119 |
| - historyStore.current.toggleFavorite(operation); |
120 |
| - setItems(historyStore.current.queries); |
121 |
| - }, |
122 |
| - [], |
123 |
| - ); |
124 |
| - |
125 |
| - const setActive: HistoryContextType['setActive'] = useCallback( |
126 |
| - (item: QueryStoreItem) => { |
127 |
| - return item; |
128 |
| - }, |
129 |
| - [], |
130 |
| - ); |
131 |
| - |
132 |
| - const deleteFromHistory: HistoryContextType['deleteFromHistory'] = |
133 |
| - useCallback((item: QueryStoreItem, clearFavorites = false) => { |
134 |
| - historyStore.current.deleteHistory(item, clearFavorites); |
135 |
| - setItems(historyStore.current.queries); |
136 |
| - }, []); |
| 100 | + const [items, setItems] = useState(() => historyStore.queries || []); |
137 | 101 |
|
138 | 102 | const value = useMemo<HistoryContextType>(
|
139 | 103 | () => ({
|
140 |
| - addToHistory, |
141 |
| - editLabel, |
| 104 | + addToHistory(operation) { |
| 105 | + historyStore.updateHistory(operation); |
| 106 | + setItems(historyStore.queries); |
| 107 | + }, |
| 108 | + editLabel(operation, index) { |
| 109 | + historyStore.editLabel(operation, index); |
| 110 | + setItems(historyStore.queries); |
| 111 | + }, |
142 | 112 | items,
|
143 |
| - toggleFavorite, |
144 |
| - setActive, |
145 |
| - deleteFromHistory, |
| 113 | + toggleFavorite(operation) { |
| 114 | + historyStore.toggleFavorite(operation); |
| 115 | + setItems(historyStore.queries); |
| 116 | + }, |
| 117 | + setActive: item => item, |
| 118 | + deleteFromHistory(item, clearFavorites) { |
| 119 | + historyStore.deleteHistory(item, clearFavorites); |
| 120 | + setItems(historyStore.queries); |
| 121 | + }, |
146 | 122 | }),
|
147 |
| - [ |
148 |
| - addToHistory, |
149 |
| - editLabel, |
150 |
| - items, |
151 |
| - toggleFavorite, |
152 |
| - setActive, |
153 |
| - deleteFromHistory, |
154 |
| - ], |
| 123 | + [items, historyStore], |
155 | 124 | );
|
156 | 125 |
|
157 | 126 | return (
|
158 |
| - <HistoryContext.Provider value={value}> |
159 |
| - {props.children} |
160 |
| - </HistoryContext.Provider> |
| 127 | + <HistoryContext.Provider value={value}>{children}</HistoryContext.Provider> |
161 | 128 | );
|
162 | 129 | }
|
163 | 130 |
|
|
0 commit comments