|
| 1 | +<!-- |
| 2 | + - SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors |
| 3 | + - SPDX-License-Identifier: AGPL-3.0-or-later |
| 4 | +--> |
| 5 | +<template> |
| 6 | + <NcSelect v-model="selectedAccounts" |
| 7 | + :aria-label-combobox="t('files_sharing', 'Accounts')" |
| 8 | + class="file-list-filter-accounts" |
| 9 | + multiple |
| 10 | + no-wrap |
| 11 | + :options="availableAccounts" |
| 12 | + :placeholder="t('files_sharing', 'Accounts')" |
| 13 | + user-select /> |
| 14 | +</template> |
| 15 | + |
| 16 | +<script setup lang="ts"> |
| 17 | +import type { IAccountData } from '../filters/AccountFilter.ts' |
| 18 | +
|
| 19 | +import { translate as t } from '@nextcloud/l10n' |
| 20 | +import { useBrowserLocation } from '@vueuse/core' |
| 21 | +import { ref, watch, watchEffect } from 'vue' |
| 22 | +import { useNavigation } from '../../../files/src/composables/useNavigation.ts' |
| 23 | +
|
| 24 | +import NcSelect from '@nextcloud/vue/dist/Components/NcSelect.js' |
| 25 | +
|
| 26 | +interface IUserSelectData { |
| 27 | + id: string |
| 28 | + user: string |
| 29 | + displayName: string |
| 30 | +} |
| 31 | +
|
| 32 | +const emit = defineEmits<{ |
| 33 | + (event: 'update:accounts', value: IAccountData[]): void |
| 34 | +}>() |
| 35 | +
|
| 36 | +const { currentView } = useNavigation() |
| 37 | +const currentLocation = useBrowserLocation() |
| 38 | +const availableAccounts = ref<IUserSelectData[]>([]) |
| 39 | +const selectedAccounts = ref<IUserSelectData[]>([]) |
| 40 | +
|
| 41 | +// Watch selected account, on change we emit the new account data to the filter instance |
| 42 | +watch(selectedAccounts, () => { |
| 43 | + // Emit selected accounts as account data |
| 44 | + const accounts = selectedAccounts.value.map(({ id: uid, displayName }) => ({ uid, displayName })) |
| 45 | + emit('update:accounts', accounts) |
| 46 | +}) |
| 47 | +
|
| 48 | +/** |
| 49 | + * Update the accounts owning nodes or have nodes shared to them |
| 50 | + * @param path The path inside the current view to load for accounts |
| 51 | + */ |
| 52 | +async function updateAvailableAccounts(path: string = '/') { |
| 53 | + availableAccounts.value = [] |
| 54 | + if (!currentView.value) { |
| 55 | + return |
| 56 | + } |
| 57 | +
|
| 58 | + const { contents } = await currentView.value.getContents(path) |
| 59 | + const available = new Map<string, IUserSelectData>() |
| 60 | + for (const node of contents) { |
| 61 | + const owner = node.owner ?? node.attributes['owner-id'] |
| 62 | + if (owner && !available.has(owner)) { |
| 63 | + available.set(owner, { |
| 64 | + id: owner, |
| 65 | + user: owner, |
| 66 | + displayName: node.attributes['owner-display-name'] ?? node.owner, |
| 67 | + }) |
| 68 | + } |
| 69 | +
|
| 70 | + const sharees = node.attributes.sharees?.sharee |
| 71 | + if (sharees) { |
| 72 | + // ensure sharees is an array (if only one share then it is just an object) |
| 73 | + for (const sharee of [sharees].flat()) { |
| 74 | + // Skip link shares and other without user |
| 75 | + if (sharee.id === '') { |
| 76 | + continue |
| 77 | + } |
| 78 | + // Add if not already added |
| 79 | + if (!available.has(sharee.id)) { |
| 80 | + available.set(sharee.id, { |
| 81 | + id: sharee.id, |
| 82 | + user: sharee.id, |
| 83 | + displayName: sharee['display-name'], |
| 84 | + }) |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + availableAccounts.value = [...available.values()] |
| 90 | +} |
| 91 | +
|
| 92 | +/** |
| 93 | + * Reset this filter |
| 94 | + */ |
| 95 | +function resetFilter() { |
| 96 | + selectedAccounts.value = [] |
| 97 | +} |
| 98 | +defineExpose({ resetFilter }) |
| 99 | +
|
| 100 | +// When the current view changes or the current directory, |
| 101 | +// then we need to rebuild the available accounts |
| 102 | +watchEffect(() => { |
| 103 | + if (currentView.value) { |
| 104 | + // we have no access to the files router here... |
| 105 | + const path = (currentLocation.value.search ?? '?dir=/').match(/(?<=&|\?)dir=([^&#]+)/)?.[1] |
| 106 | + selectedAccounts.value = [] |
| 107 | + updateAvailableAccounts(decodeURIComponent(path ?? '/')) |
| 108 | + } |
| 109 | +}) |
| 110 | +</script> |
| 111 | + |
| 112 | +<style scoped lang="scss"> |
| 113 | +.file-list-filter-accounts { |
| 114 | + max-width: 300px; |
| 115 | +} |
| 116 | +</style> |
0 commit comments