|
| 1 | +import { getUser } from '@/actions/user'; |
| 2 | +import { ChatbotIntro } from '@/components/service/chatbot/ChatbotIntro'; |
| 3 | +import { Chating } from '@/components/service/chatbot/Chating'; |
| 4 | +import { Chat } from '@/types/chat'; |
| 5 | +import { getChats } from '@/apis/chat'; |
| 6 | +import { redirect } from 'next/navigation'; |
| 7 | +import { Suspense } from 'react'; |
| 8 | +import ChatbotLoading from './loading'; |
| 9 | + |
| 10 | +// 채팅 컨텐츠 컴포넌트 - 정렬된 데이터만 반환 |
| 11 | +async function ChatContent({ |
| 12 | + user, |
| 13 | + storageId, |
| 14 | + categoryId, |
| 15 | +}: { |
| 16 | + user: any; |
| 17 | + storageId: string; |
| 18 | + categoryId: string; |
| 19 | +}) { |
| 20 | + try { |
| 21 | + // 채팅 내역 가져오기 |
| 22 | + const initialChats = await getChats(user.id, categoryId, storageId); |
| 23 | + |
| 24 | + // 채팅 내역 시간순 정렬 (오래된 순으로 정렬하여 시간 흐름대로 표시) |
| 25 | + const sortedChats = [...initialChats].sort((a, b) => { |
| 26 | + const dateA = new Date(a.createdAt || 0); |
| 27 | + const dateB = new Date(b.createdAt || 0); |
| 28 | + return dateA.getTime() - dateB.getTime(); // 오래된 순 정렬 |
| 29 | + }); |
| 30 | + |
| 31 | + // 채팅 내역이 없는 경우 기본 메시지 표시 |
| 32 | + if (sortedChats.length === 0) { |
| 33 | + sortedChats.push({ |
| 34 | + role: 'assistant', |
| 35 | + content: '안녕하세요! 도움이 필요하신가요?', |
| 36 | + }); |
| 37 | + } |
| 38 | + |
| 39 | + return <Chating initialChats={sortedChats} />; |
| 40 | + } catch (error) { |
| 41 | + console.error('채팅 내역 로드 중 오류 발생:', error); |
| 42 | + |
| 43 | + // 오류 발생 시 기본 메시지 표시 |
| 44 | + const fallbackChats = [ |
| 45 | + { |
| 46 | + role: 'assistant', |
| 47 | + content: |
| 48 | + '채팅 내역을 불러오는 중 문제가 발생했습니다. 새로고침 해보시거나 잠시 후 다시 시도해주세요.', |
| 49 | + }, |
| 50 | + ]; |
| 51 | + |
| 52 | + return <Chating initialChats={fallbackChats} />; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +// 메인 페이지 컴포넌트 |
| 57 | +export default async function ChatbotPage({ |
| 58 | + params, |
| 59 | + searchParams, |
| 60 | +}: { |
| 61 | + params: Promise<{ storageId: string }>; |
| 62 | + searchParams: Promise<{ categoryId?: string }>; |
| 63 | +}) { |
| 64 | + const { storageId } = await params; |
| 65 | + const { categoryId } = await searchParams; |
| 66 | + |
| 67 | + // 사용자 정보 가져오기 |
| 68 | + const user = await getUser(); |
| 69 | + |
| 70 | + if (!user) { |
| 71 | + // 사용자 정보가 없으면 로그인 페이지로 리다이렉트 |
| 72 | + redirect( |
| 73 | + '/auth/login?redirect=' + |
| 74 | + encodeURIComponent(`/chatbot/s/${storageId}?categoryId=${categoryId}`) |
| 75 | + ); |
| 76 | + } |
| 77 | + |
| 78 | + // 카테고리 ID가 없으면 에러 메시지 표시 |
| 79 | + if (!categoryId) { |
| 80 | + return ( |
| 81 | + <> |
| 82 | + <ChatbotIntro showSelectBox={false} /> |
| 83 | + <div className="flex flex-col justify-center items-center h-[calc(100%-160px)]"> |
| 84 | + <div className="text-center text-secondary"> |
| 85 | + <h2 className="text-xl font-bold mb-2">카테고리 정보가 필요합니다</h2> |
| 86 | + <p>올바른 카테고리 ID와 함께 페이지에 접근해주세요.</p> |
| 87 | + </div> |
| 88 | + </div> |
| 89 | + </> |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + return ( |
| 94 | + <> |
| 95 | + {/* Suspense를 사용하여 컨텐츠 로딩 중에는 loading.tsx의 스켈레톤이 표시됨 */} |
| 96 | + <Suspense fallback={<ChatbotLoading />}> |
| 97 | + <ChatbotIntro showSelectBox={false} /> |
| 98 | + <ChatContent user={user} storageId={storageId} categoryId={categoryId} /> |
| 99 | + </Suspense> |
| 100 | + </> |
| 101 | + ); |
| 102 | +} |
| 103 | + |
| 104 | +// 이 함수는 동적 라우트의 정적 생성을 위한 함수입니다 (선택적) |
| 105 | +export async function generateStaticParams() { |
| 106 | + // 대부분의 경우 동적 페이지이므로 빈 배열 반환 |
| 107 | + return []; |
| 108 | +} |
0 commit comments