/
ChatListBase.tsx
129 lines (120 loc) · 3.55 KB
/
ChatListBase.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import "simplebar-react/dist/simplebar.min.css";
import { Box, CardProps, Flex } from "@chakra-ui/react";
import { Plus } from "lucide-react";
import { useTranslation } from "next-i18next";
import { memo, useCallback, useState } from "react";
import SimpleBar from "simplebar-react";
import { ChatListItem } from "./ChatListItem";
import { ChatViewSelection } from "./ChatViewSelection";
import { CreateChatButton } from "./CreateChatButton";
import { InferencePoweredBy } from "./InferencePoweredBy";
import { ChatListViewSelection, useListChatPagination } from "./useListChatPagination";
export const ChatListBase = memo(function ChatListBase({
allowViews,
noScrollbar,
...props
}: CardProps & { allowViews?: boolean; noScrollbar?: boolean }) {
const [view, setView] = useState<ChatListViewSelection>("visible");
const { loadMoreRef, responses, mutateChatResponses } = useListChatPagination(view);
const chats = responses?.flatMap((response) => response.chats) || [];
const { t } = useTranslation(["common", "chat"]);
const handleUpdateTitle = useCallback(
({ chatId, title }: { chatId: string; title: string }) => {
mutateChatResponses(
(chatResponses) => [
...(chatResponses?.map((chatResponse) => ({
...chatResponse,
chats: chatResponse.chats.map((chat) => {
if (chat.id === chatId) {
return {
...chat,
title,
};
}
return chat;
}),
})) || []),
],
false
);
},
[mutateChatResponses]
);
const removeItemFromList = useCallback(
({ chatId }: { chatId: string }) => {
mutateChatResponses(
(chatResponses) => [
...(chatResponses?.map((chatResponse) => ({
...chatResponse,
chats: chatResponse.chats.filter((chat) => {
return chat.id !== chatId;
}),
})) || []),
],
false
);
},
[mutateChatResponses]
);
const handleCreateChat = useCallback(() => {
mutateChatResponses();
}, [mutateChatResponses]);
const content = (
<>
{chats.map((chat) => (
<ChatListItem
key={chat.id}
chat={chat}
onUpdateTitle={handleUpdateTitle}
onHide={removeItemFromList}
onDelete={removeItemFromList}
/>
))}
<div ref={loadMoreRef} />
</>
);
return (
<Box
gap="1"
height="full"
minH="0"
display="flex"
flexDirection="column"
bg="whiteAlpha.400"
_dark={{
bg: "blackAlpha.400",
}}
{...props}
>
<Flex flexDirection={["column", "row"]} alignItems="stretch" p="2" gap="3">
<CreateChatButton
leftIcon={<Plus size="16px" />}
variant="outline"
justifyContent="start"
colorScheme="blue"
borderRadius="lg"
onUpdated={handleCreateChat}
flexGrow="1"
>
{t("create_chat")}
</CreateChatButton>
{allowViews && (
<ChatViewSelection w={["full", "auto"]} onChange={(e) => setView(e.target.value as ChatListViewSelection)} />
)}
</Flex>
{noScrollbar ? (
content
) : (
<SimpleBar
style={{ padding: "8px", height: "100%", minHeight: "150px" }}
classNames={{
contentEl: "flex flex-col items-center overflow-y-hidden min-h-full",
}}
>
{content}
</SimpleBar>
)}
<InferencePoweredBy />
</Box>
);
});