Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { Icon } from '@bds/ui/icons';
import { PLACEHOLDER } from '@widgets/community/constant/input-placeholder';
import { useChangeInputMode } from '@widgets/community/context/input-mode-context';

import useClickOutside from '@shared/hooks/use-click-outside';

import CommunityModal from '../community-modal/community-modal';

import * as styles from './comment-input-box.css';
Expand Down Expand Up @@ -34,9 +36,17 @@ const CommentInputBox = ({
onClearImage,
}: CommentInputBoxProps) => {
const fileInputRef = useRef<HTMLInputElement>(null);
const wrapperRef = useRef<HTMLElement>(null);
const { mode, dispatch } = useChangeInputMode();
const { openModal, closeModal } = useModal();

const replyCreateMode = mode.type === 'reply' && mode.action === 'create';
useClickOutside(
wrapperRef,
() => dispatch({ type: 'RESET' }),
replyCreateMode,
);

const modalType: 'create' | 'edit' =
(mode.type === 'comment' || mode.type === 'reply') && mode.action === 'edit'
? 'edit'
Expand Down Expand Up @@ -105,7 +115,7 @@ const CommentInputBox = ({
: previewUrl;

return (
<section className={styles.commentWrapper}>
<section ref={wrapperRef} className={styles.commentWrapper}>
{displayImage && (
<div className={styles.imagePreviewWrapper}>
<img
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { useEffect, useRef, useState } from 'react';
import { useMutation, useQueryClient } from '@tanstack/react-query';

import CommentInputBox from '@widgets/community/components/comment-input-box/comment-input-box';
Expand Down Expand Up @@ -26,10 +26,17 @@ const DetailSection = ({ postId }: DetailSectionProps) => {

const [imageFile, setImageFile] = useState<File | null>(null);
const [imagePreview, setImagePreview] = useState<string | null>(null);
const prevModeRef = useRef(mode);

const queryClient = useQueryClient();

useEffect(() => {
const isStillCreatingMode =
prevModeRef.current.action === 'create' && mode.action === 'create';
const typeChanged = prevModeRef.current.type !== mode.type;

prevModeRef.current = mode;

if (mode.type === 'comment' && mode.action === 'edit') {
const remaining = (mode.images ?? []).filter(
(img) => !(mode.deleteImageIds ?? []).includes(img.imageId ?? -1),
Expand Down Expand Up @@ -65,6 +72,10 @@ const DetailSection = ({ postId }: DetailSectionProps) => {
return;
}

if (isStillCreatingMode && typeChanged) {
return;
}

setImagePreview(null);
setImageFile(null);
}, [mode]);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect, useState } from 'react';
import { useEffect, useRef, useState } from 'react';

import { InputBoxMode } from '@widgets/community/types/input-box-type';

Expand All @@ -8,8 +8,19 @@ export const useControlledInputBox = (mode: InputBoxMode) => {
const [content, setContent] = useState(
'initialContent' in mode ? mode.initialContent : '',
);
const prevModeRef = useRef(mode);

useEffect(() => {
const isStillCreatingMode =
prevModeRef.current.action === 'create' && mode.action === 'create';
const typeChanged = prevModeRef.current.type !== mode.type;

prevModeRef.current = mode;

if (isStillCreatingMode && typeChanged) {
return;
}

setContent('initialContent' in mode ? mode.initialContent : '');
}, [mode]);

Expand Down
Loading