3
3
import { useState } from 'react'
4
4
import { Button } from "@/components/ui/button"
5
5
import { Dialog , DialogContent , DialogDescription , DialogFooter , DialogHeader , DialogTitle } from "@/components/ui/dialog"
6
- import { Progress } from "@/components/ui/progress"
7
6
import { Card } from "@/components/ui/card"
8
- import { Label } from "@/components/ui/label"
9
- import { Select , SelectContent , SelectItem , SelectTrigger , SelectValue } from "@/components/ui/select"
10
- import { Loader2 , FileText } from 'lucide-react'
7
+ import { Loader2 , MessageSquare } from 'lucide-react'
11
8
12
9
type StructureMap = Map < string , StructureMap | { type : 'file' } >
13
10
14
11
export default function AICodeDocumentation ( { structureMap } : { structureMap : StructureMap } ) {
15
12
const [ isOpen , setIsOpen ] = useState ( false )
16
13
const [ isGenerating , setIsGenerating ] = useState ( false )
17
- const [ progress , setProgress ] = useState ( 0 )
18
14
const [ generatedDocs , setGeneratedDocs ] = useState < string | null > ( null )
19
15
20
16
const generateStructureDocs = ( structure : StructureMap , depth : number = 0 ) : string => {
@@ -35,17 +31,22 @@ export default function AICodeDocumentation({ structureMap }: { structureMap: St
35
31
36
32
const generatePrompt = ( structure : StructureMap ) : string => {
37
33
const structureDocs = generateStructureDocs ( structure )
38
- return `Project Documentation\n\nFolder Structure:\n\n${ structureDocs } `
34
+ return `Provide 2-3 sentences of feedback on the repository's structure.
35
+
36
+ Project Structure:
37
+
38
+ ${ structureDocs }
39
+
40
+ Feedback:`
41
+ }
42
+
43
+ const extractFeedback = ( response : string ) : string => {
44
+ const feedbackIndex = response . indexOf ( 'Feedback:' )
45
+ return feedbackIndex !== - 1 ? response . slice ( feedbackIndex + 9 ) . trim ( ) : response . trim ( )
39
46
}
40
47
41
48
const handleGenerate = async ( ) => {
42
49
setIsGenerating ( true )
43
- setProgress ( 0 )
44
-
45
- for ( let i = 0 ; i <= 100 ; i += 10 ) {
46
- setProgress ( i )
47
- await new Promise ( resolve => setTimeout ( resolve , 500 ) )
48
- }
49
50
50
51
try {
51
52
const prompt = generatePrompt ( structureMap )
@@ -60,83 +61,75 @@ export default function AICodeDocumentation({ structureMap }: { structureMap: St
60
61
parameters : {
61
62
max_length : 8000 ,
62
63
temperature : 0.9 ,
63
- top_p : 0.8 ,
64
- }
65
- } )
64
+ top_p : 0.95 ,
65
+ } ,
66
+ } ) ,
66
67
} )
67
68
68
- if ( ! response . ok ) throw new Error ( 'Failed to generate documentation' )
69
+ if ( ! response . ok ) {
70
+ throw new Error ( 'Failed to generate feedback' )
71
+ }
69
72
70
73
const result = await response . json ( )
71
- setGeneratedDocs ( result [ 0 ] . generated_text )
74
+
75
+ if ( result && result [ 0 ] ?. generated_text ) {
76
+ const feedback = extractFeedback ( result [ 0 ] . generated_text )
77
+ setGeneratedDocs ( feedback )
78
+ } else {
79
+ throw new Error ( 'Invalid response format' )
80
+ }
72
81
} catch ( error ) {
73
- console . error ( 'Error generating documentation:' , error )
74
- setGeneratedDocs ( 'Error generating documentation. Please try again.' )
82
+ console . error ( 'Error generating feedback:' , error )
83
+ setGeneratedDocs ( 'Error generating feedback. Please try again.' )
84
+ } finally {
85
+ setIsGenerating ( false )
75
86
}
76
-
77
- setIsGenerating ( false )
78
87
}
79
88
80
89
return (
81
90
< >
82
91
< Button onClick = { ( ) => setIsOpen ( true ) } className = "mt-4 w-full md:w-auto" >
83
- < FileText className = "h-4 w-4" /> Generate Documentation
92
+ < MessageSquare className = "h-4 w-4" /> Generate Feedback
84
93
</ Button >
85
94
< Dialog open = { isOpen } onOpenChange = { setIsOpen } >
86
- < DialogContent className = "sm:max-w-[800px] h-[90vh] flex flex-col" >
95
+ < DialogContent className = "sm:max-w-[800px] max- h-[90vh] flex flex-col" >
87
96
< DialogHeader >
88
- < DialogTitle > Generate AI Documentation </ DialogTitle >
97
+ < DialogTitle > Generate AI Feedback </ DialogTitle >
89
98
< DialogDescription >
90
- Create documentation based on your repository structure.
99
+ Get feedback on your repository structure.
100
+ < em className = "block mt-2" > Instructions: First, generate your repository structure, then click Generate Feedback.</ em >
91
101
</ DialogDescription >
92
102
</ DialogHeader >
93
103
94
- < div className = "grid gap-4 py-4" >
95
- < div className = "grid grid-cols-1 sm:grid-cols-4 items-center gap-4" >
96
- < Label htmlFor = "documentation-level" className = "text-right sm:text-left" >
97
- Documentation Level
98
- </ Label >
99
- < Select >
100
- < SelectTrigger id = "documentation-level" className = "col-span-3" >
101
- < SelectValue placeholder = "Select level" />
102
- </ SelectTrigger >
103
- < SelectContent >
104
- < SelectItem value = "basic" > Basic</ SelectItem >
105
- < SelectItem value = "detailed" > Detailed</ SelectItem >
106
- </ SelectContent >
107
- </ Select >
108
- </ div >
109
- </ div >
110
-
111
- < div className = "flex-grow overflow-hidden" >
104
+ < div className = "flex-grow overflow-hidden my-6" >
112
105
{ isGenerating ? (
113
106
< div className = "flex flex-col items-center justify-center h-full space-y-4" >
114
- < Progress value = { progress } className = "w-full " />
115
- < p className = "text-sm text-gray-500" > Generating documentation ...</ p >
107
+ < Loader2 className = "h-6 w-6 animate-spin " />
108
+ < p className = "text-sm text-gray-500" > Generating feedback ...</ p >
116
109
</ div >
117
110
) : generatedDocs ? (
118
111
< div className = "h-full overflow-y-auto" >
119
112
< Card >
120
113
< div className = "p-4 h-full overflow-y-auto" >
121
- < pre className = "text-sm font-mono break-words whitespace-pre-wrap" > { generatedDocs } </ pre >
114
+ < p className = "text-sm whitespace-pre-wrap" > { generatedDocs } </ p >
122
115
</ div >
123
116
</ Card >
124
117
</ div >
125
118
) : null }
126
119
</ div >
127
120
128
- < DialogFooter className = "mt-4" >
121
+ < DialogFooter >
129
122
< Button onClick = { handleGenerate } disabled = { isGenerating } className = "w-full sm:w-auto" >
130
123
{ isGenerating ? (
131
124
< >
132
125
< Loader2 className = "h-4 w-4 animate-spin" />
133
126
Generating...
134
127
</ >
135
- ) : 'Generate Documentation ' }
128
+ ) : 'Generate' }
136
129
</ Button >
137
130
</ DialogFooter >
138
131
< p className = "mt-4 text-sm text-gray-600" >
139
- < em > Note: The AI model can make mistakes or miss details. It may also struggle to document large repository structures .</ em >
132
+ < em > Note: This feedback is generated by an AI model and may not always be accurate. Use it as a general guideline .</ em >
140
133
</ p >
141
134
</ DialogContent >
142
135
</ Dialog >
0 commit comments