Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,15 @@ const App = (): JSX.Element => {
>
Force sync
</button>
<button
onClick={async () => {
const result = await worker.recents()
// eslint-disable-next-line no-console
console.log('recents', result)
}}
>
Recent files
</button>
</div>
)
}
Expand Down
1 change: 1 addition & 0 deletions src/dataproxy/common/DataProxyInterface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export interface SearchOptions {

export interface DataProxyWorker {
search: (query: string, options: SearchOptions) => unknown
recents: () => unknown
setup: (
clientData: ClientData,
options?: { sharedDriveIds: string[] }
Expand Down
5 changes: 5 additions & 0 deletions src/dataproxy/parent/ParentWindowProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import React, { ReactNode } from 'react'

import { QueryDefinition } from 'cozy-client'
import {
IOCozyFile,
Mutation,
MutationOptions,
QueryOptions
Expand Down Expand Up @@ -40,6 +41,10 @@ export const ParentWindowProvider = React.memo(

return result
},
recents: async (): Promise<Array<IOCozyFile>> => {
const result = await workerContext.worker.recents()
return result as Array<IOCozyFile>
},
requestLink: async (
operation: QueryDefinition | Mutation,
options?: QueryOptions | MutationOptions | undefined
Expand Down
72 changes: 70 additions & 2 deletions src/dataproxy/worker/data.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -71,3 +83,59 @@ export const removeStaleLocalData = async (): Promise<void> => {
}
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<unknown[]> => {
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
}
9 changes: 8 additions & 1 deletion src/dataproxy/worker/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down