From c96cae501cb31ee13f69539e8b2bf39611abd316 Mon Sep 17 00:00:00 2001 From: Boris Proshin Date: Wed, 16 Aug 2023 11:24:05 +0300 Subject: [PATCH] feat: added pinning functionality --- src/components/Chats.tsx | 121 ++++++++++++++++++++++++++------------- src/db/index.ts | 1 + 2 files changed, 82 insertions(+), 40 deletions(-) diff --git a/src/components/Chats.tsx b/src/components/Chats.tsx index 5f15bf0..0da49af 100644 --- a/src/components/Chats.tsx +++ b/src/components/Chats.tsx @@ -1,5 +1,13 @@ import { ActionIcon, Flex, Menu } from "@mantine/core"; -import { IconDotsVertical, IconMessages } from "@tabler/icons-react"; +import { + IconDotsVertical, + IconMessages, + IconPencil, + IconPin, + IconPinned, + IconPinnedOff, + IconTrash +} from "@tabler/icons-react"; import { Link } from "@tanstack/react-location"; import { useLiveQuery } from "dexie-react-hooks"; import { useMemo } from "react"; @@ -8,6 +16,7 @@ import { useChatId } from "../hooks/useChatId"; import { DeleteChatModal } from "./DeleteChatModal"; import { EditChatModal } from "./EditChatModal"; import { MainLink } from "./MainLink"; +import { notifications } from "@mantine/notifications"; export function Chats({ search }: { search: string }) { const chatId = useChatId(); @@ -23,47 +32,79 @@ export function Chats({ search }: { search: string }) { [chats, search] ); + const toggleChatPin = async (chatId: string, event: React.UIEvent) => { + try { + event.preventDefault(); + await db.chats.where({ id: chatId }).modify((chat) => { + chat.pinned = !chat.pinned; + }); + } catch (error: any) { + if (error.toJSON().message === "Network Error") { + notifications.show({ + title: "Error", + color: "red", + message: "No internet connection.", + }); + } + const message = error.response?.data?.error?.message; + if (message) { + notifications.show({ + title: "Error", + color: "red", + message, + }); + } + } + }; + return ( <> - {filteredChats.map((chat) => ( - ({ - marginTop: 1, - "&:hover, &.active": { - backgroundColor: - theme.colorScheme === "dark" - ? theme.colors.dark[6] - : theme.colors.gray[1], - }, - })} - > - - } - color="teal" - chat={chat} - label={chat.description} - /> - - - - - - - - - - Edit - - - Delete - - - - - ))} + {filteredChats + .sort((chat) => (chat.pinned ? -1 : 1)) + .map((chat) => ( + ({ + marginTop: 1, + "&:hover, &.active": { + backgroundColor: theme.colorScheme === "dark" ? theme.colors.dark[6] : theme.colors.gray[1], + }, + })} + > + + : } + color="teal" + chat={chat} + label={chat.description} + /> + + + + + + + + + : } + onClick={(event) => toggleChatPin(chat.id, event)} + > + {chat.pinned ? "Remove pin" : "Pin this"} + + + }>Edit + + + }> + Delete + + + + + + ))} ); } diff --git a/src/db/index.ts b/src/db/index.ts index f4f1288..9e5e9ad 100644 --- a/src/db/index.ts +++ b/src/db/index.ts @@ -7,6 +7,7 @@ export interface Chat { description: string; totalTokens: number; createdAt: Date; + pinned: boolean; } export interface Message {