/
useInvitationStatus.ts
190 lines (170 loc) · 5.71 KB
/
useInvitationStatus.ts
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
//
// Copyright 2022 DXOS.org
//
import { useReducer, type Reducer, useMemo, useCallback, useEffect } from 'react';
import { type PublicKey } from '@dxos/client';
import {
type AuthenticatingInvitationObservable,
type CancellableInvitationObservable,
Invitation,
InvitationEncoder,
} from '@dxos/client/invitations';
import { log } from '@dxos/log';
export type InvitationResult = {
spaceKey: PublicKey | null;
identityKey: PublicKey | null;
swarmKey: PublicKey | null;
target: string | null;
};
interface InvitationReducerState {
status: Invitation.State; // TODO(burdon): Rename state.
haltedAt?: Invitation.State;
result: InvitationResult;
error?: number;
observable?: CancellableInvitationObservable | AuthenticatingInvitationObservable;
id?: string;
invitationCode?: string;
authCode?: string;
}
export type InvitationAction =
| {
status:
| Invitation.State.INIT
| Invitation.State.CONNECTED
| Invitation.State.READY_FOR_AUTHENTICATION
| Invitation.State.AUTHENTICATING;
}
| {
status: Invitation.State.CONNECTING;
observable: CancellableInvitationObservable;
}
| {
status: Invitation.State.SUCCESS;
result: InvitationResult;
}
| {
status: Invitation.State.CANCELLED | Invitation.State.TIMEOUT;
haltedAt: Invitation.State;
}
| {
status: Invitation.State.ERROR;
error?: Error;
haltedAt: Invitation.State;
};
export type InvitationStatus = {
id?: string;
invitationCode?: string;
authCode?: string;
authMethod?: Invitation.AuthMethod;
type?: Invitation.Type;
status: Invitation.State;
haltedAt?: Invitation.State;
result: InvitationResult;
error?: number;
cancel(): void;
// TODO(wittjosiah): Remove?
connect(observable: CancellableInvitationObservable): void;
authenticate(authCode: string): Promise<void>;
};
export const useInvitationStatus = (initialObservable?: CancellableInvitationObservable): InvitationStatus => {
const [state, dispatch] = useReducer<Reducer<InvitationReducerState, InvitationAction>, null>(
(prev, action) => {
log('useInvitationStatus', { action });
return {
...prev,
// TODO(burdon): State.
status: action.status,
// `invitationObservable`, `secret`, and `result` is persisted between the status-actions that set them.
result: action.status === Invitation.State.SUCCESS ? action.result : prev.result,
// `error` gets set each time we enter the error state
...(action.status === Invitation.State.ERROR && { error: action.error }),
// `haltedAt` gets set on only the first error/cancelled/timeout action and reset on any others.
...((action.status === Invitation.State.ERROR ||
action.status === Invitation.State.CANCELLED ||
action.status === Invitation.State.TIMEOUT) && {
haltedAt: typeof prev.haltedAt === 'undefined' ? action.haltedAt : prev.haltedAt,
}),
observable:
action.status === Invitation.State.CONNECTING ? action.observable ?? prev.observable : prev.observable,
} as InvitationReducerState;
},
null,
(_arg: null) => {
return {
status: Invitation.State.INIT,
result: { spaceKey: null, identityKey: null, swarmKey: null, target: null },
observable: initialObservable,
};
},
);
// Handle unmount.
useEffect(() => {
const update = (invitation: Invitation) => {
switch (invitation.state) {
case Invitation.State.CONNECTED:
case Invitation.State.READY_FOR_AUTHENTICATION:
case Invitation.State.AUTHENTICATING: {
dispatch({
status: invitation.state,
});
break;
}
case Invitation.State.SUCCESS: {
dispatch({
status: invitation.state,
result: {
spaceKey: invitation.spaceKey || null,
identityKey: invitation.identityKey || null,
swarmKey: invitation.swarmKey || null,
target: invitation.target || null,
},
});
break;
}
case Invitation.State.CANCELLED:
case Invitation.State.TIMEOUT: {
dispatch({ status: invitation.state, haltedAt: state.status });
break;
}
}
};
const subscription = state.observable?.subscribe(update, (err: Error) => {
dispatch({ status: Invitation.State.ERROR, error: err, haltedAt: state.status });
});
const currentState = state.observable?.get();
if (currentState) {
update(currentState);
}
return () => subscription?.unsubscribe();
}, [state.observable, state.status]);
// Return memoized callbacks & values.
const connect = useCallback((observable: CancellableInvitationObservable) => {
dispatch({ status: Invitation.State.CONNECTING, observable });
}, []);
const authenticate = useCallback(
(authCode: string) => {
log('authenticating...', { authCode });
return (state.observable as AuthenticatingInvitationObservable).authenticate(authCode);
},
[state.observable],
);
const cancel = useCallback(async () => state.observable?.cancel(), [state.observable]);
return useMemo(() => {
const invitation = state.observable?.get();
const result = {
status: state.status,
haltedAt: state.haltedAt,
result: state.result,
error: state.error,
cancel,
connect,
authenticate,
id: invitation?.invitationId,
invitationCode: invitation ? InvitationEncoder.encode(invitation) : undefined,
authCode: invitation?.authCode,
authMethod: invitation?.authMethod,
type: invitation?.type,
};
return result;
}, [state, cancel, connect, authenticate]);
};