aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/views/files.ts
blob: 95450f0d71a8da392e2cfbef4fa5dc37c40af915 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
/**
 * SPDX-FileCopyrightText: 2023 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

import { emit, subscribe } from '@nextcloud/event-bus'
import { View, getNavigation } from '@nextcloud/files'
import { t } from '@nextcloud/l10n'
import { getContents } from '../services/Files.ts'
import { useActiveStore } from '../store/active.ts'
import { defaultView } from '../utils/filesViews.ts'

import FolderSvg from '@mdi/svg/svg/folder.svg?raw'

export const VIEW_ID = 'files'

/**
 * Register the files view to the navigation
 */
export function registerFilesView() {
	// we cache the query to allow more performant search (see below in event listener)
	let oldQuery = ''

	const Navigation = getNavigation()
	Navigation.register(new View({
		id: VIEW_ID,
		name: t('files', 'All files'),
		caption: t('files', 'List of your files and folders.'),

		icon: FolderSvg,
		// if this is the default view we set it at the top of the list - otherwise below it
		order: defaultView() === VIEW_ID ? 0 : 5,

		getContents,
	}))

	// when the search is updated
	// and we are in the files view
	// and there is already a folder fetched
	// then we "update" it to trigger a new `getContents` call to search for the query while the filelist is filtered
	subscribe('files:search:updated', ({ scope, query }) => {
		if (scope === 'globally') {
			return
		}

		if (Navigation.active?.id !== VIEW_ID) {
			return
		}

		// If neither the old query nor the new query is longer than the search minimum
		// then we do not need to trigger a new PROPFIND / SEARCH
		// so we skip unneccessary requests here
		if (oldQuery.length < 3 && query.length < 3) {
			return
		}

		const store = useActiveStore()
		if (!store.activeFolder) {
			return
		}

		oldQuery = query
		emit('files:node:updated', store.activeFolder)
	})
}