@shipfox/client-invitations 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +2 -0
- package/.turbo/turbo-type$colon$emit.log +1 -0
- package/.turbo/turbo-type.log +1 -0
- package/CHANGELOG.md +111 -0
- package/LICENSE +21 -0
- package/dist/complete-acceptance.d.ts +17 -0
- package/dist/complete-acceptance.d.ts.map +1 -0
- package/dist/complete-acceptance.js +29 -0
- package/dist/complete-acceptance.js.map +1 -0
- package/dist/feature.d.ts +9 -0
- package/dist/feature.d.ts.map +1 -0
- package/dist/feature.js +13 -0
- package/dist/feature.js.map +1 -0
- package/dist/hooks/api/accept-invitation.d.ts +11 -0
- package/dist/hooks/api/accept-invitation.d.ts.map +1 -0
- package/dist/hooks/api/accept-invitation.js +15 -0
- package/dist/hooks/api/accept-invitation.js.map +1 -0
- package/dist/hooks/api/preview-invitation.d.ts +19 -0
- package/dist/hooks/api/preview-invitation.d.ts.map +1 -0
- package/dist/hooks/api/preview-invitation.js +24 -0
- package/dist/hooks/api/preview-invitation.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -0
- package/dist/pages/invitation-accept-page.d.ts +2 -0
- package/dist/pages/invitation-accept-page.d.ts.map +1 -0
- package/dist/pages/invitation-accept-page.js +384 -0
- package/dist/pages/invitation-accept-page.js.map +1 -0
- package/dist/routes/accept.d.ts +6 -0
- package/dist/routes/accept.d.ts.map +1 -0
- package/dist/routes/accept.js +7 -0
- package/dist/routes/accept.js.map +1 -0
- package/dist/tsconfig.test.tsbuildinfo +1 -0
- package/package.json +98 -0
- package/src/complete-acceptance.test.ts +55 -0
- package/src/complete-acceptance.ts +33 -0
- package/src/feature.ts +12 -0
- package/src/hooks/api/accept-invitation.ts +17 -0
- package/src/hooks/api/preview-invitation.ts +23 -0
- package/src/index.ts +7 -0
- package/src/pages/invitation-accept-page.tsx +298 -0
- package/src/routes/accept.tsx +4 -0
- package/test/setup.ts +3 -0
- package/tsconfig.build.json +9 -0
- package/tsconfig.build.tsbuildinfo +1 -0
- package/tsconfig.json +3 -0
- package/tsconfig.test.json +8 -0
- package/vitest.config.ts +31 -0
package/package.json
ADDED
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@shipfox/client-invitations",
|
|
3
|
+
"license": "MIT",
|
|
4
|
+
"version": "0.2.0",
|
|
5
|
+
"repository": {
|
|
6
|
+
"type": "git",
|
|
7
|
+
"url": "git+https://github.com/ShipfoxHQ/shipfox.git",
|
|
8
|
+
"directory": "libs/client/invitations"
|
|
9
|
+
},
|
|
10
|
+
"type": "module",
|
|
11
|
+
"main": "dist/index.js",
|
|
12
|
+
"types": "dist/index.d.ts",
|
|
13
|
+
"imports": {
|
|
14
|
+
"#*": {
|
|
15
|
+
"types": "./src/*",
|
|
16
|
+
"workspace-source": "./src/*",
|
|
17
|
+
"development": "./src/*",
|
|
18
|
+
"default": "./dist/*"
|
|
19
|
+
},
|
|
20
|
+
"#test/*": "./test/*"
|
|
21
|
+
},
|
|
22
|
+
"exports": {
|
|
23
|
+
".": {
|
|
24
|
+
"development": {
|
|
25
|
+
"types": "./src/index.ts",
|
|
26
|
+
"default": "./src/index.ts"
|
|
27
|
+
},
|
|
28
|
+
"default": {
|
|
29
|
+
"types": "./dist/index.d.ts",
|
|
30
|
+
"default": "./dist/index.js"
|
|
31
|
+
}
|
|
32
|
+
},
|
|
33
|
+
"./feature": {
|
|
34
|
+
"development": {
|
|
35
|
+
"types": "./src/feature.ts",
|
|
36
|
+
"default": "./src/feature.ts"
|
|
37
|
+
},
|
|
38
|
+
"default": {
|
|
39
|
+
"types": "./dist/feature.d.ts",
|
|
40
|
+
"default": "./dist/feature.js"
|
|
41
|
+
}
|
|
42
|
+
},
|
|
43
|
+
"./routes/*": {
|
|
44
|
+
"development": {
|
|
45
|
+
"types": "./src/routes/*.tsx",
|
|
46
|
+
"default": "./src/routes/*.tsx"
|
|
47
|
+
},
|
|
48
|
+
"default": {
|
|
49
|
+
"types": "./dist/routes/*.d.ts",
|
|
50
|
+
"default": "./dist/routes/*.js"
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
},
|
|
54
|
+
"dependencies": {
|
|
55
|
+
"@swc/helpers": "^0.5.17",
|
|
56
|
+
"@tanstack/react-query": "^5.101.0",
|
|
57
|
+
"@shipfox/client-api": "0.2.0",
|
|
58
|
+
"@shipfox/api-workspaces-dto": "2.0.0",
|
|
59
|
+
"@shipfox/client-shell": "0.2.0",
|
|
60
|
+
"@shipfox/api-auth-dto": "2.0.0",
|
|
61
|
+
"@shipfox/react-ui": "0.3.1",
|
|
62
|
+
"@shipfox/client-auth": "0.2.0"
|
|
63
|
+
},
|
|
64
|
+
"peerDependencies": {
|
|
65
|
+
"@tanstack/react-query": "^5.101.0",
|
|
66
|
+
"@tanstack/react-router": "^1.170.16",
|
|
67
|
+
"react": "^19.0.0",
|
|
68
|
+
"react-dom": "^19.0.0"
|
|
69
|
+
},
|
|
70
|
+
"devDependencies": {
|
|
71
|
+
"@tanstack/react-query": "^5.101.0",
|
|
72
|
+
"@tanstack/react-router": "^1.170.16",
|
|
73
|
+
"@testing-library/jest-dom": "^6.6.3",
|
|
74
|
+
"@testing-library/react": "^16.3.2",
|
|
75
|
+
"@testing-library/user-event": "^14.6.1",
|
|
76
|
+
"@types/react": "^19.1.11",
|
|
77
|
+
"@types/react-dom": "^19.1.7",
|
|
78
|
+
"jsdom": "^29.0.0",
|
|
79
|
+
"react": "^19.1.1",
|
|
80
|
+
"react-dom": "^19.1.1",
|
|
81
|
+
"@shipfox/client-test-setup": "0.0.2",
|
|
82
|
+
"@shipfox/biome": "1.8.1",
|
|
83
|
+
"@shipfox/swc": "1.2.5",
|
|
84
|
+
"@shipfox/typescript": "1.1.6",
|
|
85
|
+
"@shipfox/ts-config": "1.3.8",
|
|
86
|
+
"@shipfox/vite": "1.2.4",
|
|
87
|
+
"@shipfox/vitest": "1.2.2"
|
|
88
|
+
},
|
|
89
|
+
"scripts": {
|
|
90
|
+
"build": "shipfox-swc",
|
|
91
|
+
"check": "shipfox-biome-check",
|
|
92
|
+
"check:fix": "shipfox-biome-check --write",
|
|
93
|
+
"test": "shipfox-vitest-run",
|
|
94
|
+
"test:watch": "shipfox-vitest-watch",
|
|
95
|
+
"type": "shipfox-tsc-check",
|
|
96
|
+
"type:emit": "shipfox-tsc-emit"
|
|
97
|
+
}
|
|
98
|
+
}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import {toast} from '@shipfox/react-ui/toast';
|
|
2
|
+
import {beforeEach, describe, expect, it, vi} from '@shipfox/vitest/vi';
|
|
3
|
+
|
|
4
|
+
import {completeInvitationAcceptance} from './complete-acceptance.js';
|
|
5
|
+
|
|
6
|
+
describe('completeInvitationAcceptance', () => {
|
|
7
|
+
beforeEach(() => {
|
|
8
|
+
vi.spyOn(toast, 'success').mockImplementation(() => 'toast-id');
|
|
9
|
+
});
|
|
10
|
+
|
|
11
|
+
it('refreshes auth before navigating to the joined workspace', async () => {
|
|
12
|
+
const calls: string[] = [];
|
|
13
|
+
const refreshAuth = vi.fn(() => {
|
|
14
|
+
calls.push('refreshAuth');
|
|
15
|
+
return Promise.resolve();
|
|
16
|
+
});
|
|
17
|
+
const navigate = vi.fn(() => {
|
|
18
|
+
calls.push('navigate');
|
|
19
|
+
return Promise.resolve();
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
await completeInvitationAcceptance({
|
|
23
|
+
navigate,
|
|
24
|
+
refreshAuth,
|
|
25
|
+
workspaceId: 'workspace-1',
|
|
26
|
+
workspaceName: 'Acme',
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
expect(refreshAuth).toHaveBeenCalledTimes(1);
|
|
30
|
+
expect(toast.success).toHaveBeenCalledWith('You joined Acme.');
|
|
31
|
+
expect(navigate).toHaveBeenCalledWith({
|
|
32
|
+
params: {wid: 'workspace-1'},
|
|
33
|
+
to: '/workspaces/$wid',
|
|
34
|
+
});
|
|
35
|
+
expect(calls).toEqual(['refreshAuth', 'navigate']);
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
it('still navigates when auth refresh fails', async () => {
|
|
39
|
+
const refreshAuth = vi.fn(() => Promise.reject(new Error('refresh failed')));
|
|
40
|
+
const navigate = vi.fn();
|
|
41
|
+
|
|
42
|
+
await completeInvitationAcceptance({
|
|
43
|
+
navigate,
|
|
44
|
+
refreshAuth,
|
|
45
|
+
workspaceId: 'workspace-1',
|
|
46
|
+
workspaceName: 'Acme',
|
|
47
|
+
});
|
|
48
|
+
|
|
49
|
+
expect(toast.success).toHaveBeenCalledWith('You joined Acme.');
|
|
50
|
+
expect(navigate).toHaveBeenCalledWith({
|
|
51
|
+
params: {wid: 'workspace-1'},
|
|
52
|
+
to: '/workspaces/$wid',
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
});
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
import {toast} from '@shipfox/react-ui/toast';
|
|
2
|
+
import type {NavigateOptions} from '@tanstack/react-router';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Final step of every successful invitation accept path (existing-user match,
|
|
6
|
+
* signup-with-invitation success, login-then-accept). Refreshes the auth
|
|
7
|
+
* session so the JWT carries the new membership before navigating, then
|
|
8
|
+
* routes the user into the workspace home.
|
|
9
|
+
*
|
|
10
|
+
* `refreshAuth` is passed in so this helper stays a plain function (no hook).
|
|
11
|
+
* Call sites construct it via `useRefreshAuth()` from `@shipfox/client-auth`.
|
|
12
|
+
*/
|
|
13
|
+
export async function completeInvitationAcceptance(params: {
|
|
14
|
+
workspaceId: string;
|
|
15
|
+
workspaceName: string;
|
|
16
|
+
refreshAuth: () => Promise<unknown>;
|
|
17
|
+
navigate: (opts: NavigateOptions) => Promise<void> | void;
|
|
18
|
+
}): Promise<void> {
|
|
19
|
+
// Access tokens embed memberships at issue time, so refresh before AuthGuard
|
|
20
|
+
// reads the accepted workspace.
|
|
21
|
+
try {
|
|
22
|
+
await params.refreshAuth();
|
|
23
|
+
} catch {
|
|
24
|
+
// Even if refresh fails the membership is real in the DB; surface the
|
|
25
|
+
// success toast and let the user re-auth if their session has fully
|
|
26
|
+
// expired. The next API call will redirect to login as usual.
|
|
27
|
+
}
|
|
28
|
+
toast.success(`You joined ${params.workspaceName}.`);
|
|
29
|
+
await params.navigate({
|
|
30
|
+
to: '/workspaces/$wid',
|
|
31
|
+
params: {wid: params.workspaceId},
|
|
32
|
+
});
|
|
33
|
+
}
|
package/src/feature.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import {defineClientFeature} from '@shipfox/client-shell';
|
|
2
|
+
|
|
3
|
+
export const invitationsFeature = defineClientFeature({
|
|
4
|
+
id: 'shipfox.invitations',
|
|
5
|
+
routes: [
|
|
6
|
+
{
|
|
7
|
+
path: '/invitations/accept',
|
|
8
|
+
parent: 'root',
|
|
9
|
+
impl: '@shipfox/client-invitations/routes/accept',
|
|
10
|
+
},
|
|
11
|
+
],
|
|
12
|
+
});
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
AcceptInvitationBodyDto,
|
|
3
|
+
AcceptInvitationResponseDto,
|
|
4
|
+
} from '@shipfox/api-workspaces-dto';
|
|
5
|
+
import {apiRequest} from '@shipfox/client-api';
|
|
6
|
+
import {useMutation} from '@tanstack/react-query';
|
|
7
|
+
|
|
8
|
+
async function acceptInvitation(body: AcceptInvitationBodyDto) {
|
|
9
|
+
return await apiRequest<AcceptInvitationResponseDto>('/invitations/accept', {
|
|
10
|
+
method: 'POST',
|
|
11
|
+
body,
|
|
12
|
+
});
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function useAcceptInvitation() {
|
|
16
|
+
return useMutation({mutationFn: acceptInvitation});
|
|
17
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type {PreviewInvitationResponseDto} from '@shipfox/api-workspaces-dto';
|
|
2
|
+
import {apiRequest} from '@shipfox/client-api';
|
|
3
|
+
import {useQuery} from '@tanstack/react-query';
|
|
4
|
+
|
|
5
|
+
export const previewInvitationQueryKey = (token: string) =>
|
|
6
|
+
['invitations', 'preview', token] as const;
|
|
7
|
+
|
|
8
|
+
async function previewInvitation(token: string): Promise<PreviewInvitationResponseDto> {
|
|
9
|
+
const params = new URLSearchParams({token});
|
|
10
|
+
return await apiRequest<PreviewInvitationResponseDto>(
|
|
11
|
+
`/invitations/preview?${params.toString()}`,
|
|
12
|
+
);
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export function usePreviewInvitation(token: string | undefined) {
|
|
16
|
+
return useQuery({
|
|
17
|
+
queryKey: previewInvitationQueryKey(token ?? ''),
|
|
18
|
+
queryFn: () => previewInvitation(token as string),
|
|
19
|
+
enabled: Boolean(token),
|
|
20
|
+
retry: false,
|
|
21
|
+
staleTime: 30_000,
|
|
22
|
+
});
|
|
23
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export {completeInvitationAcceptance} from './complete-acceptance.js';
|
|
2
|
+
export {useAcceptInvitation} from './hooks/api/accept-invitation.js';
|
|
3
|
+
export {
|
|
4
|
+
previewInvitationQueryKey,
|
|
5
|
+
usePreviewInvitation,
|
|
6
|
+
} from './hooks/api/preview-invitation.js';
|
|
7
|
+
export {InvitationAcceptPage} from './pages/invitation-accept-page.js';
|
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
import {AuthShell, useAuthState, useRefreshAuth} from '@shipfox/client-auth';
|
|
2
|
+
import {Button} from '@shipfox/react-ui/button';
|
|
3
|
+
import {Callout} from '@shipfox/react-ui/callout';
|
|
4
|
+
import {ShipfoxLoader} from '@shipfox/react-ui/loader';
|
|
5
|
+
import {toast} from '@shipfox/react-ui/toast';
|
|
6
|
+
import {Text} from '@shipfox/react-ui/typography';
|
|
7
|
+
import {formatDate} from '@shipfox/react-ui/utils';
|
|
8
|
+
import {Link, useNavigate, useSearch} from '@tanstack/react-router';
|
|
9
|
+
import {useCallback, useEffect, useRef} from 'react';
|
|
10
|
+
import {completeInvitationAcceptance} from '#complete-acceptance.js';
|
|
11
|
+
import {useAcceptInvitation} from '#hooks/api/accept-invitation.js';
|
|
12
|
+
import {usePreviewInvitation} from '#hooks/api/preview-invitation.js';
|
|
13
|
+
|
|
14
|
+
// Module-scoped guard so React StrictMode's double-mount in dev does not
|
|
15
|
+
// fire the accept mutation twice for the same token.
|
|
16
|
+
const inFlightAccepts = new Set<string>();
|
|
17
|
+
const toastedTerminals = new Set<string>();
|
|
18
|
+
const TOASTED_TERMINALS_MAX = 32;
|
|
19
|
+
|
|
20
|
+
export function InvitationAcceptPage() {
|
|
21
|
+
const search = useSearch({strict: false}) as {token?: unknown};
|
|
22
|
+
const token =
|
|
23
|
+
typeof search.token === 'string' && search.token.length > 0 ? search.token : undefined;
|
|
24
|
+
const navigate = useNavigate();
|
|
25
|
+
const auth = useAuthState();
|
|
26
|
+
const refreshAuth = useRefreshAuth();
|
|
27
|
+
const preview = usePreviewInvitation(token);
|
|
28
|
+
const accept = useAcceptInvitation();
|
|
29
|
+
const hasKickedAccept = useRef(false);
|
|
30
|
+
|
|
31
|
+
useEffect(() => {
|
|
32
|
+
if (!token) {
|
|
33
|
+
toast.error('This invitation link is missing a token.');
|
|
34
|
+
const timeout = window.setTimeout(() => {
|
|
35
|
+
navigate({to: '/', replace: true});
|
|
36
|
+
}, 2000);
|
|
37
|
+
return () => window.clearTimeout(timeout);
|
|
38
|
+
}
|
|
39
|
+
return undefined;
|
|
40
|
+
}, [navigate, token]);
|
|
41
|
+
|
|
42
|
+
const completeAccept = useCallback(
|
|
43
|
+
async (workspaceId: string, workspaceName: string) => {
|
|
44
|
+
await completeInvitationAcceptance({
|
|
45
|
+
workspaceId,
|
|
46
|
+
workspaceName,
|
|
47
|
+
refreshAuth,
|
|
48
|
+
navigate,
|
|
49
|
+
});
|
|
50
|
+
},
|
|
51
|
+
[navigate, refreshAuth],
|
|
52
|
+
);
|
|
53
|
+
|
|
54
|
+
const runAccept = useCallback(
|
|
55
|
+
async (params: {token: string; workspaceName: string}) => {
|
|
56
|
+
const result = await accept.mutateAsync({token: params.token});
|
|
57
|
+
await completeAccept(result.membership.workspace_id, params.workspaceName);
|
|
58
|
+
},
|
|
59
|
+
[accept, completeAccept],
|
|
60
|
+
);
|
|
61
|
+
|
|
62
|
+
// Auto-accept when authenticated and emails match (also handles already_used
|
|
63
|
+
// for the canonical authenticated path: backend returns already_member=true).
|
|
64
|
+
useEffect(() => {
|
|
65
|
+
if (!token) return;
|
|
66
|
+
if (auth.isLoading) return;
|
|
67
|
+
if (hasKickedAccept.current) return;
|
|
68
|
+
if (inFlightAccepts.has(token)) return;
|
|
69
|
+
const data = preview.data;
|
|
70
|
+
if (!data) return;
|
|
71
|
+
if (data.status !== 'pending') return;
|
|
72
|
+
if (!auth.isAuthenticated) return;
|
|
73
|
+
|
|
74
|
+
const authedEmail = auth.user?.email?.toLowerCase();
|
|
75
|
+
if (authedEmail !== data.email.toLowerCase()) return;
|
|
76
|
+
|
|
77
|
+
hasKickedAccept.current = true;
|
|
78
|
+
inFlightAccepts.add(token);
|
|
79
|
+
|
|
80
|
+
runAccept({token, workspaceName: data.workspace_name})
|
|
81
|
+
.catch(() => {
|
|
82
|
+
// The mutation surfaces its error via accept.error; the UI re-renders
|
|
83
|
+
// into the auth-match-error state below.
|
|
84
|
+
})
|
|
85
|
+
.finally(() => {
|
|
86
|
+
inFlightAccepts.delete(token);
|
|
87
|
+
});
|
|
88
|
+
}, [auth.isAuthenticated, auth.isLoading, auth.user?.email, preview.data, runAccept, token]);
|
|
89
|
+
|
|
90
|
+
if (!token) {
|
|
91
|
+
return (
|
|
92
|
+
<AuthShell title="Invalid link" description="This invitation link is incomplete.">
|
|
93
|
+
<Callout role="alert" type="error">
|
|
94
|
+
This invitation link is missing a token. Ask your administrator to resend it.
|
|
95
|
+
</Callout>
|
|
96
|
+
<Text size="sm" className="text-center text-foreground-neutral-muted" aria-live="polite">
|
|
97
|
+
Redirecting to the dashboard…
|
|
98
|
+
</Text>
|
|
99
|
+
</AuthShell>
|
|
100
|
+
);
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
if (preview.isLoading) {
|
|
104
|
+
return (
|
|
105
|
+
<AuthShell title="Loading invitation" description="One moment please.">
|
|
106
|
+
<CenteredLoader />
|
|
107
|
+
</AuthShell>
|
|
108
|
+
);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
if (preview.isError) {
|
|
112
|
+
return (
|
|
113
|
+
<AuthShell title="Couldn't load invitation" description="Try again in a moment.">
|
|
114
|
+
<Callout role="alert" type="error">
|
|
115
|
+
We couldn't reach the server. Check your connection and retry.
|
|
116
|
+
</Callout>
|
|
117
|
+
<Button className="w-full" onClick={() => preview.refetch()}>
|
|
118
|
+
Try again
|
|
119
|
+
</Button>
|
|
120
|
+
</AuthShell>
|
|
121
|
+
);
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
const data = preview.data;
|
|
125
|
+
if (!data) {
|
|
126
|
+
return null;
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
if (data.status === 'invalid') {
|
|
130
|
+
notifyOnce(`invalid:${token}`, () => toast.error('This invitation link is no longer valid.'));
|
|
131
|
+
return (
|
|
132
|
+
<AuthShell title="Invalid invitation" description="The link you used didn't work.">
|
|
133
|
+
<Callout role="alert" type="error">
|
|
134
|
+
This invitation link is not valid. Ask your administrator to send a new one.
|
|
135
|
+
</Callout>
|
|
136
|
+
<GoHomeButton navigate={navigate} />
|
|
137
|
+
</AuthShell>
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
if (data.status === 'expired') {
|
|
142
|
+
notifyOnce(`expired:${token}`, () => toast.error('This invitation has expired.'));
|
|
143
|
+
return (
|
|
144
|
+
<AuthShell title="Invitation expired" description="The link is no longer accepting joins.">
|
|
145
|
+
<Callout role="alert" type="error">
|
|
146
|
+
This invitation expired on {formatDate(data.expires_at)}. Ask your administrator to send a
|
|
147
|
+
new one.
|
|
148
|
+
</Callout>
|
|
149
|
+
<GoHomeButton navigate={navigate} />
|
|
150
|
+
</AuthShell>
|
|
151
|
+
);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
if (data.status === 'already_used') {
|
|
155
|
+
notifyOnce(`already-used:${token}`, () =>
|
|
156
|
+
toast.info(`This invitation has already been accepted.`),
|
|
157
|
+
);
|
|
158
|
+
return (
|
|
159
|
+
<AuthShell title="Invitation already used" description={data.workspace_name}>
|
|
160
|
+
<Callout role="alert" type="info">
|
|
161
|
+
This invitation has already been accepted.{' '}
|
|
162
|
+
{auth.isAuthenticated
|
|
163
|
+
? `Open your dashboard to access ${data.workspace_name} if your account is a member.`
|
|
164
|
+
: `Log in to access ${data.workspace_name}.`}
|
|
165
|
+
</Callout>
|
|
166
|
+
{auth.isAuthenticated ? null : (
|
|
167
|
+
<Button asChild className="w-full">
|
|
168
|
+
<Link to="/auth/login">Log in</Link>
|
|
169
|
+
</Button>
|
|
170
|
+
)}
|
|
171
|
+
<GoHomeButton navigate={navigate} secondary />
|
|
172
|
+
</AuthShell>
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
|
|
176
|
+
const inviterLine =
|
|
177
|
+
data.invited_by_display != null
|
|
178
|
+
? `Invited by ${data.invited_by_display} to join as ${data.email}.`
|
|
179
|
+
: `Invited to join as ${data.email}.`;
|
|
180
|
+
|
|
181
|
+
if (!auth.isAuthenticated) {
|
|
182
|
+
const redirect = `/invitations/accept?token=${encodeURIComponent(token)}`;
|
|
183
|
+
const signupHref = `/auth/signup?redirect=${encodeURIComponent(redirect)}`;
|
|
184
|
+
const loginHref = `/auth/login?redirect=${encodeURIComponent(redirect)}`;
|
|
185
|
+
return (
|
|
186
|
+
<AuthShell title={data.workspace_name} description={inviterLine}>
|
|
187
|
+
<div className="flex flex-col gap-16">
|
|
188
|
+
<Button asChild className="w-full">
|
|
189
|
+
<Link to={signupHref}>Create account</Link>
|
|
190
|
+
</Button>
|
|
191
|
+
<Button asChild className="w-full" variant="secondary">
|
|
192
|
+
<Link to={loginHref}>I already have an account</Link>
|
|
193
|
+
</Button>
|
|
194
|
+
</div>
|
|
195
|
+
<Text size="xs" className="text-center text-foreground-neutral-muted">
|
|
196
|
+
Not {data.email}? Contact your administrator.
|
|
197
|
+
</Text>
|
|
198
|
+
</AuthShell>
|
|
199
|
+
);
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
const viewerEmail = auth.user?.email?.toLowerCase();
|
|
203
|
+
const inviteeEmail = data.email.toLowerCase();
|
|
204
|
+
if (viewerEmail !== inviteeEmail) {
|
|
205
|
+
const redirect = `/invitations/accept?token=${encodeURIComponent(token)}`;
|
|
206
|
+
const logoutHref = `/auth/logout?redirect=${encodeURIComponent(redirect)}`;
|
|
207
|
+
return (
|
|
208
|
+
<AuthShell title={data.workspace_name} description={inviterLine}>
|
|
209
|
+
<Callout role="alert" type="warning">
|
|
210
|
+
You're signed in as {auth.user?.email}, but this invitation is for {data.email}.
|
|
211
|
+
</Callout>
|
|
212
|
+
<div className="flex flex-col gap-16">
|
|
213
|
+
<Button asChild className="w-full">
|
|
214
|
+
<Link to={logoutHref}>Log out and continue</Link>
|
|
215
|
+
</Button>
|
|
216
|
+
<Button
|
|
217
|
+
className="w-full"
|
|
218
|
+
variant="secondary"
|
|
219
|
+
onClick={() => navigate({to: '/', replace: true})}
|
|
220
|
+
>
|
|
221
|
+
Cancel
|
|
222
|
+
</Button>
|
|
223
|
+
</div>
|
|
224
|
+
</AuthShell>
|
|
225
|
+
);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
// Authenticated + matches — auto-accept is in flight or about to render its
|
|
229
|
+
// result. Show either the pending state or the error state.
|
|
230
|
+
if (accept.isError) {
|
|
231
|
+
return (
|
|
232
|
+
<AuthShell title={data.workspace_name} description={inviterLine}>
|
|
233
|
+
<Callout role="alert" type="error">
|
|
234
|
+
We couldn't add you to {data.workspace_name}. Try again in a moment.
|
|
235
|
+
</Callout>
|
|
236
|
+
<Button
|
|
237
|
+
className="w-full"
|
|
238
|
+
isLoading={accept.isPending}
|
|
239
|
+
onClick={() => {
|
|
240
|
+
runAccept({token, workspaceName: data.workspace_name}).catch(() => {
|
|
241
|
+
// The mutation state drives the rendered error; avoid an
|
|
242
|
+
// unhandled rejection from the click handler.
|
|
243
|
+
});
|
|
244
|
+
}}
|
|
245
|
+
>
|
|
246
|
+
Try again
|
|
247
|
+
</Button>
|
|
248
|
+
</AuthShell>
|
|
249
|
+
);
|
|
250
|
+
}
|
|
251
|
+
|
|
252
|
+
return (
|
|
253
|
+
<AuthShell title={data.workspace_name} description={inviterLine}>
|
|
254
|
+
<div className="flex flex-col items-center gap-12" aria-live="polite">
|
|
255
|
+
<ShipfoxLoader />
|
|
256
|
+
<Text size="sm" className="text-foreground-neutral-subtle">
|
|
257
|
+
Adding you to {data.workspace_name}…
|
|
258
|
+
</Text>
|
|
259
|
+
</div>
|
|
260
|
+
</AuthShell>
|
|
261
|
+
);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
function CenteredLoader() {
|
|
265
|
+
return (
|
|
266
|
+
<div className="flex justify-center py-16" aria-busy="true">
|
|
267
|
+
<ShipfoxLoader />
|
|
268
|
+
</div>
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
function GoHomeButton({
|
|
273
|
+
navigate,
|
|
274
|
+
secondary,
|
|
275
|
+
}: {
|
|
276
|
+
navigate: ReturnType<typeof useNavigate>;
|
|
277
|
+
secondary?: boolean;
|
|
278
|
+
}) {
|
|
279
|
+
return (
|
|
280
|
+
<Button
|
|
281
|
+
className="w-full"
|
|
282
|
+
variant={secondary ? 'secondary' : 'primary'}
|
|
283
|
+
onClick={() => navigate({to: '/', replace: true})}
|
|
284
|
+
>
|
|
285
|
+
Go to dashboard
|
|
286
|
+
</Button>
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
function notifyOnce(key: string, fn: () => void): void {
|
|
291
|
+
if (toastedTerminals.has(key)) return;
|
|
292
|
+
if (toastedTerminals.size >= TOASTED_TERMINALS_MAX) {
|
|
293
|
+
const oldest = toastedTerminals.values().next().value;
|
|
294
|
+
if (oldest !== undefined) toastedTerminals.delete(oldest);
|
|
295
|
+
}
|
|
296
|
+
toastedTerminals.add(key);
|
|
297
|
+
fn();
|
|
298
|
+
}
|
package/test/setup.ts
ADDED