-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
253 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
import { NextResponse } from "next/server"; | ||
import { DirectMessage } from "@prisma/client"; | ||
|
||
import { currentProfile } from "@/lib/current-profile"; | ||
import { db } from "@/lib/db"; | ||
|
||
const MESSAGES_BATCH = 10; | ||
|
||
export async function GET( | ||
req: Request | ||
) { | ||
try { | ||
const profile = await currentProfile(); | ||
const { searchParams } = new URL(req.url); | ||
|
||
const cursor = searchParams.get("cursor"); | ||
const conversationId = searchParams.get("conversationId"); | ||
|
||
if (!profile) { | ||
return new NextResponse("Unauthorized", { status: 401 }); | ||
} | ||
|
||
if (!conversationId) { | ||
return new NextResponse("Conversation ID missing", { status: 400 }); | ||
} | ||
|
||
let messages: DirectMessage[] = []; | ||
|
||
if (cursor) { | ||
messages = await db.directMessage.findMany({ | ||
take: MESSAGES_BATCH, | ||
skip: 1, | ||
cursor: { | ||
id: cursor, | ||
}, | ||
where: { | ||
conversationId, | ||
}, | ||
include: { | ||
member: { | ||
include: { | ||
profile: true, | ||
} | ||
} | ||
}, | ||
orderBy: { | ||
createdAt: "desc", | ||
} | ||
}) | ||
} else { | ||
messages = await db.directMessage.findMany({ | ||
take: MESSAGES_BATCH, | ||
where: { | ||
conversationId, | ||
}, | ||
include: { | ||
member: { | ||
include: { | ||
profile: true, | ||
} | ||
} | ||
}, | ||
orderBy: { | ||
createdAt: "desc", | ||
} | ||
}); | ||
} | ||
|
||
let nextCursor = null; | ||
|
||
if (messages.length === MESSAGES_BATCH) { | ||
nextCursor = messages[MESSAGES_BATCH - 1].id; | ||
} | ||
|
||
return NextResponse.json({ | ||
items: messages, | ||
nextCursor | ||
}); | ||
} catch (error) { | ||
console.log("[DIRECT_MESSAGES_GET]", error); | ||
return new NextResponse("Internal Error", { status: 500 }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import { useEffect, useState } from "react"; | ||
|
||
type ChatScrollProps = { | ||
chatRef: React.RefObject<HTMLDivElement>; | ||
bottomRef: React.RefObject<HTMLDivElement>; | ||
shouldLoadMore: boolean; | ||
loadMore: () => void; | ||
count: number; | ||
}; | ||
|
||
export const useChatScroll = ({ | ||
chatRef, | ||
bottomRef, | ||
shouldLoadMore, | ||
loadMore, | ||
count, | ||
}: ChatScrollProps) => { | ||
const [hasInitialized, setHasInitialized] = useState(false); | ||
|
||
useEffect(() => { | ||
const topDiv = chatRef?.current; | ||
|
||
const handleScroll = () => { | ||
const scrollTop = topDiv?.scrollTop; | ||
|
||
if (scrollTop === 0 && shouldLoadMore) { | ||
loadMore() | ||
} | ||
}; | ||
|
||
topDiv?.addEventListener("scroll", handleScroll); | ||
|
||
return () => { | ||
topDiv?.removeEventListener("scroll", handleScroll); | ||
} | ||
}, [shouldLoadMore, loadMore, chatRef]); | ||
|
||
useEffect(() => { | ||
const bottomDiv = bottomRef?.current; | ||
const topDiv = chatRef.current; | ||
const shouldAutoScroll = () => { | ||
if (!hasInitialized && bottomDiv) { | ||
setHasInitialized(true); | ||
return true; | ||
} | ||
|
||
if (!topDiv) { | ||
return false; | ||
} | ||
|
||
const distanceFromBottom = topDiv.scrollHeight - topDiv.scrollTop - topDiv.clientHeight; | ||
return distanceFromBottom <= 100; | ||
} | ||
|
||
if (shouldAutoScroll()) { | ||
setTimeout(() => { | ||
bottomRef.current?.scrollIntoView({ | ||
behavior: "smooth", | ||
}); | ||
}, 100); | ||
} | ||
}, [bottomRef, chatRef, count, hasInitialized]); | ||
} |