@@ -16,9 +16,39 @@ import PostLock from "./PostLock";
1616import PollData from "./PollData" ;
1717import ExternalLink from "./ExternalLink" ;
1818import UpvotePercentageLabel from "./UpvotePercentageLabel" ;
19+ import { useState } from "react" ;
20+ import { summarizeText as summarizeTextApi } from "../api/GeminiClient" ;
1921
2022const PostComponent = ( { post } : { post : Post } ) => {
2123 document . title = he . decode ( post . title ) ;
24+ const [ summarizedText , setSummarizedText ] = useState ( "" ) ;
25+ const [ isSummarizing , setIsSummarizing ] = useState ( false ) ;
26+ const [ summarizationError , setSummarizationError ] = useState ( "" ) ;
27+
28+ const summarizeText = async ( ) => {
29+ setIsSummarizing ( true ) ;
30+ setSummarizationError ( "" ) ;
31+ try {
32+ let textToSummarize = "" ;
33+ if ( post . url_overridden_by_dest && ! isImage ( post . url_overridden_by_dest ) ) {
34+ textToSummarize = post . url_overridden_by_dest ;
35+ } else {
36+ textToSummarize = post ?. selftext_html ?? "" ;
37+ }
38+
39+ if ( textToSummarize . trim ( ) === "" ) {
40+ setSummarizationError ( "No text available to summarize." ) ;
41+ return ;
42+ }
43+
44+ const summary = await summarizeTextApi ( textToSummarize ) ;
45+ setSummarizedText ( summary ) ;
46+ } catch ( error ) {
47+ setSummarizationError ( "Failed to summarize. Please try again." ) ;
48+ } finally {
49+ setIsSummarizing ( false ) ;
50+ }
51+ } ;
2252
2353 return (
2454 < div
@@ -122,7 +152,26 @@ const PostComponent = ({ post }: { post: Post }) => {
122152 ) }
123153 </ a >
124154 < PostLock locked = { post . locked } />
125- < PostStats score = { post . score } num_comments = { post . num_comments } />
155+ < div className = "flex justify-between items-center" >
156+ < PostStats score = { post . score } num_comments = { post . num_comments } />
157+ < button
158+ onClick = { summarizeText }
159+ className = "text-xs text-gray-500 hover:text-gray-700 dark:text-gray-400 dark:hover:text-gray-200 mt-2"
160+ >
161+ { isSummarizing ? "Summarizing..." : "✨ AI Summarize" }
162+ </ button >
163+ </ div >
164+ { summarizationError && (
165+ < div className = "text-sm mt-3 p-2 bg-red-100 dark:bg-red-900 text-red-700 dark:text-red-200 rounded-md" >
166+ < p > { summarizationError } </ p >
167+ </ div >
168+ ) }
169+ { summarizedText && (
170+ < div className = "text-sm mt-4 p-4 bg-gray-100 dark:bg-gray-800 rounded-md border border-gray-300 dark:border-gray-600" >
171+ < h3 className = "font-bold mb-2" > ✨ AI Summary</ h3 >
172+ < p > { summarizedText } </ p >
173+ </ div >
174+ ) }
126175 </ div >
127176 ) ;
128177} ;
0 commit comments