@@ -13,7 +13,7 @@ interface ChatState {
1313}
1414
1515interface ChatAction {
16- type : 'SET_MESSAGES' | 'ADD_MESSAGE' | 'SET_LOADING' | 'SET_SENDING' | 'SET_ERROR' | 'SET_ISSUE' | 'SET_ISSUE_NUMBER' | 'SET_REFRESHING' ;
16+ type : 'SET_MESSAGES' | 'ADD_MESSAGE' | 'UPDATE_MESSAGE' | 'DELETE_MESSAGE' | ' SET_LOADING' | 'SET_SENDING' | 'SET_ERROR' | 'SET_ISSUE' | 'SET_ISSUE_NUMBER' | 'SET_REFRESHING' ;
1717 payload : any ;
1818}
1919
@@ -33,6 +33,18 @@ const chatReducer = (state: ChatState, action: ChatAction): ChatState => {
3333 return { ...state , messages : action . payload , loading : false } ;
3434 case 'ADD_MESSAGE' :
3535 return { ...state , messages : [ ...state . messages , action . payload ] } ;
36+ case 'UPDATE_MESSAGE' :
37+ return {
38+ ...state ,
39+ messages : state . messages . map ( msg =>
40+ msg . id === action . payload . id ? { ...msg , content : action . payload . content , isEdited : true } : msg
41+ )
42+ } ;
43+ case 'DELETE_MESSAGE' :
44+ return {
45+ ...state ,
46+ messages : state . messages . filter ( msg => msg . id !== action . payload )
47+ } ;
3648 case 'SET_LOADING' :
3749 return { ...state , loading : action . payload } ;
3850 case 'SET_SENDING' :
@@ -54,6 +66,8 @@ interface ChatContextType {
5466 state : ChatState ;
5567 dispatch : React . Dispatch < ChatAction > ;
5668 sendMessage : ( content : string , issueNumber ?: number ) => Promise < void > ;
69+ editMessage : ( messageId : number , content : string ) => Promise < void > ;
70+ deleteMessage : ( messageId : number ) => Promise < void > ;
5771 refreshMessages : ( issueNumber ?: number ) => Promise < void > ;
5872 fetchIssueDetails : ( issueNumber ?: number ) => Promise < void > ;
5973 setCurrentIssueNumber : ( issueNumber : number ) => void ;
@@ -169,6 +183,105 @@ export const ChatProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
169183 }
170184 } , [ state . currentIssueNumber , state . isRefreshing ] ) ;
171185
186+ // 즉시 새로고침 함수 (디바운싱 없음)
187+ const refreshMessagesImmediately = useCallback ( async ( issueNumber ?: number ) => {
188+ try {
189+ console . log ( '🚀 Immediate refresh called with issueNumber:' , issueNumber ) ;
190+
191+ const token = localStorage . getItem ( 'github_token' ) ;
192+ if ( ! token ) {
193+ console . log ( '❌ No token found, returning early' ) ;
194+ return ;
195+ }
196+
197+ const targetIssueNumber = issueNumber || state . currentIssueNumber ;
198+ if ( ! targetIssueNumber ) {
199+ console . log ( '❌ No issue number found, returning early' ) ;
200+ return ;
201+ }
202+
203+ console . log ( '📡 Fetching comments from GitHub API immediately...' ) ;
204+ dispatch ( { type : 'SET_LOADING' , payload : true } ) ;
205+
206+ const comments = await githubAPI . getMessages ( token , { } , targetIssueNumber ) ;
207+ console . log ( '📨 Comments received:' , comments . length ) ;
208+
209+ const messages : ChatMessage [ ] = comments . map ( comment => ( {
210+ id : comment . id ,
211+ content : comment . body ,
212+ author : {
213+ id : comment . user . id ,
214+ username : comment . user . login ,
215+ avatar : comment . user . avatar_url ,
216+ } ,
217+ timestamp : new Date ( comment . created_at ) . toLocaleString ( 'ko-KR' ) ,
218+ isEdited : comment . created_at !== comment . updated_at ,
219+ isOwn : false ,
220+ htmlUrl : comment . html_url ,
221+ } ) ) ;
222+
223+ console . log ( '💬 Messages converted:' , messages . length ) ;
224+ dispatch ( { type : 'SET_MESSAGES' , payload : messages } ) ;
225+ dispatch ( { type : 'SET_LOADING' , payload : false } ) ;
226+ console . log ( '✅ Immediate refresh completed successfully' ) ;
227+
228+ } catch ( error ) {
229+ console . error ( '❌ 즉시 새로고침 실패:' , error ) ;
230+ dispatch ( { type : 'SET_ERROR' , payload : '메시지를 불러오는데 실패했습니다.' } ) ;
231+ dispatch ( { type : 'SET_LOADING' , payload : false } ) ;
232+ }
233+ } , [ state . currentIssueNumber ] ) ;
234+
235+ const editMessage = useCallback ( async ( messageId : number , content : string ) => {
236+ try {
237+ const token = localStorage . getItem ( 'github_token' ) ;
238+ if ( ! token ) throw new Error ( '토큰이 없습니다.' ) ;
239+
240+ await githubAPI . editMessage ( token , messageId , content ) ;
241+
242+ // 메시지 수정 후 즉시 새로고침
243+ const targetIssueNumber = state . currentIssueNumber ;
244+ if ( targetIssueNumber ) {
245+ console . log ( '🔄 Auto-refreshing messages after edit...' ) ;
246+ await refreshMessagesImmediately ( targetIssueNumber ) ;
247+ } else {
248+ // 새로고침이 실패하면 로컬 상태에서만 업데이트
249+ dispatch ( {
250+ type : 'UPDATE_MESSAGE' ,
251+ payload : {
252+ id : messageId ,
253+ content : content
254+ }
255+ } ) ;
256+ }
257+ } catch ( error ) {
258+ console . error ( '메시지 수정 실패:' , error ) ;
259+ dispatch ( { type : 'SET_ERROR' , payload : '메시지 수정에 실패했습니다.' } ) ;
260+ }
261+ } , [ state . currentIssueNumber , refreshMessagesImmediately ] ) ;
262+
263+ const deleteMessage = useCallback ( async ( messageId : number ) => {
264+ try {
265+ const token = localStorage . getItem ( 'github_token' ) ;
266+ if ( ! token ) throw new Error ( '토큰이 없습니다.' ) ;
267+
268+ await githubAPI . deleteMessage ( token , messageId ) ;
269+
270+ // 메시지 삭제 후 즉시 새로고침
271+ const targetIssueNumber = state . currentIssueNumber ;
272+ if ( targetIssueNumber ) {
273+ console . log ( '🔄 Auto-refreshing messages after deletion...' ) ;
274+ await refreshMessagesImmediately ( targetIssueNumber ) ;
275+ } else {
276+ // 새로고침이 실패하면 로컬 상태에서만 제거
277+ dispatch ( { type : 'DELETE_MESSAGE' , payload : messageId } ) ;
278+ }
279+ } catch ( error ) {
280+ console . error ( '메시지 삭제 실패:' , error ) ;
281+ dispatch ( { type : 'SET_ERROR' , payload : '메시지 삭제에 실패했습니다.' } ) ;
282+ }
283+ } , [ state . currentIssueNumber , refreshMessagesImmediately ] ) ;
284+
172285 const fetchIssueDetails = useCallback ( async ( issueNumber ?: number ) => {
173286 try {
174287 const token = localStorage . getItem ( 'github_token' ) ;
@@ -194,6 +307,8 @@ export const ChatProvider: React.FC<{ children: ReactNode }> = ({ children }) =>
194307 state,
195308 dispatch,
196309 sendMessage,
310+ editMessage,
311+ deleteMessage,
197312 refreshMessages,
198313 fetchIssueDetails,
199314 setCurrentIssueNumber
0 commit comments