diff --git a/lib/utils/message-pinning-utils.js b/lib/utils/message-pinning-utils.js --- a/lib/utils/message-pinning-utils.js +++ b/lib/utils/message-pinning-utils.js @@ -1,7 +1,10 @@ // @flow import { isInvalidPinSourceForThread } from '../shared/message-utils.js'; -import { threadHasPermission } from '../shared/thread-utils.js'; +import { + threadHasPermission, + useThreadHasPermission, +} from '../shared/thread-utils.js'; import type { MessageInfo, RawMessageInfo } from '../types/message-types.js'; import type { ThreadInfo, @@ -23,10 +26,23 @@ return isValidMessage && hasManagePinsPermission; } +function useCanToggleMessagePin( + messageInfo: RawMessageInfo | MessageInfo, + threadInfo: ThreadInfo, +): boolean { + const isValidMessage = !isInvalidPinSourceForThread(messageInfo, threadInfo); + const hasManagePinsPermission = useThreadHasPermission( + threadInfo, + threadPermissions.MANAGE_PINS, + ); + + return isValidMessage && hasManagePinsPermission; +} + function pinnedMessageCountText(pinnedCount: number): string { const messageNoun = pinnedCount === 1 ? 'message' : 'messages'; return `${pinnedCount} pinned ${messageNoun}`; } -export { canToggleMessagePin, pinnedMessageCountText }; +export { canToggleMessagePin, useCanToggleMessagePin, pinnedMessageCountText }; diff --git a/native/chat/message.react.js b/native/chat/message.react.js --- a/native/chat/message.react.js +++ b/native/chat/message.react.js @@ -8,7 +8,7 @@ } from 'react-native'; import { messageKey } from 'lib/shared/message-utils.js'; -import { canToggleMessagePin } from 'lib/utils/message-pinning-utils.js'; +import { useCanToggleMessagePin } from 'lib/utils/message-pinning-utils.js'; import type { ChatNavigationProp } from './chat.react.js'; import MultimediaMessage from './multimedia-message.react.js'; @@ -92,9 +92,9 @@ [focused, item], ); - const canTogglePins = React.useMemo( - () => canToggleMessagePin(props.item.messageInfo, props.item.threadInfo), - [props.item.messageInfo, props.item.threadInfo], + const canTogglePins = useCanToggleMessagePin( + props.item.messageInfo, + props.item.threadInfo, ); const innerMessageNode = React.useMemo(() => {