|
2 | 2 | * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors |
3 | 3 | * SPDX-License-Identifier: AGPL-3.0-or-later |
4 | 4 | */ |
5 | | -import type { ContentsWithRoot, File, Folder } from '@nextcloud/files' |
| 5 | +import type { ContentsWithRoot, File, Folder, Node } from '@nextcloud/files' |
6 | 6 | import type { FileStat, ResponseDataDetailed } from 'webdav' |
7 | 7 |
|
8 | | -import { davGetDefaultPropfind, davResultToNode, davRootPath } from '@nextcloud/files' |
| 8 | +import { defaultRootPath, getDefaultPropfind, resultToNode as davResultToNode } from '@nextcloud/files/dav' |
9 | 9 | import { CancelablePromise } from 'cancelable-promise' |
10 | 10 | import { join } from 'path' |
11 | 11 | import { client } from './WebdavClient.ts' |
| 12 | +import { searchNodes } from './WebDavSearch.ts' |
| 13 | +import { getPinia } from '../store/index.ts' |
| 14 | +import { useFilesStore } from '../store/files.ts' |
| 15 | +import { useSearchStore } from '../store/search.ts' |
12 | 16 | import logger from '../logger.ts' |
13 | | - |
14 | 17 | /** |
15 | 18 | * Slim wrapper over `@nextcloud/files` `davResultToNode` to allow using the function with `Array.map` |
16 | 19 | * @param stat The result returned by the webdav library |
17 | 20 | */ |
18 | | -export const resultToNode = (stat: FileStat): File | Folder => davResultToNode(stat) |
| 21 | +export const resultToNode = (stat: FileStat): Node => davResultToNode(stat) |
19 | 22 |
|
20 | | -export const getContents = (path = '/'): CancelablePromise<ContentsWithRoot> => { |
21 | | - path = join(davRootPath, path) |
| 23 | +/** |
| 24 | + * Get contents implementation for the files view. |
| 25 | + * This also allows to fetch local search results when the user is currently filtering. |
| 26 | + * |
| 27 | + * @param path - The path to query |
| 28 | + */ |
| 29 | +export function getContents(path = '/'): CancelablePromise<ContentsWithRoot> { |
22 | 30 | const controller = new AbortController() |
23 | | - const propfindPayload = davGetDefaultPropfind() |
| 31 | + const searchStore = useSearchStore(getPinia()) |
| 32 | + |
| 33 | + if (searchStore.query.length >= 3) { |
| 34 | + return new CancelablePromise((resolve, reject, cancel) => { |
| 35 | + cancel(() => controller.abort()) |
| 36 | + getLocalSearch(path, searchStore.query, controller.signal) |
| 37 | + .then(resolve) |
| 38 | + .catch(reject) |
| 39 | + }) |
| 40 | + } else { |
| 41 | + return defaultGetContents(path) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +/** |
| 46 | + * Generic `getContents` implementation for the users files. |
| 47 | + * |
| 48 | + * @param path - The path to get the contents |
| 49 | + */ |
| 50 | +export function defaultGetContents(path: string): CancelablePromise<ContentsWithRoot> { |
| 51 | + path = join(defaultRootPath, path) |
| 52 | + const controller = new AbortController() |
| 53 | + const propfindPayload = getDefaultPropfind() |
24 | 54 |
|
25 | 55 | return new CancelablePromise(async (resolve, reject, onCancel) => { |
26 | 56 | onCancel(() => controller.abort()) |
@@ -56,3 +86,25 @@ export const getContents = (path = '/'): CancelablePromise<ContentsWithRoot> => |
56 | 86 | } |
57 | 87 | }) |
58 | 88 | } |
| 89 | + |
| 90 | +/** |
| 91 | + * Get the local search results for the current folder. |
| 92 | + * |
| 93 | + * @param path - The path |
| 94 | + * @param query - The current search query |
| 95 | + * @param signal - The aboort signal |
| 96 | + */ |
| 97 | +async function getLocalSearch(path: string, query: string, signal: AbortSignal): Promise<ContentsWithRoot> { |
| 98 | + const filesStore = useFilesStore(getPinia()) |
| 99 | + let folder = filesStore.getDirectoryByPath('files', path) |
| 100 | + if (!folder) { |
| 101 | + const rootPath = join(defaultRootPath, path) |
| 102 | + const stat = await client.stat(rootPath, { details: true }) as ResponseDataDetailed<FileStat> |
| 103 | + folder = resultToNode(stat.data) as Folder |
| 104 | + } |
| 105 | + const contents = await searchNodes(query, { dir: path, signal }) |
| 106 | + return { |
| 107 | + folder, |
| 108 | + contents, |
| 109 | + } |
| 110 | +} |
0 commit comments