diff --git a/src/components/TopicDetail/TopicDetailAI.tsx b/src/components/TopicDetail/TopicDetailAI.tsx index 4dd0a910c..f2c6d74e7 100644 --- a/src/components/TopicDetail/TopicDetailAI.tsx +++ b/src/components/TopicDetail/TopicDetailAI.tsx @@ -7,6 +7,7 @@ import { Fragment, useCallback, useEffect, + useMemo, } from 'react'; import { billingDetailsOptions } from '../../queries/billing'; import { getAiCourseLimitOptions } from '../../queries/ai-course'; @@ -18,7 +19,7 @@ import { Loader2Icon, LockIcon, SendIcon, - Trash2 + Trash2, } from 'lucide-react'; import { showLoginPopup } from '../../lib/popup'; import { cn } from '../../lib/classname'; @@ -60,6 +61,7 @@ export function TopicDetailAI(props: TopicDetailAIProps) { const textareaRef = useRef(null); const scrollareaRef = useRef(null); + const formRef = useRef(null); const sanitizedTopicId = topicId?.includes('@') ? topicId?.split('@')?.[1] @@ -95,10 +97,9 @@ export function TopicDetailAI(props: TopicDetailAIProps) { const isLimitExceeded = (tokenUsage?.used || 0) >= (tokenUsage?.limit || 0); const isPaidUser = userBillingDetails?.status === 'active'; - const handleChatSubmit = (e: FormEvent) => { - e.preventDefault(); + const handleChatSubmit = (overrideMessage?: string) => { + const trimmedMessage = (overrideMessage ?? message).trim(); - const trimmedMessage = message.trim(); if ( !trimmedMessage || isStreamingMessage || @@ -228,6 +229,27 @@ export function TopicDetailAI(props: TopicDetailAIProps) { roadmapTreeMapping?.subjects && roadmapTreeMapping?.subjects?.length > 0; const hasChatHistory = aiChatHistory.length > 1; + const testMyKnowledgePrompt = + 'Act as an interviewer and test my understanding of this topic'; + const explainTopicPrompt = 'Explain this topic in detail'; + const predefinedMessages = useMemo( + () => [ + { + label: 'Explain like I am five', + message: 'Explain this topic like I am a 5 years old', + }, + { + label: 'Test my Knowledge', + message: testMyKnowledgePrompt, + }, + { + label: 'Explain Topic', + message: explainTopicPrompt, + }, + ], + [], + ); + return (
{isDataLoading && ( @@ -332,6 +354,24 @@ export function TopicDetailAI(props: TopicDetailAIProps) { )}
+
+ {predefinedMessages.map((m) => ( + { + setMessage(m.message); + handleChatSubmit(m.message); + }} + /> + ))} +
+
{aiChatHistory.map((chat, index) => { + const isTextMyKnowledgePrompt = + chat.role === 'user' && + chat.content === testMyKnowledgePrompt; + const isTextExplainTopicPrompt = + chat.role === 'user' && chat.content === explainTopicPrompt; + + let content = chat.content; + if (isTextMyKnowledgePrompt) { + content = 'Starting Interview'; + } else if (isTextExplainTopicPrompt) { + content = 'Explain Topic'; + } + return ( @@ -364,8 +417,12 @@ export function TopicDetailAI(props: TopicDetailAIProps) {
{ + e.preventDefault(); + handleChatSubmit(); + }} > {isLimitExceeded && isLoggedIn() && (
@@ -416,7 +473,8 @@ export function TopicDetailAI(props: TopicDetailAIProps) { autoFocus={true} onKeyDown={(e) => { if (e.key === 'Enter' && !e.shiftKey) { - handleChatSubmit(e as unknown as FormEvent); + e.preventDefault(); + handleChatSubmit(); } }} ref={textareaRef} @@ -432,3 +490,22 @@ export function TopicDetailAI(props: TopicDetailAIProps) {
); } + +type PredefinedMessageButtonProps = { + label: string; + message: string; + onClick: () => void; +}; + +function PredefinedMessageButton(props: PredefinedMessageButtonProps) { + const { label, message, onClick } = props; + + return ( + + ); +}