11import React , { useActionState , useEffect , useState } from "react" ;
2- import { getNote } from "../services/firebaseService" ;
2+ import { getNote , updateNote } from "../services/firebaseService" ;
33import { Link , useNavigate , useParams } from "react-router-dom" ;
44import ReactFlipCard from "reactjs-flip-card" ;
55import { GoArrowLeft } from "react-icons/go" ;
6+ import { httpsCallable } from "firebase/functions" ;
7+ import { db , functions } from "../config/firebase" ;
8+ import { doc , getDoc } from "firebase/firestore" ;
9+ import { useAuth } from "../contexts/AuthContext" ;
10+ import { getPlainText } from "../utils/textHelper" ;
11+ import { toast } from "react-toastify" ;
612
713function FlashcardsViewer ( ) {
814 const navigate = useNavigate ( ) ;
@@ -12,20 +18,50 @@ function FlashcardsViewer() {
1218 const [ flashcards , setFlashcards ] = useState ( [ ] ) ;
1319 const [ currentIndex , setCurrentIndex ] = useState ( 0 ) ;
1420 const [ showAnswer , setShowAnswer ] = useState ( false ) ;
21+ const { currentUser } = useAuth ( ) ;
1522
23+ let hasGenerated = false ;
1624 useEffect ( ( ) => {
1725 const fetchNote = async ( ) => {
1826 setLoading ( true ) ;
1927 try {
2028 const noteData = await getNote ( id ) ;
21- if ( noteData ) {
29+ if ( ! noteData ) {
30+ navigate ( "/" ) ;
31+ }
32+
33+ if ( noteData . flashcards . length > 0 ) {
34+ // flashcards exist, load them
2235 setTitle ( noteData . title ) ;
2336 setFlashcards ( noteData . flashcards ) ;
24- } else {
25- navigate ( "/" ) ;
37+ } else if ( ! hasGenerated ) {
38+ // no flashcards, generate them
39+ hasGenerated = true ;
40+
41+ const generateFlashcards = httpsCallable (
42+ functions ,
43+ "generateFlashcards"
44+ ) ;
45+ const results = await generateFlashcards ( {
46+ content : getPlainText ( noteData . content ) ,
47+ } ) ;
48+ const flashcards = results . data . flashcards ;
49+
50+ await updateNote ( id , { flashcards } ) ; // Save them to the note
51+
52+ setFlashcards ( flashcards ) ;
53+
54+ // show many credits left
55+ const userRef = doc ( db , "users" , currentUser . uid ) ;
56+ const userDoc = await getDoc ( userRef ) ;
57+ const userData = userDoc . data ( ) ;
58+
59+ toast . success (
60+ `Flashcards generated! You have ${ userData . usagesLeft } credits left.`
61+ ) ;
2662 }
2763 } catch ( error ) {
28- console . log ( error ) ;
64+ console . log ( "Error in flashcards viwer:" , error ) ;
2965 navigate ( "/" ) ;
3066 }
3167 setLoading ( false ) ;
@@ -47,7 +83,7 @@ function FlashcardsViewer() {
4783 }
4884 } ;
4985
50- if ( loading ) {
86+ if ( loading || flashcards . length === 0 ) {
5187 return (
5288 < div className = "flex items-center justify-center h-full" >
5389 < div
@@ -63,7 +99,7 @@ function FlashcardsViewer() {
6399 < div className = "mx-auto max-w-3xl text-center mt-16 px-4" >
64100 { /* Title Section */ }
65101 < h2 className = "text-xl sm:text-2xl md:text-3xl font-bold text-text mb-8" >
66- { `${ title } Flashcards` }
102+ { `${ title || "Untitled Note" } Flashcards` }
67103 </ h2 >
68104
69105 { /* Flashcard Display Section */ }
0 commit comments