refactor(permissions): retrieve special permissions from hook

Signed-off-by: Erik Michelson <github@erik.michelson.eu>
This commit is contained in:
Erik Michelson 2023-08-14 00:11:11 +02:00
parent dfcf662a85
commit 6d69b294cb
3 changed files with 35 additions and 21 deletions

View file

@ -0,0 +1,24 @@
/*
* SPDX-FileCopyrightText: 2023 The HedgeDoc developers (see AUTHORS file)
*
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { useApplicationState } from '../../../../../../../hooks/common/use-application-state'
import { useMemo } from 'react'
import { SpecialGroup } from '@hedgedoc/commons'
/**
* Returns the special permissions for the current note.
* @return A memoized object containing the special permissions for the current note.
*/
export const useGetSpecialPermissions = () => {
const groupPermissions = useApplicationState((state) => state.noteDetails?.permissions.sharedToGroups)
return useMemo(() => {
const everyone = groupPermissions?.find((group) => group.groupName === (SpecialGroup.EVERYONE as string))
const loggedIn = groupPermissions?.find((group) => group.groupName === (SpecialGroup.LOGGED_IN as string))
return {
[SpecialGroup.EVERYONE]: everyone,
[SpecialGroup.LOGGED_IN]: loggedIn
}
}, [groupPermissions])
}

View file

@ -17,6 +17,7 @@ import { AccessLevel, SpecialGroup } from '@hedgedoc/commons'
import React, { useCallback, useMemo } from 'react' import React, { useCallback, useMemo } from 'react'
import { useAsync } from 'react-use' import { useAsync } from 'react-use'
import { PermissionInconsistentAlert } from './permission-inconsistent-alert' import { PermissionInconsistentAlert } from './permission-inconsistent-alert'
import { useGetSpecialPermissions } from './hooks/use-get-special-permissions'
export interface PermissionEntryUserProps { export interface PermissionEntryUserProps {
entry: NoteUserPermissionEntry entry: NoteUserPermissionEntry
@ -34,16 +35,15 @@ export const PermissionEntryUser: React.FC<PermissionEntryUserProps & Permission
}) => { }) => {
const noteId = useApplicationState((state) => state.noteDetails?.primaryAddress) const noteId = useApplicationState((state) => state.noteDetails?.primaryAddress)
const { showErrorNotification } = useUiNotifications() const { showErrorNotification } = useUiNotifications()
const groupPermissions = useApplicationState((state) => state.noteDetails.permissions.sharedToGroups) const { [SpecialGroup.EVERYONE]: everyonePermission, [SpecialGroup.LOGGED_IN]: loggedInPermission } =
useGetSpecialPermissions()
const permissionInconsistent = useMemo(() => { const permissionInconsistent = useMemo(
const everyonePermission = groupPermissions.find((group) => group.groupName === (SpecialGroup.EVERYONE as string)) () =>
const loggedInPermission = groupPermissions.find((group) => group.groupName === (SpecialGroup.LOGGED_IN as string))
return (
(everyonePermission && everyonePermission.canEdit && !entry.canEdit) || (everyonePermission && everyonePermission.canEdit && !entry.canEdit) ||
(loggedInPermission && loggedInPermission.canEdit && !entry.canEdit) (loggedInPermission && loggedInPermission.canEdit && !entry.canEdit),
[everyonePermission, loggedInPermission, entry]
) )
}, [groupPermissions, entry])
const onRemoveEntry = useCallback(() => { const onRemoveEntry = useCallback(() => {
if (!noteId) { if (!noteId) {

View file

@ -3,13 +3,13 @@
* *
* SPDX-License-Identifier: AGPL-3.0-only * SPDX-License-Identifier: AGPL-3.0-only
*/ */
import { useApplicationState } from '../../../../../../hooks/common/use-application-state'
import { useIsOwner } from '../../../../../../hooks/common/use-is-owner' import { useIsOwner } from '../../../../../../hooks/common/use-is-owner'
import type { PermissionDisabledProps } from './permission-disabled.prop' import type { PermissionDisabledProps } from './permission-disabled.prop'
import { PermissionEntrySpecialGroup } from './permission-entry-special-group' import { PermissionEntrySpecialGroup } from './permission-entry-special-group'
import { AccessLevel, SpecialGroup } from '@hedgedoc/commons' import { AccessLevel, SpecialGroup } from '@hedgedoc/commons'
import React, { Fragment, useMemo } from 'react' import React, { Fragment, useMemo } from 'react'
import { Trans, useTranslation } from 'react-i18next' import { Trans, useTranslation } from 'react-i18next'
import { useGetSpecialPermissions } from './hooks/use-get-special-permissions'
/** /**
* Section of the permission modal for managing special group access to the note. * Section of the permission modal for managing special group access to the note.
@ -18,16 +18,10 @@ import { Trans, useTranslation } from 'react-i18next'
*/ */
export const PermissionSectionSpecialGroups: React.FC<PermissionDisabledProps> = ({ disabled }) => { export const PermissionSectionSpecialGroups: React.FC<PermissionDisabledProps> = ({ disabled }) => {
useTranslation() useTranslation()
const groupPermissions = useApplicationState((state) => state.noteDetails?.permissions.sharedToGroups)
const isOwner = useIsOwner() const isOwner = useIsOwner()
const { [SpecialGroup.EVERYONE]: groupEveryone, [SpecialGroup.LOGGED_IN]: groupLoggedIn } = useGetSpecialPermissions()
const specialGroupEntries = useMemo(() => { const specialGroupEntries = useMemo(() => {
if (!groupPermissions) {
return
}
const groupEveryone = groupPermissions.find((entry) => entry.groupName === (SpecialGroup.EVERYONE as string))
const groupLoggedIn = groupPermissions.find((entry) => entry.groupName === (SpecialGroup.LOGGED_IN as string))
return { return {
everyoneLevel: groupEveryone everyoneLevel: groupEveryone
? groupEveryone.canEdit ? groupEveryone.canEdit
@ -41,11 +35,7 @@ export const PermissionSectionSpecialGroups: React.FC<PermissionDisabledProps> =
: AccessLevel.NONE, : AccessLevel.NONE,
loggedInInconsistentAlert: groupEveryone && (!groupLoggedIn || (groupEveryone.canEdit && !groupLoggedIn.canEdit)) loggedInInconsistentAlert: groupEveryone && (!groupLoggedIn || (groupEveryone.canEdit && !groupLoggedIn.canEdit))
} }
}, [groupPermissions]) }, [groupEveryone, groupLoggedIn])
if (!specialGroupEntries) {
return null
}
return ( return (
<Fragment> <Fragment>