Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 | 2x 2x 2x 2x 13x 13x 13x 13x 13x 5x 13x 2x 2x 13x 13x 2x | import React from "react";
import { Link, useLocation, useNavigate } from "react-router-dom";
import { useMutation, useQuery } from "@tanstack/react-query";
import { Building2, CheckCircle2 } from "lucide-react";
import MainPage from "@/components/page/MainPage/MainPage";
import { AccessSummary } from "@/components/members/AccessControls";
import {
INVITE_STATUS,
SectionCard,
SkeletonLines,
StatusBadge,
Strong,
encodeResourceId,
} from "@/components/detail";
import {
InvitePreview,
InviteStatus,
MembershipKind,
ROLE_LABELS,
} from "@/objects/Organisation";
import { APP_ROUTES, ROUTE_PARAMS } from "@/routes/const";
import { QUERY_KEYS } from "@/services/query_keys";
import { useApiHandler } from "@/services/useApiHandler";
import { errorDetail } from "@/util/apiError";
import { tokenFromHash } from "@/util/invite";
import { parseServerDate } from "@/util/time";
const UNUSABLE: Record<Exclude<InviteStatus, "pending">, string> = {
accepted: "This invite has already been used.",
revoked: "This invite was withdrawn. Ask an admin of the organisation for a new one.",
expired: "This invite has expired. Ask an admin of the organisation for a new one.",
};
const homeOf = (kind: MembershipKind, id: string) =>
kind === "project"
? APP_ROUTES.PROJECT.home.replace(ROUTE_PARAMS.PROJECT.id, encodeResourceId(id))
: APP_ROUTES.ORGANISATION.home.replace(
ROUTE_PARAMS.ORGANISATION.id,
encodeResourceId(id),
);
/**
* Where an invite link lands. The token is in the link's fragment, so the
* page reads it here and sends it in request bodies; the server checks that
* the link is still live and that it was sent to the signed-in user's email.
*/
const AcceptInvite: React.FC = () => {
const { hash } = useLocation();
const navigate = useNavigate();
const { getApiHandler } = useApiHandler();
const token = tokenFromHash(hash);
const preview = useQuery<InvitePreview>({
queryKey: [QUERY_KEYS.GET.INVITE_PREVIEW, token],
queryFn: () => getApiHandler().invite_router.preview(token!),
enabled: !!token,
retry: false,
});
const accept = useMutation({
mutationFn: () => getApiHandler().invite_router.accept(token!),
onSuccess: (result) => navigate(homeOf(result.kind, result.target_id)),
});
// Typed here: TypeScript 5.0 reads useQuery's data as `any`.
const invite: InvitePreview | undefined = preview.data;
return (
<MainPage
title="Join an organisation"
description="An admin has invited you to an organisation or project."
links={[]}
>
<div className="max-w-xl">
<SectionCard variant="panel" icon={Building2} title="Invitation">
{!token ? (
<p className="text-sm text-gray-600 dark:text-gray-300">
This invite link is incomplete. Ask for it to be sent again.
</p>
) : preview.isLoading ? (
<SkeletonLines count={3} />
) : preview.isError || !invite ? (
<p className="text-sm text-gray-600 dark:text-gray-300">
{errorDetail(preview.error, "This invite link is not valid.")}
</p>
) : (
<div className="space-y-4">
<div className="flex items-start justify-between gap-3">
<p className="text-sm text-gray-700 dark:text-gray-200">
{invite.invited_by ? (
<>
<Strong>{invite.invited_by}</Strong> invited you to join{" "}
</>
) : (
<>You've been invited to join </>
)}
<Strong>{invite.target_name}</Strong> (a {invite.kind}) as{" "}
{invite.role === "admin" ? "an" : "a"}{" "}
{ROLE_LABELS[invite.role].toLowerCase()}.
</p>
<StatusBadge status={invite.status} scale={INVITE_STATUS} size="xs" />
</div>
{invite.role === "member" && invite.access && (
<div>
<p className="text-xs text-gray-500 dark:text-gray-400">
What you'll be able to do in each area:
</p>
<AccessSummary access={invite.access} />
</div>
)}
{invite.status !== "pending" ? (
<p className="text-sm text-gray-600 dark:text-gray-300">
{UNUSABLE[invite.status]}
</p>
) : !invite.for_you ? (
<p className="text-sm text-gray-600 dark:text-gray-300">
This invite is for <Strong>{invite.email}</Strong>, and
you're signed in with a different address. Sign in as the
person it was sent to.
</p>
) : (
<>
<p className="text-xs text-gray-400 dark:text-gray-500">
The link works until{" "}
{parseServerDate(invite.expires_at).toLocaleDateString(
undefined,
{ day: "numeric", month: "long", year: "numeric" },
)}
.
</p>
{accept.isError && (
<p role="alert" className="text-sm text-red-600 dark:text-red-400">
{errorDetail(accept.error, "Could not join the organisation.")}
</p>
)}
<div className="flex items-center gap-3">
<button
type="button"
onClick={() => accept.mutate()}
disabled={accept.isPending}
className="inline-flex items-center gap-2 px-3 py-1.5 rounded-lg bg-gray-900 dark:bg-white text-white dark:text-gray-900 text-sm font-medium hover:opacity-80 disabled:opacity-50 transition-opacity"
>
<CheckCircle2 className="size-4" />
{accept.isPending ? "Joining…" : "Accept and join"}
</button>
<Link
to={APP_ROUTES.USER.overview}
className="text-sm text-gray-500 hover:text-gray-800 dark:text-gray-400 dark:hover:text-gray-200"
>
Not now
</Link>
</div>
</>
)}
</div>
)}
</SectionCard>
</div>
</MainPage>
);
};
export default AcceptInvite;
|