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()}`; };