mirror of
https://github.com/lukevella/rallly.git
synced 2025-05-03 12:16:04 +02:00
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import * as React from "react";
|
|
import ReactLinkify from "react-linkify";
|
|
|
|
import Tooltip from "../tooltip";
|
|
|
|
export const truncateLink = (href: string, text: string, key: number) => {
|
|
const textWithoutProtocol = text.replace(/^https?:\/\//i, "");
|
|
const beginningOfPath = textWithoutProtocol.indexOf("/");
|
|
let finalText = textWithoutProtocol;
|
|
if (beginningOfPath !== -1) {
|
|
finalText = textWithoutProtocol.substring(0, beginningOfPath + 15);
|
|
}
|
|
if (finalText.length === textWithoutProtocol.length) {
|
|
return (
|
|
<a key={key} href={href}>
|
|
{finalText}
|
|
</a>
|
|
);
|
|
} else {
|
|
finalText += "…";
|
|
return (
|
|
<Tooltip
|
|
key={key}
|
|
content={
|
|
<div className="max-w-md break-all font-mono text-xs">{href}</div>
|
|
}
|
|
>
|
|
<a href={href}>{finalText}</a>
|
|
</Tooltip>
|
|
);
|
|
}
|
|
};
|
|
|
|
const TruncatedLinkify: React.VoidFunctionComponent<{
|
|
children?: React.ReactNode;
|
|
}> = ({ children }) => {
|
|
return (
|
|
<ReactLinkify componentDecorator={truncateLink}>{children}</ReactLinkify>
|
|
);
|
|
};
|
|
|
|
export default TruncatedLinkify;
|