1
- import { useContext , useEffect , useRef } from "react" ;
1
+ import { useContext , useEffect , useRef , useState } from "react" ;
2
2
import { AppSettingsProviderContext } from "@renderer/context" ;
3
3
import debounce from "lodash/debounce" ;
4
4
import { DISCUSS_URL , WEB_API_URL } from "@/constants" ;
5
5
import { Button } from "@renderer/components/ui" ;
6
6
import { t } from "i18next" ;
7
- import { LoaderSpin } from ".. /components" ;
7
+ import { LoaderSpin } from "@renderer /components" ;
8
8
9
9
export default ( ) => {
10
10
const containerRef = useRef < HTMLDivElement > ( null ) ;
11
11
const { EnjoyApp, user, webApi, logout } = useContext (
12
12
AppSettingsProviderContext
13
13
) ;
14
+ const [ authorized , setAuthorized ] = useState ( false ) ;
14
15
15
16
const loadCommunity = async ( ) => {
16
17
let url = `${ DISCUSS_URL } /login` ;
17
18
let ssoUrl = `${ WEB_API_URL } /discourse/sso` ;
18
- const accessToken = user ?. accessToken ;
19
- if ( ! accessToken ) return ;
19
+ if ( ! authorized || ! user ?. accessToken ) return ;
20
20
21
21
try {
22
22
const { discussUrl, discussSsoUrl } = await webApi . config ( "discuss" ) ;
@@ -34,7 +34,7 @@ export default () => {
34
34
containerRef . current . getBoundingClientRect ( ) ;
35
35
EnjoyApp . view . loadCommunity (
36
36
{ x, y, width, height } ,
37
- { navigatable : false , accessToken, url, ssoUrl }
37
+ { navigatable : false , accessToken : user . accessToken , url, ssoUrl }
38
38
) ;
39
39
} ;
40
40
@@ -46,6 +46,7 @@ export default () => {
46
46
47
47
useEffect ( ( ) => {
48
48
if ( ! containerRef . current ) return ;
49
+ if ( ! authorized ) return ;
49
50
50
51
loadCommunity ( ) ;
51
52
const observer = new ResizeObserver ( ( ) => {
@@ -56,29 +57,22 @@ export default () => {
56
57
return ( ) => {
57
58
observer . disconnect ( ) ;
58
59
} ;
59
- } , [ ] ) ;
60
+ } , [ authorized , containerRef . current ] ) ;
60
61
61
62
useEffect ( ( ) => {
63
+ if ( ! user ?. accessToken ) return ;
64
+
65
+ webApi . me ( ) . then ( ( ) => {
66
+ setAuthorized ( true ) ;
67
+ } ) ;
68
+
62
69
return ( ) => {
63
70
EnjoyApp . view . remove ( ) ;
64
71
} ;
65
72
} , [ ] ) ;
66
73
67
74
return (
68
75
< div ref = { containerRef } className = "w-full h-full" >
69
- { ! user ?. accessToken && (
70
- < div className = "bg-destructive text-white py-2 px-4 h-10 flex items-center sticky top-0 z-10" >
71
- < span className = "text-sm" > { t ( "authorizationExpired" ) } </ span >
72
- < Button
73
- variant = "outline"
74
- size = "sm"
75
- className = "ml-2 py-1 px-2 text-xs h-auto w-auto"
76
- onClick = { logout }
77
- >
78
- { t ( "reLogin" ) }
79
- </ Button >
80
- </ div >
81
- ) }
82
76
< LoaderSpin />
83
77
</ div >
84
78
) ;
0 commit comments