@@ -13,6 +13,20 @@ import {
1313const MAX_VISIBLE_COMMENTS = 3 ;
1414const POPULAR_LIKE_THRESHOLD = 5 ;
1515
16+ const getCreatedAtTime = ( question ) => new Date ( question . createdAt ?? 0 ) . getTime ( ) ;
17+
18+ const sortQuestionGroups = ( groups ) => ( {
19+ popularQuestions : [ ...groups . popularQuestions ] . sort (
20+ ( a , b ) => ( b . likeCount ?? 0 ) - ( a . likeCount ?? 0 ) || getCreatedAtTime ( b ) - getCreatedAtTime ( a )
21+ ) ,
22+ unresolvedQuestions : [ ...groups . unresolvedQuestions ] . sort (
23+ ( a , b ) => getCreatedAtTime ( b ) - getCreatedAtTime ( a )
24+ ) ,
25+ resolvedQuestions : [ ...groups . resolvedQuestions ] . sort (
26+ ( a , b ) => getCreatedAtTime ( b ) - getCreatedAtTime ( a )
27+ ) ,
28+ } ) ;
29+
1630const getQuestionGroupKey = ( question ) => {
1731 if ( question . isResolved ) return 'resolvedQuestions' ;
1832 if ( ( question . likeCount ?? 0 ) >= POPULAR_LIKE_THRESHOLD ) return 'popularQuestions' ;
@@ -33,7 +47,7 @@ const regroupQuestions = (questions) => {
3347 groups [ getQuestionGroupKey ( question ) ] . push ( question ) ;
3448 } ) ;
3549
36- return groups ;
50+ return sortQuestionGroups ( groups ) ;
3751} ;
3852
3953const updateQuestionGroupsByCommentEvent = ( groups , eventData ) => {
@@ -62,6 +76,41 @@ const updateQuestionGroupsByCommentEvent = (groups, eventData) => {
6276 return hasUpdatedQuestion ? regroupQuestions ( questions ) : groups ;
6377} ;
6478
79+ const updateQuestionGroupsByQuestionEvent = ( groups , eventData ) => {
80+ if ( ! eventData ?. questionId ) return groups ;
81+
82+ const questions = [
83+ ...groups . popularQuestions ,
84+ ...groups . unresolvedQuestions ,
85+ ...groups . resolvedQuestions ,
86+ ] ;
87+
88+ if ( eventData . isDeleted ) {
89+ return regroupQuestions ( questions . filter ( question => question . questionId !== eventData . questionId ) ) ;
90+ }
91+
92+ let hasUpdatedQuestion = false ;
93+ const updatedQuestions = questions . map ( question => {
94+ if ( question . questionId !== eventData . questionId ) return question ;
95+
96+ hasUpdatedQuestion = true ;
97+ const isResolved = eventData . isResolved ?? question . isResolved ;
98+ const likeCount = eventData . likeCount ?? question . likeCount ;
99+
100+ return {
101+ ...question ,
102+ content : eventData . content ?? question . content ,
103+ isResolved,
104+ isPopular : ! isResolved && ( likeCount ?? 0 ) >= POPULAR_LIKE_THRESHOLD ,
105+ likeCount,
106+ iLiked : eventData . isLiked ?? question . iLiked ,
107+ isLiked : eventData . isLiked ?? question . isLiked ,
108+ } ;
109+ } ) ;
110+
111+ return hasUpdatedQuestion ? regroupQuestions ( updatedQuestions ) : groups ;
112+ } ;
113+
65114const addQuestionToGroups = ( groups , question ) => {
66115 if ( ! question ?. questionId ) return groups ;
67116
@@ -274,6 +323,11 @@ function QnAListPage() {
274323 applyQuestionGroups ( nextGroups ) ;
275324 } , [ applyQuestionGroups , buildQuestionFromCreatedEvent ] ) ;
276325
326+ const handleQuestionUpdatedEvent = useCallback ( ( eventData ) => {
327+ const nextGroups = updateQuestionGroupsByQuestionEvent ( questionGroupsRef . current , eventData ) ;
328+ applyQuestionGroups ( nextGroups ) ;
329+ } , [ applyQuestionGroups ] ) ;
330+
277331 const handleUnderstandingCheckCreatedEvent = useCallback ( ( eventData ) => {
278332 if ( ! eventData ?. checkId ) return ;
279333 if ( understandingRef . current ?. current ?. checkId === eventData . checkId ) return ;
@@ -335,6 +389,9 @@ function QnAListPage() {
335389 case 'question-created' :
336390 void handleQuestionCreatedEvent ( data ) ;
337391 break ;
392+ case 'question-updated' :
393+ handleQuestionUpdatedEvent ( data ) ;
394+ break ;
338395 case 'understanding-check-created' :
339396 handleUnderstandingCheckCreatedEvent ( data ) ;
340397 break ;
@@ -348,6 +405,7 @@ function QnAListPage() {
348405 } , [
349406 handleCommentCreatedEvent ,
350407 handleQuestionCreatedEvent ,
408+ handleQuestionUpdatedEvent ,
351409 handleUnderstandingCheckCreatedEvent ,
352410 handleUnderstandingResponseUpdatedEvent ,
353411 ] ) ;
@@ -415,14 +473,12 @@ function QnAListPage() {
415473 if ( ! res . ok ) throw new Error ( ) ;
416474 const json = await res . json ( ) ;
417475 if ( json . isSuccess ) {
418- const update = ( list ) => list . map ( q =>
419- q . questionId === questionId
420- ? { ...q , likeCount : json . result . likeCount , iLiked : json . result . isLiked }
421- : q
422- ) ;
423- setPopularQuestions ( update ) ;
424- setUnresolvedQuestions ( update ) ;
425- setResolvedQuestions ( update ) ;
476+ const nextGroups = updateQuestionGroupsByQuestionEvent ( questionGroupsRef . current , {
477+ questionId,
478+ likeCount : json . result . likeCount ,
479+ isLiked : json . result . isLiked ,
480+ } ) ;
481+ applyQuestionGroups ( nextGroups ) ;
426482 }
427483 } catch ( err ) {
428484 console . error ( '좋아요 실패:' , err ) ;
0 commit comments