Hey is a decentralized and permissionless social media app built with Lens Protocol 馃尶
1import { LENS_NAMESPACE } from "@hey/data/constants";
2import { Regex } from "@hey/data/regex";
3import type { AccountFragment } from "@hey/indexer";
4import formatAddress from "./formatAddress";
5import isAccountDeleted from "./isAccountDeleted";
6
7interface AccountInfo {
8 name: string;
9 link: string;
10 username: string;
11}
12
13const sanitizeDisplayName = (name?: null | string): null | string => {
14 if (!name) {
15 return null;
16 }
17
18 return name.replace(Regex.accountNameFilter, " ").trim().replace(/\s+/g, " ");
19};
20
21const UNKNOWN_ACCOUNT: AccountInfo = {
22 link: "",
23 name: "...",
24 username: "..."
25};
26
27const DELETED_ACCOUNT: AccountInfo = {
28 link: "",
29 name: "Deleted Account",
30 username: "deleted"
31};
32
33const getAccount = (account?: AccountFragment): AccountInfo => {
34 if (!account) {
35 return UNKNOWN_ACCOUNT;
36 }
37
38 if (isAccountDeleted(account)) {
39 return DELETED_ACCOUNT;
40 }
41
42 const { username, address } = account;
43
44 const usernameValue = username?.value;
45 const localName = username?.localName;
46
47 const usernamePrefix = username ? "" : "#";
48 const usernameValueOrAddress =
49 (usernameValue?.includes(LENS_NAMESPACE) ? localName : usernameValue) ||
50 formatAddress(address);
51
52 const link =
53 username && usernameValue.includes(LENS_NAMESPACE)
54 ? `/u/${localName}`
55 : `/account/${address}`;
56
57 return {
58 link,
59 name: sanitizeDisplayName(account.metadata?.name) || usernameValueOrAddress,
60 username: `${usernamePrefix}${usernameValueOrAddress}`
61 };
62};
63
64export default getAccount;