From acbad2f2cf7c13e03e0a0dc1067041ba4c59930c Mon Sep 17 00:00:00 2001 From: Sean Hatfield Date: Wed, 22 May 2024 14:27:56 -0700 Subject: [PATCH] [FEAT] Clear all chats button + export button styles updates (#1495) * implement clear all chats button + export button styles updates * Hide clear button if no chats are visible update clear function --------- Co-authored-by: timothycarambat --- .../src/pages/GeneralSettings/Chats/index.jsx | 78 ++++++++++++++----- server/endpoints/system.js | 4 +- 2 files changed, 60 insertions(+), 22 deletions(-) diff --git a/frontend/src/pages/GeneralSettings/Chats/index.jsx b/frontend/src/pages/GeneralSettings/Chats/index.jsx index d5b3d509..52a3c434 100644 --- a/frontend/src/pages/GeneralSettings/Chats/index.jsx +++ b/frontend/src/pages/GeneralSettings/Chats/index.jsx @@ -7,7 +7,7 @@ import useQuery from "@/hooks/useQuery"; import ChatRow from "./ChatRow"; import showToast from "@/utils/toast"; import System from "@/models/system"; -import { CaretDown, Download } from "@phosphor-icons/react"; +import { CaretDown, Download, Trash } from "@phosphor-icons/react"; import { saveAs } from "file-saver"; const exportOptions = { @@ -49,6 +49,12 @@ export default function WorkspaceChats() { const [showMenu, setShowMenu] = useState(false); const menuRef = useRef(); const openMenuButton = useRef(); + const query = useQuery(); + const [loading, setLoading] = useState(true); + const [chats, setChats] = useState([]); + const [offset, setOffset] = useState(Number(query.get("offset") || 0)); + const [canNext, setCanNext] = useState(false); + const handleDumpChats = async (exportType) => { const chats = await System.exportChats(exportType); if (!!chats) { @@ -62,6 +68,18 @@ export default function WorkspaceChats() { } }; + const handleClearAllChats = async () => { + if ( + !window.confirm( + `Are you sure you want to clear all chats?\n\nThis action is irreversible.` + ) + ) + return false; + await System.deleteChat(-1); + setChats([]); + showToast("Cleared all chats.", "success"); + }; + const toggleMenu = () => { setShowMenu(!showMenu); }; @@ -83,6 +101,16 @@ export default function WorkspaceChats() { }; }, []); + useEffect(() => { + async function fetchChats() { + const { chats: _chats, hasPages = false } = await System.chats(offset); + setChats(_chats); + setCanNext(hasPages); + setLoading(false); + } + fetchChats(); + }, [offset]); + return (
@@ -100,7 +128,7 @@ export default function WorkspaceChats() {
+ {chats.length > 0 && ( + + )}

These are all the recorded chats and messages that have been sent by users ordered by their creation date.

- + ); } -function ChatsContainer() { - const query = useQuery(); - const [loading, setLoading] = useState(true); - const [chats, setChats] = useState([]); - const [offset, setOffset] = useState(Number(query.get("offset") || 0)); - const [canNext, setCanNext] = useState(false); - +function ChatsContainer({ + loading, + chats, + setChats, + offset, + setOffset, + canNext, +}) { const handlePrevious = () => { setOffset(Math.max(offset - 1, 0)); }; @@ -155,20 +200,11 @@ function ChatsContainer() { setOffset(offset + 1); }; - const handleDeleteChat = (chatId) => { + const handleDeleteChat = async (chatId) => { + await System.deleteChat(chatId); setChats((prevChats) => prevChats.filter((chat) => chat.id !== chatId)); }; - useEffect(() => { - async function fetchChats() { - const { chats: _chats, hasPages = false } = await System.chats(offset); - setChats(_chats); - setCanNext(hasPages); - setLoading(false); - } - fetchChats(); - }, [offset]); - if (loading) { return ( { try { const { id } = request.params; - await WorkspaceChats.delete({ id: Number(id) }); + Number(id) === -1 + ? await WorkspaceChats.delete({}, true) + : await WorkspaceChats.delete({ id: Number(id) }); response.json({ success: true, error: null }); } catch (e) { console.error(e);