index.tsx 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import { useEffect, useRef } from 'react';
  2. import { View, Image, Text, TouchableOpacity, Platform } from 'react-native';
  3. import { Colors } from 'src/theme';
  4. import MapLibreGL, { PointAnnotationRef } from '@maplibre/maplibre-react-native';
  5. import { useNavigation } from '@react-navigation/native';
  6. import { NAVIGATION_PAGES } from 'src/types';
  7. import moment from 'moment';
  8. import { styles } from '../MarkerItem/styles';
  9. const UserItem = ({ marker }: { marker: any }) => {
  10. const calloutUserRef = useRef<PointAnnotationRef>(null);
  11. const navigation = useNavigation();
  12. useEffect(() => {
  13. if (Platform.OS === 'android') {
  14. calloutUserRef.current?.refresh();
  15. }
  16. }, [marker]);
  17. const formatDateToLocalTime = (utcDate: string) => {
  18. const date = moment.utc(utcDate).local();
  19. const now = moment();
  20. if (now.diff(date, 'days') === 1) {
  21. return 'yesterday';
  22. }
  23. if (now.diff(date, 'weeks') === 1) {
  24. return 'last week';
  25. }
  26. return date.fromNow();
  27. };
  28. return (
  29. <>
  30. {Platform.OS === 'ios' ? (
  31. <MapLibreGL.PointAnnotation
  32. id="selected_user_callout"
  33. coordinate={marker.coordinates}
  34. anchor={{ x: 0.5, y: 1 }}
  35. >
  36. <View style={styles.customView}>
  37. <View style={styles.calloutContainer}>
  38. <View style={[styles.calloutImageContainer, { borderColor: Colors.WHITE }]}>
  39. <Image
  40. source={{ uri: marker.avatar.uri }}
  41. style={styles.userImage}
  42. resizeMode="contain"
  43. />
  44. </View>
  45. <View style={styles.calloutTextContainer}>
  46. <View style={{ flexDirection: 'row', alignItems: 'center', gap: 8 }}>
  47. <Image source={{ uri: marker.flag.uri }} style={styles.flag} resizeMode="cover" />
  48. <Text style={styles.seriesName}>
  49. {marker.first_name + ' ' + marker.last_name}
  50. </Text>
  51. </View>
  52. <Text style={styles.markerName}>
  53. Last seen: {formatDateToLocalTime(marker.last_seen)}
  54. </Text>
  55. </View>
  56. <TouchableOpacity
  57. style={[styles.calloutButton]}
  58. onPress={() =>
  59. navigation.navigate(
  60. ...([NAVIGATION_PAGES.PUBLIC_PROFILE_VIEW, { userId: marker.id }] as never)
  61. )
  62. }
  63. >
  64. <Text style={styles.calloutButtonText}>See profile</Text>
  65. </TouchableOpacity>
  66. </View>
  67. </View>
  68. </MapLibreGL.PointAnnotation>
  69. ) : (
  70. <MapLibreGL.PointAnnotation
  71. id="selected_user_callout"
  72. coordinate={marker.coordinates}
  73. anchor={{ x: 0.5, y: 1.1 }}
  74. onSelected={() =>
  75. navigation.navigate(
  76. ...([NAVIGATION_PAGES.PUBLIC_PROFILE_VIEW, { userId: marker.id }] as never)
  77. )
  78. }
  79. selected={true}
  80. ref={calloutUserRef}
  81. >
  82. <View style={styles.customView}>
  83. <View style={styles.calloutContainer}>
  84. <View style={styles.calloutImageContainer}>
  85. <Image
  86. source={{ uri: marker.avatar.uri }}
  87. style={styles.userImage}
  88. resizeMode="contain"
  89. />
  90. </View>
  91. <View style={styles.calloutTextContainer}>
  92. <View style={{ flexDirection: 'row', alignItems: 'center', gap: 8 }}>
  93. <Image source={{ uri: marker.flag.uri }} style={styles.flag} resizeMode="cover" />
  94. <Text style={styles.seriesName}>
  95. {marker.first_name + ' ' + marker.last_name}
  96. </Text>
  97. </View>
  98. <Text style={styles.markerName}>
  99. Last seen: {formatDateToLocalTime(marker.last_seen)}
  100. </Text>
  101. </View>
  102. <TouchableOpacity
  103. style={[styles.calloutButton]}
  104. onPress={() =>
  105. navigation.navigate(
  106. ...([NAVIGATION_PAGES.PUBLIC_PROFILE_VIEW, { userId: marker.id }] as never)
  107. )
  108. }
  109. >
  110. <Text style={styles.calloutButtonText}>See profile</Text>
  111. </TouchableOpacity>
  112. </View>
  113. </View>
  114. </MapLibreGL.PointAnnotation>
  115. )}
  116. </>
  117. );
  118. };
  119. export default UserItem;