From 00c376b5e551bceff624484c94e828a4f9c3dbca Mon Sep 17 00:00:00 2001 From: Ayyub I Date: Mon, 2 Oct 2023 16:05:58 -0500 Subject: [PATCH] re-added public comments --- packages/web/app/api/v1/cards/route.ts | 2 +- packages/web/components/BetaCard.tsx | 168 ++++++++++++++++++++++++- 2 files changed, 166 insertions(+), 4 deletions(-) diff --git a/packages/web/app/api/v1/cards/route.ts b/packages/web/app/api/v1/cards/route.ts index ad3b97f1..037fc41c 100644 --- a/packages/web/app/api/v1/cards/route.ts +++ b/packages/web/app/api/v1/cards/route.ts @@ -7,7 +7,7 @@ export const dynamic = "force-dynamic"; const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL || "PLACEHOLDER"; const supabaseSecretServiceKey = - process.env.SUPABASE_SERVICE_ROLE_SECRET || "PLACEHOLDER"; + process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY || "PLACEHOLDER"; /* Create card * Not creating directly from client because: diff --git a/packages/web/components/BetaCard.tsx b/packages/web/components/BetaCard.tsx index 602d7edf..f51d31d8 100644 --- a/packages/web/components/BetaCard.tsx +++ b/packages/web/components/BetaCard.tsx @@ -2,21 +2,113 @@ import { ICard, ICitation, IResponse } from "@/lib/api"; import { CARD_SHOW_PATH, getPageURL } from "@/lib/paths"; +import { supabase } from "@/lib/supabase/supabaseClient"; import { faCheck, faShare } from "@fortawesome/free-solid-svg-icons"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import moment from "moment"; -import { useState } from "react"; +import { useEffect, useState } from "react"; import useClipboardApi from "use-clipboard-api"; import CardResponse from "./CardResponse"; import Citation from "./Citation"; +type SupabaseRealtimePayload = { + old: T; + new: T; +}; + +type Comment = { + display_name: string; + content: string; + created_at: Date; + card_id: string; +}; + const BetaCard = ({ card }: { card: ICard }) => { const responses: IResponse[] = card.responses ?? []; const citations: ICitation[] = card.citations ?? []; const [value, copy] = useClipboardApi(); const currentUrl = getPageURL(`${CARD_SHOW_PATH}/${card.id}`); const [recentlyCopied, setRecentlyCopied] = useState(false); + const [comments, setComments] = useState(null); + const [commentContent, setCommentContent] = useState(""); + const [displayName, setDisplayName] = useState(""); const [showCitations, setShowCitations] = useState(false); + const [showComments, setShowComments] = useState(false); + + useEffect(() => { + const fetchComments = async () => { + try { + const { data, error } = await supabase + .from("comments") + .select("*") + .eq("card_id", card.id) + .order("created_at", { ascending: false }); + if (error) throw error; + setComments(data); + } catch (error) {} + }; + fetchComments(); + }, [card.id]); + + useEffect(() => { + const channel = (supabase.channel(`cards:id=eq.${card.id}`) as any) + .on( + "postgres_changes", + { + event: "INSERT", + schema: "public", + }, + (payload: SupabaseRealtimePayload) => { + if (payload.new.card_id === card.id) { + setComments((prevComments) => [ + payload.new, + ...(prevComments || []), + ]); + } + } + ) + .subscribe(); + + // Cleanup subscription on component unmount + return () => { + channel.unsubscribe(); + }; + }, [card.id]); + + const handleCommentSubmit = async () => { + const newComment = { + card_id: card.id, + content: commentContent, + display_name: displayName, + created_at: new Date(), + }; + + + setComments((prevComments) => + prevComments + ? prevComments.filter((comment) => comment !== newComment) + : null + ); + + setDisplayName(""); // Resetting display name + setCommentContent(""); // Resetting comment content + + try { + const { data, error } = await supabase + .from("comments") + .insert([newComment]); + if (error) throw error; + setDisplayName(""); // Resetting display name after successful post + setCommentContent(""); // Resetting comment content after successful post + } catch (error) { + // If there's an error, revert the change to the comments + setComments((prevComments) => + prevComments + ? prevComments.filter((comment) => comment !== newComment) + : null + ); + } + }; return (
@@ -57,7 +149,7 @@ const BetaCard = ({ card }: { card: ICard }) => { {/* Citations Section */}
)}
+ + {/* Comments Section */} +
+ + + {showComments && ( + <> +

Comments

+ +
+ setDisplayName(e.target.value)} + /> +
+ +
+