From c3abbfbf2709d1ba2c3ee50d1243af51a7065652 Mon Sep 17 00:00:00 2001 From: timothycarambat <rambat1010@gmail.com> Date: Thu, 2 Nov 2023 16:12:29 -0700 Subject: [PATCH] Fix admin chat pagination --- .../Modals/MangeWorkspace/Settings/index.jsx | 1 - frontend/src/index.css | 4 ++-- frontend/src/pages/Admin/Chats/index.jsx | 16 +++------------- server/endpoints/admin.js | 12 ++++++------ server/endpoints/api/admin/index.js | 13 ++++++++----- server/models/workspaceChats.js | 17 ++++++++++++++--- 6 files changed, 33 insertions(+), 30 deletions(-) diff --git a/frontend/src/components/Modals/MangeWorkspace/Settings/index.jsx b/frontend/src/components/Modals/MangeWorkspace/Settings/index.jsx index 1e61df428..e12e88d12 100644 --- a/frontend/src/components/Modals/MangeWorkspace/Settings/index.jsx +++ b/frontend/src/components/Modals/MangeWorkspace/Settings/index.jsx @@ -31,7 +31,6 @@ export default function WorkspaceSettings({ workspace }) { const [totalVectors, setTotalVectors] = useState(null); const [canDelete, setCanDelete] = useState(false); - useEffect(() => { async function fetchKeys() { const canDelete = await System.getCanDeleteWorkspaces(); diff --git a/frontend/src/index.css b/frontend/src/index.css index b0b8d613a..2553629d3 100644 --- a/frontend/src/index.css +++ b/frontend/src/index.css @@ -355,6 +355,6 @@ dialog::backdrop { top: 100%; } -.user-reply>div:first-of-type { +.user-reply > div:first-of-type { border: 2px solid white; -} \ No newline at end of file +} diff --git a/frontend/src/pages/Admin/Chats/index.jsx b/frontend/src/pages/Admin/Chats/index.jsx index ffd2ed1b3..673e39108 100644 --- a/frontend/src/pages/Admin/Chats/index.jsx +++ b/frontend/src/pages/Admin/Chats/index.jsx @@ -9,6 +9,7 @@ import Admin from "../../../models/admin"; import useQuery from "../../../hooks/useQuery"; import ChatRow from "./ChatRow"; +const PAGE_SIZE = 20; export default function AdminChats() { return ( <div className="w-screen h-screen overflow-hidden bg-sidebar flex"> @@ -45,21 +46,10 @@ function ChatsContainer() { const [canNext, setCanNext] = useState(false); const handlePrevious = () => { - if (chats.length === 0) { - setOffset(0); - return; - } - - const chat = chats.at(-1); - if (chat.id - 20 <= 0) { - setOffset(0); - return; - } - - setOffset(chat.id - 1); + setOffset(Math.max(offset - 1, 0)); }; const handleNext = () => { - setOffset(chats[0].id + 1); + setOffset(offset + 1); }; useEffect(() => { diff --git a/server/endpoints/admin.js b/server/endpoints/admin.js index 1d21d92bc..a3852bd5a 100644 --- a/server/endpoints/admin.js +++ b/server/endpoints/admin.js @@ -264,15 +264,15 @@ function adminEndpoints(app) { const { offset = 0, limit = 20 } = reqBody(request); const chats = await WorkspaceChats.whereWithData( - { id: { gte: offset } }, - limit + {}, + limit, + offset * limit, + { id: "desc" } ); const totalChats = await WorkspaceChats.count(); - const hasPages = totalChats > offset + limit; + const hasPages = totalChats > (offset + 1) * limit; - response - .status(200) - .json({ chats: chats.reverse(), hasPages, totalChats }); + response.status(200).json({ chats: chats, hasPages, totalChats }); } catch (e) { console.error(e); response.sendStatus(500).end(); diff --git a/server/endpoints/api/admin/index.js b/server/endpoints/api/admin/index.js index 1eeef9953..ee147c304 100644 --- a/server/endpoints/api/admin/index.js +++ b/server/endpoints/api/admin/index.js @@ -513,14 +513,17 @@ function apiAdminEndpoints(app) { response.sendStatus(401).end(); return; } - + const pgSize = 20; const { offset = 0 } = reqBody(request); const chats = await WorkspaceChats.whereWithData( - { id: { gte: offset } }, - 20 + {}, + pgSize, + offset * pgSize, + { id: "desc" } ); - const hasPages = (await WorkspaceChats.count()) > 20; - response.status(200).json({ chats: chats.reverse(), hasPages }); + + const hasPages = (await WorkspaceChats.count()) > (offset + 1) * pgSize; + response.status(200).json({ chats: chats, hasPages }); } catch (e) { console.error(e); response.sendStatus(500).end(); diff --git a/server/models/workspaceChats.js b/server/models/workspaceChats.js index 2e3a98a5d..36703efa4 100644 --- a/server/models/workspaceChats.js +++ b/server/models/workspaceChats.js @@ -108,11 +108,17 @@ const WorkspaceChats = { } }, - where: async function (clause = {}, limit = null, orderBy = null) { + where: async function ( + clause = {}, + limit = null, + orderBy = null, + offset = null + ) { try { const chats = await prisma.workspace_chats.findMany({ where: clause, ...(limit !== null ? { take: limit } : {}), + ...(offset !== null ? { skip: offset } : {}), ...(orderBy !== null ? { orderBy } : {}), }); return chats; @@ -134,12 +140,17 @@ const WorkspaceChats = { } }, - whereWithData: async function (clause = {}, limit = null, orderBy = null) { + whereWithData: async function ( + clause = {}, + limit = null, + offset = null, + orderBy = null + ) { const { Workspace } = require("./workspace"); const { User } = require("./user"); try { - const results = await this.where(clause, limit, orderBy); + const results = await this.where(clause, limit, orderBy, offset); for (const res of results) { const workspace = await Workspace.get({ id: res.workspaceId }); -- GitLab