Skip to content
This repository was archived by the owner on Apr 18, 2024. It is now read-only.

Commit dd2cf52

Browse files
committed
fix: fix login drawer
1 parent 570fa82 commit dd2cf52

File tree

3 files changed

+47
-10
lines changed

3 files changed

+47
-10
lines changed

src/components/ChatGPTApp.tsx

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,22 @@
22

33
import { ChatRoom } from "@/app/[lang]/chatgpt/ChatRoom";
44
import { LoginPage } from "@/app/[lang]/chatgpt/LoginPage";
5-
import React, { useState } from "react";
5+
import React, { useEffect, useState } from "react";
66

7-
export const ChatGPTApp = ({ loggedIn, initMessage }: { loggedIn?: boolean; initMessage?: string }) => {
7+
type ChatGPTAppProps = {
8+
loggedIn?: boolean;
9+
updateLoginStatus?: (loggedIn: boolean) => void;
10+
initMessage?: string;
11+
};
12+
export const ChatGPTApp = ({ loggedIn, initMessage, updateLoginStatus }: ChatGPTAppProps) => {
813
const [isLoggedIn, setIsLoggedIn] = useState(loggedIn ?? false);
914

15+
useEffect(() => {
16+
if (updateLoginStatus) {
17+
updateLoginStatus(isLoggedIn);
18+
}
19+
}, [isLoggedIn]);
20+
1021
return isLoggedIn ? (
1122
<ChatRoom setIsLoggedIn={setIsLoggedIn} initMessage={initMessage} />
1223
) : (

src/components/ClickPrompt/ExecutePromptButton.tsx

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
"use client";
22

3-
import React, { MouseEventHandler, useState } from "react";
3+
import React, { MouseEventHandler, useEffect, useState } from "react";
44
import { Text, useDisclosure } from "@chakra-ui/react";
55
import * as UserAPI from "@/api/user";
66
import { ResponseCreateConversation } from "@/pages/api/chatgpt/conversation";
@@ -31,17 +31,24 @@ function ExecutePromptButton(props: ExecButtonProps) {
3131
const [hasLogin, setHasLogin] = useState(false);
3232

3333
const handleClick = async () => {
34+
setIsLoading(true);
35+
3436
try {
35-
await UserAPI.isLoggedIn();
37+
const response = await UserAPI.isLoggedIn();
38+
if (!response.loggedIn) {
39+
onOpen();
40+
setIsLoading(false);
41+
return;
42+
}
43+
3644
setHasLogin(true);
3745
} catch (e) {
38-
onOpen();
46+
console.log(e);
3947
setHasLogin(false);
4048
}
4149

4250
let conversationId = props.conversationId;
4351
if (!props.conversationId) {
44-
setIsLoading(true);
4552
const conversation: ResponseCreateConversation = await createConversation();
4653
if (!conversation) {
4754
return;
@@ -58,14 +65,27 @@ function ExecutePromptButton(props: ExecButtonProps) {
5865
}
5966
}
6067

61-
onClose();
6268
setIsLoading(false);
6369
};
6470

71+
useEffect(() => {
72+
console.log(`hasLogin: ${hasLogin}`);
73+
if (hasLogin) {
74+
onClose();
75+
}
76+
}, [hasLogin]);
77+
6578
const handleClose = () => {
6679
onClose();
6780
};
6881

82+
const updateLoginStatus = (status: boolean) => {
83+
if (status) {
84+
setHasLogin(true);
85+
onClose();
86+
}
87+
};
88+
6989
return (
7090
<>
7191
<StyledPromptButton>
@@ -76,7 +96,7 @@ function ExecutePromptButton(props: ExecButtonProps) {
7696
</Button>
7797
<ClickPromptBird />
7898
</StyledPromptButton>
79-
{!hasLogin && LoggingDrawer(isOpen, handleClose, hasLogin, props)}
99+
{!hasLogin && LoggingDrawer(isOpen, handleClose, hasLogin, props, updateLoginStatus)}
80100
</>
81101
);
82102
}

src/components/ClickPrompt/LoggingDrawer.tsx

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,21 @@ import { ChatGPTApp } from "@/components/ChatGPTApp";
55
import React from "react";
66
import { CPButtonProps } from "@/components/ClickPrompt/Button.shared";
77

8-
export function LoggingDrawer(isOpen: boolean, handleClose: () => void, isLoggedIn: boolean, props: CPButtonProps) {
8+
export function LoggingDrawer(
9+
isOpen: boolean,
10+
handleClose: () => void,
11+
isLoggedIn: boolean,
12+
props: CPButtonProps,
13+
updateStatus?: (loggedIn: boolean) => void,
14+
) {
915
return (
1016
<Drawer isOpen={isOpen} placement='right' onClose={handleClose} size={"2xl"}>
1117
<DrawerOverlay />
1218
<DrawerContent>
1319
<DrawerCloseButton className='text-white z-50' />
1420
<DrawerBody padding={0}>
1521
<div className='bg-[#343541] flex flex-1 h-[100%] overflow-y-auto items-center justify-center'>
16-
<ChatGPTApp loggedIn={isLoggedIn} initMessage={props.text} />
22+
<ChatGPTApp loggedIn={isLoggedIn} initMessage={props.text} updateLoginStatus={updateStatus} />
1723
</div>
1824
</DrawerBody>
1925
</DrawerContent>

0 commit comments

Comments
 (0)