diff --git a/src/App.tsx b/src/App.tsx index b603cf0..be46c36 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -42,6 +42,15 @@ const App = (): JSX.Element => { > Force sync + ) } diff --git a/src/dataproxy/common/DataProxyInterface.ts b/src/dataproxy/common/DataProxyInterface.ts index 45a0486..356d416 100644 --- a/src/dataproxy/common/DataProxyInterface.ts +++ b/src/dataproxy/common/DataProxyInterface.ts @@ -13,6 +13,7 @@ export interface SearchOptions { export interface DataProxyWorker { search: (query: string, options: SearchOptions) => unknown + recents: () => unknown setup: ( clientData: ClientData, options?: { sharedDriveIds: string[] } diff --git a/src/dataproxy/parent/ParentWindowProvider.tsx b/src/dataproxy/parent/ParentWindowProvider.tsx index faa4187..cbbe2fc 100644 --- a/src/dataproxy/parent/ParentWindowProvider.tsx +++ b/src/dataproxy/parent/ParentWindowProvider.tsx @@ -3,6 +3,7 @@ import React, { ReactNode } from 'react' import { QueryDefinition } from 'cozy-client' import { + IOCozyFile, Mutation, MutationOptions, QueryOptions @@ -40,6 +41,10 @@ export const ParentWindowProvider = React.memo( return result }, + recents: async (): Promise> => { + const result = await workerContext.worker.recents() + return result as Array + }, requestLink: async ( operation: QueryDefinition | Mutation, options?: QueryOptions | MutationOptions | undefined diff --git a/src/dataproxy/worker/data.ts b/src/dataproxy/worker/data.ts index c12fb67..3a26446 100644 --- a/src/dataproxy/worker/data.ts +++ b/src/dataproxy/worker/data.ts @@ -1,11 +1,23 @@ -import CozyClient from 'cozy-client' +import CozyClient, { Q, QueryDefinition } from 'cozy-client' +import { QueryOptions } from 'cozy-client/types/types' import Minilog from 'cozy-minilog' -import { LOCALSTORAGE_KEY_DELETING_DATA } from '@/consts' +import { LOCALSTORAGE_KEY_DELETING_DATA, FILES_DOCTYPE } from '@/consts' +import { getPouchLink } from '@/helpers/client' import { ClientData } from '../common/DataProxyInterface' + +export const TRASH_DIR_ID = 'io.cozy.files.trash-dir' +export const SHARED_DRIVES_DIR_ID = 'io.cozy.files.shared-drives-dir' // This folder mostly contains external drives like Nextcloud + const log = Minilog('👷‍♂️ [Worker utils]') +const DEFAULT_CACHE_TIMEOUT_QUERIES = 9 * 60 * 1000 +/* eslint-disable @typescript-eslint/no-unsafe-member-access, @typescript-eslint/no-unsafe-call, @typescript-eslint/no-unsafe-assignment, @typescript-eslint/no-explicit-any */ +const defaultFetchPolicy: QueryOptions['fetchPolicy'] = ( + CozyClient as any +).fetchPolicies.olderThan(DEFAULT_CACHE_TIMEOUT_QUERIES) + interface SessionInfo { last_seen: string long_run: boolean @@ -71,3 +83,59 @@ export const removeStaleLocalData = async (): Promise => { } return } + +function buildRecentsQuery(doctype: string): { + definition: QueryDefinition + options: QueryOptions +} { + return { + definition: Q(doctype) + .where({ + updated_at: { + $gt: null + } + }) + .partialIndex({ + type: 'file', + trashed: false, + dir_id: { + $nin: [SHARED_DRIVES_DIR_ID, TRASH_DIR_ID] + } + }) + .indexFields(['updated_at']) + .sortBy([{ updated_at: 'desc' }]) + .limitBy(50), + options: { + as: 'recent-view-query-' + doctype, + fetchPolicy: defaultFetchPolicy + } + } +} + +export const queryRecents = async (client: CozyClient): Promise => { + if (!client) { + throw new Error('Client is not initialized') + } + const pouchLink = getPouchLink(client) + if (!pouchLink) { + throw new Error('PouchLink is not initialized') + } + const doctypes = pouchLink.doctypes.filter(doctype => + doctype.startsWith(FILES_DOCTYPE) + ) + + // to be sure to have all the shared drives at first page display + await pouchLink.pouches.waitForCurrentReplications() + + const sharedDrivesRecentsPromises = doctypes.map(doctype => { + const request = buildRecentsQuery(doctype) + return client.requestQuery(request.definition, request.options) + }) + const recents: unknown[] = ( + (await Promise.all(sharedDrivesRecentsPromises)) as Array<{ + data?: unknown[] + }> + ).flatMap(recentResult => recentResult.data || []) + + return recents +} diff --git a/src/dataproxy/worker/worker.ts b/src/dataproxy/worker/worker.ts index 458cbad..aa3c79c 100644 --- a/src/dataproxy/worker/worker.ts +++ b/src/dataproxy/worker/worker.ts @@ -29,7 +29,7 @@ import { DataProxyWorkerPartialState, SearchOptions } from '@/dataproxy/common/DataProxyInterface' -import { queryIsTrustedDevice } from '@/dataproxy/worker/data' +import { queryIsTrustedDevice, queryRecents } from '@/dataproxy/worker/data' import { platformWorker, searchEngineStorage @@ -169,6 +169,13 @@ const dataProxy: DataProxyWorker = { return results }, + recents: () => { + if (!client) { + throw new Error('Client is not initialized') + } + return queryRecents(client) + }, + requestLink: async ( operation: QueryDefinition | Mutation, options?: QueryOptions | MutationOptions | undefined