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 domains

Steven Tey08210eb54a1e
diff --cc components/app/links/link-card.tsx
index 48bb57cb5e,f920eb5d04..025a920c8d
--- a/components/app/links/link-card.tsx
+++ b/components/app/links/link-card.tsx
@@@ -51,9 -51,9 +51,9 @@@ export default function LinkCard({ prop
const { data: clicks } = useSWR<number>(
isVisible &&
- (domain
- ? `/api/projects/${slug}/domains/${domain}/links/${key}/stats/clicks`
- : `/api/links/${key}/stats/clicks`),
+ (slug
- ? `/api/projects/${slug}/links/${key}/clicks?domain=${domain}`
- : `/api/edge/links/${key}/clicks`),
++ ? `/api/projects/${slug}/links/${key}/stats/clicks?domain=${domain}`
++ : `/api/edge/links/${key}/stats/clicks`),
fetcher,
{
fallbackData: props.clicks,
diff --cc components/app/modals/add-edit-link-modal/index.tsx
index 02c1097bd4,027f79ccd8..deb6729e14
--- a/components/app/modals/add-edit-link-modal/index.tsx
+++ b/components/app/modals/add-edit-link-modal/index.tsx
@@@ -38,7 -38,7 +38,8 @@@ import IOSSection from "./ios-section"
import Preview from "./preview";
import AndroidSection from "./android-section";
import { DEFAULT_LINK_PROPS } from "@/lib/constants";
+import useDomains from "@/lib/swr/use-domains";
+ import { toast } from "react-hot-toast";
function AddEditLinkModal({
showAddEditLinkModal,
@@@ -58,11 -58,10 +59,11 @@@
const [keyExistsError, setKeyExistsError] = useState(false);
const [urlError, setUrlError] = useState(false);
- const [generatingSlug, setGeneratingSlug] = useState(false);
+ const [generatingKey, setGeneratingKey] = useState(false);
const [saving, setSaving] = useState(false);
+ const { domains, primaryDomain } = useDomains();
+
const [data, setData] = useState<LinkProps>(
props || {
...DEFAULT_LINK_PROPS,
@@@ -90,11 -88,11 +91,11 @@@
}
}, [debouncedKey]);
- const generateRandomSlug = useCallback(async () => {
- setGeneratingSlug(true);
+ const generateRandomKey = useCallback(async () => {
+ setGeneratingKey(true);
const res = await fetch(
- domain
- ? `/api/projects/${slug}/domains/${domain}/links/random`
+ slug
+ ? `/api/projects/${slug}/links/random?domain=${domain}`
: `/api/links/random`,
);
const key = await res.json();
diff --cc components/stats/bar-chart.tsx
index a609f0f5a3,6afa147cd1..6217af28f0
--- a/components/stats/bar-chart.tsx
+++ b/components/stats/bar-chart.tsx
@@@ -12,7 -12,9 +12,8 @@@ import { nFormatter } from "@/lib/utils
import styles from "./bar-chart.module.css";
import useSWR from "swr";
import { fetcher } from "@/lib/utils";
-import useProject from "@/lib/swr/use-project";
import { LoadingCircle } from "@/components/shared/icons";
+ import useEndpoint from "@/lib/hooks/use-endpoint";
const LEFT_AXIS_WIDTH = 30;
const CHART_MAX_HEIGHT = 400;
diff --cc components/stats/index.tsx
index e91881d431,13ebe94b18..adfe06e4be
--- a/components/stats/index.tsx
+++ b/components/stats/index.tsx
@@@ -7,10 -5,16 +5,10 @@@ import Locations from "@/components/sta
import Referer from "@/components/stats/referer";
import Toggle from "@/components/stats/toggle";
-export default function Stats({
- atModalTop,
- domain,
-}: {
- atModalTop?: boolean;
- domain?: string;
-}) {
+export default function Stats({ atModalTop }: { atModalTop?: boolean }) {
return (
<div className="bg-gray-50 py-10">
- <Toggle atModalTop={atModalTop} />
+ <Toggle atModalTop={atModalTop} domain={domain} />
<div className="mx-auto grid max-w-4xl gap-5">
<Clicks />
<div className="grid grid-cols-1 gap-5 sm:grid-cols-2">
diff --cc pages/api/links/index.ts
index 35b4095e0e,d7329f299e..e13d1ba165
--- a/pages/api/links/index.ts
+++ b/pages/api/links/index.ts
@@@ -1,9 -1,8 +1,9 @@@
import { NextApiRequest, NextApiResponse } from "next";
import { addLink, getLinksForProject } from "@/lib/api/links";
import { Session, withUserAuth } from "@/lib/auth";
- import { isBlacklistedDomain } from "@/lib/utils";
+ import { isBlacklistedDomain, isBlacklistedKey } from "@/lib/utils";
import { log } from "@/lib/utils";
+import { DUB_PROJECT_ID } from "@/lib/constants";
export const config = {
api: {
diff --cc pages/api/projects/[slug]/links/[key]/stats/index.ts
index 0000000000,07f6dab951..07f6dab951
mode 000000,100644..100644
--- a/pages/api/projects/[slug]/links/[key]/stats/index.ts
+++ b/pages/api/projects/[slug]/links/[key]/stats/index.ts
diff --cc pages/api/projects/index.ts
index 39266751f6,807644b424..134d4fba8b
--- a/pages/api/projects/index.ts
+++ b/pages/api/projects/index.ts
@@@ -1,14 -1,9 +1,9 @@@
import { NextApiRequest, NextApiResponse } from "next";
import { Session, withUserAuth } from "@/lib/auth";
- import { DEFAULT_REDIRECTS, RESERVED_KEYS } from "@/lib/constants";
- import {
- addDomain,
- removeDomain,
- setRootDomain,
- validateDomain,
- } from "@/lib/api/domains";
+ import { DEFAULT_REDIRECTS } from "@/lib/constants";
-import { addDomain } from "@/lib/domains";
++import { addDomain, validateDomain } from "@/lib/api/domains";
import prisma from "@/lib/prisma";
- import { validDomainRegex } from "@/lib/utils";
-import { isReservedKey, validDomainRegex } from "@/lib/utils";
++import { isReservedKey } from "@/lib/utils";
export default withUserAuth(
async (req: NextApiRequest, res: NextApiResponse, session: Session) => {
@@@ -36,10 -31,11 +31,10 @@@
let slugError = null;
if (slug.includes(" ") || slug.includes(".")) {
slugError = "Slug cannot contain spaces or periods";
- } else if (RESERVED_KEYS.has(slug) || DEFAULT_REDIRECTS[slug]) {
+ } else if ((await isReservedKey(slug)) || DEFAULT_REDIRECTS[slug]) {
slugError = "Cannot use reserved slugs";
}
- const validDomain =
- validDomainRegex.test(domain) && !domain.endsWith(".dub.sh");
+ const validDomain = await validateDomain(domain);
if (slugError || !validDomain) {
return res.status(422).json({
slugError,
diff --cc prisma/schema.prisma
index 32019e9fb1,42fe293e88..dcab8162db
--- a/prisma/schema.prisma
+++ b/prisma/schema.prisma
@@@ -2,36 -2,32 +2,36 @@@
// learn more about it in the docs: https://pris.ly/d/prisma-schema
datasource db {
- provider = "mysql"
- url = env("DATABASE_URL")
- provider = "mysql"
- url = env("DATABASE_URL")
- relationMode = "prisma"
++ provider = "mysql"
++ url = env("DATABASE_URL")
+ relationMode = "prisma"
}
generator client {
-- provider = "prisma-client-js"
++ provider = "prisma-client-js"
+ previewFeatures = ["fullTextSearch", "fullTextIndex"]
}
model User {
- id String @id @default(cuid())
- name String?
- email String? @unique
- emailVerified DateTime?
- image String?
- id String @id @default(cuid())
- name String?
- email String? @unique
- emailVerified DateTime?
- image String?
- stripeId String? @unique // Stripe customer ID
- usage Int @default(0)
- usageLimit Int @default(1000)
- billingCycleStart Int? // day of the month when the billing cycle starts
- accounts Account[]
- sessions Session[]
- projects ProjectUsers[]
- sentEmails SentEmail[]
- links Link[]
- createdAt DateTime @default(now())
- usageUpdatedAt DateTime @default(now())
++ id String @id @default(cuid())
++ name String?
++ email String? @unique
++ emailVerified DateTime?
++ image String?
+
+ // we need to move all 4 of these over to project level
- stripeId String? @unique // Stripe customer ID
- usage Int @default(0)
- usageLimit Int @default(1000)
++ stripeId String? @unique // Stripe customer ID
++ usage Int @default(0)
++ usageLimit Int @default(1000)
+ billingCycleStart Int? // day of the month when the billing cycle starts
+
- accounts Account[]
- sessions Session[]
- projects ProjectUsers[]
- sentEmails SentEmail[]
- links Link[]
- createdAt DateTime @default(now())
- usageUpdatedAt DateTime @default(now())
++ accounts Account[]
++ sessions Session[]
++ projects ProjectUsers[]
++ sentEmails SentEmail[]
++ links Link[]
++ createdAt DateTime @default(now())
++ usageUpdatedAt DateTime @default(now())
}
model Account {
@@@ -60,7 -56,7 +60,7 @@@ model Session
userId String
expires DateTime
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
--
++
@@index([userId])
}
@@@ -73,49 -69,30 +73,49 @@@ model VerificationToken
}
model Project {
- id String @id @default(cuid())
- name String
- slug String @unique
- logo String?
- domain String @unique
- domainVerified Boolean @default(false)
- createdAt DateTime @default(now())
- updatedAt DateTime @updatedAt
- users ProjectUsers[]
- invites ProjectInvite[]
- domainLastChecked DateTime @default(now())
- usage Int @default(0)
- ownerUsageLimit Int @default(1000)
- ownerExceededUsage Boolean @default(false)
- sentEmails SentEmail[]
- links Link[]
+ id String @id @default(cuid())
+ name String
+ slug String @unique
+ logo String?
+ domain String @unique
+ domainVerified Boolean @default(false)
+ domainLastChecked DateTime @default(now())
+ createdAt DateTime @default(now())
+ updatedAt DateTime @updatedAt
+ users ProjectUsers[]
+ invites ProjectInvite[]
+ usage Int @default(0)
+ ownerUsageLimit Int @default(1000)
+ ownerExceededUsage Boolean @default(false)
+ sentEmails SentEmail[]
+ domains Domain[]
+ links Link[]
+ tags Tag[]
}
-model ProjectInvite {
- email String
- expires DateTime
- project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
- projectId String
+model Domain {
- id String @id @default(cuid())
- slug String @unique
- verified Boolean @default(false)
- target String?
- type String @default("redirect")
- project Project? @relation(fields: [projectId], references: [id], onDelete: Cascade)
- projectId String?
- primary Boolean @default(false)
- links Link[]
- lastChecked DateTime @default(now())
- createdAt DateTime @default(now())
- updatedAt DateTime @updatedAt
++ id String @id @default(cuid())
++ slug String @unique
++ verified Boolean @default(false)
++ target String?
++ type String @default("redirect")
++ project Project? @relation(fields: [projectId], references: [id], onDelete: Cascade)
++ projectId String?
++ primary Boolean @default(false)
++ links Link[]
++ lastChecked DateTime @default(now())
+ createdAt DateTime @default(now())
++ updatedAt DateTime @updatedAt
+
+ @@index(projectId)
+}
+
+model ProjectInvite {
+ email String
+ expires DateTime
+ project Project @relation(fields: [projectId], references: [id], onDelete: Cascade)
+ projectId String
+ createdAt DateTime @default(now())
@@unique([email, projectId])
@@index([projectId])
@@@ -156,72 -133,54 +156,73 @@@ model SentEmail
}
model Link {
- id String @id @default(cuid())
- domain String // domain of the link (e.g. dub.sh) – also stored on Redis
- key String // key of the link (e.g. /github) – also stored on Redis
- url String @db.LongText // target url (e.g. https://github.com/steven-tey/dub) – also stored on Redis
- archived Boolean @default(false) // whether the link is archived or not
- expiresAt DateTime? // when the link expires – stored on Redis via ttl
- password String? // password to access the link – also stored on Redis
-
- proxy Boolean @default(false) // Proxy to use custom OG tags (stored on redis) – if false, will use OG tags from target url
- title String? // OG title for the link (e.g. Dub - Open-Source Bitly Alternative)
- description String? @db.VarChar(280) // OG description for the link (e.g. An open-source link management tool for modern marketing teams to create, share, and track short links.)
- image String? @db.LongText // OG image for the link (e.g. https://dub.sh/og-image.png)
+ id String @id @default(cuid())
+ domain String // domain of the link (e.g. dub.sh) – also stored on Redis
+ key String // key of the link (e.g. /github) – also stored on Redis
+ url String @db.LongText // target url (e.g. https://github.com/steven-tey/dub) – also stored on Redis
+ archived Boolean @default(false) // whether the link is archived or not (TODO: REMOVE AFTER ADDING TAGS)
+ expiresAt DateTime? // when the link expires – stored on Redis via ttl
+ password String? // password to access the link – also stored on Redis
+
- proxy Boolean @default(false) // Proxy to use custom OG tags – if false, will use OG tags from target url
++ proxy Boolean @default(false) // Proxy to use custom OG tags (stored on redis) – if false, will use OG tags from target url
+ title String? // OG title for the link (e.g. Dub - Open-Source Bitly Alternative)
+ description String? @db.VarChar(280) // OG description for the link (e.g. An open-source link management tool for modern marketing teams to create, share, and track short links.)
+ image String? @db.LongText // OG image for the link (e.g. https://dub.sh/og-image.png)
// UTM parameters
- utm_source String? // UTM source for the link (e.g. youtube.com)
- utm_medium String? // UTM medium for the link (e.g. social)
- utm_campaign String? // UTM campaign for the link (e.g. summer-sale)
- utm_term String? // UTM term for the link (e.g. dub)
- utm_content String? // UTM content for the link (e.g. description)
+ utm_source String? // UTM source for the link (e.g. youtube.com)
+ utm_medium String? // UTM medium for the link (e.g. social)
+ utm_campaign String? // UTM campaign for the link (e.g. summer-sale)
+ utm_term String? // UTM term for the link (e.g. dub)
+ utm_content String? // UTM content for the link (e.g. description)
// Custom device targeting
- ios String? @db.LongText // custom link for iOS devices
- android String? @db.LongText // custom link for Android devices
-
- // Click related stats/settings
- clicks Int @default(0) // number of clicks
- publicStats Boolean @default(false) // whether to show public stats or not
+ ios String? @db.LongText // custom link for iOS devices
+ android String? @db.LongText // custom link for Android devices
// User who created the link
- user User? @relation(fields: [userId], references: [id])
- userId String?
+ user User? @relation(fields: [userId], references: [id])
+ userId String?
// Project that the link belongs to
- project Project? @relation(fields: [domain], references: [domain], onUpdate: Cascade, onDelete: Cascade)
- createdAt DateTime @default(now())
- updatedAt DateTime @updatedAt
+ project Project? @relation(fields: [projectId], references: [id], onUpdate: Cascade, onDelete: Cascade)
+ projectId String?
+ // Relational reference to the project domain
+ projectDomain Domain? @relation(fields: [domain], references: [slug], onUpdate: Cascade, onDelete: Cascade)
+
+ // Link metadata
- clicks Int @default(0) // number of clicks
- createdAt DateTime @default(now())
- updatedAt DateTime @updatedAt
++ clicks Int @default(0) // number of clicks
++ publicStats Boolean @default(false) // whether to show public stats or not
++ createdAt DateTime @default(now())
++ updatedAt DateTime @updatedAt
- @@unique([domain, key])
+ // Link tags
+ tag Tag? @relation(fields: [tagId], references: [id])
+ tagId String?
- // indices for links per domain/project
- @@index([domain, archived, expiresAt, createdAt(sort: Desc)])
- @@index([domain, archived, expiresAt, clicks(sort: Desc)])
+ // TODO: might wanna add indices for utm_source, utm_medium, utm_campaign
- // indices for links per domain/project specific to a user
- @@index([domain, archived, expiresAt, userId, createdAt(sort: Desc)])
- @@index([domain, archived, expiresAt, userId, clicks(sort: Desc)])
+ @@unique([domain, key])
+ @@index(projectId)
+ @@index(domain)
+ @@index(tagId)
- @@fulltext([key, url, title, description])
+ @@index(proxy)
+ @@index(password)
+ @@index(createdAt(sort: Desc))
+ @@index(clicks(sort: Desc))
+ @@index(userId)
++ @@fulltext([key, url, title, description])
+}
- // TODO: might wanna add indices for utm_source, utm_medium, utm_campaign
-
- @@index([userId])
-}
+model Tag {
+ id String @id @default(cuid())
+ name String
+ createdAt DateTime @default(now())
+ updatedAt DateTime @updatedAt
+ project Project @relation(fields: [projectId], references: [id], onUpdate: Cascade, onDelete: Cascade)
+ projectId String
+ links Link[]
+
+ @@unique([name, projectId])
+ @@index(projectId)
+}