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

Commit 24594ab

Browse files
committed
refactor: remove all unnecessary any type guard
Signed-off-by: teobler <[email protected]>
1 parent d4455cb commit 24594ab

File tree

7 files changed

+74
-36
lines changed

7 files changed

+74
-36
lines changed

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

+5-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@ 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";
8+
import type { Response, SharedApi } from "@/types/shared";
99

1010
interface ClickPromptButtonProps extends SharedApi {
1111
loading?: boolean;
1212
onClick?: MouseEventHandler;
1313
size?: ButtonSize;
1414
text: string;
1515
children?: React.ReactNode;
16+
loginApi: () => Promise<Response>;
1617
}
1718

1819
export function ClickPromptButton({
@@ -29,12 +30,13 @@ export function ClickPromptButton({
2930
deleteAllConversationsApi,
3031
sendMsgWithStreamResApi,
3132
logoutApi,
33+
loginApi,
3234
}: ClickPromptButtonProps) {
3335
const [isLoading, setIsLoading] = useState(loading);
3436
const [isLoggedIn, setIsLoggedIn] = useState(false);
3537
const { isOpen, onOpen, onClose } = useDisclosure();
3638

37-
const handleClick = async (event: any) => {
39+
const handleClick = async (event: React.MouseEvent) => {
3840
setIsLoading(true);
3941
const isLoggedIn = await isLoggedInApi();
4042
setIsLoggedIn(isLoggedIn);
@@ -90,6 +92,7 @@ export function ClickPromptButton({
9092
deleteAllConversationsApi,
9193
sendMsgWithStreamResApi,
9294
logoutApi,
95+
loginApi,
9396
})}
9497
</Box>
9598
);

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

+7-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +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";
7+
import type { Response, SharedApi } from "@/types/shared";
88

99
interface ExecButtonProps extends SharedApi {
1010
loading?: boolean;
1111
text: string;
1212
children?: React.ReactNode;
13-
handleResponse?: (response: any) => void;
13+
handleResponse?: (response: ReadableStream<Uint8Array> | null) => void;
1414
conversationId?: number;
1515
updateConversationId?: (conversationId: number) => void;
16+
loginApi: () => Promise<Response>;
1617
}
1718

1819
export const ExecutePromptButton = ({
@@ -30,6 +31,7 @@ export const ExecutePromptButton = ({
3031
deleteAllConversationsApi,
3132
sendMsgWithStreamResApi,
3233
logoutApi,
34+
loginApi,
3335
}: ExecButtonProps) => {
3436
const [isLoading, setIsLoading] = useState(loading);
3537
const { isOpen, onOpen, onClose } = useDisclosure();
@@ -62,9 +64,9 @@ export const ExecutePromptButton = ({
6264
}
6365

6466
if (newConversationId) {
65-
const response: any = await sendMsgWithStreamResApi(newConversationId, text);
67+
const response = await sendMsgWithStreamResApi(newConversationId, text);
6668
if (response && handleResponse) {
67-
handleResponse(response as any);
69+
handleResponse(response);
6870
}
6971
}
7072

@@ -113,6 +115,7 @@ export const ExecutePromptButton = ({
113115
deleteAllConversationsApi,
114116
sendMsgWithStreamResApi,
115117
logoutApi,
118+
loginApi,
116119
})}
117120
</>
118121
);

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

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
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";
4+
import type { Response, SharedApi } from "@/types/shared";
55

66
interface LoggingDrawerProps extends Omit<SharedApi, "isLoggedInApi"> {
77
isOpen: boolean;
88
handleClose: () => void;
99
isLoggedIn: boolean;
1010
updateStatus?: (loggedIn: boolean) => void;
1111
initMessage: string;
12+
loginApi: () => Promise<Response>;
1213
}
1314

1415
export function LoggingDrawer({
@@ -24,6 +25,7 @@ export function LoggingDrawer({
2425
deleteAllConversationsApi,
2526
sendMsgWithStreamResApi,
2627
logoutApi,
28+
loginApi,
2729
}: LoggingDrawerProps) {
2830
return (
2931
<Drawer isOpen={isOpen} placement="right" onClose={handleClose} size={"2xl"}>
@@ -43,6 +45,7 @@ export function LoggingDrawer({
4345
deleteAllConversationsApi={deleteAllConversationsApi}
4446
sendMsgWithStreamResApi={sendMsgWithStreamResApi}
4547
logoutApi={logoutApi}
48+
loginApi={loginApi}
4649
/>
4750
</div>
4851
</DrawerBody>

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

+5-8
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
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";
4+
import type { Response, SharedApi } from "@/types/shared";
55

66
interface ChatGPTAppProps extends Omit<SharedApi, "isLoggedInApi"> {
77
loggedIn?: boolean;
88
updateLoginStatus?: (loggedIn: boolean) => void;
99
initMessage?: string;
10-
};
10+
loginApi: () => Promise<Response>;
11+
}
1112
export const ChatGPTApp = ({
1213
loggedIn,
1314
initMessage,
@@ -19,6 +20,7 @@ export const ChatGPTApp = ({
1920
deleteAllConversationsApi,
2021
sendMsgWithStreamResApi,
2122
logoutApi,
23+
loginApi,
2224
}: ChatGPTAppProps) => {
2325
const [isLoggedIn, setIsLoggedIn] = useState(loggedIn ?? false);
2426

@@ -41,11 +43,6 @@ export const ChatGPTApp = ({
4143
logoutApi={logoutApi}
4244
/>
4345
) : (
44-
<LoginPage
45-
setIsLoggedIn={setIsLoggedIn}
46-
loginApi={function (key: string): Promise<any> {
47-
throw new Error("Function not implemented.");
48-
}}
49-
/>
46+
<LoginPage setIsLoggedIn={setIsLoggedIn} loginApi={loginApi} />
5047
);
5148
};

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

+12-12
Original file line numberDiff line numberDiff line change
@@ -10,7 +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";
13+
import type { Chat, Conversation, SharedApi } from "@/types/shared";
1414

1515
const ChatInput = styled("input")`
1616
background: #ffffff;
@@ -93,10 +93,10 @@ export const ChatRoom = ({
9393
}: ChatRoomProps) => {
9494
const chatsWrapper = React.useRef<HTMLDivElement>(null);
9595
const [disable, setDisable] = React.useState(false);
96-
const [chatHistory, setChatHistory] = React.useState<any>([]);
96+
const [chatHistory, setChatHistory] = React.useState<Chat[]>([]);
9797
const [message, setMessage] = React.useState(initMessage ?? "");
9898

99-
const [conversations, setConversations] = useState<any>([]);
99+
const [conversations, setConversations] = useState<Conversation[]>([]);
100100
const [currentConversation, setCurrentConversation] = useState<number | null>(null);
101101
// editing conversation name
102102
const [editing, setEditing] = useState<number | null>(null);
@@ -110,11 +110,11 @@ export const ChatRoom = ({
110110
method: "POST",
111111
body: JSON.stringify({
112112
action: "get_conversations",
113-
} as any),
113+
}),
114114
});
115-
const data = (await response.json()) as any;
115+
const data = await response.json();
116116
if (!response.ok) {
117-
alert("Error: " + JSON.stringify((data as any).error));
117+
alert("Error: " + JSON.stringify(data.error));
118118
return;
119119
}
120120
setConversations(data);
@@ -155,7 +155,7 @@ export const ChatRoom = ({
155155
async function changeConversationName(conversationId: number, name: string) {
156156
await changeConversationNameApi(conversationId, name);
157157

158-
setConversations((c: any[]) =>
158+
setConversations((c) =>
159159
c.map((conversation) => {
160160
if (conversation.id === conversationId) {
161161
return {
@@ -175,7 +175,7 @@ export const ChatRoom = ({
175175
if (conversationId == null) {
176176
return;
177177
}
178-
setEditingName(conversations.find((c: any) => c.id === conversationId)?.name ?? "");
178+
setEditingName(conversations.find((c) => c.id === conversationId)?.name ?? "");
179179
setEditing(conversationId);
180180
return;
181181
}
@@ -208,7 +208,7 @@ export const ChatRoom = ({
208208
if (!data) {
209209
return;
210210
}
211-
setConversations(conversations.filter((conversation: any) => conversation.id !== conversationId));
211+
setConversations(conversations.filter((conversation) => conversation.id !== conversationId));
212212
}
213213

214214
async function deleteAllConversations() {
@@ -242,7 +242,7 @@ export const ChatRoom = ({
242242
// TODO(CGQAQ): custom name of user
243243
// name: "User",
244244
},
245-
] as any;
245+
] as Chat[];
246246

247247
setChatHistory([...updatedHistory]);
248248

@@ -312,7 +312,7 @@ export const ChatRoom = ({
312312
New chat
313313
</div>
314314
<div className="overflow-y-auto overflow-container">
315-
{conversations.map((conversation: any) => (
315+
{conversations.map((conversation) => (
316316
<div
317317
key={conversation.id}
318318
className={`${
@@ -399,7 +399,7 @@ export const ChatRoom = ({
399399
ref={chatsWrapper}
400400
className="flex flex-col gap-4 w-full px-4 max-h-[80%] overflow-y-auto mt-11 scroll-smooth"
401401
>
402-
{chatHistory.map((chat: any, index: number) => {
402+
{chatHistory.map((chat, index) => {
403403
return (
404404
<div key={index} className="flex flex-col gap-14 ">
405405
{chat.role === "user" ? (

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

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import React, { Dispatch, SetStateAction } from "react";
22
import { Button, Input } from "@chakra-ui/react";
3+
import type { Response } from "@/types/shared";
34

45
export const LoginPage = ({
56
setIsLoggedIn,
67
loginApi,
78
}: {
89
setIsLoggedIn: Dispatch<SetStateAction<boolean>>;
9-
loginApi: (key: string) => Promise<any>;
10+
loginApi: (key: string) => Promise<Response>;
1011
}) => {
1112
const [openAiKey, setOpenAiKey] = React.useState("");
1213

Original file line numberDiff line numberDiff line change
@@ -1,10 +1,41 @@
11
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>;
2+
isLoggedInApi: () => Promise<boolean>;
3+
changeConversationNameApi: (conversationId: number, name: string) => Promise<void>;
4+
createConversationApi: (name?: string) => Promise<Conversation>;
5+
getChatsByConversationIdApi: (conversationId: number) => Promise<Chat[]>;
6+
deleteConversationApi: (conversationId: number) => Promise<Conversation>;
7+
deleteAllConversationsApi: () => Promise<Response>;
8+
sendMsgWithStreamResApi: (
9+
conversationId: number,
10+
message: string,
11+
name?: string
12+
) => Promise<ReadableStream<Uint8Array> | null>;
13+
logoutApi: () => Promise<Response>;
1014
}
15+
16+
export interface Conversation {
17+
id: number;
18+
user_id: number;
19+
name: string;
20+
deleted?: NumBool;
21+
created_at?: string;
22+
}
23+
24+
enum NumBool {
25+
True = 1,
26+
False = 0,
27+
}
28+
29+
export interface Chat {
30+
id?: number;
31+
conversation_id: number;
32+
role: string; // line 14
33+
content: string;
34+
name?: string;
35+
created_at?: string;
36+
}
37+
38+
export type Response = {
39+
message?: string;
40+
error?: string;
41+
};

0 commit comments

Comments
 (0)