mirror of
https://github.com/kamranahmedse/developer-roadmap.git
synced 2025-09-01 05:21:43 +02:00
wip
This commit is contained in:
@@ -1,18 +1,15 @@
|
|||||||
import { useQuery } from '@tanstack/react-query';
|
import { useQuery } from '@tanstack/react-query';
|
||||||
import { useState } from 'react';
|
import { useState } from 'react';
|
||||||
import { flushSync } from 'react-dom';
|
|
||||||
import { useToast } from '../../hooks/use-toast';
|
|
||||||
import { queryClient } from '../../stores/query-client';
|
import { queryClient } from '../../stores/query-client';
|
||||||
import { UpgradeAccountModal } from '../Billing/UpgradeAccountModal';
|
import { UpgradeAccountModal } from '../Billing/UpgradeAccountModal';
|
||||||
import { AlertCircleIcon } from 'lucide-react';
|
import { AlertCircleIcon } from 'lucide-react';
|
||||||
import { isLoggedIn } from '../../lib/jwt';
|
|
||||||
import { showLoginPopup } from '../../lib/popup';
|
|
||||||
import { getAiCourseLimitOptions } from '../../queries/ai-course';
|
import { getAiCourseLimitOptions } from '../../queries/ai-course';
|
||||||
import { billingDetailsOptions } from '../../queries/billing';
|
import { billingDetailsOptions } from '../../queries/billing';
|
||||||
import { AIQuizLayout } from './AIQuizLayout';
|
import { AIQuizLayout } from './AIQuizLayout';
|
||||||
import { GenerateAIQuiz } from './GenerateAIQuiz';
|
import { GenerateAIQuiz } from './GenerateAIQuiz';
|
||||||
import { aiQuizOptions, generateAIQuiz } from '../../queries/ai-quiz';
|
import { aiQuizOptions, generateAIQuiz } from '../../queries/ai-quiz';
|
||||||
import { AIQuizContent } from './AIQuizContent';
|
import { AIQuizContent } from './AIQuizContent';
|
||||||
|
import { LoadingChip } from '../LoadingChip';
|
||||||
|
|
||||||
type AIQuizProps = {
|
type AIQuizProps = {
|
||||||
quizSlug?: string;
|
quizSlug?: string;
|
||||||
@@ -54,18 +51,24 @@ export function AIQuiz(props: AIQuizProps) {
|
|||||||
<UpgradeAccountModal onClose={() => setShowUpgradeModal(false)} />
|
<UpgradeAccountModal onClose={() => setShowUpgradeModal(false)} />
|
||||||
)}
|
)}
|
||||||
|
|
||||||
{!isLoading && aiQuizError && (
|
<div className="relative grow">
|
||||||
<div className="absolute inset-0 z-10 flex h-full flex-col items-center justify-center bg-white">
|
{isLoading && (
|
||||||
<div className="flex flex-col items-center justify-center gap-2">
|
<div className="absolute inset-0 z-20 flex h-full flex-col items-center justify-center bg-white">
|
||||||
<AlertCircleIcon className="size-10 text-gray-500" />
|
<LoadingChip message="Loading Quiz" />
|
||||||
<p className="text-center">
|
|
||||||
{aiQuizError?.message || 'Something went wrong'}
|
|
||||||
</p>
|
|
||||||
</div>
|
</div>
|
||||||
</div>
|
)}
|
||||||
)}
|
|
||||||
|
{!isLoading && aiQuizError && (
|
||||||
|
<div className="absolute inset-0 z-20 flex h-full flex-col items-center justify-center bg-white">
|
||||||
|
<div className="flex flex-col items-center justify-center gap-2">
|
||||||
|
<AlertCircleIcon className="size-10 text-gray-500" />
|
||||||
|
<p className="text-center">
|
||||||
|
{aiQuizError?.message || 'Something went wrong'}
|
||||||
|
</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
<div className="grow">
|
|
||||||
{quizSlug && !aiQuizError && (
|
{quizSlug && !aiQuizError && (
|
||||||
<AIQuizContent
|
<AIQuizContent
|
||||||
quizSlug={quizSlug}
|
quizSlug={quizSlug}
|
||||||
|
@@ -12,7 +12,7 @@ import { useEffect, useId, useState } from 'react';
|
|||||||
import { isLoggedIn } from '../../lib/jwt';
|
import { isLoggedIn } from '../../lib/jwt';
|
||||||
import { showLoginPopup } from '../../lib/popup';
|
import { showLoginPopup } from '../../lib/popup';
|
||||||
import { UpgradeAccountModal } from '../Billing/UpgradeAccountModal';
|
import { UpgradeAccountModal } from '../Billing/UpgradeAccountModal';
|
||||||
import { useIsPaidUser } from '../../queries/billing';
|
import { billingDetailsOptions, useIsPaidUser } from '../../queries/billing';
|
||||||
import {
|
import {
|
||||||
clearQuestionAnswerChatMessages,
|
clearQuestionAnswerChatMessages,
|
||||||
storeQuestionAnswerChatMessages,
|
storeQuestionAnswerChatMessages,
|
||||||
@@ -27,24 +27,37 @@ import { getUrlParams } from '../../lib/browser';
|
|||||||
import { useParams } from '../../hooks/use-params';
|
import { useParams } from '../../hooks/use-params';
|
||||||
import { FormatItem } from '../ContentGenerator/FormatItem';
|
import { FormatItem } from '../ContentGenerator/FormatItem';
|
||||||
import { AIQuizLayout } from './AIQuizLayout';
|
import { AIQuizLayout } from './AIQuizLayout';
|
||||||
|
import { queryClient } from '../../stores/query-client';
|
||||||
|
import { useQuery } from '@tanstack/react-query';
|
||||||
|
import { getAiCourseLimitOptions } from '../../queries/ai-course';
|
||||||
|
|
||||||
const allowedFormats = ['mcq', 'open-ended', 'mixed'] as const;
|
const allowedFormats = ['mcq', 'open-ended', 'mixed'] as const;
|
||||||
export type AllowedFormat = (typeof allowedFormats)[number];
|
export type AllowedFormat = (typeof allowedFormats)[number];
|
||||||
|
|
||||||
export function AIQuizGenerator() {
|
export function AIQuizGenerator() {
|
||||||
const [isUpgradeModalOpen, setIsUpgradeModalOpen] = useState(false);
|
const [isUpgradeModalOpen, setIsUpgradeModalOpen] = useState(false);
|
||||||
const { isPaidUser, isLoading: isPaidUserLoading } = useIsPaidUser();
|
|
||||||
|
|
||||||
const toast = useToast();
|
const toast = useToast();
|
||||||
const [title, setTitle] = useState('');
|
const [title, setTitle] = useState('');
|
||||||
const [selectedFormat, setSelectedFormat] = useState<AllowedFormat>('mcq');
|
const [selectedFormat, setSelectedFormat] = useState<AllowedFormat>('mcq');
|
||||||
|
|
||||||
// question answer chat options
|
|
||||||
const [showFineTuneOptions, setShowFineTuneOptions] = useState(false);
|
const [showFineTuneOptions, setShowFineTuneOptions] = useState(false);
|
||||||
const [questionAnswerChatMessages, setQuestionAnswerChatMessages] = useState<
|
const [questionAnswerChatMessages, setQuestionAnswerChatMessages] = useState<
|
||||||
QuestionAnswerChatMessage[]
|
QuestionAnswerChatMessage[]
|
||||||
>([]);
|
>([]);
|
||||||
|
|
||||||
|
const {
|
||||||
|
data: tokenUsage,
|
||||||
|
isLoading: isTokenUsageLoading,
|
||||||
|
refetch: refetchTokenUsage,
|
||||||
|
} = useQuery(getAiCourseLimitOptions(), queryClient);
|
||||||
|
|
||||||
|
const { data: userBillingDetails, isLoading: isBillingDetailsLoading } =
|
||||||
|
useQuery(billingDetailsOptions(), queryClient);
|
||||||
|
|
||||||
|
const isLimitExceeded = (tokenUsage?.used || 0) >= (tokenUsage?.limit || 0);
|
||||||
|
const isPaidUser = userBillingDetails?.status === 'active';
|
||||||
|
|
||||||
const titleFieldId = useId();
|
const titleFieldId = useId();
|
||||||
const fineTuneOptionsId = useId();
|
const fineTuneOptionsId = useId();
|
||||||
|
|
||||||
@@ -88,6 +101,11 @@ export function AIQuizGenerator() {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isPaidUser && isLimitExceeded) {
|
||||||
|
setIsUpgradeModalOpen(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
let sessionId = '';
|
let sessionId = '';
|
||||||
if (showFineTuneOptions) {
|
if (showFineTuneOptions) {
|
||||||
clearQuestionAnswerChatMessages();
|
clearQuestionAnswerChatMessages();
|
||||||
@@ -117,7 +135,8 @@ export function AIQuizGenerator() {
|
|||||||
{isUpgradeModalOpen && (
|
{isUpgradeModalOpen && (
|
||||||
<UpgradeAccountModal onClose={() => setIsUpgradeModalOpen(false)} />
|
<UpgradeAccountModal onClose={() => setIsUpgradeModalOpen(false)} />
|
||||||
)}
|
)}
|
||||||
{!isPaidUser && !isPaidUserLoading && isLoggedIn() && (
|
|
||||||
|
{!isPaidUser && !isBillingDetailsLoading && isLoggedIn() && (
|
||||||
<div className="absolute bottom-full left-1/2 -translate-x-1/2 -translate-y-8 text-gray-500 max-md:hidden">
|
<div className="absolute bottom-full left-1/2 -translate-x-1/2 -translate-y-8 text-gray-500 max-md:hidden">
|
||||||
You are on the free plan
|
You are on the free plan
|
||||||
<button
|
<button
|
||||||
@@ -193,6 +212,16 @@ export function AIQuizGenerator() {
|
|||||||
id={fineTuneOptionsId}
|
id={fineTuneOptionsId}
|
||||||
checked={showFineTuneOptions}
|
checked={showFineTuneOptions}
|
||||||
onChange={(e) => {
|
onChange={(e) => {
|
||||||
|
if (!isLoggedIn()) {
|
||||||
|
showLoginPopup();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!isPaidUser && isLimitExceeded) {
|
||||||
|
setIsUpgradeModalOpen(true);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!trimmedTitle) {
|
if (!trimmedTitle) {
|
||||||
toast.error('Please enter a topic first');
|
toast.error('Please enter a topic first');
|
||||||
return;
|
return;
|
||||||
|
@@ -11,6 +11,7 @@ import {
|
|||||||
} from '../../queries/ai-quiz';
|
} from '../../queries/ai-quiz';
|
||||||
import { queryClient } from '../../stores/query-client';
|
import { queryClient } from '../../stores/query-client';
|
||||||
import { AIQuizContent } from './AIQuizContent';
|
import { AIQuizContent } from './AIQuizContent';
|
||||||
|
import { AlertCircleIcon } from 'lucide-react';
|
||||||
|
|
||||||
type GenerateAIQuizProps = {
|
type GenerateAIQuizProps = {
|
||||||
onQuizSlugChange?: (quizSlug: string) => void;
|
onQuizSlugChange?: (quizSlug: string) => void;
|
||||||
@@ -102,7 +103,14 @@ export function GenerateAIQuiz(props: GenerateAIQuizProps) {
|
|||||||
};
|
};
|
||||||
|
|
||||||
if (error) {
|
if (error) {
|
||||||
return <div className="text-red-500">{error}</div>;
|
return (
|
||||||
|
<div className="absolute inset-0 z-20 flex h-full flex-col items-center justify-center bg-white">
|
||||||
|
<div className="flex flex-col items-center justify-center gap-2">
|
||||||
|
<AlertCircleIcon className="size-10 text-gray-500" />
|
||||||
|
<p className="text-center">{error}</p>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isLoading) {
|
if (isLoading) {
|
||||||
@@ -113,5 +121,5 @@ export function GenerateAIQuiz(props: GenerateAIQuizProps) {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
return <AIQuizContent questions={questions} />;
|
return <AIQuizContent questions={questions} />;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user