Commit c4dfb854 authored by Steven's avatar Steven

fix: ensure dispatch is used for actions in useMemoInit hook

parent 28e4ade0
...@@ -10,7 +10,7 @@ export const useMemoInit = ( ...@@ -10,7 +10,7 @@ export const useMemoInit = (
username: string, username: string,
autoFocus?: boolean, autoFocus?: boolean,
) => { ) => {
const { actions } = useEditorContext(); const { actions, dispatch } = useEditorContext();
const initializedRef = useRef(false); const initializedRef = useRef(false);
useEffect(() => { useEffect(() => {
...@@ -18,28 +18,30 @@ export const useMemoInit = ( ...@@ -18,28 +18,30 @@ export const useMemoInit = (
initializedRef.current = true; initializedRef.current = true;
const init = async () => { const init = async () => {
actions.setLoading("loading", true); dispatch(actions.setLoading("loading", true));
try { try {
if (memoName) { if (memoName) {
// Load existing memo // Load existing memo
const loadedState = await memoService.load(memoName); const loadedState = await memoService.load(memoName);
dispatch(
actions.initMemo({ actions.initMemo({
content: loadedState.content, content: loadedState.content,
metadata: loadedState.metadata, metadata: loadedState.metadata,
timestamps: loadedState.timestamps, timestamps: loadedState.timestamps,
}); }),
);
} else { } else {
// Load from cache for new memo // Load from cache for new memo
const cachedContent = cacheService.load(cacheService.key(username, cacheKey)); const cachedContent = cacheService.load(cacheService.key(username, cacheKey));
if (cachedContent) { if (cachedContent) {
actions.updateContent(cachedContent); dispatch(actions.updateContent(cachedContent));
} }
} }
} catch (error) { } catch (error) {
console.error("Failed to initialize editor:", error); console.error("Failed to initialize editor:", error);
} finally { } finally {
actions.setLoading("loading", false); dispatch(actions.setLoading("loading", false));
if (autoFocus) { if (autoFocus) {
setTimeout(() => { setTimeout(() => {
...@@ -50,5 +52,5 @@ export const useMemoInit = ( ...@@ -50,5 +52,5 @@ export const useMemoInit = (
}; };
init(); init();
}, [memoName, cacheKey, username, autoFocus, actions, editorRef]); }, [memoName, cacheKey, username, autoFocus, actions, dispatch, editorRef]);
}; };
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment