diff --git a/apps/client/src/widgets/community/components/comment-input-box/comment-input-box.tsx b/apps/client/src/widgets/community/components/comment-input-box/comment-input-box.tsx index 4d9998d5..d659e7df 100644 --- a/apps/client/src/widgets/community/components/comment-input-box/comment-input-box.tsx +++ b/apps/client/src/widgets/community/components/comment-input-box/comment-input-box.tsx @@ -41,6 +41,13 @@ const CommentInputBox = ({ const { openModal, closeModal } = useModal(); const replyCreateMode = mode.type === 'reply' && mode.action === 'create'; + + const handleFocus = () => { + if (mode.type === 'reset') { + dispatch({ type: 'COMMENT_CREATE' }); + } + }; + useClickOutside( wrapperRef, () => dispatch({ type: 'RESET' }), @@ -137,10 +144,11 @@ const CommentInputBox = ({
{ const queryClient = useQueryClient(); useEffect(() => { - const isStillCreatingMode = - prevModeRef.current.action === 'create' && mode.action === 'create'; - const typeChanged = prevModeRef.current.type !== mode.type; - + const prev = prevModeRef.current; prevModeRef.current = mode; if (mode.type === 'comment' && mode.action === 'edit') { @@ -72,7 +69,12 @@ const DetailSection = ({ postId }: DetailSectionProps) => { return; } - if (isStillCreatingMode && typeChanged) { + const shouldPreserveImage = + (prev.type === 'reset' && mode.action === 'create') || + (prev.action === 'create' && mode.type === 'reset') || + (prev.action === 'create' && mode.action === 'create'); + + if (shouldPreserveImage) { return; } @@ -336,7 +338,6 @@ const DetailSection = ({ postId }: DetailSectionProps) => { <> { - dispatch({ type: 'RESET' }); - }, [postId]); const value = useMemo(() => ({ mode, dispatch }), [mode]); return ( diff --git a/apps/client/src/widgets/community/hooks/create-input-mode-reducer.ts b/apps/client/src/widgets/community/hooks/create-input-mode-reducer.ts index 7c263449..3283f869 100644 --- a/apps/client/src/widgets/community/hooks/create-input-mode-reducer.ts +++ b/apps/client/src/widgets/community/hooks/create-input-mode-reducer.ts @@ -15,6 +15,12 @@ export const createInputModeReducer = ({ action, }: ComposeModeReducerType): InputBoxMode => { switch (action.type) { + case 'COMMENT_CREATE': + return { + type: 'comment', + action: 'create', + postId, + } as const; case 'COMMENT_EDIT': return { type: 'comment', @@ -68,6 +74,6 @@ export const createInputModeReducer = ({ case 'RESET': default: - return { type: 'comment', action: 'create', postId } as const; + return { type: 'reset', action: 'reset' } as const; } }; diff --git a/apps/client/src/widgets/community/hooks/use-controll-input-box.ts b/apps/client/src/widgets/community/hooks/use-controll-input-box.ts index 8318b7f7..7e3c490e 100644 --- a/apps/client/src/widgets/community/hooks/use-controll-input-box.ts +++ b/apps/client/src/widgets/community/hooks/use-controll-input-box.ts @@ -1,28 +1,19 @@ -import { useEffect, useRef, useState } from 'react'; +import { useEffect, useState } from 'react'; import { InputBoxMode } from '@widgets/community/types/input-box-type'; import { LIMIT_SHORT_TEXT } from '@shared/constants/text-limits'; export const useControlledInputBox = (mode: InputBoxMode) => { - const [content, setContent] = useState( - 'initialContent' in mode ? mode.initialContent : '', - ); - const prevModeRef = useRef(mode); + const [content, setContent] = useState(''); - useEffect(() => { - const isStillCreatingMode = - prevModeRef.current.action === 'create' && mode.action === 'create'; - const typeChanged = prevModeRef.current.type !== mode.type; - - prevModeRef.current = mode; + const initialContent = 'initialContent' in mode ? mode.initialContent : null; - if (isStillCreatingMode && typeChanged) { - return; + useEffect(() => { + if (initialContent !== null) { + setContent(initialContent); } - - setContent('initialContent' in mode ? mode.initialContent : ''); - }, [mode]); + }, [initialContent]); const handleChange = (e: React.ChangeEvent) => { const next = e.target.value; @@ -32,7 +23,7 @@ export const useControlledInputBox = (mode: InputBoxMode) => { }; const reset = () => { - setContent('initialContent' in mode ? mode.initialContent : ''); + setContent(''); }; return { content, handleChange, reset }; diff --git a/apps/client/src/widgets/community/types/input-box-type.ts b/apps/client/src/widgets/community/types/input-box-type.ts index 3714f415..d121f1b4 100644 --- a/apps/client/src/widgets/community/types/input-box-type.ts +++ b/apps/client/src/widgets/community/types/input-box-type.ts @@ -1,6 +1,7 @@ import { Image } from '@shared/types/type.ts'; export type InputBoxMode = + | { type: 'reset'; action: 'reset' } | { type: 'comment'; action: 'create'; postId: string; images?: Image[] } | { type: 'comment'; @@ -24,6 +25,7 @@ export type InputBoxMode = }; export type ReducerAction = + | { type: 'COMMENT_CREATE' } | { type: 'COMMENT_EDIT'; commentId: number;