Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

refactor(org): simplify InviteDialog and remove seat-limit checks

Richie McIlroy9faadfddb0ea
diff --git a/apps/web/app/(org)/dashboard/settings/organization/components/InviteDialog.tsx b/apps/web/app/(org)/dashboard/settings/organization/components/InviteDialog.tsx
index 86e32d8d3..0b34c4655 100644
--- a/apps/web/app/(org)/dashboard/settings/organization/components/InviteDialog.tsx
+++ b/apps/web/app/(org)/dashboard/settings/organization/components/InviteDialog.tsx
@@ -9,41 +9,26 @@ import {
DialogTitle,
Input,
} from "@cap/ui";
-import type { Organisation } from "@cap/web-domain";
import { faUserGroup } from "@fortawesome/free-solid-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { useMutation } from "@tanstack/react-query";
import { useRouter } from "next/navigation";
-import { useState } from "react";
+import { useId, useState } from "react";
import { toast } from "sonner";
import { sendOrganizationInvites } from "@/actions/organization/send-invites";
-import { calculateSeats } from "@/utils/organization";
import { useDashboardContext } from "../../../Contexts";
interface InviteDialogProps {
isOpen: boolean;
- setIsOpen: (isOpen: boolean) => void;
- isOwner: boolean;
- showOwnerToast: () => void;
- handleManageBilling: () => Promise<void>;
+ setIsOpen: (open: boolean) => void;
}
-export const InviteDialog = ({
- isOpen,
- setIsOpen,
- isOwner,
- showOwnerToast,
- handleManageBilling,
-}: InviteDialogProps) => {
+export const InviteDialog = ({ isOpen, setIsOpen }: InviteDialogProps) => {
const router = useRouter();
const { activeOrganization } = useDashboardContext();
const [inviteEmails, setInviteEmails] = useState<string[]>([]);
const [emailInput, setEmailInput] = useState("");
- const [upgradeLoading, setUpgradeLoading] = useState(false);
-
- const { inviteQuota, remainingSeats } = calculateSeats(
- activeOrganization || {},
- );
+ const emailInputId = useId();
const handleAddEmails = () => {
const newEmails = emailInput
@@ -51,13 +36,6 @@ export const InviteDialog = ({
.map((email) => email.trim())
.filter((email) => email !== "");
- if (inviteEmails.length + newEmails.length > remainingSeats) {
- toast.error(
- `Not enough seats available. You have ${remainingSeats} seats remaining.`,
- );
- return;
- }
-
setInviteEmails([...new Set([...inviteEmails, ...newEmails])]);
setEmailInput("");
};
@@ -66,37 +44,14 @@ export const InviteDialog = ({
setInviteEmails(inviteEmails.filter((e) => e !== email));
};
- const handleUpgradePlan = async () => {
- if (!isOwner) {
- showOwnerToast();
- return;
- }
-
- setUpgradeLoading(true);
- setIsOpen(false);
- try {
- await handleManageBilling();
- } catch (_error) {
- setUpgradeLoading(false);
- }
- };
-
const sendInvites = useMutation({
mutationFn: async () => {
- if (!isOwner) {
- showOwnerToast();
- throw new Error("Not authorized");
- }
-
- if (inviteEmails.length > remainingSeats) {
- throw new Error(
- `Not enough seats available. You have ${remainingSeats} seats remaining.`,
- );
+ if (!activeOrganization?.organization.id) {
+ throw new Error("No active organization");
}
-
return await sendOrganizationInvites(
inviteEmails,
- activeOrganization?.organization.id as Organisation.OrganisationId,
+ activeOrganization.organization.id,
);
},
onSuccess: () => {
@@ -130,66 +85,42 @@ export const InviteDialog = ({
</DialogTitle>
</DialogHeader>
<div className="p-5">
- {remainingSeats > 0 ? (
- <>
- <Input
- id="emails"
- value={emailInput}
- onChange={(e) => setEmailInput(e.target.value)}
- placeholder="[email protected]"
- onBlur={handleAddEmails}
- onKeyDown={(e) => {
- if (e.key === "Enter" || e.key === ",") {
- e.preventDefault();
- handleAddEmails();
+ <Input
+ id={emailInputId}
+ value={emailInput}
+ onChange={(e) => setEmailInput(e.target.value)}
+ placeholder="[email protected]"
+ onBlur={handleAddEmails}
+ onKeyDown={(e) => {
+ if (e.key === "Enter" || e.key === ",") {
+ e.preventDefault();
+ handleAddEmails();
+ }
+ }}
+ />
+ <div className="flex overflow-y-auto flex-col gap-2.5 mt-4 max-h-60">
+ {inviteEmails.map((email) => (
+ <div
+ key={email}
+ className="flex justify-between items-center p-3 rounded-xl border transition-colors duration-200 cursor-pointer border-gray-4 hover:bg-gray-3"
+ >
+ <span className="text-sm text-gray-12">{email}</span>
+ <Button
+ style={
+ {
+ "--gradient-border-radius": "8px",
+ } as React.CSSProperties
}
- }}
- />
- <div className="flex overflow-y-auto flex-col gap-2.5 mt-4 max-h-60">
- {inviteEmails.map((email) => (
- <div
- key={email}
- className="flex justify-between items-center p-3 rounded-xl border transition-colors duration-200 cursor-pointer border-gray-4 hover:bg-gray-3"
- >
- <span className="text-sm text-gray-12">{email}</span>
- <Button
- style={
- {
- "--gradient-border-radius": "8px",
- } as React.CSSProperties
- }
- type="button"
- variant="destructive"
- size="xs"
- onClick={() => handleRemoveEmail(email)}
- disabled={!isOwner}
- >
- Remove
- </Button>
- </div>
- ))}
+ type="button"
+ variant="destructive"
+ size="xs"
+ onClick={() => handleRemoveEmail(email)}
+ >
+ Remove
+ </Button>
</div>
- </>
- ) : (
- <div className="flex flex-col gap-2 p-4 bg-amber-50 rounded-xl border border-amber-200">
- <p className="font-medium text-amber-800">No Seats Available</p>
- <p className="text-sm text-amber-700">
- You've reached your seat limit. Please upgrade your plan or
- remove existing members to invite new ones.
- </p>
- <Button
- type="button"
- size="sm"
- variant="dark"
- className="self-start mt-2"
- spinner={upgradeLoading}
- disabled={upgradeLoading || !isOwner}
- onClick={handleUpgradePlan}
- >
- Upgrade Plan
- </Button>
- </div>
- )}
+ ))}
+ </div>
</div>
<DialogFooter className="p-5 border-t border-gray-4">
<Button
@@ -205,11 +136,7 @@ export const InviteDialog = ({
size="sm"
variant="dark"
spinner={sendInvites.isPending}
- disabled={
- sendInvites.isPending ||
- inviteEmails.length === 0 ||
- remainingSeats === 0
- }
+ disabled={sendInvites.isPending || inviteEmails.length === 0}
onClick={() => sendInvites.mutate()}
>
Send Invites