From f12ed8ec6d66a1200c8ddea024ea34e9cdb8ac51 Mon Sep 17 00:00:00 2001 From: jarvis2f <137974272+jarvis2f@users.noreply.github.com> Date: Fri, 21 Feb 2025 17:00:58 +0800 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20fix:=20Enhance=20file=20retrieva?= =?UTF-8?q?l=20logic=20with=20custom=20sorting=20and=20improved=20filter?= =?UTF-8?q?=20handling?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../repository/impl/FileRepositoryImpl.java | 20 ++++++++++++++----- web/src/components/file-extra.tsx | 6 +++--- web/src/components/file-filters.tsx | 2 +- web/src/components/file-viewer.tsx | 4 ---- web/src/hooks/use-files.ts | 10 ++++++++++ 5 files changed, 29 insertions(+), 13 deletions(-) diff --git a/api/src/main/java/telegram/files/repository/impl/FileRepositoryImpl.java b/api/src/main/java/telegram/files/repository/impl/FileRepositoryImpl.java index b42ebd9..533859a 100644 --- a/api/src/main/java/telegram/files/repository/impl/FileRepositoryImpl.java +++ b/api/src/main/java/telegram/files/repository/impl/FileRepositoryImpl.java @@ -155,14 +155,24 @@ public class FileRepositoryImpl implements FileRepository { } } String countClause = whereClause; - if (fromMessageId > 0) { - whereClause += " AND message_id < #{fromMessageId}"; - params.put("fromMessageId", fromMessageId); - } String orderBy = "message_id DESC"; - if (StrUtil.isNotBlank(sort) && StrUtil.isNotBlank(order)) { + boolean customSort = StrUtil.isNotBlank(sort) && StrUtil.isNotBlank(order); + if (customSort) { orderBy = "%s %s".formatted(sort, order); } + if (fromMessageId > 0) { + params.put("fromMessageId", fromMessageId); + if (customSort) { + long fromSortField = Convert.toLong(filter.get("fromSortField")); + whereClause += " AND (%s %s %s OR (%s = %s AND message_id < #{fromMessageId}))".formatted(sort, + Objects.equals(order, "asc") ? ">" : "<", + fromSortField, + sort, + fromSortField); + } else { + whereClause += " AND message_id < #{fromMessageId}"; + } + } log.trace("Get files with where: %s params: %s".formatted(whereClause, params)); return Future.all( SqlTemplate diff --git a/web/src/components/file-extra.tsx b/web/src/components/file-extra.tsx index da18afb..697de2a 100644 --- a/web/src/components/file-extra.tsx +++ b/web/src/components/file-extra.tsx @@ -61,7 +61,7 @@ function FileCaption({ file, rowHeight, ellipsis }: FileExtraProps) {

{file.caption} @@ -95,7 +95,7 @@ function FilePath({ file, ellipsis }: FileExtraProps) {

-

+

{formatDistanceToNow(new Date(file.completionDate), { diff --git a/web/src/components/file-filters.tsx b/web/src/components/file-filters.tsx index 9ef4e5a..c802a7e 100644 --- a/web/src/components/file-filters.tsx +++ b/web/src/components/file-filters.tsx @@ -501,7 +501,7 @@ export default function FileFilters({ /> {!localFilters.offline && ( -

+
{ - console.log("FileViewer rendered", file); - }, [file]); - useEffect(() => { const handleKeyDown = (e: KeyboardEvent) => { if (file === undefined || !open) return; diff --git a/web/src/hooks/use-files.ts b/web/src/hooks/use-files.ts index b70dbd9..b2aa0cb 100644 --- a/web/src/hooks/use-files.ts +++ b/web/src/hooks/use-files.ts @@ -70,6 +70,16 @@ export function useFiles(accountId: string, chatId: string) { } params.set("fromMessageId", previousPageData.nextFromMessageId.toString()); + if (filters.offline && previousPageData.files.length > 0) { + const lastFile = previousPageData.files[previousPageData.files.length - 1]; + if (filters.sort === "size") { + params.set("fromSortField", lastFile!.size.toString()); + } else if (filters.sort === "completion_date") { + params.set("fromSortField", lastFile!.completionDate.toString()); + } else if (filters.sort === "date") { + params.set("fromSortField", lastFile!.date.toString()); + } + } return `/telegram/${accountId}/chat/${chatId}/files?${params.toString()}`; };