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

Commit 4a87711

Browse files
committed
Merge remote-tracking branch 'origin/dev' into dev
2 parents 7de2315 + 67fb2ca commit 4a87711

File tree

5 files changed

+70
-28
lines changed

5 files changed

+70
-28
lines changed

src/app/[lang]/chatgpt-startling-by-each-step/[id]/StartlingStepDetail.tsx

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

33
import React, { useEffect } from "react";
4+
import { Textarea } from "@chakra-ui/react";
5+
46
import { HumanBlock } from "@/app/[lang]/chatgpt-samples/components/HumanBlock";
57
import { Avatar, Box } from "@/components/ChakraUI";
68
import SimpleMarkdown from "@/components/SimpleMarkdown";
79
import { AiBlock } from "@/app/[lang]/chatgpt-samples/components/AiBlock";
810
import { ChatGptIcon } from "@/components/CustomIcon";
911
import { StartlingStep } from "@/app/[lang]/chatgpt-startling-by-each-step/[id]/startling.type";
10-
import { Textarea } from "@chakra-ui/react";
1112
import { fillStepWithValued, StepDetail } from "@/app/[lang]/chatgpt-startling-by-each-step/[id]/StepDetail";
1213
import { ResponseSend } from "@/pages/api/chatgpt/chat";
13-
import { ExecutePromptButton } from "@/components/ClickPrompt/ExecutePromptButton";
14+
import ExecutePromptButton from "@/components/ClickPrompt/ExecutePromptButton";
1415

1516
type StepProps = {
1617
index: number;
@@ -90,11 +91,11 @@ function StartlingStepDetail({
9091
</Box>
9192
</HumanBlock>
9293
<ExecutePromptButton
93-
conversationId={conversationId}
94-
updateConversationId={updateConversationId}
9594
text={ask}
96-
handleResponse={handleResponse}
9795
name={content.name}
96+
handleResponse={handleResponse}
97+
conversationId={conversationId}
98+
updateConversationId={updateConversationId}
9899
/>
99100
<AiBlock direction='row' gap='2'>
100101
<Box>

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
) : (
Lines changed: 41 additions & 18 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";
@@ -20,62 +20,85 @@ export type ExecButtonProps = {
2020
text: string;
2121
size?: ButtonSize;
2222
children?: React.ReactNode;
23-
handleResponse?: any;
23+
handleResponse?: (response: ResponseSend) => void;
2424
conversationId?: number;
2525
updateConversationId?: (conversationId: number) => void;
2626
};
2727

28-
export function ExecutePromptButton(props: ExecButtonProps) {
28+
function ExecutePromptButton(props: ExecButtonProps) {
2929
const [isLoading, setIsLoading] = useState(props.loading);
3030
const { isOpen, onOpen, onClose } = useDisclosure();
3131
const [hasLogin, setHasLogin] = useState(false);
32-
const [localId, setLocalId] = useState(props.conversationId);
3332

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

42-
if (!localId) {
43-
setIsLoading(true);
44-
let conversation: ResponseCreateConversation = await createConversation();
50+
let conversationId = props.conversationId;
51+
if (!props.conversationId) {
52+
const conversation: ResponseCreateConversation = await createConversation();
4553
if (!conversation) {
4654
return;
4755
}
4856

49-
let conversationId = conversation.id || 0;
50-
setLocalId(conversationId);
57+
conversationId = conversation.id as number;
5158
props.updateConversationId ? props.updateConversationId(conversationId) : null;
5259
}
5360

54-
let response: any = await sendMessage(localId!!, props.text);
55-
console.log(response);
56-
if (!response) {
57-
props.handleResponse ? props.handleResponse(response as ResponseSend) : null;
61+
if (conversationId) {
62+
const response: any = await sendMessage(conversationId, props.text);
63+
if (response && props.handleResponse) {
64+
props.handleResponse(response as ResponseSend);
65+
}
5866
}
5967

60-
onClose();
6168
setIsLoading(false);
6269
};
6370

71+
useEffect(() => {
72+
console.log(`hasLogin: ${hasLogin}`);
73+
if (hasLogin) {
74+
onClose();
75+
}
76+
}, [hasLogin]);
77+
6478
const handleClose = () => {
6579
onClose();
6680
};
6781

82+
const updateLoginStatus = (status: boolean) => {
83+
if (status) {
84+
setHasLogin(true);
85+
onClose();
86+
}
87+
};
88+
6889
return (
6990
<>
7091
<StyledPromptButton>
71-
<Button colorScheme='twitter' className='bg-blue' onClick={handleClick} {...props}>
92+
<Button colorScheme='twitter' className='bg-blue' onClick={handleClick}>
7293
{props.children}
7394
{!isLoading && <Text>Prompt</Text>}
7495
{isLoading && <BeatLoader size={8} color='black' />}
7596
</Button>
7697
<ClickPromptBird />
7798
</StyledPromptButton>
78-
{!hasLogin && LoggingDrawer(isOpen, handleClose, hasLogin, props)}
99+
{!hasLogin && LoggingDrawer(isOpen, handleClose, hasLogin, props, updateLoginStatus)}
79100
</>
80101
);
81102
}
103+
104+
export default ExecutePromptButton;

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>

tsconfig.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,8 @@
3939
"next-env.d.ts",
4040
"**/*.ts",
4141
"**/*.tsx",
42-
".next/types/**/*.ts"
42+
".next/types/**/*.ts",
43+
"/Volumes/source/ai/prompt-cheatsheet/.next/types/**/*.ts"
4344
],
4445
"exclude": [
4546
"node_modules"

0 commit comments

Comments
 (0)