Skip to content

The open alternative to Salesforce, designed for AI.

1.0 KB
Raw Blame
import { SocialLink } from '@/ui/field/display/components/SocialLink/SocialLink';
import { isNonEmptyString } from '@sniptt/guards';
import { getSafeUrl } from 'twenty-shared/utils';
import { RoundedLink } from '@/ui/navigation/link/components/RoundedLink/RoundedLink';
import { checkUrlType } from '@/ui/field/display/utils/checkUrlType';
import { isSocialLinkType } from '@/ui/field/display/utils/isSocialLinkType';

type LinkDisplayProps = {
  value: { url: string; label?: string | null };
};

export const LinkDisplay = ({ value }: LinkDisplayProps) => {
  const url = value.url;

  if (!isNonEmptyString(url)) {
    return <></>;
  }

  const absoluteUrl = getSafeUrl(url) ?? '';

  const displayedValue = isNonEmptyString(value.label)
    ? value.label
    : url?.replace(/^http[s]?:\/\/(?:[w]+\.)?/gm, '').replace(/^[w]+\./gm, '');

  const type = checkUrlType(absoluteUrl);

  if (isSocialLinkType(type)) {
    return <SocialLink href={absoluteUrl} type={type} label={value.label} />;
  }

  return <RoundedLink href={absoluteUrl} label={displayedValue} />;
};