|
| 1 | +import { useEffectEvent } from '@rocket.chat/fuselage-hooks'; |
1 | 2 | import { useLoginWithIframe, useLoginWithToken, useSetting } from '@rocket.chat/ui-contexts'; |
2 | | -import { useCallback, useEffect, useState } from 'react'; |
| 3 | +import { useCallback, useState } from 'react'; |
3 | 4 |
|
4 | 5 | export const useIframe = () => { |
5 | 6 | const [iframeLoginUrl, setIframeLoginUrl] = useState<string | undefined>(undefined); |
@@ -31,54 +32,48 @@ export const useIframe = () => { |
31 | 32 | [iframeLogin, tokenLogin], |
32 | 33 | ); |
33 | 34 |
|
34 | | - const tryLogin = useCallback( |
35 | | - async (callback?: (error: Error | null | undefined, result: unknown) => void) => { |
36 | | - if (!enabled) { |
37 | | - return; |
38 | | - } |
39 | | - |
40 | | - let url = accountIframeUrl; |
41 | | - let separator = '?'; |
42 | | - if (url.indexOf('?') > -1) { |
43 | | - separator = '&'; |
44 | | - } |
| 35 | + const tryLogin = useEffectEvent(async (callback?: (error: Error | null | undefined, result: unknown) => void) => { |
| 36 | + if (!enabled) { |
| 37 | + return; |
| 38 | + } |
45 | 39 |
|
46 | | - if (navigator.userAgent.indexOf('Electron') > -1) { |
47 | | - url += `${separator}client=electron`; |
48 | | - } |
| 40 | + let url = accountIframeUrl; |
| 41 | + let separator = '?'; |
| 42 | + if (url.indexOf('?') > -1) { |
| 43 | + separator = '&'; |
| 44 | + } |
49 | 45 |
|
50 | | - try { |
51 | | - const result = await fetch(apiUrl, { |
52 | | - method: apiMethod, |
53 | | - headers: undefined, |
54 | | - credentials: 'include', |
55 | | - }); |
| 46 | + if (navigator.userAgent.indexOf('Electron') > -1) { |
| 47 | + url += `${separator}client=electron`; |
| 48 | + } |
56 | 49 |
|
57 | | - if (!result.ok || result.status !== 200) { |
58 | | - setIframeLoginUrl(url); |
59 | | - callback?.(new Error(), null); |
60 | | - return; |
61 | | - } |
| 50 | + try { |
| 51 | + const result = await fetch(apiUrl, { |
| 52 | + method: apiMethod, |
| 53 | + headers: undefined, |
| 54 | + credentials: 'include', |
| 55 | + }); |
62 | 56 |
|
63 | | - loginWithToken(await result.json(), async (error: Meteor.Error | Meteor.TypedError | Error | null | undefined) => { |
64 | | - if (error) { |
65 | | - setIframeLoginUrl(url); |
66 | | - } else { |
67 | | - setIframeLoginUrl(undefined); |
68 | | - } |
69 | | - callback?.(error, await result.json()); |
70 | | - }); |
71 | | - } catch (error) { |
| 57 | + if (!result.ok || result.status !== 200) { |
72 | 58 | setIframeLoginUrl(url); |
73 | | - callback?.(error instanceof Error ? error : undefined, null); |
| 59 | + callback?.(new Error(), null); |
| 60 | + return; |
74 | 61 | } |
75 | | - }, |
76 | | - [apiMethod, apiUrl, accountIframeUrl, loginWithToken, enabled], |
77 | | - ); |
78 | 62 |
|
79 | | - useEffect(() => { |
80 | | - tryLogin(); |
81 | | - }, [tryLogin]); |
| 63 | + const body = await result.json(); |
| 64 | + loginWithToken(body, async (error: Meteor.Error | Meteor.TypedError | Error | null | undefined) => { |
| 65 | + if (error) { |
| 66 | + setIframeLoginUrl(url); |
| 67 | + } else { |
| 68 | + setIframeLoginUrl(undefined); |
| 69 | + } |
| 70 | + callback?.(error, body); |
| 71 | + }); |
| 72 | + } catch (error) { |
| 73 | + setIframeLoginUrl(url); |
| 74 | + callback?.(error instanceof Error ? error : undefined, null); |
| 75 | + } |
| 76 | + }); |
82 | 77 |
|
83 | 78 | return { |
84 | 79 | enabled, |
|
0 commit comments