Skip to content

Commit 2abeff3

Browse files
committed
Implement fix based on CodeRabbit suggestions
1 parent 63586b8 commit 2abeff3

4 files changed

Lines changed: 13 additions & 5 deletions

File tree

app/(root)/page.tsx

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
1+
import { auth } from "@clerk/nextjs/server";
12
import BookCard from "@/components/BookCard";
23
import HeroSection from "@/components/HeroSection";
34
import { getAllBooks } from "@/lib/actions/book.actions";
45
import { IBook } from "@/types";
56
// import { sampleBooks } from "@/lib/constants";
67

78
const Page = async () => {
8-
const bookResults = await getAllBooks();
9+
const { userId } = await auth();
10+
const bookResults = userId
11+
? await getAllBooks(userId)
12+
: { success: true, data: [] };
913
const books = bookResults.success && bookResults.data ? bookResults.data : [];
1014

1115
return (

components/UploadForm.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ const UploadForm = () => {
124124

125125
// Check if book already exists
126126
try {
127-
const checkResult = await checkBookExists(formData.title);
127+
const checkResult = await checkBookExists(formData.title, userId);
128128
if (checkResult?.exists && checkResult.data) {
129129
toast.info("Book with the same title already exists");
130130
form.reset();
@@ -134,6 +134,7 @@ const UploadForm = () => {
134134

135135
const fileTitle = generateSlug(formData.title) || `book-${new Date().getTime()}`;
136136
const pdfFile = formData.pdfFile;
137+
// parsePDFFile depends on browser APIs (window/document/canvas), so keep it in this client flow.
137138
const parsedPdf = await parsePDFFile(pdfFile);
138139
if(parsedPdf.content.length === 0){
139140
toast.error("Failed to parse PDF file. Please try again with a different file.");

lib/actions/book.actions.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export const createBook = async (data: CreateBook) => {
3737
export const saveBookSegments = async (bookId: string, clerkId:string, segments: TextSegment[]) => {
3838
try {
3939
await connectToDb();
40-
console.log("Saving book segments", bookId, clerkId, segments);
4140

4241
const segmentsToSave = segments.map(segment => ({
4342
...segment,
@@ -105,10 +104,10 @@ export const getBookBySlug = async (slug: string, clerkId: string) => {
105104
}
106105
}
107106

108-
export const getAllBooks = async () => {
107+
export const getAllBooks = async (clerkId: string) => {
109108
try {
110109
await connectToDb();
111-
const books = await Book.find().sort({createdAt: -1}).lean();
110+
const books = await Book.find({ clerkId }).sort({createdAt: -1}).lean();
112111
return {
113112
success: true,
114113
data: serializeData(books)

lib/utils.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,10 @@ export const formatDuration = (seconds: number): string => {
9292

9393
export async function parsePDFFile(file: File) {
9494
try {
95+
if (typeof window === 'undefined' || typeof document === 'undefined') {
96+
throw new Error('parsePDFFile must run in a browser runtime (window/document unavailable)');
97+
}
98+
9599
// Use the legacy build for broader browser compatibility in client-side parsing.
96100
const pdfjsLib = await import('pdfjs-dist/legacy/build/pdf.mjs');
97101

0 commit comments

Comments
 (0)