From 1eeb18728180baeb0de5c72373a264a750ecb5eb Mon Sep 17 00:00:00 2001 From: godlike0108 Date: Wed, 13 Dec 2023 13:08:44 +0800 Subject: [PATCH] fix: remove debounce useRef to prevent cache --- app/src/modules/character/actions.ts | 2 +- app/src/modules/character/reducers.ts | 4 ++-- app/src/modules/character/sagas.ts | 5 ++--- app/src/pages/canvas/index.tsx | 23 ++++++++++++++--------- 4 files changed, 19 insertions(+), 15 deletions(-) diff --git a/app/src/modules/character/actions.ts b/app/src/modules/character/actions.ts index 35073f7..bb15d9a 100644 --- a/app/src/modules/character/actions.ts +++ b/app/src/modules/character/actions.ts @@ -44,7 +44,7 @@ export const createCharacterImage = ( data, }); -type CreateCharacterImageSuccessBody = { blob: Blob; characterType: string }; +type CreateCharacterImageSuccessBody = { blob: Blob }; export type CreateCharacterImageSuccessAction = Action & { data: CreateCharacterImageSuccessBody; }; diff --git a/app/src/modules/character/reducers.ts b/app/src/modules/character/reducers.ts index 56234c1..f163373 100644 --- a/app/src/modules/character/reducers.ts +++ b/app/src/modules/character/reducers.ts @@ -31,8 +31,8 @@ const characterReducer = (state = initialState, action: CharacterAction) => { return { ...state, characterType, customCharacterType }; } case CREATE_CHARACTER_IMAGE_SUCCESS: { - const { blob, characterType } = action.data; - return { ...state, characterImage: blob, characterType }; + const { blob } = action.data; + return { ...state, characterImage: blob }; } default: return state; diff --git a/app/src/modules/character/sagas.ts b/app/src/modules/character/sagas.ts index 3d65fbf..02697d3 100644 --- a/app/src/modules/character/sagas.ts +++ b/app/src/modules/character/sagas.ts @@ -15,6 +15,7 @@ let controller = new AbortController(); // Sample worker saga function* createCharacterImage(action: CreateCharacterImageAction) { const { sketchBlob, prompt } = action.data; + console.log(prompt); const formData = new FormData(); formData.append("sketch_file", sketchBlob); formData.append("prompt", addPrefix(prompt)); @@ -35,9 +36,7 @@ function* createCharacterImage(action: CreateCharacterImageAction) { signal: controller.signal, }, ); - yield put( - createCharacterImageSuccess({ blob: data, characterType: prompt }), - ); + yield put(createCharacterImageSuccess({ blob: data })); } catch (e) { console.error(e); } diff --git a/app/src/pages/canvas/index.tsx b/app/src/pages/canvas/index.tsx index baeb7a7..ce3eed3 100644 --- a/app/src/pages/canvas/index.tsx +++ b/app/src/pages/canvas/index.tsx @@ -1,5 +1,5 @@ /// -import { FC, useEffect, useRef, useState } from "react"; +import { FC, useEffect, useState } from "react"; import { useDispatch, useSelector } from "react-redux"; import { useNavigate } from "react-router-dom"; import styled from "styled-components"; @@ -16,7 +16,6 @@ import { Header } from "../../components/Layout/Layout"; import { Button, DropdownMenu, TextField } from "@radix-ui/themes"; import { CaretDownIcon } from "@radix-ui/react-icons"; import { CHARACTER_BASE } from "../../shared/characterTypes"; -import { debounce } from "lodash"; import Loader from "../../components/Loader"; const PreviewContainer = styled.div` @@ -151,12 +150,16 @@ const CanvasPage = () => { } }, [characterType]); - const renderPreview = useRef( - debounce((val) => { - setSketchBlob(val); - dispatch(createCharacterImage({ sketchBlob: val, prompt })); - }, 1000), - ).current; + // NOTE: useRef cached the updated value so I remove the debounce temporarily + const renderPreview = (val) => { + setSketchBlob(val); + dispatch( + createCharacterImage({ + sketchBlob: val, + prompt: characterType === "Custom" ? prompt : characterType, + }), + ); + }; useEffect(() => { if (sketchBlob) { @@ -203,7 +206,9 @@ const CanvasPage = () => { setPrompt(e.target.value)} + onChange={(e) => { + setPrompt(e.target.value); + }} /> )}