diff --git a/package.json b/package.json index 0a534b16..4dfe4489 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "chirp", "description": "Chirp is a social media app built with Next.js, Prisma, and Supabase", - "version": "1.9.5", + "version": "1.9.6", "private": true, "scripts": { "start": "next start", diff --git a/src/app/api/messages/chat/route.ts b/src/app/api/messages/chat/route.ts index 9a720015..751f92c3 100644 --- a/src/app/api/messages/chat/route.ts +++ b/src/app/api/messages/chat/route.ts @@ -6,6 +6,11 @@ import { prisma } from "@/lib/prisma"; export async function GET(request: Request) { const { searchParams } = new URL(request.url); const conversation_id = searchParams.get("conversation_id") as string; + const cursorQuery = searchParams.get("cursor") || undefined; + const take = Number(searchParams.get("limit")) || 20; + + const skip = cursorQuery ? 1 : 0; + const cursor = cursorQuery ? { id: cursorQuery } : undefined; const messageSchema = z.string(); const zod = messageSchema.safeParse(conversation_id); @@ -16,12 +21,24 @@ export async function GET(request: Request) { try { const chat = await prisma.message.findMany({ + skip, + take, + cursor, where: { conversation_id: conversation_id, }, + + orderBy: { + created_at: "desc", + }, }); - return NextResponse.json(chat, { status: 200 }); + const nextId = chat.length < take ? undefined : chat[chat.length - 1].id; + + return NextResponse.json({ + chat, + nextId, + }); } catch (error: any) { return NextResponse.json({ error: error.message }, { status: 500 }); } diff --git a/src/app/layout.tsx b/src/app/layout.tsx index a2a6d020..aa14a7ab 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -9,8 +9,7 @@ import { AuthFlow } from "@/features/auth"; import { MobileTweetButton } from "@/features/create-tweet"; import { MobileNavbar } from "@/features/navbar"; import { Sidebar } from "@/features/sidebar"; -import { NextAuthProvider } from "@/utils/next-auth-provider"; -import { ReactQueryProvider } from "@/utils/react-query-provider"; +import { AppProviders } from "@/providers"; import { Hamburger } from "./hamburger"; import { JoinTwitter } from "./join-twitter"; @@ -35,40 +34,38 @@ export default async function RootLayout({ lang="en" >
-