repo_description: Hey is a decentralized and permissionless social media app built with Lens Protocol 🌿
repo_topics: blockchain crypto dapp graphql hacktoberfest lens-protocol nextjs react social-media tailwindcss turborepo typescript vitest wagmi web3
code: import { LENS_NAMESPACE } from "@/data/constants";
  import { Regex } from "@/data/regex";
  import type { AccountFragment } from "@/indexer/generated";
  import formatAddress from "./formatAddress";
  import isAccountDeleted from "./isAccountDeleted";
  
  interface AccountInfo {
    name: string;
    link: string;
    username: string;
  }
  
  const sanitizeDisplayName = (name?: null | string): null | string => {
    if (!name) {
      return null;
    }
  
    return name.replace(Regex.accountNameFilter, " ").trim().replace(/\s+/g, " ");
  };
  
  const UNKNOWN_ACCOUNT: AccountInfo = {
    link: "",
    name: "...",
    username: "..."
  };
  
  const DELETED_ACCOUNT: AccountInfo = {
    link: "",
    name: "Deleted Account",
    username: "deleted"
  };
  
  const getAccount = (account?: AccountFragment): AccountInfo => {
    if (!account) {
      return UNKNOWN_ACCOUNT;
    }
  
    if (isAccountDeleted(account)) {
      return DELETED_ACCOUNT;
    }
  
    const { username, address } = account;
  
    const usernameValue = username?.value;
    const localName = username?.localName;
  
    const usernamePrefix = username ? "" : "#";
    const usernameValueOrAddress =
      (usernameValue?.includes(LENS_NAMESPACE) ? localName : usernameValue) ||
      formatAddress(address);
  
    const link =
      username && usernameValue.includes(LENS_NAMESPACE)
        ? `/u/${localName}`
        : `/account/${address}`;
  
    return {
      link,
      name: sanitizeDisplayName(account.metadata?.name) || usernameValueOrAddress,
      username: `${usernamePrefix}${usernameValueOrAddress}`
    };
  };
  
  export default getAccount;
  