diff --git a/client/ui/App.tsx b/client/ui/App.tsx index 340b11a..c83233b 100644 --- a/client/ui/App.tsx +++ b/client/ui/App.tsx @@ -15,8 +15,9 @@ import { checkApiSuccessOrSncakbar } from "./snackbar.ts" import RegisterDialog from "./dialog/RegisterDialog.tsx" import LoginDialog from "./dialog/LoginDialog.tsx" import UserProfileDialog from "./dialog/UserProfileDialog.tsx" -import ContactsList from "./main/ContactsList.tsx"; -import RecentsList from "./main/RecentsList.tsx"; +import ContactsList from "./main/ContactsList.tsx" +import RecentsList from "./main/RecentsList.tsx" +import useAsyncEffect from "./useAsyncEffect.ts" declare global { namespace React { @@ -56,7 +57,7 @@ export default function App() { nickname: "Maya Fey", }, ] as User[]) - + const [navigationItemSelected, setNavigationItemSelected] = React.useState('Recents') const navigationRailRef = React.useRef(null) @@ -85,29 +86,27 @@ export default function App() { const [currentChatId, setCurrentChatId] = React.useState('') - React.useEffect(() => { - ; (async () => { - const split = Split(['#SideBar', '#ChatFragment'], { - sizes: data.split_sizes ? data.split_sizes : [25, 75], - minSize: [200, 400], - gutterSize: 2, - onDragEnd: function () { - data.split_sizes = split.getSizes() - data.apply() - } - }) - - Client.connect() - const re = await Client.auth(data.access_token || "") - if (re.code == 401) - loginDialogRef.current!.open = true - else if (re.code != 200) { - if (checkApiSuccessOrSncakbar(re, "驗證失敗")) return - } else if (re.code == 200) { - setMyUserProfileCache(Client.myUserProfile as User) + useAsyncEffect(async () => { + const split = Split(['#SideBar', '#ChatFragment'], { + sizes: data.split_sizes ? data.split_sizes : [25, 75], + minSize: [200, 400], + gutterSize: 2, + onDragEnd: function () { + data.split_sizes = split.getSizes() + data.apply() } - })() - }, []) + }) + + Client.connect() + const re = await Client.auth(data.access_token || "") + if (re.code == 401) + loginDialogRef.current!.open = true + else if (re.code != 200) { + if (checkApiSuccessOrSncakbar(re, "驗證失敗")) return + } else if (re.code == 200) { + setMyUserProfileCache(Client.myUserProfile as User) + } + }) return (
{ setIsShowChatFragment(true) }} - display={navigationItemSelected == "Contacts"} - contactsList={contactsList} - setContactsList={setContactsList} /> + display={navigationItemSelected == "Contacts"} /> }
{ diff --git a/client/ui/AppMobile.tsx b/client/ui/AppMobile.tsx index fd0a66e..7ff4834 100644 --- a/client/ui/AppMobile.tsx +++ b/client/ui/AppMobile.tsx @@ -14,6 +14,7 @@ import LoginDialog from "./dialog/LoginDialog.tsx" import UserProfileDialog from "./dialog/UserProfileDialog.tsx" import ContactsList from "./main/ContactsList.tsx" import RecentsList from "./main/RecentsList.tsx" +import useAsyncEffect from "./useAsyncEffect.ts" declare global { namespace React { @@ -79,19 +80,17 @@ export default function AppMobile() { const [myUserProfileCache, setMyUserProfileCache]: [User, React.Dispatch>] = React.useState(null as unknown as User) - React.useEffect(() => { - ; (async () => { - Client.connect() - const re = await Client.auth(data.access_token || "") - if (re.code == 401) - loginDialogRef.current!.open = true - else if (re.code != 200) { - if (checkApiSuccessOrSncakbar(re, "驗證失敗")) return - } else if (re.code == 200) { - setMyUserProfileCache(Client.myUserProfile as User) - } - })() - }, []) + useAsyncEffect(async () => { + Client.connect() + const re = await Client.auth(data.access_token || "") + if (re.code == 401) + loginDialogRef.current!.open = true + else if (re.code != 200) { + if (checkApiSuccessOrSncakbar(re, "驗證失敗")) return + } else if (re.code == 200) { + setMyUserProfileCache(Client.myUserProfile as User) + } + }) return (
{ target: string, @@ -26,16 +27,14 @@ export default function ChatFragment({ target, ...props }: Args) { setTabItemSelected(tabRef.current?.value || "Chat") }) - React.useEffect(() => { - ;(async () => { - const re = await Client.invoke('Chat.getInfo', { - token: data.access_token, - target: target, - }) - if (re.code != 200) - return checkApiSuccessOrSncakbar(re, "對話錯誤") - setChatInfo(re.data as Chat) - })() + useAsyncEffect(async () => { + const re = await Client.invoke('Chat.getInfo', { + token: data.access_token, + target: target, + }) + if (re.code != 200) + return checkApiSuccessOrSncakbar(re, "對話錯誤") + setChatInfo(re.data as Chat) }, [target]) console.log(tabItemSelected) diff --git a/client/ui/useAsyncEffect.ts b/client/ui/useAsyncEffect.ts new file mode 100644 index 0000000..3f0f0d0 --- /dev/null +++ b/client/ui/useAsyncEffect.ts @@ -0,0 +1,5 @@ +export default function useAsyncEffect(func: Function, deps?: React.DependencyList) { + React.useEffect(() => { + ;(async () => await func()) + }, deps) +}