import { zodResolver } from "@hookform/resolvers/zod";
import { Button } from "@rallly/ui/button";
import {
Dialog,
DialogContent,
DialogDescription,
DialogFooter,
DialogHeader,
DialogTitle,
} from "@rallly/ui/dialog";
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuItemIconLabel,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from "@rallly/ui/dropdown-menu";
import {
Form,
FormControl,
FormDescription,
FormField,
FormItem,
FormLabel,
FormMessage,
} from "@rallly/ui/form";
import { Input } from "@rallly/ui/input";
import { PencilIcon, TagIcon, TrashIcon } from "lucide-react";
import { useTranslation } from "next-i18next";
import React from "react";
import { SubmitHandler, useForm } from "react-hook-form";
import { useMount } from "react-use";
import { z } from "zod";
import { OptimizedAvatarImage } from "@/components/optimized-avatar-image";
import { useDeleteParticipantMutation } from "@/components/poll/mutations";
import { Trans } from "@/components/trans";
import { trpc } from "@/trpc/client";
import { useFormValidation } from "@/utils/form-validation";
import { usePostHog } from "@/utils/posthog";
export const ParticipantDropdown = ({
participant,
onEdit,
children,
disabled,
align,
}: {
disabled?: boolean;
participant: {
name: string;
userId?: string;
email?: string;
id: string;
};
align?: "start" | "end";
onEdit: () => void;
children: React.ReactNode;
}) => {
const [isChangeNameModalVisible, setIsChangeNameModalVisible] =
React.useState(false);
const [isDeleteParticipantModalVisible, setIsDeleteParticipantModalVisible] =
React.useState(false);
return (
<>
{children}
{participant.name}
{participant.email ? (
{participant.email}
) : null}
setIsChangeNameModalVisible(true)}>
setIsDeleteParticipantModalVisible(true)}
>
>
);
};
const DeleteParticipantModal = ({
open,
onOpenChange,
participantId,
participantName,
}: {
open: boolean;
onOpenChange: (open: boolean) => void;
participantId: string;
participantName: string;
}) => {
const deleteParticipant = useDeleteParticipantMutation();
return (
);
};
type ChangeNameForm = {
name: string;
};
const changeNameSchema = z.object({
name: z.string().trim().min(1),
});
const ChangeNameModal = (props: {
oldName: string;
participantId: string;
open: boolean;
onOpenChange: (open: boolean) => void;
}) => {
const posthog = usePostHog();
const queryClient = trpc.useUtils();
const changeName = trpc.polls.participants.rename.useMutation({
onSuccess: (_, { participantId, newName }) => {
queryClient.polls.participants.invalidate();
posthog?.capture("changed name", {
participantId,
oldName: props.oldName,
newName,
});
},
});
const form = useForm({
defaultValues: {
name: props.oldName,
},
resolver: zodResolver(changeNameSchema),
});
const { control, reset, handleSubmit, setFocus, formState } = form;
useMount(() => {
setFocus("name", {
shouldSelect: true,
});
});
const handler = React.useCallback>(
async ({ name }) => {
if (formState.isDirty) {
// change name
await changeName.mutateAsync({
participantId: props.participantId,
newName: name,
});
}
props.onOpenChange(false);
},
[changeName, formState.isDirty, props],
);
const { requiredString } = useFormValidation();
const formName = `change-name-${props.participantId}`;
const { t } = useTranslation();
return (
);
};