mattermost-mobile/app/screens/thread/thread.tsx
Daniel Espino García afd818996e
Improve autocomplete behaviour (#6559)
* Fix positioning issues with Autocomplete

* Fix positioning for iOS and iPad

* Adapt search to new autocomplete approach

* Adapt for android

* Fix lint

* Fix calculations on channel edit

* Address feedback

* Address feedback

Co-authored-by: Daniel Espino <danielespino@MacBook-Pro-de-Daniel.local>
2022-08-13 08:34:26 -04:00

104 lines
3.4 KiB
TypeScript

// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import React, {useCallback, useEffect, useRef, useState} from 'react';
import {DeviceEventEmitter, LayoutChangeEvent, StyleSheet, View} from 'react-native';
import {KeyboardTrackingViewRef} from 'react-native-keyboard-tracking-view';
import {Edge, SafeAreaView} from 'react-native-safe-area-context';
import FreezeScreen from '@components/freeze_screen';
import PostDraft from '@components/post_draft';
import RoundedHeaderContext from '@components/rounded_header_context';
import {Events} from '@constants';
import {THREAD_ACCESSORIES_CONTAINER_NATIVE_ID} from '@constants/post_draft';
import {useAppState} from '@hooks/device';
import useDidUpdate from '@hooks/did_update';
import {popTopScreen} from '@screens/navigation';
import EphemeralStore from '@store/ephemeral_store';
import ThreadPostList from './thread_post_list';
import type PostModel from '@typings/database/models/servers/post';
type ThreadProps = {
componentId: string;
rootPost?: PostModel;
};
const edges: Edge[] = ['left', 'right'];
const styles = StyleSheet.create({
flex: {flex: 1},
});
const Thread = ({componentId, rootPost}: ThreadProps) => {
const appState = useAppState();
const postDraftRef = useRef<KeyboardTrackingViewRef>(null);
const [containerHeight, setContainerHeight] = useState(0);
useEffect(() => {
const listener = DeviceEventEmitter.addListener(Events.PAUSE_KEYBOARD_TRACKING_VIEW, (pause: boolean) => {
if (pause) {
postDraftRef.current?.pauseTracking(rootPost!.id);
return;
}
postDraftRef.current?.resumeTracking(rootPost!.id);
});
return () => listener.remove();
}, []);
useEffect(() => {
return () => {
EphemeralStore.setCurrentThreadId('');
};
}, []);
useDidUpdate(() => {
if (!rootPost) {
popTopScreen(componentId);
}
}, [componentId, rootPost]);
const onLayout = useCallback((e: LayoutChangeEvent) => {
setContainerHeight(e.nativeEvent.layout.height);
}, []);
return (
<FreezeScreen>
<SafeAreaView
style={styles.flex}
mode='margin'
edges={edges}
testID='thread.screen'
onLayout={onLayout}
>
<RoundedHeaderContext/>
{Boolean(rootPost?.id) &&
<>
<View style={styles.flex}>
<ThreadPostList
forceQueryAfterAppState={appState}
nativeID={rootPost!.id}
rootPost={rootPost!}
/>
</View>
<PostDraft
channelId={rootPost!.channelId}
scrollViewNativeID={rootPost!.id}
accessoriesContainerID={THREAD_ACCESSORIES_CONTAINER_NATIVE_ID}
rootId={rootPost!.id}
keyboardTracker={postDraftRef}
testID='thread.post_draft'
containerHeight={containerHeight}
isChannelScreen={false}
/>
</>
}
</SafeAreaView>
</FreezeScreen>
);
};
export default Thread;