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

Commit 8bada41

Browse files
committed
refactor: extract component individually
Signed-off-by: teobler <[email protected]>
1 parent 9d6769d commit 8bada41

File tree

5 files changed

+74
-38
lines changed

5 files changed

+74
-38
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { StyledBird } from "@/SharedButton";
2+
import clickPromptLogo from "*.svg?url";
3+
import React from "react";
4+
5+
type ClickPromptBirdProps = { width?: number; height?: number };
6+
7+
export const ClickPromptBird = ({ width = 38, height = 32 }: ClickPromptBirdProps) => (
8+
<StyledBird src={clickPromptLogo} alt="ClickPrompt Logo" width={width} height={height} />
9+
);

packages/click-prompt-button/src/ClickPromptButton.tsx

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import React, { MouseEventHandler, useState } from "react";
22
import { Box, Button, Text, Tooltip, useDisclosure } from "@chakra-ui/react";
33
import { BeatLoader } from "react-spinners";
4-
import { ClickPromptSmall } from "./CustomIcon";
5-
import clickPromptLogo from "@/assets/clickprompt-light.svg?url";
6-
import { ButtonSize, StyledBird, StyledPromptButton } from "./Button.shared";
7-
import { LoggingDrawer } from "./LoggingDrawer";
4+
import { ClickPromptSmall } from "@/CustomIcon";
5+
import { ButtonSize, StyledPromptButton } from "@/SharedButton";
6+
import { LoggingDrawer } from "@/LoggingDrawer";
7+
import { ClickPromptBird } from "@/ClickPromptBird";
88

99
interface ClickPromptButtonProps {
1010
loading?: boolean;
@@ -22,15 +22,6 @@ interface ClickPromptButtonProps {
2222
logoutApi: () => Promise<any>;
2323
}
2424

25-
export type ClickPromptBirdParams = { width?: number; height?: number };
26-
27-
export function ClickPromptBird(props: ClickPromptBirdParams) {
28-
const width = props.width || 38;
29-
const height = props.height || 32;
30-
31-
return <StyledBird src={clickPromptLogo} alt="ClickPrompt Logo" width={width} height={height} />;
32-
}
33-
3425
export function ClickPromptButton({
3526
isLoggedInApi,
3627
children,
Lines changed: 58 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,56 @@
1-
import React, { MouseEventHandler, useEffect, useState } from "react";
1+
import React, { useEffect, useState } from "react";
22
import { Button, Text, useDisclosure } from "@chakra-ui/react";
33
import { BeatLoader } from "react-spinners";
4-
import { ClickPromptBird } from "./ClickPromptButton";
5-
import { ButtonSize, StyledPromptButton } from "./Button.shared";
6-
import { LoggingDrawer } from "./LoggingDrawer";
4+
import { StyledPromptButton } from "@/SharedButton";
5+
import { LoggingDrawer } from "@/LoggingDrawer";
6+
import { ClickPromptBird } from "@/ClickPromptBird";
77

88
export type ExecButtonProps = {
99
loading?: boolean;
10-
onClick?: MouseEventHandler;
11-
name: string;
1210
text: string;
13-
size?: ButtonSize;
1411
children?: React.ReactNode;
1512
handleResponse?: (response: any) => void;
1613
conversationId?: number;
1714
updateConversationId?: (conversationId: number) => void;
18-
isLoggedIn: () => Promise<any>;
1915
createConversation: (name?: string) => Promise<any>;
2016
sendMessage: (conversageId: number, message: string, name?: string) => Promise<any>;
17+
isLoggedInApi: () => Promise<any>;
18+
changeConversationNameApi: (conversation_id: number, name: string) => Promise<any>;
19+
createConversationApi: (name?: string) => Promise<any>;
20+
getChatsByConversationIdApi: (conversationId: number) => Promise<any>;
21+
deleteConversationApi: (conversationId: number) => Promise<any>;
22+
deleteAllConversationsApi: () => Promise<any>;
23+
sendMsgWithStreamResApi: (conversageId: number, message: string, name?: string) => Promise<any>;
24+
logoutApi: () => Promise<any>;
2125
};
2226

23-
function ExecutePromptButton(props: ExecButtonProps) {
24-
const [isLoading, setIsLoading] = useState(props.loading);
27+
export const ExecutePromptButton = ({
28+
loading,
29+
text,
30+
children,
31+
handleResponse,
32+
conversationId,
33+
updateConversationId,
34+
createConversation,
35+
sendMessage,
36+
isLoggedInApi,
37+
changeConversationNameApi,
38+
createConversationApi,
39+
getChatsByConversationIdApi,
40+
deleteConversationApi,
41+
deleteAllConversationsApi,
42+
sendMsgWithStreamResApi,
43+
logoutApi,
44+
}: ExecButtonProps) => {
45+
const [isLoading, setIsLoading] = useState(loading);
2546
const { isOpen, onOpen, onClose } = useDisclosure();
2647
const [hasLogin, setHasLogin] = useState(false);
2748

2849
const handleClick = async () => {
2950
setIsLoading(true);
3051

3152
try {
32-
const isLoggedIn = await props.isLoggedIn();
53+
const isLoggedIn = await isLoggedInApi();
3354
if (!isLoggedIn) {
3455
onOpen();
3556
setIsLoading(false);
@@ -40,21 +61,21 @@ function ExecutePromptButton(props: ExecButtonProps) {
4061
setHasLogin(false);
4162
}
4263

43-
let conversationId = props.conversationId;
44-
if (!props.conversationId) {
45-
const conversation = await props.createConversation();
64+
let newConversationId = conversationId;
65+
if (!conversationId) {
66+
const conversation = await createConversation();
4667
if (!conversation) {
4768
return;
4869
}
4970

50-
conversationId = conversation.id as number;
51-
props.updateConversationId ? props.updateConversationId(conversationId) : null;
71+
newConversationId = conversation.id as number;
72+
updateConversationId ? updateConversationId(newConversationId) : null;
5273
}
5374

54-
if (conversationId) {
55-
const response: any = await props.sendMessage(conversationId, props.text);
56-
if (response && props.handleResponse) {
57-
props.handleResponse(response as any);
75+
if (newConversationId) {
76+
const response: any = await sendMessage(newConversationId, text);
77+
if (response && handleResponse) {
78+
handleResponse(response as any);
5879
}
5980
}
6081

@@ -83,15 +104,27 @@ function ExecutePromptButton(props: ExecButtonProps) {
83104
<>
84105
<StyledPromptButton>
85106
<Button colorScheme="twitter" className="bg-blue" onClick={handleClick}>
86-
{props.children}
107+
{children}
87108
{!isLoading && <Text>Prompt</Text>}
88109
{isLoading && <BeatLoader size={8} color="black" />}
89110
</Button>
90111
<ClickPromptBird />
91112
</StyledPromptButton>
92-
{!hasLogin && LoggingDrawer(isOpen, handleClose, hasLogin, props, updateLoginStatus)}
113+
{!hasLogin &&
114+
LoggingDrawer({
115+
isOpen,
116+
handleClose,
117+
updateStatus: updateLoginStatus,
118+
isLoggedIn: hasLogin,
119+
initMessage: text,
120+
changeConversationNameApi: changeConversationNameApi,
121+
createConversationApi: createConversationApi,
122+
getChatsByConversationIdApi: getChatsByConversationIdApi,
123+
deleteConversationApi: deleteConversationApi,
124+
deleteAllConversationsApi: deleteAllConversationsApi,
125+
sendMsgWithStreamResApi: sendMsgWithStreamResApi,
126+
logoutApi: logoutApi,
127+
})}
93128
</>
94129
);
95-
}
96-
97-
export default ExecutePromptButton;
130+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
"use client";
2+
3+
export { ClickPromptButton } from "./ClickPromptButton";

0 commit comments

Comments
 (0)