|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { MutateMessageMenuEntry } from "@/components/messages/MutateMessageMenu/MutateMessageMenuEntry"; |
| 4 | +import { |
| 5 | + useDropMessage, |
| 6 | + useGetMessageMutators, |
| 7 | +} from "@/lib/byzzbench-client/generated"; |
| 8 | +import { ActionIcon, Burger, Menu, rem } from "@mantine/core"; |
| 9 | +import { showNotification } from "@mantine/notifications"; |
| 10 | +import { IconSend, IconTrash } from "@tabler/icons-react"; |
| 11 | +import { useQueryClient } from "@tanstack/react-query"; |
| 12 | +import React from "react"; |
| 13 | + |
| 14 | +type MutateMessageMenuProps = { |
| 15 | + messageId: number; |
| 16 | +}; |
| 17 | + |
| 18 | +export const MutateMessageMenu = ({ messageId }: MutateMessageMenuProps) => { |
| 19 | + const queryClient = useQueryClient(); |
| 20 | + const { data } = useGetMessageMutators(messageId); |
| 21 | + const { mutate: dropMessage } = useDropMessage(messageId); |
| 22 | + |
| 23 | + return ( |
| 24 | + <Menu> |
| 25 | + <Menu.Target> |
| 26 | + <Burger /> |
| 27 | + </Menu.Target> |
| 28 | + <Menu.Dropdown> |
| 29 | + <Menu.Label>Mutate and Deliver</Menu.Label> |
| 30 | + {data?.data.map((mutator) => ( |
| 31 | + <MutateMessageMenuEntry key={mutator} mutatorId={mutator} /> |
| 32 | + ))} |
| 33 | + |
| 34 | + <Menu.Divider /> |
| 35 | + |
| 36 | + <Menu.Label>Danger zone</Menu.Label> |
| 37 | + <Menu.Item |
| 38 | + color="red" |
| 39 | + leftSection={ |
| 40 | + <IconTrash style={{ width: rem(14), height: rem(14) }} /> |
| 41 | + } |
| 42 | + onClick={(e) => { |
| 43 | + e.preventDefault(); |
| 44 | + dropMessage(null as never, { |
| 45 | + onSuccess: () => { |
| 46 | + showNotification({ |
| 47 | + message: `Message ${messageId} dropped`, |
| 48 | + }); |
| 49 | + }, |
| 50 | + onError: () => { |
| 51 | + showNotification({ |
| 52 | + message: "Failed to drop message", |
| 53 | + color: "red", |
| 54 | + }); |
| 55 | + }, |
| 56 | + onSettled: async () => { |
| 57 | + await queryClient.invalidateQueries(); |
| 58 | + }, |
| 59 | + }); |
| 60 | + }} |
| 61 | + > |
| 62 | + Drop message |
| 63 | + </Menu.Item> |
| 64 | + </Menu.Dropdown> |
| 65 | + </Menu> |
| 66 | + ); |
| 67 | + |
| 68 | + return ( |
| 69 | + <ActionIcon |
| 70 | + onClick={(e) => { |
| 71 | + e.preventDefault(); |
| 72 | + const mutate = (a: any, b: any) => {}; |
| 73 | + mutate(null as never, { |
| 74 | + onSuccess: () => { |
| 75 | + showNotification({ |
| 76 | + message: `Message ${messageId} delivered`, |
| 77 | + }); |
| 78 | + }, |
| 79 | + onError: () => { |
| 80 | + showNotification({ |
| 81 | + message: "Failed to deliver message", |
| 82 | + color: "red", |
| 83 | + }); |
| 84 | + }, |
| 85 | + onSettled: async () => { |
| 86 | + await queryClient.invalidateQueries(); |
| 87 | + }, |
| 88 | + }); |
| 89 | + }} |
| 90 | + > |
| 91 | + <IconSend /> |
| 92 | + </ActionIcon> |
| 93 | + ); |
| 94 | +}; |
0 commit comments