aboutsummaryrefslogtreecommitdiffstats
path: root/apps/files/src/composables/useNavigation.ts
blob: 2fff5633e230ec20208773427fd38f2a9a72e171 (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
/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */
import type { View } from '@nextcloud/files'
import type { ShallowRef } from 'vue'

import { getNavigation } from '@nextcloud/files'
import { onMounted, onUnmounted, shallowRef, triggerRef } from 'vue'

/**
 * Composable to get the currently active files view from the files navigation
 */
export function useNavigation() {
	const navigation = getNavigation()
	const views: ShallowRef<View[]> = shallowRef(navigation.views)
	const currentView: ShallowRef<View | null> = shallowRef(navigation.active)

	/**
	 * Event listener to update the `currentView`
	 * @param event The update event
	 */
	function onUpdateActive(event: CustomEvent<View|null>) {
		currentView.value = event.detail
	}

	/**
	 * Event listener to update all registered views
	 */
	function onUpdateViews() {
		views.value = navigation.views
		triggerRef(views)
	}

	onMounted(() => {
		navigation.addEventListener('update', onUpdateViews)
		navigation.addEventListener('updateActive', onUpdateActive)
	})
	onUnmounted(() => {
		navigation.removeEventListener('update', onUpdateViews)
		navigation.removeEventListener('updateActive', onUpdateActive)
	})

	return {
		currentView,
		views,
	}
}