aboutsummaryrefslogtreecommitdiff
path: root/src/components/profile/FriendsCount.tsx
blob: a733823fb6d3f11d0dc61fe94b421c1d6a78043d (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
import React from 'react';
import {View, Text, StyleSheet, ViewProps} from 'react-native';
import {TouchableOpacity} from 'react-native-gesture-handler';
import {useNavigation} from '@react-navigation/native';
import {RootState} from '../../store/rootReducer';
import {useSelector, useStore} from 'react-redux';
import {ScreenType} from '../../types';
import {canViewProfile, canViewProfileFoo, normalize} from '../../utils';

interface FriendsCountProps extends ViewProps {
  userXId: string | undefined;
  screenType: ScreenType;
}

const FriendsCount: React.FC<FriendsCountProps> = ({
  style,
  userXId,
  screenType,
}) => {
  const {friends} = useSelector((state: RootState) =>
    userXId ? state.userX[screenType][userXId] : state.friends,
  );
  const count = friends ? friends.length : 0;

  const displayedCount: string =
    count < 5e3
      ? `${count}`
      : count < 1e5
      ? `${(count / 1e3).toFixed(1)}k`
      : count < 1e6
      ? `${(count / 1e3).toFixed(0)}k`
      : `${count / 1e6}m`;

  const navigation = useNavigation();
  const state: RootState = useStore().getState();

  return (
    <TouchableOpacity
      onPress={() =>
        navigation.navigate('FriendsListScreen', {
          userXId,
          screenType,
        })
      }
      disabled={
        !canViewProfile(state, userXId, screenType) || friends.length === 0
      }>
      <View style={[styles.container, style]}>
        <Text style={styles.count}>{displayedCount}</Text>
        <Text style={styles.label}>Friends</Text>
      </View>
    </TouchableOpacity>
  );
};

const styles = StyleSheet.create({
  container: {
    alignItems: 'center',
  },
  count: {
    fontWeight: '700',
    fontSize: normalize(14),
  },
  label: {
    fontWeight: '500',
    fontSize: normalize(14),
  },
});

export default FriendsCount;