Skip to content

Commit dcc2165

Browse files
committed
refactored summary + flashcards features + better ux
1 parent 307643a commit dcc2165

7 files changed

Lines changed: 295 additions & 94 deletions

File tree

functions/index.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ export const generateSummary = functions.https.onCall(async (request) => {
3636
const MAX_DAILY_USAGES = 5;
3737
const HOURS_24 = 24 * 60 * 60 * 1000;
3838

39-
if (userData.usagesLeft === 0) {
39+
if (userData.usagesLeft <= 0) {
4040
const lastReset = userData.lastReset?.toMillis?.() ?? 0;
4141

4242
if (Date.now() - lastReset >= HOURS_24) {
@@ -138,7 +138,7 @@ export const generateFlashcards = functions.https.onCall(async (request) => {
138138
const MAX_DAILY_USAGES = 5;
139139
const HOURS_24 = 24 * 60 * 60 * 1000;
140140

141-
if (userData.usagesLeft === 0) {
141+
if (userData.usagesLeft <= 0) {
142142
const lastReset = userData.lastReset?.toMillis?.() ?? 0;
143143

144144
if (Date.now() - lastReset >= HOURS_24) {

package-lock.json

Lines changed: 23 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"react-icons": "^5.5.0",
2323
"react-quill-new": "^3.4.6",
2424
"react-router-dom": "^7.4.0",
25+
"react-toastify": "^11.0.5",
2526
"reactjs-flip-card": "^2.0.3",
2627
"yup": "^1.6.1"
2728
},

src/App.jsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import React from "react";
1+
import React, { useEffect } from "react";
22
import Navbar from "./components/Navbar";
33
import { Navigate, Route, Routes, useLocation } from "react-router-dom";
44
import HomePage from "./pages/HomePage";
@@ -17,6 +17,7 @@ function App() {
1717
const location = useLocation();
1818
const hideNavbar = ["/signin", "/signup"].includes(location.pathname);
1919
const { currentUser } = useAuth();
20+
2021
return (
2122
<div className="container mx-auto px-4 sm:px-6 lg:px-8 ">
2223
{!hideNavbar && <Navbar />}

src/main.jsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,26 @@ import "./index.css";
44
import App from "./App.jsx";
55
import { AuthProvider } from "./contexts/AuthContext.jsx";
66
import { BrowserRouter } from "react-router-dom";
7+
import { ToastContainer, toast } from "react-toastify";
78

89
createRoot(document.getElementById("root")).render(
910
<StrictMode>
1011
<AuthProvider>
1112
<BrowserRouter>
1213
<App />
14+
<ToastContainer
15+
position="top-right"
16+
autoClose={5000}
17+
hideProgressBar={false}
18+
newestOnTop={false}
19+
closeOnClick={false}
20+
rtl={false}
21+
pauseOnFocusLoss
22+
draggable
23+
pauseOnHover
24+
theme="light"
25+
className={"text-sm"}
26+
/>
1327
</BrowserRouter>
1428
</AuthProvider>
1529
</StrictMode>

src/pages/FlashcardsViewer.jsx

Lines changed: 43 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import React, { useActionState, useEffect, useState } from "react";
2-
import { getNote } from "../services/firebaseService";
2+
import { getNote, updateNote } from "../services/firebaseService";
33
import { Link, useNavigate, useParams } from "react-router-dom";
44
import ReactFlipCard from "reactjs-flip-card";
55
import { 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

713
function 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

Comments
 (0)