All files / app/components/ProfileLink ProfileLink.tsx

100% Statements 11/11
71.42% Branches 5/7
100% Functions 2/2
100% Lines 11/11

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 6125x 25x 25x   25x 25x                       567x                 567x 567x   567x               1x 1x                                            
import { ButtonBase } from "@mui/material";
import StyledLink from "components/StyledLink";
import { useProfileSheet } from "features/profile/ProfileSheetContext";
import { MouseEvent, ReactNode } from "react";
import { routeToUser } from "routes";
import { useIsNativeEmbed } from "utils/nativeLink";
 
interface ProfileLinkProps {
  userId?: number;
  username: string;
  children: ReactNode;
  className?: string;
  openInNewTab?: boolean;
  "aria-label"?: string;
  style?: React.CSSProperties;
}
 
export default function ProfileLink({
  userId,
  username,
  children,
  className,
  openInNewTab,
  style,
  "aria-label": ariaLabel,
}: ProfileLinkProps) {
  const isNativeEmbed = useIsNativeEmbed();
  const { openProfileSheet } = useProfileSheet();
 
  if (isNativeEmbed && userId !== undefined) {
    return (
      <ButtonBase
        component="span"
        className={className}
        style={style}
        aria-label={ariaLabel}
        onClick={(e: MouseEvent) => {
          e.stopPropagation();
          openProfileSheet(userId);
        }}
        sx={{ cursor: "pointer", position: "static" }}
      >
        {children}
      </ButtonBase>
    );
  }
 
  return (
    <StyledLink
      href={routeToUser(username)}
      className={className}
      style={style}
      aria-label={ariaLabel}
      target={openInNewTab ? "_blank" : undefined}
      rel={openInNewTab ? "noopener noreferrer" : undefined}
    >
      {children}
    </StyledLink>
  );
}