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
49 changes: 41 additions & 8 deletions src/features/Common/AIChatModal.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useQueryClient } from '@tanstack/react-query' // 1. useQueryClient ์ž„ํฌํŠธ
import { useQuery, useQueryClient } from '@tanstack/react-query'
import React, { useEffect, useRef, useState } from 'react'

import ChatIcon from '@/assets/icons/common/chat.svg'
Expand All @@ -20,11 +20,31 @@ function AIChatModal({ isHome = true }: AIChatModalProps) {
const [isLoading, setIsLoading] = useState(false)
const chatBoxRef = useRef<HTMLDivElement>(null)

const { data: historyData, isLoading: isHistoryLoading } = useQuery({
queryKey: ['chatHistory'],
queryFn: nlpApi.getHistory,
})

useEffect(() => {
if (historyData?.isSuccess && historyData.result) {
const rawMessages = historyData.result.messages
const mappedMessages: ChatMessage[] = Array.isArray(rawMessages)
? rawMessages.map((msg) => ({
id: crypto.randomUUID(),
sender: msg.role === 'user' ? 'user' : 'bot',
text: msg.content ?? '',
}))
: []

setMessages(mappedMessages)
}
}, [historyData])

useEffect(() => {
if (chatBoxRef.current) {
chatBoxRef.current.scrollTop = chatBoxRef.current.scrollHeight
}
}, [messages, isLoading])
}, [messages, isLoading, isHistoryLoading])

const handleSendMessage = async () => {
if (!inputValue.trim() || isLoading) return
Expand Down Expand Up @@ -52,6 +72,8 @@ function AIChatModal({ isHome = true }: AIChatModalProps) {
}
setMessages((prev) => [...prev, botMessage])

queryClient.invalidateQueries({ queryKey: ['chatHistory'] })

if (response.result.action === 'UPDATED') {
queryClient.invalidateQueries({ queryKey: ['calendar'] })
queryClient.invalidateQueries({ queryKey: ['events'] })
Expand All @@ -67,14 +89,18 @@ function AIChatModal({ isHome = true }: AIChatModalProps) {
},
])
}
// eslint-disable-next-line @typescript-eslint/no-unused-vars
} catch (error) {
const errorMessage =
error instanceof Error
? error.message
: 'AI ๋น„์„œ ์„œ๋ฒ„์™€ ์—ฐ๊ฒฐ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค. ์ž ์‹œ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”.'

setMessages((prev) => [
...prev,
{
id: crypto.randomUUID(),
sender: 'bot',
text: 'AI ๋น„์„œ ์„œ๋ฒ„์™€ ์—ฐ๊ฒฐ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค. ์ž ์‹œ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”.',
text: `์—ฐ๊ฒฐ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค. (์›์ธ: ${errorMessage})`,
},
])
} finally {
Expand All @@ -88,7 +114,7 @@ function AIChatModal({ isHome = true }: AIChatModalProps) {
}
}

const isChatEmpty = messages.length === 0 && !isLoading
const isChatEmpty = messages.length === 0 && !isLoading && !isHistoryLoading
const hideEmptyChatBox = !isHome && isChatEmpty

return (
Expand All @@ -101,7 +127,14 @@ function AIChatModal({ isHome = true }: AIChatModalProps) {
</S.Title>

<S.ChatBox ref={chatBoxRef} isEmpty={isChatEmpty} isHidden={hideEmptyChatBox}>
{isChatEmpty ? (
{isHistoryLoading ? (
<S.BotMessageWrapper>
<img src={RobotIcon} width={32} height={32} style={{ flexShrink: 0 }} alt="robot" />
<S.BotContentArea>
<S.BotFallbackBubble>์ž ์‹œ๋งŒ ๊ธฐ๋‹ค๋ ค์ฃผ์„ธ์š”...</S.BotFallbackBubble>
</S.BotContentArea>
</S.BotMessageWrapper>
) : isChatEmpty ? (
<S.EmptyState>
<img src={ChatIcon} alt="์ฑ„ํŒ… ์‹œ์ž‘" width="150" height="150" />
</S.EmptyState>
Expand Down Expand Up @@ -150,11 +183,11 @@ function AIChatModal({ isHome = true }: AIChatModalProps) {
onChange={(e) => setInputValue(e.target.value)}
onKeyDown={handleKeyDown}
placeholder="์˜ˆ์‹œ) ๋‚ด์ผ ์˜คํ›„ 3์‹œ ์น˜๊ณผ ์ง„๋ฃŒ ๋ฐ›์œผ๋Ÿฌ ๊ฐ"
disabled={isLoading}
disabled={isLoading || isHistoryLoading}
/>
<S.SendButton
onClick={handleSendMessage}
disabled={isLoading || !inputValue.trim()}
disabled={isLoading || isHistoryLoading || !inputValue.trim()}
aria-label="์ „์†ก"
>
โ†‘
Expand Down
58 changes: 54 additions & 4 deletions src/features/Friends/SharedScheduleItem.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
import { useMutation, useQueryClient } from '@tanstack/react-query'

import { eventShareApi } from '@/shared/api/friends/eventShare'
import { getErrorMessage } from '@/shared/utils'
import { useToastStore } from '@/store/useToastStore'

import * as S from './SharedScheduleItem.style'

interface SharedScheduleItemProps {
Expand All @@ -19,6 +25,9 @@ export default function SharedScheduleItem({
accentColor = '#5c6ac4',
onCancelSuccess,
}: SharedScheduleItemProps) {
const queryClient = useQueryClient()
const { showToast } = useToastStore()

const formatDate = (dateStr: string) => {
if (!dateStr) return ''
const date = new Date(dateStr)
Expand All @@ -35,9 +44,45 @@ export default function SharedScheduleItem({
? formatDate(startDate)
: `${formatDate(startDate)} - ${formatDate(endDate)}`

const leaveEventMutation = useMutation({
mutationFn: () => eventShareApi.leaveEvent(eventId),
onSuccess: (response) => {
if (response.isSuccess) {
queryClient.invalidateQueries({ queryKey: ['calendar'] })
queryClient.invalidateQueries({ queryKey: ['events'] })
queryClient.invalidateQueries({ queryKey: ['todos'] })

showToast({
title: '๊ทธ๋ฃน ํƒˆํ‡ด ์™„๋ฃŒ',
message: '์„ฑ๊ณต์ ์œผ๋กœ ํƒˆํ‡ด๋˜์—ˆ์Šต๋‹ˆ๋‹ค.',
toastType: 'success',
})

onCancelSuccess?.()
} else {
showToast({
title: 'ํƒˆํ‡ด ์ฒ˜๋ฆฌ ์‹คํŒจ',
message: response.message || 'ํƒˆํ‡ด ์ฒ˜๋ฆฌ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค.',
toastType: 'error',
})
}
},
onError: (error) => {
console.error(error)
const errorMessage = getErrorMessage(error)

showToast({
title: '์˜ค๋ฅ˜ ๋ฐœ์ƒ',
message: errorMessage || '์„œ๋ฒ„ ์—ฐ๊ฒฐ์— ์‹คํŒจํ–ˆ์Šต๋‹ˆ๋‹ค. ์ž ์‹œ ํ›„ ๋‹ค์‹œ ์‹œ๋„ํ•ด์ฃผ์„ธ์š”.',
toastType: 'error',
})
},
})

const handleCancelShare = () => {
console.log(`eventId ${eventId} ๊ณต์œ  ์ทจ์†Œ ํด๋ฆญ`)
onCancelSuccess?.()
if (window.confirm('์ •๋ง ์ด ๊ณต์œ  ์ด๋ฒคํŠธ์—์„œ ํƒˆํ‡ดํ•˜์‹œ๊ฒ ์Šต๋‹ˆ๊นŒ?')) {
leaveEventMutation.mutate()
}
}

return (
Expand All @@ -50,8 +95,13 @@ export default function SharedScheduleItem({

<S.MetaArea>
<S.SharerBadge>๊ณต์œ ์ž: {sharerName}</S.SharerBadge>
<S.CancelButton bgColor="#fff1f0" textColor="#ff4d4f" onClick={handleCancelShare}>
๊ณต์œ  ์ทจ์†Œ
<S.CancelButton
bgColor="#fff1f0"
textColor="#ff4d4f"
onClick={handleCancelShare}
disabled={leaveEventMutation.isPending}
>
{leaveEventMutation.isPending ? '์ทจ์†Œ ์ค‘...' : '๊ณต์œ  ์ทจ์†Œ'}
</S.CancelButton>
</S.MetaArea>
</S.Container>
Expand Down
7 changes: 7 additions & 0 deletions src/shared/api/friends/eventShare.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type {
ActionApiResponse,
InvitationsApiResponse,
LeaveEventResponse,
SharedEventsApiResponse,
} from '@/shared/types/eventShare/eventShare'

Expand Down Expand Up @@ -32,4 +33,10 @@ export const eventShareApi = {
const { data } = await axiosInstance.get<InvitationsApiResponse>(`${BASE_URL}/invitations`)
return data
},
leaveEvent: async (eventId: number): Promise<LeaveEventResponse> => {
const { data } = await axiosInstance.delete<LeaveEventResponse>(
`/events/${eventId}/participants/leave`,
)
return data
},
}
5 changes: 5 additions & 0 deletions src/shared/api/home/home.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type {
BriefingResponse,
ChatHistoryResponse,
ChatResponse,
ReminderResponse,
SuggestionListResponse,
Expand Down Expand Up @@ -47,4 +48,8 @@ export const nlpApi = {
const res = await axiosInstance.post<ChatResponse>('/chat', { message })
return res.data
},
getHistory: async (): Promise<ChatHistoryResponse> => {
const res = await axiosInstance.get<ChatHistoryResponse>('/chat/history')
return res.data
},
}
2 changes: 2 additions & 0 deletions src/shared/types/eventShare/eventShare.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,5 @@ export interface InvitationsResult {
export type InvitationsApiResponse = TCommonResponse<InvitationsResult>

export type ActionApiResponse = TCommonResponse<null>

export type LeaveEventResponse = TCommonResponse<null>
13 changes: 13 additions & 0 deletions src/shared/types/home/home.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,3 +75,16 @@ export interface ChatMessage {
text: string
action?: ChatActionType
}

export type ChatHistoryResponse = TCommonResponse<ChatHistory>

export interface ChatHistory {
messages: boolean | undefined
result: {
messages: {
role: 'user' | 'assistant'
content: string
}[]
summary: string | null
}
}
Loading