All files / packages/ui/src/components/Avatar index.tsx

100% Statements 10/10
89.47% Branches 17/19
100% Functions 4/4
100% Lines 9/9

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                                      2x                   6x   6x 6x       6x   6x             6x                           6x                                                       6x                                                                                
import { memo, useMemo } from "react";
import { ImageSourcePropType, Pressable, Text, View } from "react-native";
 
import { CachedImage } from "@repo/ui/components/CachedImage";
import { makeStyles } from "@repo/ui/themes/makeStyles";
import { Theme } from "@repo/ui/themes/types";
import { textStyles } from "@repo/ui/themes/typography";
 
interface Props {
  uri?: string;
  source?: ImageSourcePropType;
  name?: string;
  size?: number;
  selected?: boolean;
  onPress?: () => void;
  showName?: boolean;
  numberOfLines?: number;
}
 
export const Avatar = memo(function Avatar({
  uri,
  source,
  name,
  size = 64,
  selected = false,
  onPress,
  showName = false,
  numberOfLines = 2,
}: Props) {
  const styles = useStyles();
 
  const dynamicAvatarStyle = useMemo(
    () => ({ width: size, height: size, borderRadius: size / 2 }),
    [size],
  );
 
  const imageSource = useMemo(
    () =>
      uri
        ? { uri, priority: "normal" as const, cache: "immutable" as const }
        : null,
    [uri],
  );
 
  const content =
    imageSource || source ? (
      <CachedImage
        source={imageSource ?? source!}
        style={dynamicAvatarStyle}
        resizeMode="cover"
      />
    ) : (
      <View style={[styles.fallback, dynamicAvatarStyle]}>
        <Text style={[styles.initial, { fontSize: size * 0.35 }]}>
          {name?.charAt(0).toUpperCase()}
        </Text>
      </View>
    );
 
  return (
    <Pressable
      onPress={onPress}
      style={styles.wrapper}
      accessibilityRole="button"
      accessibilityLabel={name || "Avatar"}
      accessibilityState={{ selected }}
    >
      {/* Outer Ring */}
      <View
        style={[
          dynamicAvatarStyle,
          styles.ring,
          selected ? styles.ringSelected : styles.ringDefault,
        ]}
      >
        {content}
      </View>
 
      {showName && name && (
        <Text numberOfLines={numberOfLines} style={styles.name}>
          {name}
        </Text>
      )}
    </Pressable>
  );
});
 
const useStyles = makeStyles((theme: Theme) => ({
  wrapper: {
    alignItems: "center",
  },
 
  ring: {
    alignItems: "center",
    justifyContent: "center",
    overflow: "hidden",
  },
 
  ringSelected: {
    borderWidth: 3,
    borderColor: theme.colors.green80,
  },
 
  ringDefault: {
    borderWidth: 3,
    borderColor: theme.colors.white,
  },
 
  fallback: {
    alignItems: "center",
    justifyContent: "center",
    backgroundColor: theme.colors.gray30,
  },
 
  initial: {
    fontWeight: "600",
    color: theme.colors.white,
  },
 
  name: {
    marginTop: 8,
    fontSize: 14,
    textAlign: "center",
    color: theme.colors.text.primary,
    ...textStyles.content.semiBold,
  },
}));