Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Add smart truncate algorithm

Steven Tey44692663688d
diff --git a/apps/web/emails/clicks-summary.tsx b/apps/web/emails/clicks-summary.tsx
index fe9e130dc2..78e77d13c8 100644
--- a/apps/web/emails/clicks-summary.tsx
+++ b/apps/web/emails/clicks-summary.tsx
@@ -1,4 +1,4 @@
-import { DUB_WORDMARK, nFormatter, truncate } from "@dub/utils";
+import { DUB_WORDMARK, nFormatter, smartTruncate } from "@dub/utils";
import {
Body,
Column,
@@ -28,15 +28,15 @@ export default function ClicksSummary({
createdLinks = 25,
topLinks = [
{
- link: "acme.com/sales",
- clicks: 2187,
+ link: "acmesuperlongdomain.com/instagram",
+ clicks: 1820,
},
{
- link: "acme.com/instagram",
- clicks: 1820,
+ link: "acmesuperlongdomain.com/super-long-path-that-is-way-too-long-and-should-be-truncated",
+ clicks: 2187,
},
{
- link: "acme.com/facebook",
+ link: "getacme.link",
clicks: 1552,
},
{
@@ -44,7 +44,7 @@ export default function ClicksSummary({
clicks: 1229,
},
{
- link: "acme.com/linkedin",
+ link: "acme.com/linkedin/more/path",
clicks: 1055,
},
],
@@ -124,21 +124,33 @@ export default function ClicksSummary({
Clicks
</Column>
</Row>
- {topLinks.map(({ link, clicks }, index) => (
- <div key={index}>
- <Row>
- <Column align="left" className="text-sm font-medium">
- {truncate(link, 30)}
- </Column>
- <Column align="right" className="text-sm text-gray-600">
- {nFormatter(clicks)}
- </Column>
- </Row>
- {index !== topLinks.length - 1 && (
- <Hr className="my-2 w-full border border-gray-200" />
- )}
- </div>
- ))}
+ {topLinks.map(({ link, clicks }, index) => {
+ const [domain, ...pathParts] = link.split("/");
+ const path = pathParts.join("/") || "_root";
+ return (
+ <div key={index}>
+ <Row>
+ <Column align="left">
+ <Link
+ href={`https://app.dub.co/${workspaceSlug}/analytics?domain=${domain}&key=${path}`}
+ className="text-sm font-medium text-black underline"
+ >
+ {smartTruncate(link, 36)}↗
+ </Link>
+ </Column>
+ <Column
+ align="right"
+ className="text-sm text-gray-600"
+ >
+ {nFormatter(clicks)}
+ </Column>
+ </Row>
+ {index !== topLinks.length - 1 && (
+ <Hr className="my-2 w-full border border-gray-200" />
+ )}
+ </div>
+ );
+ })}
</Section>
</>
)}
diff --git a/packages/utils/src/functions/index.ts b/packages/utils/src/functions/index.ts
index 0eaa2f238e..ea4f77b6a4 100644
--- a/packages/utils/src/functions/index.ts
+++ b/packages/utils/src/functions/index.ts
@@ -19,6 +19,7 @@ export * from "./nanoid";
export * from "./nformatter";
export * from "./punycode";
export * from "./random-value";
+export * from "./smart-truncate";
export * from "./stable-sort";
export * from "./time-ago";
export * from "./trim";
diff --git a/packages/utils/src/functions/smart-truncate.ts b/packages/utils/src/functions/smart-truncate.ts
new file mode 100644
index 0000000000..6d790b0212
--- /dev/null
+++ b/packages/utils/src/functions/smart-truncate.ts
@@ -0,0 +1,53 @@
+/*
+ smart truncation algorithm that dynamically adjusts based on the length of the domain and the path
+ it gives priority to the path and truncates the domain if it's too long
+ at minimum the domain should still show 8 characters though
+*/
+
+import { truncate } from "./truncate";
+
+// Function to truncate domain while preserving TLD and 2 characters before the dot
+const truncateDomain = (domain: string, maxLength: number): string => {
+ const parts = domain.split(".");
+ const tld = parts.pop() || "";
+ const rest = parts.join(".");
+ const minRestLength = 3;
+
+ if (rest.length + tld.length + 1 <= maxLength) {
+ return `${rest}.${tld}`;
+ }
+
+ if (maxLength <= minRestLength + tld.length + 1) {
+ return `${rest.slice(0, minRestLength)}.${tld}`;
+ }
+
+ const truncatedRest = truncate(rest, maxLength - tld.length - 1)!;
+ return `${truncatedRest}${rest.slice(-2)}.${tld}`;
+};
+
+export const smartTruncate = (link: string, length: number): string => {
+ if (link.length <= length) {
+ return link;
+ }
+
+ const [domain, ...pathParts] = link.split("/");
+ const path = pathParts.join("/");
+ const minDomainLength = 12;
+
+ // Calculate maximum domain length
+ const maxDomainLength = Math.max(
+ minDomainLength,
+ Math.floor(length / 2), // Allow up to half of total length for domain
+ );
+
+ // Truncate domain if necessary, preserving TLD
+ const truncatedDomain = truncateDomain(domain, maxDomainLength);
+
+ // Calculate remaining length for path
+ const remainingLength = Math.max(0, length - truncatedDomain.length - 1);
+
+ // Truncate path
+ const truncatedPath = truncate(path, remainingLength);
+
+ return `${truncatedDomain}/${truncatedPath}`;
+};