aboutsummaryrefslogtreecommitdiff
path: root/src/components/moments/MomentPostContent.tsx
blob: 4a1f38946f868a79316984db10800a813cbfa2f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
import {useNavigation} from '@react-navigation/native';
import React, {useEffect, useRef, useState} from 'react';
import {Image, StyleSheet, Text, View, ViewProps} from 'react-native';
import {TouchableWithoutFeedback} from 'react-native-gesture-handler';
import Animated, {Easing} from 'react-native-reanimated';
import {useDispatch, useStore} from 'react-redux';
import {getCommentsCount} from '../../services';
import {RootState} from '../../store/rootReducer';
import {MomentTagType, ScreenType, UserType} from '../../types';
import {
  getTimePosted,
  navigateToProfile,
  normalize,
  SCREEN_HEIGHT,
  SCREEN_WIDTH,
} from '../../utils';
import {mentionPartTypes, renderTextWithMentions} from '../../utils/comments';
import {CommentsCount} from '../comments';
import {MomentTags} from '../common';

interface MomentPostContentProps extends ViewProps {
  screenType: ScreenType;
  momentId: string;
  caption: string;
  pathHash: string;
  dateTime: string;
  momentTags: MomentTagType[];
}
const MomentPostContent: React.FC<MomentPostContentProps> = ({
  screenType,
  momentId,
  caption,
  pathHash,
  dateTime,
  style,
  momentTags,
}) => {
  const state: RootState = useStore().getState();
  const navigation = useNavigation();
  const dispatch = useDispatch();
  const [elapsedTime, setElapsedTime] = useState('');
  const [comments_count, setCommentsCount] = useState('');
  const [tags, setTags] = useState<MomentTagType[]>(momentTags);
  const imageRef = useRef(null);
  const [visible, setVisible] = useState(false);

  const [fadeValue, setFadeValue] = useState<Animated.Value<number>>(
    new Animated.Value(0),
  );

  useEffect(() => {
    setTags(momentTags);
  }, [momentTags]);

  useEffect(() => {
    const fetchCommentsCount = async () => {
      const count = await getCommentsCount(momentId, false);
      setCommentsCount(count);
    };
    setElapsedTime(getTimePosted(dateTime));
    fetchCommentsCount();
  }, [dateTime, momentId]);

  useEffect(() => {
    const fade = async () => {
      Animated.timing(fadeValue, {
        toValue: 1,
        duration: 250,
        easing: Easing.linear,
      }).start();
    };
    fade();
  }, [fadeValue]);

  return (
    <View style={[styles.container, style]}>
      <TouchableWithoutFeedback
        onPress={() => {
          setVisible(!visible);
          setFadeValue(new Animated.Value(0));
        }}>
        <Image
          ref={imageRef}
          style={styles.image}
          source={{uri: pathHash}}
          resizeMode={'cover'}
        />
        {tags.length > 0 && (
          <Image
            source={require('../../assets/icons/tag_indicate.png')}
            style={[styles.tagIcon]}
          />
        )}
      </TouchableWithoutFeedback>
      {visible && (
        <Animated.View style={[styles.tapTag, {opacity: fadeValue}]}>
          <MomentTags editing={false} tags={tags} imageRef={imageRef} />
        </Animated.View>
      )}
      <View style={styles.footerContainer}>
        <CommentsCount
          commentsCount={comments_count}
          momentId={momentId}
          screenType={screenType}
        />
        <Text style={styles.text}>{elapsedTime}</Text>
      </View>
      {renderTextWithMentions({
        value: caption,
        styles: styles.captionText,
        partTypes: mentionPartTypes('white'),
        onPress: (user: UserType) =>
          navigateToProfile(state, dispatch, navigation, screenType, user),
      })}
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    height: SCREEN_HEIGHT,
  },
  image: {
    width: SCREEN_WIDTH,
    aspectRatio: 1,
    marginBottom: '3%',
  },
  footerContainer: {
    flexDirection: 'row',
    justifyContent: 'space-between',
    marginLeft: '7%',
    marginRight: '5%',
    marginBottom: '2%',
  },
  text: {
    position: 'relative',
    paddingBottom: '1%',
    paddingTop: '1%',
    marginLeft: '7%',
    marginRight: '2%',
    color: '#ffffff',
    fontWeight: 'bold',
  },
  captionText: {
    position: 'relative',
    paddingBottom: '34%',
    paddingTop: '1%',
    marginLeft: '5%',
    marginRight: '5%',
    color: '#ffffff',
    fontWeight: '500',
    fontSize: normalize(13),
    lineHeight: normalize(15.51),
    letterSpacing: normalize(0.6),
  },
  tapTag: {
    position: 'absolute',
  },
  tagIcon: {
    width: normalize(30),
    height: normalize(30),
    position: 'absolute',
    bottom: '7%',
    left: normalize(20),
  },
});
export default MomentPostContent;