diff --git a/src/components/messaging/ChatView.tsx b/src/components/messaging/ChatView.tsx index 66781a7..e02e204 100644 --- a/src/components/messaging/ChatView.tsx +++ b/src/components/messaging/ChatView.tsx @@ -1,6 +1,6 @@ import { useState, useRef, useEffect } from "react"; import { useAuth } from "@/contexts/AuthContext"; -import { useChatMessages, usePartnerInfo } from "@/hooks/useDirectMessages"; +import { useChatMessages, usePartnerInfo, STAFF_GROUP_ID } from "@/hooks/useDirectMessages"; import { supabase } from "@/integrations/supabase/client"; import { ScrollArea } from "@/components/ui/scroll-area"; import { Button } from "@/components/ui/button"; @@ -19,6 +19,7 @@ import { interface Props { partnerId: string | null; partnerName: string; + onDeleted?: () => void; } function getInitials(name: string) { @@ -32,12 +33,26 @@ function formatMessageDate(dateStr: string) { return format(d, "MMM d, h:mm a"); } -export default function ChatView({ partnerId, partnerName }: Props) { +export default function ChatView({ partnerId, partnerName, onDeleted }: Props) { const { user, isAdmin, isStaff } = useAuth(); const { messages, loading, sendMessage, deleteMessage } = useChatMessages(partnerId); const partnerInfo = usePartnerInfo(partnerId); const [draft, setDraft] = useState(""); const [sending, setSending] = useState(false); + const [deleteConvOpen, setDeleteConvOpen] = useState(false); + + const canDeleteConversation = !!partnerId && partnerId !== STAFF_GROUP_ID; + const deleteConversation = async () => { + if (!user || !partnerId) return; + const { error } = await supabase + .from("direct_messages") + .delete() + .or(`and(sender_id.eq.${user.id},recipient_id.eq.${partnerId}),and(sender_id.eq.${partnerId},recipient_id.eq.${user.id})`); + setDeleteConvOpen(false); + if (error) { toast({ title: "Couldn't delete conversation", description: error.message, variant: "destructive" }); return; } + toast({ title: "Conversation deleted" }); + onDeleted?.(); + }; const [creatingRequestId, setCreatingRequestId] = useState(null); const [deletingId, setDeletingId] = useState(null); const scrollRef = useRef(null); @@ -133,7 +148,7 @@ export default function ChatView({ partnerId, partnerName }: Props) { {getInitials(partnerName)} -
+
{partnerName}
{partnerInfo && (partnerInfo.ownerName || partnerInfo.unit || partnerInfo.associationName) && (
@@ -141,8 +156,35 @@ export default function ChatView({ partnerId, partnerName }: Props) {
)}
+ {canDeleteConversation && ( + + )}
+ + + + Delete this conversation? + + This permanently deletes the conversation with {partnerName} for everyone. This can't be undone. + + + + Cancel + + Delete + + + + + {/* Messages */}
{loading ? ( diff --git a/src/pages/MessagesPage.tsx b/src/pages/MessagesPage.tsx index 6b4753e..4b19129 100644 --- a/src/pages/MessagesPage.tsx +++ b/src/pages/MessagesPage.tsx @@ -62,7 +62,11 @@ export default function MessagesPage() { onMessageWholeBoard={handleMessageWholeBoard} />
- + { setSelectedPartnerId(null); setPartnerName(""); }} + />