(options: {
readonly popupName: string;
readonly callbackPath?: string;
readonly noAuthorizationUrlMessage?: string;
readonly popupBlockedMessage?: string;
readonly popupClosedMessage?: string;
readonly detectPopupClosed?: boolean;
readonly startErrorMessage?: string;
})
| 141 | } |
| 142 | |
| 143 | export function useOAuthPopupFlow< |
| 144 | TPayload extends OAuthCompletionPayload = OAuthCompletionPayload, |
| 145 | >(options: { |
| 146 | readonly popupName: string; |
| 147 | readonly callbackPath?: string; |
| 148 | readonly noAuthorizationUrlMessage?: string; |
| 149 | readonly popupBlockedMessage?: string; |
| 150 | readonly popupClosedMessage?: string; |
| 151 | readonly detectPopupClosed?: boolean; |
| 152 | readonly startErrorMessage?: string; |
| 153 | }) { |
| 154 | const { |
| 155 | detectPopupClosed = true, |
| 156 | callbackPath, |
| 157 | noAuthorizationUrlMessage, |
| 158 | popupBlockedMessage, |
| 159 | popupClosedMessage, |
| 160 | popupName, |
| 161 | startErrorMessage, |
| 162 | } = options; |
| 163 | const doStartOAuth = useAtomSet(startOAuth, { mode: "promiseExit" }); |
| 164 | const doCancelOAuth = useAtomSet(cancelOAuth, { mode: "promiseExit" }); |
| 165 | const doOAuthConnectionCompleted = useAtomSet(oauthConnectionCompleted, { mode: "promiseExit" }); |
| 166 | const reportHandledError = useReportHandledError(); |
| 167 | const [busy, setBusy] = useState(false); |
| 168 | const [error, setError] = useState<string | null>(null); |
| 169 | const cleanupRef = useRef<(() => void) | null>(null); |
| 170 | const sessionRef = useRef<{ readonly state: string } | null>(null); |
| 171 | |
| 172 | const cancelSession = useCallback( |
| 173 | (state: string) => { |
| 174 | void doCancelOAuth({ payload: { state: OAuthState.make(state) } }); |
| 175 | }, |
| 176 | [doCancelOAuth], |
| 177 | ); |
| 178 | |
| 179 | const cancel = useCallback(() => { |
| 180 | const session = sessionRef.current; |
| 181 | cleanupRef.current?.(); |
| 182 | cleanupRef.current = null; |
| 183 | sessionRef.current = null; |
| 184 | if (session) cancelSession(session.state); |
| 185 | setBusy(false); |
| 186 | }, [cancelSession]); |
| 187 | |
| 188 | useEffect( |
| 189 | () => () => { |
| 190 | const session = sessionRef.current; |
| 191 | cleanupRef.current?.(); |
| 192 | cleanupRef.current = null; |
| 193 | sessionRef.current = null; |
| 194 | if (session) cancelSession(session.state); |
| 195 | }, |
| 196 | [cancelSession], |
| 197 | ); |
| 198 | |
| 199 | const openAuthorization = useCallback( |
| 200 | async (input: StartOAuthAuthorizationInput<TPayload>) => { |
no test coverage detected