1+ // src/components/CommentForm.tsx
12import { useState } from 'react' ;
23import {
34 Box ,
45 Button ,
5- Container ,
66 Form ,
77 Modal ,
88 SpaceBetween ,
99 Textarea ,
1010 Header ,
1111} from "@cloudscape-design/components" ;
1212import LoadingBar from "@cloudscape-design/chat-components/loading-bar" ;
13- import { generateClient } from 'aws-amplify/data' ;
14- import { Schema } from '../../amplify/data/resource' ;
1513import { NewLineToBr } from './utils/NewLineToBr' ;
16-
17- const client = generateClient < Schema > ( ) ;
14+ import { useCommentSummary } from './utils/commentSummary' ;
1815
1916interface CommentFormProps {
2017 classId : string ;
@@ -27,8 +24,9 @@ export const CommentForm = ({
2724} : CommentFormProps ) => {
2825 const [ post , setPost ] = useState ( '' ) ;
2926 const [ alertVisible , setAlertVisible ] = useState ( false ) ;
30- const [ summary , setSummary ] = useState ( '' ) ;
31- const [ isGenerating , setIsGenerating ] = useState ( false ) ;
27+
28+ // Use comment summary hook
29+ const { summary, isGenerating, generateSummarization } = useCommentSummary ( classId ) ;
3230
3331 const submitHandler = async ( event : any ) => {
3432 event . preventDefault ( ) ;
@@ -40,79 +38,11 @@ export const CommentForm = ({
4038 }
4139 } ;
4240
43- const cancelHandler = ( ) => {
44- setPost ( "" ) ;
45- }
46-
47- const askBedrock = async ( prompt : string ) => {
48- const response = await client . queries . askBedrock ( { prompt : prompt } ) ;
49- const res = JSON . parse ( response . data ?. body ! ) ;
50- const content = res . content [ 0 ] . text ;
51- return content || null ;
52- } ;
53-
54- const generateSummarization = async ( ) => {
55- setIsGenerating ( true ) ;
56- console . log ( "Generating summarization..." ) ;
57-
58- try {
59- const { data : comments , errors } = await client . models . Comment . list ( {
60- filter : { classId : { eq : classId } } ,
61- limit : 1000
62- } ) ;
63-
64- if ( errors ) {
65- console . error ( 'Error fetching comments:' , errors ) ;
66- return ;
67- }
68-
69- if ( ! comments || comments . length === 0 ) {
70- console . log ( "No comments to summarize" ) ;
71- setSummary ( "No comments available to summarize." ) ;
72- return ;
73- }
74-
75- console . log ( `Total comments found: ${ comments . length } ` ) ;
76- console . log ( 'All comments:' , comments ) ;
77-
78- const commentsText = comments
79- . map ( comment => comment . content )
80- . join ( "\n" ) ;
81-
82- console . log ( 'Full comments text being sent to Bedrock:' , commentsText ) ;
83- console . log ( 'Number of characters in prompt:' , commentsText . length ) ;
84-
85- const prompt = `📊 Summarize the following comments in a structured format:
86-
87- ${ commentsText }
88-
89- Format your response as follows:
90-
91- 📚 Summary:
92- [Provide a concise summary of the positive and negative sentiment]
93-
94- ⭐️ Number of positive comment :
95- ⭐️ Number of Negative comment :
96-
97- 💫 Key Reason:
98- [Main reason for the score]` ;
99-
100- const response = await askBedrock ( prompt ) ;
101- console . log ( "Bedrock response:" , response ) ;
102- setSummary ( response ) ;
103-
104- } catch ( error ) {
105- console . error ( "Error in generateSummarization:" , error ) ;
106- setSummary ( "An error occurred while generating the summary." ) ;
107- } finally {
108- setIsGenerating ( false ) ;
109- }
110- } ;
111-
11241 return (
11342 < form onSubmit = { submitHandler } >
11443 < Form >
11544 < SpaceBetween size = "l" >
45+ { /* Summary section */ }
11646 < SpaceBetween size = "s" >
11747 < Box >
11848 < Button
@@ -142,7 +72,9 @@ export const CommentForm = ({
14272
14373 < Box
14474 padding = "m"
145- color = { summary && summary !== "Generated summary will appear here." ? "text-body-default" : "text-body-secondary" }
75+ color = { summary && summary !== "Generated summary will appear here."
76+ ? "text-body-default"
77+ : "text-body-secondary" }
14678 fontSize = "body-m"
14779 >
14880 < div
@@ -156,11 +88,14 @@ export const CommentForm = ({
15688 backgroundColor : '#fafbfc'
15789 } }
15890 >
159- < NewLineToBr > { summary || "Generated summary will appear here." } </ NewLineToBr >
91+ < NewLineToBr >
92+ { summary || "Generated summary will appear here." }
93+ </ NewLineToBr >
16094 </ div >
16195 </ Box >
16296 </ SpaceBetween >
16397
98+ { /* Comment input section */ }
16499 < SpaceBetween size = "s" >
165100 < Header variant = "h4" > Add Comment</ Header >
166101 < Textarea
@@ -193,4 +128,4 @@ export const CommentForm = ({
193128 </ Form >
194129 </ form >
195130 ) ;
196- } ;
131+ } ;
0 commit comments