🐛 Fixing pagination for "Shared with me"/ES (#778)

This commit is contained in:
Montassar Ghanmy
2025-01-08 17:58:01 +01:00
committed by GitHub
parent f04e5f0535
commit fd0ff703b6
10 changed files with 78 additions and 16 deletions
@@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/no-explicit-any, @typescript-eslint/explicit-module-boundary-types */
import { SearchDocumentsOptions } from "src/services/documents/types";
import { TdriveServiceProvider } from "../../framework";
import { ListResult } from "../../framework/api/crud-service";
import {
@@ -62,6 +63,7 @@ export interface SearchServiceAPI extends TdriveServiceProvider {
getRepository<Entity>(table: string, entityType: EntityTarget<Entity>): SearchRepository<Entity>;
upsert(entities: any[]): Promise<void>;
remove(entities: any[]): Promise<void>;
handlePagination(options: SearchDocumentsOptions): void;
type: SearchConfiguration["type"];
}
@@ -15,6 +15,7 @@ import { DatabaseServiceAPI } from "../database/api";
import SearchRepository from "./repository";
import { Client as OpenClient } from "@opensearch-project/opensearch";
import { Client as ESClient } from "@elastic/elasticsearch";
import { SearchDocumentsOptions } from "src/services/documents/types";
@ServiceName("search")
@Consumes(["database"])
@@ -96,4 +97,30 @@ export default class Search extends TdriveService<SearchServiceAPI> {
api(): SearchServiceAPI {
return this;
}
handlePagination(options: SearchDocumentsOptions): void {
if (this.type === "mongodb") {
// No custom pagination logic for MongoDB
return;
}
if (this.type === "elasticsearch" || this.type === "opensearch") {
if (options.nextPage?.page_token) {
// Set the scroll ID as the page token
options.pagination = {
...options.pagination,
page_token: options.nextPage.page_token,
};
} else {
// Clear pagination if no nextPage token is provided
options.pagination = {
limitStr: options.pagination?.limitStr,
};
}
return;
}
// Handle unsupported platform types
throw new Error(`Pagination handling is not implemented for platform type: ${this.type}`);
}
}
@@ -133,6 +133,9 @@ export class DocumentsService {
options.sort = this.getSortFieldMapping(options.sort);
}
// Handle pagination differently for non-MongoDB platforms
globalResolver.platformServices.search.handlePagination(options);
const fileList: ListResult<DriveFile> = await this.search(options, context);
const result = fileList.getEntities();
@@ -57,12 +57,18 @@ export type SearchDocumentsOptions = {
onlyDirectlyShared?: boolean;
onlyUploadedNotByMe?: boolean;
pagination?: Paginable;
nextPage?: {
page_token: string;
};
};
export type BrowseDocumentsOptions = {
filter?: SearchDocumentsBody;
sort?: SortType;
paginate?: Paginable;
nextPage?: {
page_token: string;
};
};
export type SearchDocumentsBody = {
@@ -213,6 +213,7 @@ export class DocumentsController {
onlyUploadedNotByMe: true,
sort: request.body.sort,
pagination: request.body.paginate,
nextPage: request.body.nextPage,
};
return {
@@ -70,7 +70,13 @@ export class DriveApiClient {
);
}
static async browse(companyId: string, id: string | 'trash' | '', filter: BrowseFilter, sort: BrowseSort, paginate: BrowsePaginate) {
static async browse(
companyId: string,
id: string | 'trash' | '',
filter: BrowseFilter,
sort: BrowseSort,
paginate: BrowsePaginate,
) {
return await Api.post<BrowseQuery, DriveItemDetails>(
`/internal/services/documents/v1/companies/${companyId}/browse/${id}${appendTdriveToken()}`,
{
@@ -79,7 +85,10 @@ export class DriveApiClient {
paginate: {
page_token: paginate.page.toString(),
limitStr: paginate.limit.toString(),
}
},
nextPage: {
page_token: paginate.nextPage?.page_token || '',
},
},
);
}
@@ -93,7 +102,7 @@ export class DriveApiClient {
static async restore(companyId: string, id: string | 'trash' | '') {
return await Api.post<any, any>(
`/internal/services/documents/v1/companies/${companyId}/item/${id}/restore${appendTdriveToken()}`,
{}
{},
);
}
@@ -111,13 +120,10 @@ export class DriveApiClient {
if (!data.version) data.version = {} as Partial<DriveItemVersion>;
return new Promise<DriveItem>((resolve, reject) => {
Api.post<
{ item: Partial<DriveItem>; version: Partial<DriveItemVersion> },
DriveItem
>(
Api.post<{ item: Partial<DriveItem>; version: Partial<DriveItemVersion> }, DriveItem>(
`/internal/services/documents/v1/companies/${companyId}/item${appendTdriveToken()}`,
data as { item: Partial<DriveItem>; version: Partial<DriveItemVersion> },
(res) => {
res => {
if ((res as any)?.statusCode || (res as any)?.error) {
reject(res);
}
@@ -151,7 +157,9 @@ export class DriveApiClient {
static getDownloadUrl(companyId: string, id: string, versionId?: string) {
if (versionId)
return Api.route(`/internal/services/documents/v1/companies/${companyId}/item/${id}/download?version_id=${versionId}`);
return Api.route(
`/internal/services/documents/v1/companies/${companyId}/item/${id}/download?version_id=${versionId}`,
);
return Api.route(`/internal/services/documents/v1/companies/${companyId}/item/${id}/download`);
}
@@ -9,7 +9,7 @@ import {
DriveItemPagination,
DriveItemSort,
} from '../state/store';
import { BrowseFilter, DriveItem, DriveItemVersion } from '../types';
import { BrowseFilter, DriveItem, DriveItemDetails, DriveItemVersion } from '../types';
import { SharedWithMeFilterState } from '../state/shared-with-me-filter';
import Languages from 'features/global/services/languages-service';
import { useUserQuota } from 'features/users/hooks/use-user-quota';
@@ -70,11 +70,12 @@ export const useDriveActions = (inPublicSharing?: boolean) => {
let pagination = await snapshot.getPromise(DriveItemPagination);
if (resetPagination) {
pagination = { page: 0, limit: pagination.limit };
pagination = { page: 0, limit: pagination.limit, nextPage: pagination.nextPage };
set(DriveItemPagination, pagination);
}
let details: DriveItemDetails | undefined;
try {
const details = await DriveApiClient.browse(
details = await DriveApiClient.browse(
companyId,
parentId,
filter,
@@ -94,7 +95,13 @@ export const useDriveActions = (inPublicSharing?: boolean) => {
} catch (e) {
ToasterService.error(Languages.t('hooks.use-drive-actions.unable_load_file'));
} finally {
set(DriveItemPagination, { page: pagination.limit, limit: pagination.limit });
set(DriveItemPagination, {
page: pagination.limit,
limit: pagination.limit,
nextPage: {
page_token: details?.nextPage?.page_token || '',
},
});
}
}
},
@@ -11,9 +11,9 @@ export const DriveItemAtom = atomFamily<Partial<DriveItemDetails> | null, string
default: () => null,
});
export const DriveItemSelectedList = atom<{[key: string]: boolean }>({
export const DriveItemSelectedList = atom<{ [key: string]: boolean }>({
key: 'DriveItemSelectedList',
default: {}
default: {},
});
export const DriveItemSort = atom<BrowseSort>({
@@ -5,6 +5,9 @@ export type BrowseQuery = {
page_token: string;
limitStr: string;
};
nextPage?: {
page_token: string;
};
}
export type BrowseFilter = {
@@ -21,6 +24,9 @@ export type BrowsePaginate = {
page: number;
limit: number;
lastPage?: boolean;
nextPage?: {
page_token: string;
};
};
export type DriveItemDetails = {
@@ -33,6 +39,9 @@ export type DriveItemDetails = {
room: string;
token?: string;
}[];
nextPage?: {
page_token: string;
}
};
export type DriveItem = {
@@ -137,7 +137,6 @@ export default memo(
useEffect(() => {
setChecked({});
refresh(parentId);
if (!inPublicSharing) refresh('trash');
}, [parentId, refresh, filter]);
const uploadItemModal = useCallback(() => {