// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved. // See LICENSE.txt for license information. import React, {useCallback, useEffect, useRef} from 'react'; import {DeviceEventEmitter, Platform} from 'react-native'; import {KeyboardTrackingView, KeyboardTrackingViewRef} from 'react-native-keyboard-tracking-view'; import {PostDraft as PostDraftConstants, View as ViewConstants} from '@constants'; import {useIsTablet} from '@hooks/device'; import Archived from './archived'; import DraftHandler from './draft_handler'; import ReadOnly from './read_only'; type Props = { testID?: string; accessoriesContainerID?: string; canPost: boolean; channelId: string; channelIsArchived?: boolean; channelIsReadOnly: boolean; deactivatedChannel: boolean; rootId?: string; scrollViewNativeID?: string; } export default function PostDraft({ testID, accessoriesContainerID, canPost, channelId, channelIsArchived, channelIsReadOnly, deactivatedChannel, rootId, scrollViewNativeID, }: Props) { const keyboardTracker = useRef(null); const resetScrollViewAnimationFrame = useRef(); const isTablet = useIsTablet(); const updateNativeScrollView = useCallback((scrollViewNativeIDToUpdate: string) => { if (keyboardTracker?.current && scrollViewNativeID === scrollViewNativeIDToUpdate) { resetScrollViewAnimationFrame.current = requestAnimationFrame(() => { keyboardTracker.current?.resetScrollView(scrollViewNativeIDToUpdate); if (resetScrollViewAnimationFrame.current != null) { cancelAnimationFrame(resetScrollViewAnimationFrame.current); } resetScrollViewAnimationFrame.current = undefined; }); } }, [scrollViewNativeID]); useEffect(() => { const listener = DeviceEventEmitter.addListener(PostDraftConstants.UPDATE_NATIVE_SCROLLVIEW, updateNativeScrollView); return () => { listener.remove(); if (resetScrollViewAnimationFrame.current) { cancelAnimationFrame(resetScrollViewAnimationFrame.current); } }; }, [updateNativeScrollView]); if (channelIsArchived || deactivatedChannel) { const archivedTestID = `${testID}.archived`; return ( ); } if (channelIsReadOnly || !canPost) { const readOnlyTestID = `${testID}.read_only`; return ( ); } const draftHandler = ( ); if (Platform.OS === 'android') { return draftHandler; } return ( {draftHandler} ); }