diff --git a/plugins/view-resources/src/visibilityTester.ts b/plugins/view-resources/src/visibilityTester.ts index 4a89d9dd17..6b04ad751b 100644 --- a/plugins/view-resources/src/visibilityTester.ts +++ b/plugins/view-resources/src/visibilityTester.ts @@ -27,6 +27,16 @@ import { getClient } from '@hcengineering/presentation' import { get } from 'svelte/store' import { spaceSpace } from './utils' +function convertToSingleDoc (doc?: Doc | Doc[]): Doc | undefined { + if (doc === undefined) { + return undefined + } + if (Array.isArray(doc)) { + return doc.length === 1 ? doc[0] : undefined + } + return doc +} + function isTypedSpace (space: Space): space is TypedSpace { return getClient().getHierarchy().isDerived(space._class, core.class.TypedSpace) } @@ -58,11 +68,12 @@ export async function canDeleteObject (doc?: Doc | Doc[]): Promise { } export async function canEditSpace (doc?: Doc | Doc[]): Promise { - if (doc === undefined || Array.isArray(doc)) { + const one = convertToSingleDoc(doc) + if (one === undefined) { return false } - const space = doc as Space + const space = one as Space if (isSpaceOwner(space)) { return true @@ -84,11 +95,12 @@ export async function canEditSpace (doc?: Doc | Doc[]): Promise { } export async function canArchiveSpace (doc?: Doc | Doc[]): Promise { - if (doc === undefined || Array.isArray(doc)) { + const one = convertToSingleDoc(doc) + if (one === undefined) { return false } - const space = doc as Space + const space = one as Space if (isSpaceOwner(space)) { return true @@ -110,11 +122,12 @@ export async function canArchiveSpace (doc?: Doc | Doc[]): Promise { } export async function canDeleteSpace (doc?: Doc | Doc[]): Promise { - if (doc === undefined || Array.isArray(doc)) { + const one = convertToSingleDoc(doc) + if (one === undefined) { return false } - const space = doc as Space + const space = one as Space if (isSpaceOwner(space)) { return true @@ -132,21 +145,23 @@ export async function canDeleteSpace (doc?: Doc | Doc[]): Promise { } export async function canJoinSpace (doc?: Doc | Doc[]): Promise { - if (doc === undefined || Array.isArray(doc)) { + const one = convertToSingleDoc(doc) + if (one === undefined) { return false } - const space = doc as Space + const space = one as Space return !(space.members ?? []).includes(getCurrentAccount().uuid) } export async function canLeaveSpace (doc?: Doc | Doc[]): Promise { - if (doc === undefined || Array.isArray(doc)) { + const one = convertToSingleDoc(doc) + if (one === undefined) { return false } - const space = doc as Space + const space = one as Space return (space.members ?? []).includes(getCurrentAccount().uuid) }