aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/store/search.ts
blob: 417f662ca00c63085224f4a486b56358ee4fcb54 (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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
/*!
 * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

import type { INode, View } from '@nextcloud/files'
import type RouterService from '../services/RouterService'
import type { SearchScope } from '../types'

import { emit, subscribe } from '@nextcloud/event-bus'
import { defineStore } from 'pinia'
import { ref, watch } from 'vue'
import { VIEW_ID } from '../views/search'
import logger from '../logger'
import debounce from 'debounce'

export const useSearchStore = defineStore('search', () => {
	/**
	 * The current search query
	 */
	const query = ref('')

	/**
	 * Where to start the search
	 */
	const base = ref<INode>()

	/**
	 * Scope of the search.
	 * Scopes:
	 * - filter: only filter current file list
	 * - locally: search from current location recursivly
	 * - globally: search everywhere
	 */
	const scope = ref<SearchScope>('filter')

	// reset the base if query is cleared
	watch(scope, () => {
		if (scope.value !== 'locally') {
			base.value = undefined
		}

		updateSearch()
	})

	watch(query, (old, current) => {
		// skip if only whitespaces changed
		if (old.trim() === current.trim()) {
			return
		}

		updateSearch()
	})

	// initialize the search store
	initialize()

	/**
	 * Debounced update of the current route
	 * @private
	 */
	const updateRouter = debounce((isSearch: boolean, fileid?: number) => {
		const router = window.OCP.Files.Router as RouterService
		router.goToRoute(
			undefined,
			{
				view: VIEW_ID,
				...(fileid === undefined ? {} : { fileid: String(fileid) }),
			},
			{
				query: query.value,
			},
			isSearch,
		)
	})

	/**
	 * Handle updating the filter if needed.
	 * Also update the search view by updating the current route if needed.
	 *
	 * @private
	 */
	function updateSearch() {
		// emit the search event to update the filter
		emit('files:search:updated', { query: query.value, scope: scope.value })
		const router = window.OCP.Files.Router as RouterService

		// if we are on the search view and the query was unset or scope was set to 'filter' we need to move back to the files view
		if (router.params.view === VIEW_ID && (query.value === '' || scope.value === 'filter')) {
			scope.value = 'filter'
			return router.goToRoute(
				undefined,
				{
					view: 'files',
				},
				{
					...router.query,
					query: undefined,
				},
			)
		}

		// for the filter scope we do not need to adjust the current route anymore
		// also if the query is empty we do not need to do anything
		if (scope.value === 'filter' || !query.value) {
			return
		}

		// we only use the directory if we search locally
		const fileid = scope.value === 'locally' ? base.value?.fileid : undefined
		const isSearch = router.params.view === VIEW_ID

		logger.debug('Update route for updated search query', { query: query.value, fileid, isSearch })
		updateRouter(isSearch, fileid)
	}

	/**
	 * Event handler that resets the store if the file list view was changed.
	 *
	 * @param view - The new view that is active
	 * @private
	 */
	function onViewChanged(view: View) {
		if (view.id !== VIEW_ID) {
			query.value = ''
			scope.value = 'filter'
		}
	}

	/**
	 * Initialize the store from the router if needed
	 */
	function initialize() {
		subscribe('files:navigation:changed', onViewChanged)

		const router = window.OCP.Files.Router as RouterService
		// if we initially load the search view (e.g. hard page refresh)
		// then we need to initialize the store from the router
		if (router.params.view === VIEW_ID) {
			query.value = [router.query.query].flat()[0] ?? ''

			if (query.value) {
				scope.value = 'globally'
				logger.debug('Directly navigated to search view', { query: query.value })
			} else {
				// we do not have any query so we need to move to the files list
				logger.info('Directly navigated to search view without any query, redirect to files view.')
				router.goToRoute(
					undefined,
					{
						...router.params,
						view: 'files',
					},
					{
						...router.query,
						query: undefined,
					},
					true,
				)
			}
		}
	}

	return {
		base,
		query,
		scope,
	}
})