Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

5.3 KB
Raw Blame
import { zodResolver } from "@hookform/resolvers/zod";
import { type Role } from "@langfuse/shared";
import { useId } from "react";
import { useForm } from "react-hook-form";
import * as z from "zod";

import { Dialog } from "@/src/components/design-system/Dialog/Dialog";
import {
  Form,
  FormControl,
  FormDescription,
  FormField,
  FormItem,
  FormLabel,
  FormMessage,
} from "@/src/components/ui/form";
import { Input } from "@/src/components/ui/input";
import { RoleSelect } from "@/src/features/rbac/components/RoleSelect";
import { reportTrpcErrorWithoutToast } from "@/src/utils/api";

const roleValues = {
  OWNER: "OWNER",
  ADMIN: "ADMIN",
  MEMBER: "MEMBER",
  VIEWER: "VIEWER",
  AI_GATEWAY: "AI_GATEWAY",
  NONE: "NONE",
} as const satisfies Record<Role, Role>;

const formSchema = z.object({
  email: z.string().trim().pipe(z.email()),
  orgRole: z.enum(roleValues),
  projectRole: z.enum(roleValues).exclude([roleValues.AI_GATEWAY]),
});

type FormValues = z.infer<typeof formSchema>;

type CreateProjectMemberDialogProps = {
  project: { id: string; name: string } | undefined;
  hasOnlySingleProjectAccess: boolean;
  hasProjectRoleEntitlement: boolean;
  canManageOrgOwnership: boolean;
  canManageProjectOwnership: boolean;
  isAiGatewayEnabled: boolean;
  isPending: boolean;
  createProjectMember: (values: FormValues) => Promise<void>;
  onSuccess: () => void;
};

export function CreateProjectMemberDialog({
  project,
  hasOnlySingleProjectAccess,
  hasProjectRoleEntitlement,
  canManageOrgOwnership,
  canManageProjectOwnership,
  isAiGatewayEnabled,
  isPending,
  createProjectMember,
  onSuccess,
}: CreateProjectMemberDialogProps) {
  const formId = useId();
  const form = useForm<FormValues>({
    resolver: zodResolver(formSchema),
    defaultValues: {
      email: "",
      orgRole: hasOnlySingleProjectAccess ? roleValues.NONE : roleValues.MEMBER,
      projectRole: hasOnlySingleProjectAccess
        ? roleValues.MEMBER
        : roleValues.NONE,
    },
  });

  async function onSubmit(values: FormValues) {
    try {
      await createProjectMember(values);
      form.reset();
      onSuccess();
    } catch (error) {
      form.setError("email", {
        type: "manual",
        message:
          error instanceof Error ? error.message : "Failed to add member",
      });
      reportTrpcErrorWithoutToast(error, "members");
    }
  }

  return (
    <Dialog
      actions={[
        {
          label: "Grant access",
          type: "submit",
          form: formId,
          loading: isPending,
        },
      ]}
      title={`Add new member to the ${
        hasOnlySingleProjectAccess ? "project" : "organization"
      }`}
    >
      <Form {...form}>
        <form
          id={formId}
          className="space-y-6"
          onSubmit={form.handleSubmit(onSubmit)}
        >
          <Dialog.Body>
            <FormField
              control={form.control}
              name="email"
              render={({ field }) => (
                <FormItem>
                  <FormLabel>Email</FormLabel>
                  <FormControl>
                    <Input placeholder="[email protected]" {...field} />
                  </FormControl>
                  <FormMessage />
                </FormItem>
              )}
            />
            {!hasOnlySingleProjectAccess && (
              <FormField
                control={form.control}
                name="orgRole"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel>Organization Role</FormLabel>
                    <RoleSelect
                      roles={Object.values(roleValues).filter(
                        (role) =>
                          isAiGatewayEnabled || role !== roleValues.AI_GATEWAY,
                      )}
                      canManageOwnership={canManageOrgOwnership}
                      value={field.value}
                      onValueChange={field.onChange}
                    />
                    <FormMessage />
                  </FormItem>
                )}
              />
            )}
            {project !== undefined && hasProjectRoleEntitlement && (
              <FormField
                control={form.control}
                name="projectRole"
                render={({ field }) => (
                  <FormItem>
                    <FormLabel>Project Role</FormLabel>
                    <RoleSelect
                      isProjectRole
                      canManageOwnership={canManageProjectOwnership}
                      roles={Object.values(roleValues).filter(
                        (role) =>
                          role !== roleValues.AI_GATEWAY &&
                          (!hasOnlySingleProjectAccess ||
                            role !== roleValues.NONE),
                      )}
                      value={field.value}
                      onValueChange={field.onChange}
                    />
                    {!hasOnlySingleProjectAccess && (
                      <FormDescription>
                        This project role will override the default role for
                        this current project ({project.name}).
                      </FormDescription>
                    )}
                    <FormMessage />
                  </FormItem>
                )}
              />
            )}
          </Dialog.Body>
        </form>
      </Form>
    </Dialog>
  );
}