All files / atoms/Post/mobile Post.native.tsx

92.59% Statements 25/27
82.05% Branches 32/39
100% Functions 7/7
100% Lines 22/22

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199                1x                                 14x 14x 14x   14x 14x 1x 1x     14x 2x     14x 11x                         14x 1x                   14x 1x   1x             1x                               14x 2x   1x                         14x                                                                                                                                                                                        
import { cn } from '@sb/libs/utils';
import { FC, ReactNode } from 'react';
import { ResizeMode, Video } from 'expo-av';
import { PostNativeProps } from '../Post.types';
import { Image, Text, TouchableOpacity, View } from 'react-native';
import { useThemeColors } from '@sb/hooks/Utilities/useThemeColors';
import { ImageIcon, MediaError, Sponsor1, Sponsor2, Sponsor3, Sponsor4 } from '@sb/assets';
 
export const Post: FC<PostNativeProps> = ({
  id,
  title,
  author,
  onPress,
  category,
  hasError,
  timestamp,
  className,
  redirectUrl,
  description,
  thumbnailUrl,
  mediaAspectRatio,
  truncate = false,
  loadingAnimation = true,
  showCategoryIndicator = false,
}) => {
  const { themedColors } = useThemeColors();
  const isVideo = thumbnailUrl.match(/\.(mp4|webm|ogg)$/i);
  const isImage = thumbnailUrl.match(/\.(jpg|png|jpeg|gif)$/i);
 
  const getDescriptionStyle = (): string => {
    if (description.length < 50) return 'text-xl';
    Iif (description.length < 120) return 'text-lg';
    return 'text-base';
  };
 
  const handlePress = () => {
    Eif (redirectUrl) onPress?.();
  };
 
  const MediaLoadingSkeleton = (): ReactNode => {
    return (
      <View
        style={{
          backgroundColor: themedColors.colorSecondary,
        }}
        accessibilityLabel="Post Media Skeleton Loader"
        className="absolute left-0 top-0 flex h-full w-full animate-pulse items-center justify-center"
      >
        <Image source={ImageIcon} className="size-16 animate-pulse" />
      </View>
    );
  };
 
  const MediaErrorView = (): ReactNode => {
    return (
      <View
        accessibilityLabel="Post Media Error Placeholder"
        className="h-full w-full items-center justify-center"
      >
        <Image source={MediaError} resizeMode="contain" className="size-16" />
      </View>
    );
  };
 
  const RenderImage = (): ReactNode => {
    Iif (hasError || !isImage) return <MediaErrorView />;
 
    const IMAGE_MAP: Record<string, any> = {
      '/img/Post/sponsor1.png': Sponsor1,
      '/img/Post/sponsor2.png': Sponsor2,
      '/img/Post/sponsor3.png': Sponsor3,
      '/img/Post/sponsor4.png': Sponsor4,
    };
 
    return (
      <Image
        // TODO: Remove IMAGE_MAP and use {uri: thumbnailUrl} syntax
        // source={{ uri: thumbnailUrl }}
        source={IMAGE_MAP[thumbnailUrl]}
        accessible={true}
        resizeMode="cover"
        fadeDuration={500}
        className="h-full w-full"
        tintColor="bg-transparent"
        alt={`${title ? title : 'Post'} Image thumbnail`}
        style={{ width: '100%', height: '100%', aspectRatio: mediaAspectRatio }}
        accessibilityLabel={title ? `${title} Post Image thumbnail` : 'Post Image thumbnail'}
      />
    );
  };
  const RenderVideo = (): ReactNode => {
    if (hasError || !isVideo) return <MediaErrorView />;
 
    return (
      <Video
        isMuted
        isLooping
        shouldPlay
        resizeMode={ResizeMode.COVER}
        source={{ uri: thumbnailUrl }}
        style={{ width: '100%', height: '100%', aspectRatio: mediaAspectRatio }}
        accessibilityLabel={title ? `${title} Post Video thumbnail` : 'Post Video thumbnail'}
      />
    );
  };
 
  return (
    <TouchableOpacity
      testID={`${id}-post`}
      role="combobox"
      activeOpacity={0.9}
      onPress={handlePress}
      accessibilityRole="combobox"
      style={{
        backgroundColor: themedColors.colorLoader_Text,
      }}
      accessibilityLabel={`${id}-title ${id}-description`}
      className={cn('w-full overflow-hidden rounded-md', className)}
    >
      <View className="relative h-52 min-w-full" accessible={false}>
        {showCategoryIndicator && (
          <View
            style={{
              backgroundColor: themedColors.colorStatus_Closed,
            }}
            className="absolute right-2 top-2 z-10 rounded-lg px-2 py-1 opacity-90"
          >
            <Text
              style={{
                color: themedColors.colorText_Main,
              }}
              className="text-xs font-semibold uppercase"
            >
              {category}
            </Text>
          </View>
        )}
 
        {loadingAnimation ? <MediaLoadingSkeleton /> : isImage ? <RenderImage /> : <RenderVideo />}
      </View>
 
      <View className="bg-transparent px-3 py-2">
        <View accessibilityLabel={`${title ? title + ' - ' : ''}${description}`}>
          {title && (
            <Text
              style={{
                color: themedColors.colorText_Active,
              }}
              nativeID={`${id}-title`}
              className="font-Bold text-xl"
            >
              {title}
            </Text>
          )}
          <Text
            nativeID={`${id}-description`}
            numberOfLines={truncate ? 2 : undefined}
            style={{
              color: themedColors.colorStatic_Button_Text,
            }}
            className={cn('my-3 font-normal', getDescriptionStyle())}
          >
            {description}
          </Text>
        </View>
 
        {author && (
          <View
            accessibilityRole="text"
            className="mt-1 flex flex-row items-center justify-between"
          >
            <Text
              style={{
                color: themedColors.colorStatus_Closed,
              }}
              className="text-xs font-normal"
              accessibilityLabel={`Author: ${author}`}
            >
              By: {author}
            </Text>
            {timestamp && (
              <Text
                accessibilityRole="text"
                className="text-xs font-normal"
                style={{
                  color: themedColors.colorStatus_Closed,
                }}
                accessibilityLabel={`Posted on ${new Date(timestamp).toLocaleString()}`}
              >
                {new Date(timestamp).toLocaleString()}
              </Text>
            )}
          </View>
        )}
      </View>
    </TouchableOpacity>
  );
};