diff --git a/components/messages/message-chat-action.tsx b/components/messages/message-chat-action.tsx new file mode 100644 index 0000000..f7a87a2 --- /dev/null +++ b/components/messages/message-chat-action.tsx @@ -0,0 +1,46 @@ +import { Copy, Trash2 } from "lucide-react"; +import { Button } from "../ui/button"; +import { Id } from "@/convex/_generated/dataModel"; +import useApiMutation from "@/lib/hooks/use-api-mutation"; +import { api } from "@/convex/_generated/api"; +import { toast } from "sonner"; + +type MessageChatActionProps = { + messageId: Id<"messages">; + content: string; +}; + +const MessageChatAction = ({ content, messageId }: MessageChatActionProps) => { + const { mutate: deleteMessage, isPending } = useApiMutation( + api.message.remove + ); + + const handleCopy = () => { + navigator.clipboard.writeText(content); + toast.success("Message copied to clipboard"); + }; + + const handleDelete = () => { + deleteMessage({ messageId }) + .then(() => toast.success("Message deleted")) + .catch(() => toast.error("Failed to delete message")); + }; + + return ( + <> + + + + ); +}; + +export default MessageChatAction; diff --git a/components/messages/message-chat.tsx b/components/messages/message-chat.tsx index 046877a..b2d63a5 100644 --- a/components/messages/message-chat.tsx +++ b/components/messages/message-chat.tsx @@ -3,6 +3,7 @@ import { useCurrentUser } from "@/lib/hooks/use-current-user"; import { cn } from "@/lib/utils"; import Hint from "../hint"; import { Avatar, AvatarFallback, AvatarImage } from "../ui/avatar"; +import MessageChatAction from "./message-chat-action"; type MessageChatProps = { message: Doc<"messages">; @@ -13,7 +14,7 @@ const MessageChat = ({ message }: MessageChatProps) => { return (
@@ -30,13 +31,19 @@ const MessageChat = ({ message }: MessageChatProps) => {

{message.content}

+
+ +
); }; diff --git a/components/messages/messages-list.tsx b/components/messages/messages-list.tsx index 39bb044..33456ef 100644 --- a/components/messages/messages-list.tsx +++ b/components/messages/messages-list.tsx @@ -15,7 +15,7 @@ type MessagesListProps = { const MessagesList = ({ projectId }: MessagesListProps) => { const { messages, isLoading, fetchMore, status } = useMessages(projectId); - const scrollRef = useScroll("instant", "end"); + const scrollRef = useScroll("instant", "end", [messages]); return ( diff --git a/convex/message.ts b/convex/message.ts index c275093..60256f5 100644 --- a/convex/message.ts +++ b/convex/message.ts @@ -38,10 +38,26 @@ export const create = mutation({ }, }); +export const remove = mutation({ + args: { + messageId: v.id("messages"), + }, + handler: async (ctx, args) => { + const identity = ctx.auth.getUserIdentity(); + if (!identity) + throw new Error("Unauthenticated user cannot delete messages"); + + const message = await ctx.db.get(args.messageId); + if (!message) throw new Error("Message not found"); + + return ctx.db.delete(message._id); + }, +}); + export const list = query({ args: { projectId: v.id("projects"), - paginationOpts: paginationOptsValidator + paginationOpts: paginationOptsValidator, }, handler: async (ctx, args) => { return ctx.db