2024-01-25 14:11:07 +00:00
|
|
|
import React, { ReactElement } from 'react';
|
|
|
|
import { View, FlatList } from 'react-native';
|
2024-01-26 20:40:18 +00:00
|
|
|
import { useSafeAreaInsets } from 'react-native-safe-area-context';
|
2024-01-25 14:11:07 +00:00
|
|
|
|
|
|
|
import { useAutocompleteParams, useKeyboardHeight, useTrackingViewHeight } from '../../context';
|
|
|
|
import { AutocompleteItem } from './AutocompleteItem';
|
|
|
|
import { useAutocomplete } from '../../hooks';
|
|
|
|
import { IAutocompleteItemProps } from '../../interfaces';
|
|
|
|
import { AutocompletePreview } from './AutocompletePreview';
|
|
|
|
import { useRoomContext } from '../../../../views/RoomView/context';
|
2024-01-31 13:57:17 +00:00
|
|
|
import { useStyle } from './styles';
|
2024-01-25 14:11:07 +00:00
|
|
|
|
|
|
|
export const Autocomplete = ({ onPress }: { onPress: IAutocompleteItemProps['onPress'] }): ReactElement | null => {
|
|
|
|
const { rid } = useRoomContext();
|
|
|
|
const trackingViewHeight = useTrackingViewHeight();
|
|
|
|
const keyboardHeight = useKeyboardHeight();
|
2024-01-26 20:40:18 +00:00
|
|
|
const { bottom } = useSafeAreaInsets();
|
2024-01-25 14:11:07 +00:00
|
|
|
const { text, type, params } = useAutocompleteParams();
|
|
|
|
const items = useAutocomplete({
|
|
|
|
rid,
|
|
|
|
text,
|
|
|
|
type,
|
|
|
|
commandParams: params
|
|
|
|
});
|
|
|
|
const [styles, colors] = useStyle();
|
2024-01-31 13:57:17 +00:00
|
|
|
const viewBottom = trackingViewHeight + keyboardHeight + (keyboardHeight > 0 ? 0 : bottom) - 4;
|
2024-01-25 14:11:07 +00:00
|
|
|
|
|
|
|
if (items.length === 0 || !type) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type !== '/preview') {
|
|
|
|
return (
|
|
|
|
<View
|
|
|
|
style={[
|
|
|
|
styles.root,
|
|
|
|
{
|
2024-01-26 20:40:18 +00:00
|
|
|
bottom: viewBottom
|
2024-01-25 14:11:07 +00:00
|
|
|
}
|
|
|
|
]}
|
|
|
|
>
|
|
|
|
<FlatList
|
|
|
|
contentContainerStyle={styles.listContentContainer}
|
|
|
|
data={items}
|
|
|
|
renderItem={({ item }) => <AutocompleteItem item={item} onPress={onPress} />}
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
testID='autocomplete'
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === '/preview') {
|
|
|
|
return (
|
2024-01-26 20:40:18 +00:00
|
|
|
<View style={[styles.root, { backgroundColor: colors.surfaceLight, bottom: viewBottom }]}>
|
2024-01-25 14:11:07 +00:00
|
|
|
<FlatList
|
|
|
|
contentContainerStyle={styles.listContentContainer}
|
|
|
|
style={styles.list}
|
|
|
|
horizontal
|
|
|
|
data={items}
|
|
|
|
renderItem={({ item }) => <AutocompletePreview item={item} onPress={onPress} />}
|
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
testID='autocomplete'
|
|
|
|
/>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
};
|