@@ -4,7 +4,7 @@ import { useState, ChangeEvent, FormEvent, useEffect } from 'react';
44import { useRouter } from 'next/navigation' ;
55import Link from 'next/link' ;
66import { generateDocumentation , AVAILABLE_MODELS } from '@/lib/openrouter' ;
7- import { createProject , saveMdxFiles , updateProjectStatus , saveAiJob , updateAiJob } from '@/lib/docService' ;
7+ import { createProject , saveMdxFiles , updateProjectStatus , saveAiJob , updateAiJob , getUserDailyAiJobCount } from '@/lib/docService' ;
88import { getUser } from '@/lib/supabase' ;
99import FileUploader from '@/components/api-doc-generator/FileUploader' ;
1010
@@ -27,19 +27,23 @@ export default function GenerateDocumentationPage() {
2727 const [ isLoading , setIsLoading ] = useState ( false ) ;
2828 const [ error , setError ] = useState < string | null > ( null ) ;
2929 const [ userId , setUserId ] = useState < string | null > ( null ) ;
30+ const [ aiJobCount , setAiJobCount ] = useState < number > ( 0 ) ;
31+ const AI_JOB_DAILY_LIMIT = 7 ;
3032
31- // Get the current user
33+ // Get the current user and AI job count
3234 useEffect ( ( ) => {
33- async function fetchUser ( ) {
35+ async function fetchUserAndJobCount ( ) {
3436 const user = await getUser ( ) ;
3537 if ( user ) {
3638 setUserId ( user . id ) ;
39+ const count = await getUserDailyAiJobCount ( user . id ) ;
40+ setAiJobCount ( count ) ;
3741 } else {
3842 // Redirect to login if user is not authenticated
3943 router . push ( '/login' ) ;
4044 }
4145 }
42- fetchUser ( ) ;
46+ fetchUserAndJobCount ( ) ;
4347 } , [ router ] ) ;
4448
4549 /**
@@ -96,6 +100,11 @@ export default function GenerateDocumentationPage() {
96100 setError ( 'You must be logged in to generate documentation' ) ;
97101 return ;
98102 }
103+
104+ if ( aiJobCount >= AI_JOB_DAILY_LIMIT ) {
105+ setError ( `You have reached the limit of ${ AI_JOB_DAILY_LIMIT } AI generations per day. Please try again tomorrow.` ) ;
106+ return ;
107+ }
99108
100109 setIsLoading ( true ) ;
101110 setError ( null ) ;
@@ -186,6 +195,9 @@ export default function GenerateDocumentationPage() {
186195 sessionStorage . setItem ( 'generatedDocs' , JSON . stringify ( formattedFiles ) ) ;
187196 console . log ( '💾 Saved formatted docs to sessionStorage' ) ;
188197
198+ // Update the AI job count after successful generation
199+ setAiJobCount ( prevCount => prevCount + 1 ) ;
200+
189201 // Navigate directly to the editor instead of preview
190202 router . push ( '/editor' ) ;
191203 } catch ( err ) {
@@ -500,9 +512,9 @@ export default function GenerateDocumentationPage() {
500512 < div className = "flex justify-end" >
501513 < button
502514 type = "submit"
503- disabled = { isLoading || ! apiSpec . trim ( ) || ! userId }
515+ disabled = { isLoading || ! apiSpec . trim ( ) || ! userId || aiJobCount >= AI_JOB_DAILY_LIMIT }
504516 className = { `inline-flex items-center justify-center px-8 py-3 text-base font-medium rounded-lg transition-all duration-200 ${
505- isLoading || ! apiSpec . trim ( ) || ! userId
517+ isLoading || ! apiSpec . trim ( ) || ! userId || aiJobCount >= AI_JOB_DAILY_LIMIT
506518 ? 'cursor-not-allowed bg-muted text-muted-foreground'
507519 : 'bg-primary text-primary-foreground hover:bg-primary/90 shadow-lg shadow-primary/20 hover:shadow-xl hover:shadow-primary/30'
508520 } `}
@@ -533,7 +545,12 @@ export default function GenerateDocumentationPage() {
533545 </ >
534546 ) : (
535547 < >
536- < span > Generate Documentation</ span >
548+ < span >
549+ { aiJobCount >= AI_JOB_DAILY_LIMIT
550+ ? `Daily Limit Reached (${ aiJobCount } /${ AI_JOB_DAILY_LIMIT } )`
551+ : `Generate Documentation (${ aiJobCount } /${ AI_JOB_DAILY_LIMIT } )`
552+ }
553+ </ span >
537554 < svg
538555 className = "w-5 h-5 ml-2"
539556 fill = "none"
0 commit comments