Skip to content

Commit 4ad464c

Browse files
committed
ensure authorized before load community
1 parent 41aabf0 commit 4ad464c

File tree

1 file changed

+13
-19
lines changed

1 file changed

+13
-19
lines changed

enjoy/src/renderer/pages/community.tsx

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
import { useContext, useEffect, useRef } from "react";
1+
import { useContext, useEffect, useRef, useState } from "react";
22
import { AppSettingsProviderContext } from "@renderer/context";
33
import debounce from "lodash/debounce";
44
import { DISCUSS_URL, WEB_API_URL } from "@/constants";
55
import { Button } from "@renderer/components/ui";
66
import { t } from "i18next";
7-
import { LoaderSpin } from "../components";
7+
import { LoaderSpin } from "@renderer/components";
88

99
export default () => {
1010
const containerRef = useRef<HTMLDivElement>(null);
1111
const { EnjoyApp, user, webApi, logout } = useContext(
1212
AppSettingsProviderContext
1313
);
14+
const [authorized, setAuthorized] = useState(false);
1415

1516
const loadCommunity = async () => {
1617
let url = `${DISCUSS_URL}/login`;
1718
let ssoUrl = `${WEB_API_URL}/discourse/sso`;
18-
const accessToken = user?.accessToken;
19-
if (!accessToken) return;
19+
if (!authorized || !user?.accessToken) return;
2020

2121
try {
2222
const { discussUrl, discussSsoUrl } = await webApi.config("discuss");
@@ -34,7 +34,7 @@ export default () => {
3434
containerRef.current.getBoundingClientRect();
3535
EnjoyApp.view.loadCommunity(
3636
{ x, y, width, height },
37-
{ navigatable: false, accessToken, url, ssoUrl }
37+
{ navigatable: false, accessToken: user.accessToken, url, ssoUrl }
3838
);
3939
};
4040

@@ -46,6 +46,7 @@ export default () => {
4646

4747
useEffect(() => {
4848
if (!containerRef.current) return;
49+
if (!authorized) return;
4950

5051
loadCommunity();
5152
const observer = new ResizeObserver(() => {
@@ -56,29 +57,22 @@ export default () => {
5657
return () => {
5758
observer.disconnect();
5859
};
59-
}, []);
60+
}, [authorized, containerRef.current]);
6061

6162
useEffect(() => {
63+
if (!user?.accessToken) return;
64+
65+
webApi.me().then(() => {
66+
setAuthorized(true);
67+
});
68+
6269
return () => {
6370
EnjoyApp.view.remove();
6471
};
6572
}, []);
6673

6774
return (
6875
<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-
)}
8276
<LoaderSpin />
8377
</div>
8478
);

0 commit comments

Comments
 (0)