Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: Comments / replies sorting

Richie McIlroy331cb625e7af
diff --git a/apps/embed/app/view/[videoId]/_components/Toolbar.tsx b/apps/embed/app/view/[videoId]/_components/Toolbar.tsx
index 7926a4092..da1d7d13d 100644
--- a/apps/embed/app/view/[videoId]/_components/Toolbar.tsx
+++ b/apps/embed/app/view/[videoId]/_components/Toolbar.tsx
@@ -182,7 +182,7 @@ export const Toolbar = ({
<Button
className="min-w-[160px]"
disabled={comment.length === 0}
- variant="default"
+ variant="primary"
size="sm"
onClick={() => {
handleCommentSubmit();
@@ -192,7 +192,7 @@ export const Toolbar = ({
</Button>
<Button
className="min-w-[100px]"
- variant="outline"
+ variant="secondary"
size="sm"
onClick={() => {
setCommentBoxOpen(false);
diff --git a/apps/web/app/api/video/comment/route.ts b/apps/web/app/api/video/comment/route.ts
index 95cf5df84..5af0b5127 100644
--- a/apps/web/app/api/video/comment/route.ts
+++ b/apps/web/app/api/video/comment/route.ts
@@ -12,12 +12,22 @@ async function handlePost(request: NextRequest) {
await request.json();
const userId = user?.id || "anonymous";
+ const parentCommentIdSanitized = parentCommentId ? parentCommentId.replace("temp-", "") : null;
+
+ console.log("type", type);
+ console.log("content", content);
+ console.log("videoId", videoId);
+ console.log("timestamp", timestamp);
+ console.log("parentCommentIdSanitized", parentCommentIdSanitized);
if (!type || !content || !videoId) {
console.error("Missing required data in /api/video/comment/route.ts");
- return new Response(JSON.stringify({ error: true }), {
- status: 401,
+ return new Response(JSON.stringify({
+ error: true,
+ message: "Missing required fields: type, content, or videoId"
+ }), {
+ status: 400,
headers: {
"Content-Type": "application/json",
},
@@ -26,24 +36,42 @@ async function handlePost(request: NextRequest) {
const id = nanoId();
- await db.insert(comments).values({
- id: id,
- authorId: userId,
- type: type,
- content: content,
- videoId: videoId,
- timestamp: timestamp || null,
- parentCommentId: parentCommentId || null,
- });
-
- return new Response(
- JSON.stringify({
- status: 200,
- headers: {
- "Content-Type": "application/json",
- },
- })
- );
+ try {
+ await db.insert(comments).values({
+ id: id,
+ authorId: userId,
+ type: type,
+ content: content,
+ videoId: videoId,
+ timestamp: timestamp || null,
+ parentCommentId: parentCommentIdSanitized || null,
+ });
+
+ return new Response(
+ JSON.stringify({
+ success: true,
+ commentId: id
+ }), {
+ status: 200,
+ headers: {
+ "Content-Type": "application/json",
+ },
+ }
+ );
+ } catch (error) {
+ console.error("Error creating comment:", error);
+ return new Response(
+ JSON.stringify({
+ error: true,
+ message: "Failed to create comment"
+ }), {
+ status: 500,
+ headers: {
+ "Content-Type": "application/json",
+ },
+ }
+ );
+ }
}
export const POST = (request: NextRequest) => {
diff --git a/apps/web/app/s/[videoId]/_components/Toolbar.tsx b/apps/web/app/s/[videoId]/_components/Toolbar.tsx
index 540748333..2c6c04cf3 100644
--- a/apps/web/app/s/[videoId]/_components/Toolbar.tsx
+++ b/apps/web/app/s/[videoId]/_components/Toolbar.tsx
@@ -116,30 +116,35 @@ export const Toolbar = ({
return;
}
- const timestamp = getTimestamp();
-
- const response = await fetch("/api/video/comment", {
- method: "POST",
- headers: {
- "Content-Type": "application/json",
- },
- body: JSON.stringify({
- type: "text",
- content: comment,
- videoId: data.id,
- parentCommentId: null,
- timestamp: timestamp,
- }),
- });
+ try {
+ const timestamp = getTimestamp();
- if (!response.ok) {
- console.error("Failed to record comment");
- }
+ const response = await fetch("/api/video/comment", {
+ method: "POST",
+ headers: {
+ "Content-Type": "application/json",
+ },
+ body: JSON.stringify({
+ type: "text",
+ content: comment,
+ videoId: data.id,
+ timestamp: timestamp || null,
+ parentCommentId: null,
+ }),
+ });
- setComment("");
- setCommentBoxOpen(false);
+ if (!response.ok) {
+ const errorData = await response.json();
+ console.error("Comment submission error:", errorData);
+ return;
+ }
- refresh();
+ setComment("");
+ setCommentBoxOpen(false);
+ refresh();
+ } catch (error) {
+ console.error("Failed to submit comment:", error);
+ }
};
const Emoji = ({ label, emoji }: { label: string; emoji: string }) => (
diff --git a/apps/web/app/s/[videoId]/_components/tabs/Activity.tsx b/apps/web/app/s/[videoId]/_components/tabs/Activity.tsx
index ea2dbccaf..9ba6a8a4a 100644
--- a/apps/web/app/s/[videoId]/_components/tabs/Activity.tsx
+++ b/apps/web/app/s/[videoId]/_components/tabs/Activity.tsx
@@ -389,7 +389,7 @@ export const Activity: React.FC<ActivityProps> = ({
authorName: user?.name || "Anonymous",
content,
createdAt: new Date(),
- videoId: comments[0]?.videoId || "",
+ videoId,
parentCommentId: "",
type: "text",
timestamp: null,
@@ -407,7 +407,7 @@ export const Activity: React.FC<ActivityProps> = ({
body: JSON.stringify({
type: "text",
content,
- videoId: comments[0]?.videoId,
+ videoId,
parentCommentId: "",
}),
});
@@ -582,7 +582,11 @@ export const Activity: React.FC<ActivityProps> = ({
<Comment
key={comment.id}
comment={comment}
- replies={allComments}
+ replies={allComments.sort(
+ (a, b) =>
+ new Date(a.createdAt).getTime() -
+ new Date(b.createdAt).getTime()
+ )}
onReply={(id) => {
if (!user) {
setShowAuthOverlay(true);