⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
import { useQueryClient } from "@tanstack/react-query";
import { createFileRoute, useNavigate } from "@tanstack/react-router";
import {
CheckCircle,
Loader2,
Mail,
X,
UserXIcon,
UserCheckIcon,
} from "lucide-react";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import PageTitle from "@/components/page-title";
import useAuth from "@/components/providers/auth-provider/hooks/use-auth";
import { Badge } from "@/components/ui/badge";
import { Button } from "@/components/ui/button";
import {
Table,
TableBody,
TableCell,
TableHead,
TableHeader,
TableRow,
} from "@/components/ui/table";
import { usePendingInvitations } from "@/hooks/queries/invitation/use-pending-invitations";
import useWorkspaceCreationAccess from "@/hooks/use-workspace-creation-access";
import { authClient } from "@/lib/auth-client";
import { cn } from "@/lib/cn";
import { formatDateMedium } from "@/lib/format";
import { toast } from "@/lib/toast";
export const Route = createFileRoute("/_layout/_authenticated/invitations")({
component: InvitationsPage,
});
function InvitationsPage() {
const { t } = useTranslation();
const navigate = useNavigate();
const queryClient = useQueryClient();
const { data: invitations = [], isLoading } = usePendingInvitations();
const [acceptingId, setAcceptingId] = useState<string | null>(null);
const [rejectingId, setRejectingId] = useState<string | null>(null);
const { user } = useAuth();
// The same rule the onboarding screen applies, so this page cannot offer a
// route into a form that screen would refuse to show. It also covers the
// stale session role: the first administrator of an instance is promoted
// after their session is cached, and deciding on that would hide the only
// two controls on the page from the one user entitled to both.
const { canCreateWorkspace } = useWorkspaceCreationAccess();
const handleSkip = () => {
if (!user?.name) {
navigate({ to: "/profile-setup" });
return;
}
navigate({ to: "/onboarding" });
};
const handleAcceptInvitation = async (
invitationId: string,
organizationId: string,
) => {
setAcceptingId(invitationId);
try {
const { data, error } = await authClient.organization.acceptInvitation({
invitationId,
});
if (error) {
toast.error(error.message || t("invitations:toast.acceptError"));
return;
}
await authClient.organization.setActive({
organizationId: data?.invitation.organizationId || organizationId,
});
toast.success(t("invitations:toast.acceptSuccess"), {
icon: UserCheckIcon,
});
await queryClient.invalidateQueries({
queryKey: ["invitations", "pending"],
});
navigate({
to: "/dashboard/workspace/$workspaceId",
params: {
workspaceId: data?.invitation.organizationId || organizationId,
},
});
} catch (error) {
toast.error(
error instanceof Error
? error.message
: t("invitations:toast.acceptError"),
);
} finally {
setAcceptingId(null);
}
};
const handleRejectInvitation = async (invitationId: string) => {
setRejectingId(invitationId);
try {
const { error } = await authClient.organization.rejectInvitation({
invitationId,
});
if (error) {
toast.error(error.message || t("invitations:toast.rejectError"));
return;
}
toast.success(t("invitations:toast.rejectSuccess"), { icon: UserXIcon });
await queryClient.invalidateQueries({
queryKey: ["invitations", "pending"],
});
} catch (error) {
toast.error(
error instanceof Error
? error.message
: t("invitations:toast.rejectError"),
);
} finally {
setRejectingId(null);
}
};
const getExpiryStatus = (expiresAt: string) => {
const expiryDate = new Date(expiresAt);
const now = new Date();
const daysDiff = Math.ceil(
(expiryDate.getTime() - now.getTime()) / (1000 * 60 * 60 * 24),
);
const formattedDate = formatDateMedium(expiresAt);
if (daysDiff <= 1) {
return {
label: `${formattedDate}`,
variant: "destructive" as const,
isUrgent: true,
};
}
if (daysDiff <= 3) {
return {
label: `${formattedDate}`,
variant: "secondary" as const,
isUrgent: true,
};
}
return {
label: formattedDate,
variant: null,
isUrgent: false,
};
};
return (
<>
<PageTitle title={t("invitations:pageTitle")} />
<div className="flex w-full min-h-svh items-center justify-center p-6 bg-background">
<div className="w-full max-w-2xl space-y-8">
<div className="space-y-3 text-center">
<h1 className="text-3xl font-semibold">
{t("invitations:pendingInvitations")}
</h1>
<p className="text-sm text-muted-foreground">
{t("invitations:acceptSubtitle")}
</p>
</div>
{isLoading ? (
<div className="flex items-center justify-center py-20">
<Loader2 className="h-5 w-5 animate-spin text-muted-foreground" />
</div>
) : invitations.length === 0 ? (
<div className="flex flex-col items-center justify-center py-20 text-center">
<div className="flex h-16 w-16 items-center justify-center rounded-full bg-muted/50 mb-4">
<Mail className="h-8 w-8 text-muted-foreground/60" />
</div>
<h3 className="text-base font-semibold mb-2">
{t("invitations:noPendingTitle")}
</h3>
<p className="text-sm text-muted-foreground max-w-md mb-6">
{t("invitations:noPendingDescription")}
</p>
{canCreateWorkspace && (
// Without creation rights there is no setup to continue to,
// and this would bounce back to the onboarding screen that
// sent the user here.
<Button onClick={handleSkip} variant="default">
{t("invitations:continueToSetup")}
</Button>
)}
</div>
) : (
<div className="space-y-6">
<div className="rounded-lg border bg-card shadow-sm">
<Table>
<TableHeader>
<TableRow className="border-b">
<TableHead className="font-semibold">
{t("invitations:table.workspace")}
</TableHead>
<TableHead className="font-semibold">
{t("invitations:table.invitedBy")}
</TableHead>
<TableHead className="font-semibold">
{t("invitations:table.expires")}
</TableHead>
<TableHead className="w-[100px]" />
</TableRow>
</TableHeader>
<TableBody>
{invitations.map((invitation) => {
const expiryStatus = getExpiryStatus(
invitation.expiresAt,
);
const isAccepting = acceptingId === invitation.id;
const isRejecting = rejectingId === invitation.id;
const isProcessing = isAccepting || isRejecting;
return (
<TableRow
key={invitation.id}
className={cn(
expiryStatus.isUrgent &&
expiryStatus.variant === "destructive" &&
"bg-destructive/5",
)}
>
<TableCell className="font-medium">
{invitation.workspaceName}
</TableCell>
<TableCell className="text-muted-foreground text-sm">
{invitation.inviterName}
</TableCell>
<TableCell>
{expiryStatus.isUrgent && expiryStatus.variant ? (
<Badge
variant={expiryStatus.variant}
className="text-xs font-normal"
>
{expiryStatus.label}
</Badge>
) : (
<span className="text-sm text-muted-foreground">
{expiryStatus.label}
</span>
)}
</TableCell>
<TableCell className="text-right">
<div className="flex items-center justify-end gap-1">
<Button
variant="ghost"
size="sm"
onClick={() =>
handleAcceptInvitation(
invitation.id,
invitation.workspaceId,
)
}
disabled={isProcessing}
className="h-7 w-7 p-0"
>
{isAccepting ? (
<Loader2 className="h-3 w-3 animate-spin" />
) : (
<CheckCircle className="h-3 w-3" />
)}
</Button>
<Button
variant="ghost"
size="sm"
onClick={() =>
handleRejectInvitation(invitation.id)
}
disabled={isProcessing}
className="h-7 w-7 p-0 text-muted-foreground hover:text-destructive"
>
{isRejecting ? (
<Loader2 className="h-3 w-3 animate-spin" />
) : (
<X className="h-3 w-3" />
)}
</Button>
</div>
</TableCell>
</TableRow>
);
})}
</TableBody>
</Table>
</div>
{canCreateWorkspace && (
// Skipping exists to go and create your own workspace. When
// only instance admins may create one, it leads to a screen
// with nothing to do and no way back to this invitation.
<div className="flex justify-center pt-2">
<Button
variant="ghost"
onClick={handleSkip}
className="text-muted-foreground hover:text-foreground"
>
{t("invitations:skipForNow")}
</Button>
</div>
)}
</div>
)}
</div>
</div>
</>
);
}