forked from LAION-AI/Open-Assistant
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserMessageConversation.tsx
47 lines (40 loc) · 1.5 KB
/
UserMessageConversation.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
import { Button, CircularProgress, Flex, Spacer } from "@chakra-ui/react";
import { useTranslation } from "next-i18next";
import { get } from "src/lib/api";
import { FetchMessagesCursorResponse } from "src/types/Conversation";
import useSWRImmutable from "swr/immutable";
import { useCursorPagination } from "./DataTable/useCursorPagination";
import { MessageConversation } from "./Messages/MessageConversation";
const UserMessageConversation = () => {
const { pagination, toNextPage, toPreviousPage } = useCursorPagination();
const { t } = useTranslation("leaderboard");
const { data, error, isLoading } = useSWRImmutable<FetchMessagesCursorResponse>(
`/api/messages/user?cursor=${encodeURIComponent(pagination.cursor)}&direction=${pagination.direction}`, // If we want to show own deleted messages, add: &include_deleted=true
get,
{
revalidateOnMount: true,
}
);
if (isLoading && !data) {
return <CircularProgress isIndeterminate />;
}
if (error) {
return <>Unable to load messages.</>;
}
const userMessages = data?.items || [];
return (
<div>
<Flex mb="2">
<Button onClick={() => toPreviousPage(data)} isDisabled={!data?.prev}>
{t("previous")}
</Button>
<Spacer />
<Button onClick={() => toNextPage(data)} isDisabled={!data?.next}>
{t("next")}
</Button>
</Flex>
<MessageConversation enableLink messages={userMessages} showCreatedDate />
</div>
);
};
export default UserMessageConversation;