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.

Merge branch 'main' into feat/improve-api

Steven Teyed2e32f4cb3c
diff --git a/apps/web/app/api/cron/links/verify/route.ts b/apps/web/app/api/cron/links/verify/route.ts
deleted file mode 100644
index 097249c53f..0000000000
--- a/apps/web/app/api/cron/links/verify/route.ts
+++ /dev/null
@@ -1 +0,0 @@
-export { POST } from "../event/route";
diff --git a/apps/web/app/api/cron/verify/route.ts b/apps/web/app/api/cron/verify/route.ts
deleted file mode 100644
index f07a9c06b5..0000000000
--- a/apps/web/app/api/cron/verify/route.ts
+++ /dev/null
@@ -1 +0,0 @@
-export { POST } from "../links/event/route";
diff --git a/apps/web/lib/analytics.ts b/apps/web/lib/analytics.ts
index 6f82badbf5..57f885ca3e 100644
--- a/apps/web/lib/analytics.ts
+++ b/apps/web/lib/analytics.ts
@@ -116,6 +116,7 @@ export const getAnalytics = async ({
// get all-time clicks count if:
// 1. endpoint is /clicks
// 2. interval is not defined
+ // 3. linkId is defined
if (endpoint === "clicks" && !interval && linkId) {
let response = await conn.execute(
"SELECT clicks FROM Link WHERE `id` = ?",
diff --git a/apps/web/lib/api/links.ts b/apps/web/lib/api/links.ts
index 7005e8439f..973c392091 100644
--- a/apps/web/lib/api/links.ts
+++ b/apps/web/lib/api/links.ts
@@ -220,13 +220,19 @@ export async function checkIfKeyExists(domain: string, key: string) {
return !!link;
}
-export async function getRandomKey(domain: string): Promise<string> {
+export async function getRandomKey(
+ domain: string,
+ prefix?: string,
+): Promise<string> {
/* recursively get random key till it gets one that's available */
- const key = nanoid();
+ let key = nanoid();
+ if (prefix) {
+ key = `${prefix.replace(/^\/|\/$/g, "")}/${key}`;
+ }
const response = await checkIfKeyExists(domain, key);
if (response) {
// by the off chance that key already exists
- return getRandomKey(domain);
+ return getRandomKey(domain, prefix);
} else {
return key;
}
@@ -302,11 +308,11 @@ export async function processLink({
};
}
// can't use `/` in key on free plan
- if (key?.includes("/")) {
+ if (key?.includes("/") || payload["prefix"]) {
return {
link: payload,
error:
- "Key cannot contain '/'. You can only use this on a Pro plan and above. Upgrade to Pro to use this feature.",
+ "Key cannot contain '/'. You can only use key prefixes on a Pro plan and above. Upgrade to Pro to use this feature.",
status: 422,
code: "unprocessable_entity",
};
@@ -366,7 +372,7 @@ export async function processLink({
}
if (!key) {
- key = await getRandomKey(domain);
+ key = await getRandomKey(domain, payload["prefix"]);
}
if (bulk) {
@@ -460,6 +466,7 @@ export async function processLink({
// remove polyfill attributes from payload
delete payload["shortLink"];
delete payload["qrCode"];
+ delete payload["prefix"];
return {
link: {
diff --git a/apps/web/ui/links/link-card.tsx b/apps/web/ui/links/link-card.tsx
index defa545a4e..277f20a617 100644
--- a/apps/web/ui/links/link-card.tsx
+++ b/apps/web/ui/links/link-card.tsx
@@ -45,6 +45,8 @@ import {
QrCode,
TimerOff,
Lock,
+ Copy,
+ CopyCheck,
} from "lucide-react";
import Link from "next/link";
import { useParams } from "next/navigation";
@@ -187,6 +189,15 @@ export default function LinkCard({
};
}, [handlClickOnLinkCard]);
+ const [copiedLinkId, setCopiedLinkId] = useState(false);
+
+ const copyLinkId = () => {
+ navigator.clipboard.writeText(id);
+ setCopiedLinkId(true);
+ toast.success("Link ID copied!");
+ setTimeout(() => setCopiedLinkId(false), 3000);
+ };
+
const onKeyDown = (e: any) => {
// only run shortcut logic if:
// - usage is not exceeded
@@ -195,7 +206,7 @@ export default function LinkCard({
// - there is no existing modal backdrop
if (
(selected || openPopover) &&
- ["e", "d", "q", "a", "t", "x"].includes(e.key)
+ ["e", "d", "q", "a", "t", "i", "x"].includes(e.key)
) {
setSelected(false);
e.preventDefault();
@@ -217,6 +228,9 @@ export default function LinkCard({
setShowTransferLinkModal(true);
}
break;
+ case "i":
+ copyLinkId();
+ break;
case "x":
setShowDeleteLinkModal(true);
break;
@@ -493,25 +507,32 @@ export default function LinkCard({
shortcut="A"
className="h-9 px-2 font-medium"
/>
+ {isDubDomain(domain) && (
+ <Button
+ text="Transfer"
+ variant="outline"
+ onClick={() => {
+ setOpenPopover(false);
+ setShowTransferLinkModal(true);
+ }}
+ icon={<FolderInput className="h-4 w-4" />}
+ shortcut="T"
+ className="h-9 px-2 font-medium"
+ />
+ )}
<Button
- text="Transfer"
+ text="Copy Link ID"
variant="outline"
- onClick={() => {
- setOpenPopover(false);
- setShowTransferLinkModal(true);
- }}
- icon={<FolderInput className="h-4 w-4" />}
- shortcut="T"
+ onClick={() => copyLinkId()}
+ icon={
+ copiedLinkId ? (
+ <CopyCheck className="h-4 w-4" />
+ ) : (
+ <Copy className="h-4 w-4" />
+ )
+ }
+ shortcut="I"
className="h-9 px-2 font-medium"
- {...(!isDubDomain(domain) && {
- disabledTooltip: (
- <SimpleTooltipContent
- title="You cannot transfer custom domain links between projects."
- cta="Learn more."
- href={`${HOME_DOMAIN}/help/article/how-to-transfer-links`}
- />
- ),
- })}
/>
<Button
text="Delete"
diff --git a/apps/web/ui/modals/add-edit-link-modal/index.tsx b/apps/web/ui/modals/add-edit-link-modal/index.tsx
index 72d29e1ce1..2b852b80bd 100644
--- a/apps/web/ui/modals/add-edit-link-modal/index.tsx
+++ b/apps/web/ui/modals/add-edit-link-modal/index.tsx
@@ -88,16 +88,18 @@ function AddEditLinkModal({
} = useDomains();
const [data, setData] = useState<LinkWithTagsProps>(
- props ||
- duplicateProps || {
- ...(DEFAULT_LINK_PROPS as LinkWithTagsProps),
- domain: primaryDomain,
- key: "",
- url: "",
- tags: [],
- },
+ props || duplicateProps || DEFAULT_LINK_PROPS,
);
+ useEffect(() => {
+ if (primaryDomain) {
+ setData((prev) => ({
+ ...prev,
+ domain: primaryDomain,
+ }));
+ }
+ }, [primaryDomain]);
+
const { domain, key, url, password, proxy } = data;
const [debouncedKey] = useDebounce(key, 500);
diff --git a/packages/utils/src/constants/misc.ts b/packages/utils/src/constants/misc.ts
index f2fc9a2e0f..7484b26154 100644
--- a/packages/utils/src/constants/misc.ts
+++ b/packages/utils/src/constants/misc.ts
@@ -1,4 +1,4 @@
-export const DEFAULT_LINK_PROPS = {
+export const DEFAULT_LINK_PROPS: any = {
key: "",
url: "",
domain: "",
@@ -17,6 +17,7 @@ export const DEFAULT_LINK_PROPS = {
userId: "",
proxy: false,
+ tags: [],
};
export const GOOGLE_FAVICON_URL =