Skip to content

Commit

Permalink
🐛 Fixing pagination for "Shared with me"/ES (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
MontaGhanmy authored Jan 8, 2025
1 parent f04e5f0 commit fd0ff70
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 16 deletions.
2 changes: 2 additions & 0 deletions tdrive/backend/node/src/core/platform/services/search/api.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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"];
}

Expand Down
27 changes: 27 additions & 0 deletions tdrive/backend/node/src/core/platform/services/search/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"])
Expand Down Expand Up @@ -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}`);
}
}
3 changes: 3 additions & 0 deletions tdrive/backend/node/src/services/documents/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
6 changes: 6 additions & 0 deletions tdrive/backend/node/src/services/documents/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ export class DocumentsController {
onlyUploadedNotByMe: true,
sort: request.body.sort,
pagination: request.body.paginate,
nextPage: request.body.nextPage,
};

return {
Expand Down
26 changes: 17 additions & 9 deletions tdrive/frontend/src/app/features/drive/api-client/api-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()}`,
{
Expand All @@ -79,7 +85,10 @@ export class DriveApiClient {
paginate: {
page_token: paginate.page.toString(),
limitStr: paginate.limit.toString(),
}
},
nextPage: {
page_token: paginate.nextPage?.page_token || '',
},
},
);
}
Expand All @@ -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()}`,
{}
{},
);
}

Expand All @@ -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);
}
Expand Down Expand Up @@ -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`);
}

Expand Down
15 changes: 11 additions & 4 deletions tdrive/frontend/src/app/features/drive/hooks/use-drive-actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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,
Expand All @@ -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 || '',
},
});
}
}
},
Expand Down
4 changes: 2 additions & 2 deletions tdrive/frontend/src/app/features/drive/state/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>({
Expand Down
9 changes: 9 additions & 0 deletions tdrive/frontend/src/app/features/drive/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ export type BrowseQuery = {
page_token: string;
limitStr: string;
};
nextPage?: {
page_token: string;
};
}

export type BrowseFilter = {
Expand All @@ -21,6 +24,9 @@ export type BrowsePaginate = {
page: number;
limit: number;
lastPage?: boolean;
nextPage?: {
page_token: string;
};
};

export type DriveItemDetails = {
Expand All @@ -33,6 +39,9 @@ export type DriveItemDetails = {
room: string;
token?: string;
}[];
nextPage?: {
page_token: string;
}
};

export type DriveItem = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,6 @@ export default memo(
useEffect(() => {
setChecked({});
refresh(parentId);
if (!inPublicSharing) refresh('trash');
}, [parentId, refresh, filter]);

const uploadItemModal = useCallback(() => {
Expand Down

0 comments on commit fd0ff70

Please sign in to comment.