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.

fix domain card UX and record click

Steven Tey251c9714c543
diff --git a/apps/web/lib/tinybird/record-click.ts b/apps/web/lib/tinybird/record-click.ts
index 127d9ad06f..6bcde7a3ab 100644
--- a/apps/web/lib/tinybird/record-click.ts
+++ b/apps/web/lib/tinybird/record-click.ts
@@ -94,19 +94,16 @@ export async function recordClick({
).then((res) => res.json()),
// increment the click count for the link or domain (based on their ID)
- // also increment the usage count for the workspace
+ conn.execute(
+ "UPDATE Project p JOIN Link l ON p.id = l.projectId SET p.usage = p.usage + 1 WHERE l.id = ?",
+ [linkId],
+ ),
+ // if the link has a destination URL, increment the usage count for the workspace
// and then we have a cron that will reset it at the start of new billing cycle
- url
- ? [
- conn.execute(
- "UPDATE Link SET clicks = clicks + 1, lastClicked = NOW() WHERE id = ?",
- [linkId],
- ),
- conn.execute(
- "UPDATE Project p JOIN Link l ON p.id = l.projectId SET p.usage = p.usage + 1 WHERE l.id = ?",
- [linkId],
- ),
- ]
- : undefined,
+ url &&
+ conn.execute(
+ "UPDATE Project p JOIN Link l ON p.id = l.projectId SET p.usage = p.usage + 1 WHERE l.id = ?",
+ [linkId],
+ ),
]);
}
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 95c24a5204..3feab1bb78 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -125,11 +125,7 @@ export interface DomainProps {
verified: boolean;
primary: boolean;
archived: boolean;
- publicStats: boolean;
- target?: string;
- type: string;
placeholder?: string;
- clicks: number;
projectId: string;
expiredUrl?: string;
noindex?: boolean;
diff --git a/apps/web/scripts/migrate-domains.ts b/apps/web/scripts/migrate-domains.ts
index 613d0b0a85..c89598f9ce 100644
--- a/apps/web/scripts/migrate-domains.ts
+++ b/apps/web/scripts/migrate-domains.ts
@@ -4,6 +4,9 @@ import "dotenv-flow/config";
// Standardize domains into links
async function main() {
const domains = await prisma.domain.findMany({
+ where: {
+ projectId: "cl7wsy2836920mjrb352g5wfx",
+ },
include: {
project: {
select: {
@@ -13,11 +16,12 @@ async function main() {
select: {
userId: true,
},
+ take: 1,
},
},
},
},
- take: 5000, // TODO: Adjust this based on the number of domains
+ // take: 5000, // TODO: Adjust this based on the number of domains
});
// Create links for each domain
@@ -48,6 +52,7 @@ async function main() {
}),
};
});
+ console.log(newLinks);
const result = await prisma.link.createMany({
data: newLinks,
diff --git a/apps/web/ui/domains/domain-card.tsx b/apps/web/ui/domains/domain-card.tsx
index 0253216141..88d1bfdb84 100644
--- a/apps/web/ui/domains/domain-card.tsx
+++ b/apps/web/ui/domains/domain-card.tsx
@@ -1,6 +1,10 @@
import useDomains from "@/lib/swr/use-domains";
import useWorkspace from "@/lib/swr/use-workspace";
-import { DomainProps, DomainVerificationStatusProps } from "@/lib/types";
+import {
+ DomainProps,
+ DomainVerificationStatusProps,
+ LinkProps,
+} from "@/lib/types";
import {
AlertCircleFill,
Chart,
@@ -19,8 +23,9 @@ import {
SimpleTooltipContent,
useIntersectionObserver,
} from "@dub/ui";
+import { Copy, Gear } from "@dub/ui/src/icons";
import {
- capitalize,
+ DEFAULT_LINK_PROPS,
fetcher,
nFormatter,
punycode,
@@ -29,9 +34,11 @@ import {
import { Archive, Edit3, FileCog, FolderInput, QrCode } from "lucide-react";
import Link from "next/link";
import { useRef, useState } from "react";
+import { toast } from "sonner";
import useSWR from "swr";
import useSWRImmutable from "swr/immutable";
import { useAddEditDomainModal } from "../modals/add-edit-domain-modal";
+import { useAddEditLinkModal } from "../modals/add-edit-link-modal";
import { useArchiveDomainModal } from "../modals/archive-domain-modal";
import { useDeleteDomainModal } from "../modals/delete-domain-modal";
import { useLinkQRModal } from "../modals/link-qr-modal";
@@ -43,15 +50,32 @@ export default function DomainCard({ props }: { props: DomainProps }) {
const { id: workspaceId, slug } = useWorkspace();
const { activeWorkspaceDomains } = useDomains();
- const { slug: domain, primary, target, type, archived } = props || {};
+ const { slug: domain, primary, archived } = props || {};
+
+ const { data: linkProps } = useSWR<LinkProps>(
+ workspaceId && domain
+ ? `/api/links/info?${new URLSearchParams({ workspaceId, domain, key: "_root" }).toString()}`
+ : null,
+ fetcher,
+ );
+
+ const { setShowAddEditLinkModal, AddEditLinkModal } = useAddEditLinkModal({
+ props: linkProps || DEFAULT_LINK_PROPS,
+ });
const { setShowLinkQRModal, LinkQRModal } = useLinkQRModal({
- props: {
- domain,
- url: target,
- },
+ props: linkProps || DEFAULT_LINK_PROPS,
});
+ const [copiedLinkId, setCopiedLinkId] = useState(false);
+
+ const copyLinkId = () => {
+ navigator.clipboard.writeText(props.id);
+ setCopiedLinkId(true);
+ toast.success("Link ID copied!");
+ setTimeout(() => setCopiedLinkId(false), 3000);
+ };
+
const domainRef = useRef<any>();
const entry = useIntersectionObserver(domainRef, {});
const isVisible = !!entry?.isIntersecting;
@@ -71,9 +95,6 @@ export default function DomainCard({ props }: { props: DomainProps }) {
`/api/analytics?event=clicks&workspaceId=${workspaceId}&domain=${domain}&key=_root&interval=all_unfiltered`,
fetcher,
{
- fallbackData: {
- clicks: props.clicks,
- },
dedupingInterval: 15000,
},
);
@@ -108,8 +129,9 @@ export default function DomainCard({ props }: { props: DomainProps }) {
return (
<>
- <AddEditDomainModal />
+ <AddEditLinkModal />
<LinkQRModal />
+ <AddEditDomainModal />
<PrimaryDomainModal />
<ArchiveDomainModal />
<DeleteDomainModal />
@@ -164,79 +186,107 @@ export default function DomainCard({ props }: { props: DomainProps }) {
/>
<Popover
content={
- <div className="grid w-full gap-px p-2 sm:w-44">
- <Button
- text="Edit"
- variant="outline"
- onClick={() => {
- setOpenPopover(false);
- setShowAddEditDomainModal(true);
- }}
- icon={<Edit3 className="h-4 w-4" />}
- className="h-9 justify-start px-2 font-medium"
- />
- <Button
- text="QR Code"
- variant="outline"
- onClick={() => {
- setOpenPopover(false);
- setShowLinkQRModal(true);
- }}
- icon={<QrCode className="h-4 w-4" />}
- className="h-9 justify-start px-2 font-medium"
- />
- <Button
- text="Transfer"
- variant="outline"
- onClick={() => {
- setOpenPopover(false);
- setShowTransferDomainModal(true);
- }}
- icon={<FolderInput className="h-4 w-4" />}
- className="h-9 justify-start px-2 font-medium"
- {...(primary &&
- activeDomainsCount > 1 && {
- disabledTooltip: (
- <SimpleTooltipContent
- title="You cannot transfer your workspace's primary domain. Set another domain as primary to transfer this domain."
- cta="Learn more."
- href="https://dub.co/help/article/how-to-set-primary-domain"
- />
- ),
- })}
- />
- {!primary && (
+ <div className="w-full sm:w-44">
+ <div className="grid gap-px p-2">
<Button
- text="Set as Primary"
+ text="Edit Link"
variant="outline"
onClick={() => {
setOpenPopover(false);
- setShowPrimaryDomainModal(true);
+ setShowAddEditLinkModal(true);
}}
- icon={<FileCog className="h-4 w-4" />}
+ icon={<Edit3 className="h-4 w-4" />}
className="h-9 justify-start px-2 font-medium"
/>
- )}
- <Button
- text={archived ? "Unarchive" : "Archive"}
- variant="outline"
- onClick={() => {
- setOpenPopover(false);
- setShowArchiveDomainModal(true);
- }}
- icon={<Archive className="h-4 w-4" />}
- className="h-9 justify-start px-2 font-medium"
- />
- <Button
- text="Delete"
- variant="danger-outline"
- onClick={() => {
- setOpenPopover(false);
- setShowDeleteDomainModal(true);
- }}
- icon={<Delete className="h-4 w-4" />}
- className="h-9 justify-start px-2 font-medium"
- />
+ <Button
+ text="QR Code"
+ variant="outline"
+ onClick={() => {
+ setOpenPopover(false);
+ setShowLinkQRModal(true);
+ }}
+ icon={<QrCode className="h-4 w-4" />}
+ className="h-9 justify-start px-2 font-medium"
+ />
+ <Button
+ text="Copy Link ID"
+ variant="outline"
+ onClick={() => copyLinkId()}
+ icon={
+ copiedLinkId ? (
+ <CheckCircleFill className="h-4 w-4" />
+ ) : (
+ <Copy className="h-4 w-4" />
+ )
+ }
+ className="h-9 justify-start px-2 font-medium"
+ />
+ </div>
+ <div className="border-t border-gray-200" />
+ <div className="grid gap-px p-2">
+ <Button
+ text="Settings"
+ variant="outline"
+ onClick={() => {
+ setOpenPopover(false);
+ setShowAddEditDomainModal(true);
+ }}
+ icon={<Gear className="h-4 w-4" />}
+ className="h-9 justify-start px-2 font-medium"
+ />
+ {!primary && (
+ <Button
+ text="Set as Primary"
+ variant="outline"
+ onClick={() => {
+ setOpenPopover(false);
+ setShowPrimaryDomainModal(true);
+ }}
+ icon={<FileCog className="h-4 w-4" />}
+ className="h-9 justify-start px-2 font-medium"
+ />
+ )}
+ <Button
+ text="Transfer"
+ variant="outline"
+ onClick={() => {
+ setOpenPopover(false);
+ setShowTransferDomainModal(true);
+ }}
+ icon={<FolderInput className="h-4 w-4" />}
+ className="h-9 justify-start px-2 font-medium"
+ {...(primary &&
+ activeDomainsCount > 1 && {
+ disabledTooltip: (
+ <SimpleTooltipContent
+ title="You cannot transfer your workspace's primary domain. Set another domain as primary to transfer this domain."
+ cta="Learn more."
+ href="https://dub.co/help/article/how-to-set-primary-domain"
+ />
+ ),
+ })}
+ />
+ <Button
+ text={archived ? "Unarchive" : "Archive"}
+ variant="outline"
+ onClick={() => {
+ setOpenPopover(false);
+ setShowArchiveDomainModal(true);
+ }}
+ icon={<Archive className="h-4 w-4" />}
+ className="h-9 justify-start px-2 font-medium"
+ />
+ <Button
+ text="Delete"
+ variant="danger-outline"
+ onClick={() => {
+ setOpenPopover(false);
+ setShowDeleteDomainModal(true);
+ }}
+ icon={<Delete className="h-4 w-4" />}
+ className="h-9 justify-start px-2 font-medium"
+ />
+ </div>
</div>
}
align="end"
@@ -272,24 +322,32 @@ export default function DomainCard({ props }: { props: DomainProps }) {
</p>
</div>
<div className="flex items-center space-x-2">
- {target ? (
- <CheckCircleFill className="h-6 w-6 text-blue-500" />
+ {linkProps ? (
+ linkProps.url ? (
+ <CheckCircleFill className="h-6 w-6 text-blue-500" />
+ ) : (
+ <XCircleFill className="h-6 w-6 text-gray-400" />
+ )
) : (
- <XCircleFill className="h-6 w-6 text-gray-400" />
+ <LoadingCircle className="mr-1 h-5 w-5" />
)}
<div className="flex space-x-1">
<p className="text-sm text-gray-500">
- {target ? `${capitalize(type)}s to` : `No ${type} configured`}
+ {linkProps
+ ? linkProps.url
+ ? `Redirects to`
+ : `No redirect configured`
+ : `Checking link status`}
</p>
- {target && (
+ {linkProps?.url && (
<a
- href={target}
+ href={linkProps.url}
target="_blank"
rel="noreferrer"
className="text-sm font-medium text-gray-600 underline-offset-4 hover:underline"
>
{truncate(
- target.replace(/^(?:https?:\/\/)?(?:www\.)?/i, ""),
+ linkProps.url.replace(/^(?:https?:\/\/)?(?:www\.)?/i, ""),
24,
)}
</a>
diff --git a/apps/web/ui/modals/add-edit-domain-modal/index.tsx b/apps/web/ui/modals/add-edit-domain-modal/index.tsx
index ddfb6aaa2c..5bf0ea199c 100644
--- a/apps/web/ui/modals/add-edit-domain-modal/index.tsx
+++ b/apps/web/ui/modals/add-edit-domain-modal/index.tsx
@@ -9,7 +9,6 @@ import {
Logo,
Modal,
SimpleTooltipContent,
- Switch,
Tooltip,
TooltipContent,
useRouterStuff,
@@ -22,7 +21,6 @@ import {
Dispatch,
SetStateAction,
useCallback,
- useEffect,
useMemo,
useState,
} from "react";
@@ -51,23 +49,11 @@ function AddEditDomainModal({
verified: false,
primary: false,
archived: false,
- publicStats: false,
- target: "",
- type: "redirect",
- clicks: 0,
projectId: id || "",
},
);
- const { slug: domain, target, type, placeholder, expiredUrl } = data;
-
- // set noindex to true if target is set
- useEffect(() => {
- setData((prev) => ({
- ...prev,
- noindex: target ? true : false,
- }));
- }, [target]);
+ const { slug: domain, placeholder, expiredUrl } = data;
const [lockDomain, setLockDomain] = useState(true);
const [saving, setSaving] = useState(false);
@@ -144,7 +130,6 @@ function AddEditDomainModal({
},
body: JSON.stringify({
...data,
- target: target || null,
placeholder: placeholder || null,
expiredUrl: expiredUrl || null,
}),
@@ -203,79 +188,6 @@ function AddEditDomainModal({
)}
</div>
- <div>
- <label htmlFor="target" className="flex items-center space-x-2">
- <h2 className="text-sm font-medium text-gray-900">
- Destination URL
- </h2>
- <InfoTooltip
- content={
- <SimpleTooltipContent
- title="The page your users will get redirected to when they visit your domain."
- cta="Learn more."
- href="https://dub.co/help/article/how-to-redirect-root-domain"
- />
- }
- />
- </label>
- {plan === "free" ? (
- <Tooltip
- content={
- <TooltipContent
- title="You can't configure a custom landing page on a free plan. Upgrade to a Pro plan to proceed."
- cta="Upgrade to Pro"
- onClick={() => {
- setShowAddEditDomainModal(false);
- queryParams({
- set: {
- upgrade: "pro",
- },
- });
- }}
- />
- }
- >
- <div className="mt-2 w-full cursor-not-allowed rounded-md border border-gray-300 px-3 py-2 text-left text-sm text-gray-300 sm:max-w-md">
- https://yourdomain.com
- </div>
- </Tooltip>
- ) : (
- <div className="relative mt-2 rounded-md shadow-sm">
- <input
- name="target"
- id="target"
- className="block w-full rounded-md border-gray-300 text-gray-900 placeholder-gray-400 focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
- placeholder="https://example.com"
- value={target}
- onChange={(e) => setData({ ...data, target: e.target.value })}
- />
- </div>
- )}
- </div>
- {target && (
- <motion.div key="type" {...FADE_IN_ANIMATION_SETTINGS}>
- <label
- htmlFor="type"
- className="block text-sm font-medium text-gray-700"
- >
- Behavior
- </label>
- <select
- value={type}
- onChange={(e) =>
- setData({
- ...data,
- type: e.target.value as "redirect" | "rewrite",
- })
- }
- className="mt-2 w-full rounded-md border border-gray-300 bg-white px-3 py-2 text-gray-500 shadow-sm focus:border-gray-500 focus:outline-none focus:ring-gray-500 sm:text-sm"
- >
- <option value="redirect">Redirect</option>
- <option value="rewrite">Rewrite (Link Cloaking)</option>
- </select>
- </motion.div>
- )}
-
<button
type="button"
className="flex items-center"
@@ -375,43 +287,6 @@ function AddEditDomainModal({
/>
</div>
</div>
-
- <div className="flex w-full items-center justify-between">
- <div className="flex items-center space-x-2">
- <h2 className="text-sm font-medium text-gray-900">
- Disable Search Engine Indexing
- </h2>
- <InfoTooltip
- content={
- <SimpleTooltipContent
- title="Prevent search engines from indexing your root domain."
- cta="Learn more."
- href="https://dub.co/help/article/how-noindex-works"
- />
- }
- />
- </div>
- <Switch
- checked={data.noindex}
- fn={(checked) => setData({ ...data, noindex: checked })}
- {...(plan === "free" && {
- disabledTooltip: (
- <TooltipContent
- title="You can only disable search engine indexing on a Pro plan and above. Upgrade to proceed."
- cta="Upgrade to Pro"
- onClick={() => {
- setShowAddEditDomainModal(false);
- queryParams({
- set: {
- upgrade: "pro",
- },
- });
- }}
- />
- ),
- })}
- />
- </div>
</motion.div>
)}