|
7 | 7 | */ |
8 | 8 |
|
9 | 9 | /** |
10 | | - * invitationArrival — the register-first action a login page shows a visitor |
11 | | - * who arrived signed out from an invitation link (#25886). |
| 10 | + * invitationArrival — what a login or register page shows a visitor who |
| 11 | + * arrived signed out from an invitation link (#25886, #26473). |
12 | 12 | * |
13 | 13 | * `DefaultAcceptInvitationPage` bounces a signed-out visitor to |
14 | 14 | * `/login?redirect=/accept-invitation/ID` (`isInvitationRedirect` in |
15 | 15 | * `./signUpOffer`). Most invitees have never had an account here, so for them |
16 | 16 | * the generic "Welcome back" card with a small "Sign up" link under the form |
17 | 17 | * read as the wrong site. Both login pages — the console's own |
18 | 18 | * (`apps/console/src/pages/auth/LoginPage.tsx`) and this package's |
19 | | - * `DefaultLoginPage` — retitle the card for that visitor and render this |
20 | | - * prompt above the form in place of the small link, so there is ONE prompt and |
21 | | - * no second copy of it. |
| 19 | + * `DefaultLoginPage` — retitle the card for that visitor and render |
| 20 | + * `InvitationSignUpPrompt` above the form in place of the small link, so there |
| 21 | + * is ONE prompt and no second copy of it. Both register pages retitle theirs. |
22 | 22 | * |
23 | | - * The prompt names no organization and no inviter: the only invitation read |
24 | | - * the server serves (`GET /organization/get-invitation`) answers a signed-out |
25 | | - * caller `401`, and an anonymous preview is a server decision this package |
26 | | - * does not make. It is rendered only when the page's sign-up offer is `form`, |
| 23 | + * The title names the organization and the inviter when the server's |
| 24 | + * signed-out preview answers for the id the link carries |
| 25 | + * (`useInvitationPreview`, `GET {authUrl}/organization/invitation-preview`): |
| 26 | + * "Lin invited you to join Acme". The invited address comes back MASKED |
| 27 | + * (`z***@example.com`), so the description shows it as the address to use and |
| 28 | + * nothing prefills it: a masked value is not an address. An id the server does |
| 29 | + * not know (404), a failed read, or a server without the route keeps the |
| 30 | + * generic copy (`invitationArrivalCopy`) — the preview only ever adds names. |
| 31 | + * |
| 32 | + * The sign-up prompt is rendered only when the page's sign-up offer is `form`, |
27 | 33 | * so a server with sign-up switched off is never offered a registration it |
28 | 34 | * refuses. |
29 | 35 | */ |
30 | 36 |
|
| 37 | +import { useEffect, useState } from 'react'; |
31 | 38 | import { Link } from 'react-router-dom'; |
32 | | -import { useObjectTranslation } from '@object-ui/i18n'; |
| 39 | +import { useAuth, type AuthInvitationPreview } from '@object-ui/auth'; |
| 40 | +import { useObjectTranslation, type TranslateFn } from '@object-ui/i18n'; |
33 | 41 |
|
34 | 42 | /** A full-width outlined link with the auth primary button's box. */ |
35 | 43 | const INVITATION_SIGN_UP_CLASS = |
@@ -59,3 +67,111 @@ export function InvitationSignUpPrompt({ registerUrl }: { registerUrl: string }) |
59 | 67 | </div> |
60 | 68 | ); |
61 | 69 | } |
| 70 | + |
| 71 | +/** The basename-stripped route an invitation link opens, as `./signUpOffer` reads it. */ |
| 72 | +const INVITATION_ROUTE_PREFIX = '/accept-invitation/'; |
| 73 | + |
| 74 | +/** |
| 75 | + * The invitation id a `?redirect=` target carries, or `null` when it is not an |
| 76 | + * invitation-acceptance path. Reads the same segment `isInvitationRedirect` |
| 77 | + * tests for, so the two agree on which redirects are invitations. |
| 78 | + */ |
| 79 | +export function invitationIdOf(redirect: string | null | undefined): string | null { |
| 80 | + if (!redirect || !redirect.startsWith(INVITATION_ROUTE_PREFIX)) return null; |
| 81 | + const segment = redirect.slice(INVITATION_ROUTE_PREFIX.length).split(/[/?#]/, 1)[0]; |
| 82 | + if (!segment) return null; |
| 83 | + try { |
| 84 | + return decodeURIComponent(segment); |
| 85 | + } catch { |
| 86 | + // A malformed escape is still the segment the link carried; the server |
| 87 | + // answers its uniform 404 for it. |
| 88 | + return segment; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +/** |
| 93 | + * The signed-out invitation preview for the page's `?redirect=`, or `null`. |
| 94 | + * |
| 95 | + * `null` until the read answers, for a redirect that is not an invitation, for |
| 96 | + * the server's not-found answer, and after a failed read: each of those keeps |
| 97 | + * the page's generic invitation copy, and none of them throws into the page. |
| 98 | + * Only an answer for the CURRENT id is returned, so a page whose redirect |
| 99 | + * changes never shows the previous invitation's names. |
| 100 | + */ |
| 101 | +export function useInvitationPreview(redirect: string | null | undefined): AuthInvitationPreview | null { |
| 102 | + const { getInvitationPreview } = useAuth(); |
| 103 | + const invitationId = invitationIdOf(redirect); |
| 104 | + const [answer, setAnswer] = useState<{ id: string; preview: AuthInvitationPreview } | null>(null); |
| 105 | + useEffect(() => { |
| 106 | + if (!invitationId) return; |
| 107 | + let cancelled = false; |
| 108 | + // Inside the chain, so a synchronous throw is a rejection like any other. |
| 109 | + Promise.resolve() |
| 110 | + .then(() => getInvitationPreview(invitationId)) |
| 111 | + .then( |
| 112 | + (preview) => { |
| 113 | + if (!cancelled && preview) setAnswer({ id: invitationId, preview }); |
| 114 | + }, |
| 115 | + () => { |
| 116 | + // A failed read is not news for this page: the generic copy stays, |
| 117 | + // and the sign-in or sign-up request reports the server for itself. |
| 118 | + }, |
| 119 | + ); |
| 120 | + return () => { |
| 121 | + cancelled = true; |
| 122 | + }; |
| 123 | + }, [invitationId, getInvitationPreview]); |
| 124 | + return answer && answer.id === invitationId ? answer.preview : null; |
| 125 | +} |
| 126 | + |
| 127 | +/** |
| 128 | + * The invitation card's title and description for one of the two pages: with |
| 129 | + * a preview, "INVITER invited you to join ORG" (or "You are invited to join |
| 130 | + * ORG" when the inviter has no name) over the masked address to use; without |
| 131 | + * one, the generic copy each page showed before the preview existed. |
| 132 | + */ |
| 133 | +export function invitationArrivalCopy( |
| 134 | + page: 'login' | 'register', |
| 135 | + preview: AuthInvitationPreview | null, |
| 136 | + t: TranslateFn, |
| 137 | +): { title: string; description: string } { |
| 138 | + if (!preview) { |
| 139 | + return page === 'login' |
| 140 | + ? { |
| 141 | + title: t('auth.login.invitationTitle', { defaultValue: 'Accept your invitation' }), |
| 142 | + description: t('auth.login.invitationDescription', { |
| 143 | + defaultValue: 'Use the email address the invitation was sent to.', |
| 144 | + }), |
| 145 | + } |
| 146 | + : { |
| 147 | + title: t('auth.register.invitationTitle', { defaultValue: 'Create an account to join' }), |
| 148 | + description: t('auth.register.invitationDescription', { |
| 149 | + defaultValue: |
| 150 | + 'Register with the email address the invitation was sent to. The invitation can only be accepted with that address.', |
| 151 | + }), |
| 152 | + }; |
| 153 | + } |
| 154 | + const organization = preview.organizationName; |
| 155 | + const title = preview.inviterName |
| 156 | + ? t('auth.login.invitationPreviewTitle', { |
| 157 | + defaultValue: '{{inviter}} invited you to join {{organization}}', |
| 158 | + inviter: preview.inviterName, |
| 159 | + organization, |
| 160 | + }) |
| 161 | + : t('auth.login.invitationPreviewTitleNoInviter', { |
| 162 | + defaultValue: 'You are invited to join {{organization}}', |
| 163 | + organization, |
| 164 | + }); |
| 165 | + const description = |
| 166 | + page === 'login' |
| 167 | + ? t('auth.login.invitationPreviewDescription', { |
| 168 | + defaultValue: 'Use the email address the invitation was sent to ({{email}}).', |
| 169 | + email: preview.email, |
| 170 | + }) |
| 171 | + : t('auth.register.invitationPreviewDescription', { |
| 172 | + defaultValue: |
| 173 | + 'Register with the email address the invitation was sent to ({{email}}). The invitation can only be accepted with that address.', |
| 174 | + email: preview.email, |
| 175 | + }); |
| 176 | + return { title, description }; |
| 177 | +} |
0 commit comments