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

Commit ab3d8ea

Browse files
committed
refactor: extract shared api params
Signed-off-by: teobler <[email protected]>
1 parent 8bada41 commit ab3d8ea

File tree

6 files changed

+35
-59
lines changed

6 files changed

+35
-59
lines changed

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

+2-9
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,14 @@ import { ClickPromptSmall } from "@/CustomIcon";
55
import { ButtonSize, StyledPromptButton } from "@/SharedButton";
66
import { LoggingDrawer } from "@/LoggingDrawer";
77
import { ClickPromptBird } from "@/ClickPromptBird";
8+
import { SharedApi } from "@/types/shared";
89

9-
interface ClickPromptButtonProps {
10+
interface ClickPromptButtonProps extends SharedApi {
1011
loading?: boolean;
1112
onClick?: MouseEventHandler;
1213
size?: ButtonSize;
1314
text: string;
1415
children?: React.ReactNode;
15-
isLoggedInApi: () => Promise<any>;
16-
changeConversationNameApi: (conversation_id: number, name: string) => Promise<any>;
17-
createConversationApi: (name?: string) => Promise<any>;
18-
getChatsByConversationIdApi: (conversationId: number) => Promise<any>;
19-
deleteConversationApi: (conversationId: number) => Promise<any>;
20-
deleteAllConversationsApi: () => Promise<any>;
21-
sendMsgWithStreamResApi: (conversageId: number, message: string, name?: string) => Promise<any>;
22-
logoutApi: () => Promise<any>;
2316
}
2417

2518
export function ClickPromptButton({

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

+12-23
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,16 @@ import { BeatLoader } from "react-spinners";
44
import { StyledPromptButton } from "@/SharedButton";
55
import { LoggingDrawer } from "@/LoggingDrawer";
66
import { ClickPromptBird } from "@/ClickPromptBird";
7+
import { SharedApi } from "@/types/shared";
78

8-
export type ExecButtonProps = {
9+
interface ExecButtonProps extends SharedApi {
910
loading?: boolean;
1011
text: string;
1112
children?: React.ReactNode;
1213
handleResponse?: (response: any) => void;
1314
conversationId?: number;
1415
updateConversationId?: (conversationId: number) => void;
15-
createConversation: (name?: string) => Promise<any>;
16-
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>;
25-
};
16+
}
2617

2718
export const ExecutePromptButton = ({
2819
loading,
@@ -31,8 +22,6 @@ export const ExecutePromptButton = ({
3122
handleResponse,
3223
conversationId,
3324
updateConversationId,
34-
createConversation,
35-
sendMessage,
3625
isLoggedInApi,
3726
changeConversationNameApi,
3827
createConversationApi,
@@ -63,7 +52,7 @@ export const ExecutePromptButton = ({
6352

6453
let newConversationId = conversationId;
6554
if (!conversationId) {
66-
const conversation = await createConversation();
55+
const conversation = await createConversationApi();
6756
if (!conversation) {
6857
return;
6958
}
@@ -73,7 +62,7 @@ export const ExecutePromptButton = ({
7362
}
7463

7564
if (newConversationId) {
76-
const response: any = await sendMessage(newConversationId, text);
65+
const response: any = await sendMsgWithStreamResApi(newConversationId, text);
7766
if (response && handleResponse) {
7867
handleResponse(response as any);
7968
}
@@ -117,13 +106,13 @@ export const ExecutePromptButton = ({
117106
updateStatus: updateLoginStatus,
118107
isLoggedIn: hasLogin,
119108
initMessage: text,
120-
changeConversationNameApi: changeConversationNameApi,
121-
createConversationApi: createConversationApi,
122-
getChatsByConversationIdApi: getChatsByConversationIdApi,
123-
deleteConversationApi: deleteConversationApi,
124-
deleteAllConversationsApi: deleteAllConversationsApi,
125-
sendMsgWithStreamResApi: sendMsgWithStreamResApi,
126-
logoutApi: logoutApi,
109+
changeConversationNameApi,
110+
createConversationApi,
111+
getChatsByConversationIdApi,
112+
deleteConversationApi,
113+
deleteAllConversationsApi,
114+
sendMsgWithStreamResApi,
115+
logoutApi,
127116
})}
128117
</>
129118
);

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

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
import { Drawer, DrawerBody, DrawerCloseButton, DrawerContent, DrawerOverlay } from "@chakra-ui/react";
22
import { ChatGPTApp } from "@/chatgpt/ChatGPTApp";
33
import React from "react";
4+
import { SharedApi } from "@/types/shared";
45

5-
interface LoggingDrawerProps {
6+
interface LoggingDrawerProps extends Omit<SharedApi, "isLoggedInApi"> {
67
isOpen: boolean;
78
handleClose: () => void;
89
isLoggedIn: boolean;
910
updateStatus?: (loggedIn: boolean) => void;
1011
initMessage: string;
11-
changeConversationNameApi: (conversation_id: number, name: string) => Promise<any>;
12-
createConversationApi: (name?: string) => Promise<any>;
13-
getChatsByConversationIdApi: (conversationId: number) => Promise<any>;
14-
deleteConversationApi: (conversationId: number) => Promise<any>;
15-
deleteAllConversationsApi: () => Promise<any>;
16-
sendMsgWithStreamResApi: (conversageId: number, message: string, name?: string) => Promise<any>;
17-
logoutApi: () => Promise<any>;
1812
}
1913

2014
export function LoggingDrawer({

packages/click-prompt-button/src/chatgpt/ChatGPTApp.tsx

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,12 @@
11
import { ChatRoom } from "@/chatgpt/ChatRoom";
22
import { LoginPage } from "@/chatgpt/LoginPage";
33
import React, { useEffect, useState } from "react";
4+
import { SharedApi } from "@/types/shared";
45

5-
type ChatGPTAppProps = {
6+
interface ChatGPTAppProps extends Omit<SharedApi, "isLoggedInApi"> {
67
loggedIn?: boolean;
78
updateLoginStatus?: (loggedIn: boolean) => void;
89
initMessage?: string;
9-
changeConversationNameApi: (conversation_id: number, name: string) => Promise<any>;
10-
createConversationApi: (name?: string) => Promise<any>;
11-
getChatsByConversationIdApi: (conversationId: number) => Promise<any>;
12-
deleteConversationApi: (conversationId: number) => Promise<any>;
13-
deleteAllConversationsApi: () => Promise<any>;
14-
sendMsgWithStreamResApi: (conversageId: number, message: string, name?: string) => Promise<any>;
15-
logoutApi: () => Promise<any>;
1610
};
1711
export const ChatGPTApp = ({
1812
loggedIn,

packages/click-prompt-button/src/chatgpt/ChatRoom.tsx

+7-11
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { BeatLoader } from "react-spinners";
1010
import { useDebouncedCallback } from "use-debounce";
1111
import { Input } from "@chakra-ui/react";
1212
import SimpleMarkdown from "@/markdown/SimpleMarkdown";
13+
import { SharedApi } from "@/types/shared";
1314

1415
const ChatInput = styled("input")`
1516
background: #ffffff;
@@ -74,6 +75,11 @@ const ChatSendButton = styled("button")`
7475
outline: none;
7576
`;
7677

78+
interface ChatRoomProps extends Omit<SharedApi, "isLoggedInApi"> {
79+
setIsLoggedIn: Dispatch<SetStateAction<boolean>>;
80+
initMessage?: string;
81+
}
82+
7783
export const ChatRoom = ({
7884
setIsLoggedIn,
7985
initMessage,
@@ -84,17 +90,7 @@ export const ChatRoom = ({
8490
deleteAllConversationsApi,
8591
sendMsgWithStreamResApi,
8692
logoutApi,
87-
}: {
88-
setIsLoggedIn: Dispatch<SetStateAction<boolean>>;
89-
initMessage?: string;
90-
changeConversationNameApi: (conversation_id: number, name: string) => Promise<any>;
91-
createConversationApi: (name?: string) => Promise<any>;
92-
getChatsByConversationIdApi: (conversationId: number) => Promise<any>;
93-
deleteConversationApi: (conversationId: number) => Promise<any>;
94-
deleteAllConversationsApi: () => Promise<any>;
95-
sendMsgWithStreamResApi: (conversageId: number, message: string, name?: string) => Promise<any>;
96-
logoutApi: () => Promise<any>;
97-
}) => {
93+
}: ChatRoomProps) => {
9894
const chatsWrapper = React.useRef<HTMLDivElement>(null);
9995
const [disable, setDisable] = React.useState(false);
10096
const [chatHistory, setChatHistory] = React.useState<any>([]);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
export interface SharedApi {
2+
isLoggedInApi: () => Promise<any>;
3+
changeConversationNameApi: (conversation_id: number, name: string) => Promise<any>;
4+
createConversationApi: (name?: string) => Promise<any>;
5+
getChatsByConversationIdApi: (conversationId: number) => Promise<any>;
6+
deleteConversationApi: (conversationId: number) => Promise<any>;
7+
deleteAllConversationsApi: () => Promise<any>;
8+
sendMsgWithStreamResApi: (conversageId: number, message: string, name?: string) => Promise<any>;
9+
logoutApi: () => Promise<any>;
10+
}

0 commit comments

Comments
 (0)