Skip to content

Commit 07a9699

Browse files
committed
feat: 게시물 생성 및 수정 시 isVisible 속성 추가
1 parent df5a84c commit 07a9699

File tree

3 files changed

+42
-9
lines changed

3 files changed

+42
-9
lines changed

src/api/postings.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ type PostingMutationArg = {
88
articleTitle: string;
99
articleBody: string;
1010
images: File[] | null;
11+
isVisible: boolean;
1112
};
1213

1314
type PostingMutationResp = {
@@ -19,6 +20,7 @@ export const createPost = async ({
1920
articleTitle,
2021
articleBody,
2122
images,
23+
isVisible,
2224
}: PostingMutationArg) => {
2325
let imageUrls: string[] | undefined;
2426

@@ -35,6 +37,7 @@ export const createPost = async ({
3537
articleTitle,
3638
articleBody,
3739
images: imageUrls,
40+
isVisible,
3841
};
3942

4043
const res = await apiInstance.post<ServerResponse<PostingMutationResp>>(
@@ -51,6 +54,7 @@ export const mutatePost = async ({
5154
articleBody,
5255
images,
5356
articleId,
57+
isVisible,
5458
}: PostingMutationArg & {
5559
articleId: number;
5660
}) => {
@@ -69,6 +73,7 @@ export const mutatePost = async ({
6973
articleTitle,
7074
articleBody,
7175
images: imageUrls,
76+
isVisible,
7277
};
7378

7479
const res = await apiInstance.put<ServerResponse<PostingMutationResp>>(

src/hooks/useArticleFeedbackStream.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,14 @@ import { API_BASE_URL } from '@/api/config';
44

55
type FeedbackStatus = 'idle' | 'streaming' | 'completed' | 'error';
66

7+
const END_MARKER = '스트리밍 종료';
8+
79
export const useArticleFeedbackStream = (initialArticleId: number | null) => {
810
const [feedback, setFeedback] = useState('');
911
const [status, setStatus] = useState<FeedbackStatus>('idle');
1012
const [error, setError] = useState<string | null>(null);
1113
const eventSourceRef = useRef<EventSource | null>(null);
14+
const feedbackRef = useRef<string>('');
1215

1316
const closeStream = useCallback(() => {
1417
if (eventSourceRef.current) {
@@ -40,19 +43,40 @@ export const useArticleFeedbackStream = (initialArticleId: number | null) => {
4043
const parsed = JSON.parse(event.data);
4144
const chunk = parsed.content;
4245

46+
feedbackRef.current += chunk;
4347
setFeedback((prev) => prev + chunk);
44-
console.log(chunk);
48+
49+
if (feedbackRef.current.includes(END_MARKER)) {
50+
closeStream();
51+
const finalFeedback = feedbackRef.current
52+
.replace(END_MARKER, '')
53+
.trim();
54+
setFeedback(finalFeedback);
55+
feedbackRef.current = finalFeedback;
56+
setStatus('completed');
57+
}
4558
} catch (err) {
4659
console.log(err);
4760
}
4861
};
4962

5063
es.onerror = () => {
5164
closeStream();
52-
setFeedback((prev) => {
53-
setStatus(prev.length > 0 ? 'completed' : 'error');
54-
return prev;
55-
});
65+
66+
if (feedbackRef.current.includes(END_MARKER)) {
67+
const finalFeedback = feedbackRef.current
68+
.replace(END_MARKER, '')
69+
.trim();
70+
setFeedback(finalFeedback);
71+
feedbackRef.current = finalFeedback;
72+
setStatus('completed');
73+
} else if (feedbackRef.current.length > 0) {
74+
setError('응답이 비정상적으로 종료되었습니다.');
75+
setStatus('error');
76+
} else {
77+
setError('피드백 생성에 실패했습니다. 다시 시도해주세요.');
78+
setStatus('error');
79+
}
5680
};
5781
},
5882
[initialArticleId, closeStream]

src/pages/Posting/Posting.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,9 @@ const Posting = () => {
131131
return <Navigate to={PATH.TEAMS} />;
132132
}
133133

134-
const handleSaveArticle = async (): Promise<number | undefined> => {
134+
const handleSaveArticle = async (
135+
isVisible: boolean = true
136+
): Promise<number | undefined> => {
135137
if (isPending) return;
136138

137139
if (!postTitle) {
@@ -193,6 +195,7 @@ const Posting = () => {
193195
articleId: targetId,
194196
articleBody: articleBody,
195197
articleTitle: postTitle,
198+
isVisible: isVisible,
196199
});
197200
} else {
198201
const res = await createPost({
@@ -203,6 +206,7 @@ const Posting = () => {
203206
: null,
204207
articleBody: articleBody,
205208
articleTitle: postTitle,
209+
isVisible: isVisible,
206210
});
207211
targetId = res.articleId;
208212
}
@@ -261,7 +265,7 @@ const Posting = () => {
261265
return;
262266
}
263267

264-
const savedId = await handleSaveArticle();
268+
const savedId = await handleSaveArticle(false);
265269

266270
if (savedId) {
267271
startFeedbackStream(savedId);
@@ -328,8 +332,8 @@ const Posting = () => {
328332
gap="8px"
329333
>
330334
<GptFeedbackButton
331-
disabled={isPending || isFeedbackStreaming}
332-
isPending={isPending || isFeedbackStreaming}
335+
disabled={isPending || isFeedbackStreaming || isComplete}
336+
isPending={isPending || isFeedbackStreaming || isComplete}
333337
hasFeedback={!!feedback}
334338
onClick={handleFeedbackClick}
335339
>

0 commit comments

Comments
 (0)