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
11 changes: 4 additions & 7 deletions src/app/chatbot/components/ChatBot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export default function ChatBot() {
setInput,
handleSubmit,
handleRecommendationClick,
handlePopularTerms,
handleTodaysTerm,
} = useChatBot();

return (
Expand Down Expand Up @@ -53,19 +55,14 @@ export default function ChatBot() {
<FireIcon width={14} height={14} className="text-[#FB923C]" />
}
label="인기 용어"
onClick={() =>
handleSubmit(
undefined,
"React, Next.js, Docker 이 세 가지 용어를 각각 설명해줘."
)
}
onClick={handlePopularTerms}
/>
<QuickActionButton
icon={
<StarIcon width={14} height={14} className="text-[#FACC15]" />
}
label="오늘의 용어"
onClick={() => handleSubmit(undefined, "TypeScript")}
onClick={handleTodaysTerm}
/>
<QuickActionButton
icon={
Expand Down
31 changes: 31 additions & 0 deletions src/app/chatbot/utils/quick-actions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* 챗봇 퀵 액션 유틸리티
*/

import { getPopularTerms } from "@/lib/recommendations";
import { getTodaysTerm } from "@/lib/terms";
import type { CategoryType } from "@/config/categories";

/**
* 인기 용어 3개 가져오기 (카테고리 기반)
*/
export async function getPopularTermsQuery(
category: CategoryType = "all"
): Promise<string> {
const terms = await getPopularTerms(category, 3);
if (terms.length === 0) {
return "React, Next.js, Docker 이 세 가지 용어를 각각 설명해줘.";
}
return `${terms.join(", ")} 이 세 가지 용어를 각각 설명해줘.`;
}

/**
* 오늘의 용어 가져오기
*/
export async function getTodaysTermQuery(): Promise<string> {
const todayTerm = await getTodaysTerm();
if (!todayTerm) {
return "TypeScript";
}
return todayTerm.termKo;
}
21 changes: 21 additions & 0 deletions src/hooks/useChatBot.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

import { useState, useRef, useEffect, useCallback } from "react";
import { getChatResponse } from "@/app/chatbot/utils/actions";
import {
getPopularTermsQuery,
getTodaysTermQuery,
} from "@/app/chatbot/utils/quick-actions";
import { useUserData } from "@/contexts/auth";

interface Message {
role: "user" | "bot";
Expand All @@ -17,6 +22,8 @@ interface UseChatBotReturn {
setInput: (value: string) => void;
handleSubmit: (e?: React.FormEvent, customInput?: string) => Promise<void>;
handleRecommendationClick: (question: string) => void;
handlePopularTerms: () => Promise<void>;
handleTodaysTerm: () => Promise<void>;
}

const INITIAL_MESSAGE: Message = {
Expand All @@ -26,6 +33,7 @@ const INITIAL_MESSAGE: Message = {
};

export function useChatBot(): UseChatBotReturn {
const { userData } = useUserData();
const [messages, setMessages] = useState<Message[]>([INITIAL_MESSAGE]);
const [input, setInput] = useState("");
const [isLoading, setIsLoading] = useState(false);
Expand Down Expand Up @@ -68,6 +76,17 @@ export function useChatBot(): UseChatBotReturn {
[handleSubmit]
);

const handlePopularTerms = useCallback(async () => {
const category = userData?.selectedCategory || "all";
const query = await getPopularTermsQuery(category);
await handleSubmit(undefined, query);
}, [userData, handleSubmit]);

const handleTodaysTerm = useCallback(async () => {
const query = await getTodaysTermQuery();
await handleSubmit(undefined, query);
}, [handleSubmit]);

return {
messages,
input,
Expand All @@ -76,5 +95,7 @@ export function useChatBot(): UseChatBotReturn {
setInput,
handleSubmit,
handleRecommendationClick,
handlePopularTerms,
handleTodaysTerm,
};
}
16 changes: 16 additions & 0 deletions src/lib/recommendations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,19 @@ export async function getRecommendedTerms(
return [];
}
}

/**
* 인기 용어 가져오기 (카테고리 기반, 새로고침마다 랜덤)
*/
export async function getPopularTerms(
category: CategoryType,
count: number = 3
): Promise<string[]> {
try {
const terms = await getRecommendedTerms(category, count);
return terms.map((t) => t.term);
} catch (error) {
console.error("인기 용어 로드 실패:", error);
return [];
}
}
8 changes: 8 additions & 0 deletions src/lib/terms.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,11 @@ export function getTodaysTermServer(): TermIndexItem | null {

return index[todayIndex];
}

/**
* 서버에서 오늘의 용어 한국어 이름만 가져오기
*/
export function getTodaysTermNameServer(): string | null {
const term = getTodaysTermServer();
return term ? term.termKo : null;
}