Skip to content

Papermark is the open-source DocSend alternative and secure data rooms with built-in analytics and custom domains.

refactor: improve domain configuration experience

Marc Seitzcf1614addfad
diff --git a/components/domains/domain-card.tsx b/components/domains/domain-card.tsx
index ab34fbb4..e4d8f70c 100644
--- a/components/domains/domain-card.tsx
+++ b/components/domains/domain-card.tsx
@@ -14,6 +14,8 @@ import {
import { motion } from "motion/react";
import { mutate } from "swr";
+import { cn } from "@/lib/utils";
+
import { Button } from "@/components/ui/button";
import {
DropdownMenu,
@@ -24,8 +26,6 @@ import {
} from "@/components/ui/dropdown-menu";
import { StatusBadge } from "@/components/ui/status-badge";
-import { cn } from "@/lib/utils";
-
import { useDeleteDomainModal } from "./delete-domain-modal";
import DomainConfiguration from "./domain-configuration";
import { useDomainStatus } from "./use-domain-status";
@@ -99,16 +99,8 @@ export default function DomainCard({
</div>
</div>
<div className="overflow-hidden">
- <div className="flex items-center gap-1.5 sm:gap-2.5">
- <a
- href={`http://${domain}`}
- target="_blank"
- rel="noreferrer"
- className="truncate text-sm font-medium"
- title={domain}
- >
- {domain}
- </a>
+ <div className="flex items-center gap-1.5 truncate text-sm font-medium sm:gap-2.5">
+ {domain}
{isDefault ? (
<span className="xs:px-3 xs:py-1 flex items-center gap-1 rounded-full bg-sky-400/[.15] px-1.5 py-0.5 text-xs font-medium text-sky-600">
diff --git a/components/domains/domain-configuration.tsx b/components/domains/domain-configuration.tsx
index 6ed2b8f7..fa9437c3 100644
--- a/components/domains/domain-configuration.tsx
+++ b/components/domains/domain-configuration.tsx
@@ -6,6 +6,7 @@ import { getSubdomain } from "@/lib/domains";
import { DomainVerificationStatusProps } from "@/lib/types";
import { cn } from "@/lib/utils";
+import { CopyButton } from "../ui/copy-button";
import { TabSelect } from "../ui/tab-select";
export default function DomainConfiguration({
@@ -19,30 +20,6 @@ export default function DomainConfiguration({
const subdomain = getSubdomain(domainJson.name, domainJson.apexName);
const [recordType, setRecordType] = useState(!!subdomain ? "CNAME" : "A");
- if (status === "Pending Verification") {
- const txtVerification = domainJson.verification.find(
- (x: any) => x.type === "TXT",
- );
- return (
- <div>
- <DnsRecord
- instructions={`Please set the following TXT record on <code>${domainJson.apexName}</code> to prove ownership of <code>${domainJson.name}</code>:`}
- records={[
- {
- type: txtVerification.type,
- name: txtVerification.domain.slice(
- 0,
- txtVerification.domain.length - domainJson.apexName.length - 1,
- ),
- value: txtVerification.value,
- },
- ]}
- warning="Warning: if you are using this domain for another site, setting this TXT record will transfer domain ownership away from that site and break it. Please exercise caution when setting this record; make sure that the domain that is shown in the TXT verification value is actually the <b><i>domain you want to use on Papermark.io</i></b> – <b><i>not your production site</i></b>."
- />
- </div>
- );
- }
-
if (status === "Conflicting DNS Records") {
return (
<div className="pt-5">
@@ -95,6 +72,11 @@ export default function DomainConfiguration({
);
}
+ const txtVerification =
+ status === "Pending Verification"
+ ? domainJson.verification.find((x: any) => x.type === "TXT")
+ : undefined;
+
return (
<div className="pt-2">
<div className="-ml-1.5 border-b border-gray-200 dark:border-gray-400">
@@ -116,15 +98,38 @@ export default function DomainConfiguration({
recordType === "A" ? "apex domain" : "subdomain"
} <code>${
recordType === "A" ? domainJson.apexName : domainJson.name
- }</code>, set the following ${recordType} record on your DNS provider:`}
+ }</code>, set the following ${txtVerification ? "records" : `${recordType} record`} on your DNS provider:`}
records={[
{
type: recordType,
name: recordType === "A" ? "@" : (subdomain ?? "www"),
- value: recordType === "A" ? `76.76.21.21` : `cname.vercel-dns.com`,
+ value:
+ recordType === "A"
+ ? (configJson.recommendedIPv4[0].value[0] ?? "76.76.21.21")
+ : (configJson.recommendedCNAME[0].value ??
+ "cname.vercel-dns.com"),
ttl: "86400",
},
+ ...(txtVerification
+ ? [
+ {
+ type: txtVerification.type,
+ name: txtVerification.domain.slice(
+ 0,
+ txtVerification.domain.length -
+ domainJson.apexName.length -
+ 1,
+ ),
+ value: txtVerification.value,
+ },
+ ]
+ : []),
]}
+ warning={
+ txtVerification
+ ? "Warning: if you are using this domain for another site, setting this TXT record will transfer domain ownership away from that site and break it. Please exercise caution when setting this record; make sure that the domain that is shown in the TXT verification value is actually the <b><i>domain you want to use on Papermark</i></b> – <b><i>not your production site</i></b>."
+ : undefined
+ }
/>
</div>
);
@@ -151,16 +156,16 @@ const DnsRecord = ({
const hasTtl = records.some((x) => x.ttl);
return (
- <div className="mt-3 text-left text-gray-600">
- <div className="my-5 text-gray-600 dark:text-gray-400">
+ <div className="mt-3 text-left text-gray-600 dark:text-gray-400">
+ <div className="my-5">
<MarkdownText text={instructions} />
</div>
<div
className={cn(
"grid items-end gap-x-10 gap-y-1 overflow-x-auto rounded-lg bg-gray-100/80 p-4 text-sm scrollbar-hide",
hasTtl
- ? "grid-cols-[repeat(4,min-content)]"
- : "grid-cols-[repeat(3,min-content)]",
+ ? "grid-cols-[repeat(4,max-content)]"
+ : "grid-cols-[repeat(3,max-content)]",
)}
>
{["Type", "Name", "Value"].concat(hasTtl ? "TTL" : []).map((s) => (
@@ -179,11 +184,11 @@ const DnsRecord = ({
</p>
<p key={record.value} className="flex items-end gap-1 font-mono">
{record.value}{" "}
- {/* <CopyButton
+ <CopyButton
variant="neutral"
className="-mb-0.5"
value={record.value}
- /> */}
+ />
</p>
{hasTtl && (
<p key={record.ttl} className="font-mono">
diff --git a/components/ui/copy-button.tsx b/components/ui/copy-button.tsx
new file mode 100644
index 00000000..fa4afd13
--- /dev/null
+++ b/components/ui/copy-button.tsx
@@ -0,0 +1,58 @@
+"use client";
+
+import { VariantProps, cva } from "class-variance-authority";
+import { CheckIcon, CopyIcon, LucideIcon } from "lucide-react";
+import { toast } from "sonner";
+
+import { useCopyToClipboard } from "@/lib/hooks/use-copy-to-clipboard";
+import { cn } from "@/lib/utils";
+
+const copyButtonVariants = cva(
+ "relative group rounded-full p-1.5 transition-all duration-75",
+ {
+ variants: {
+ variant: {
+ default: "bg-transparent hover:bg-neutral-100 active:bg-neutral-200",
+ neutral: "bg-transparent hover:bg-neutral-100 active:bg-neutral-200",
+ },
+ },
+ defaultVariants: {
+ variant: "default",
+ },
+ },
+);
+
+export function CopyButton({
+ variant = "default",
+ value,
+ className,
+ icon,
+ successMessage,
+}: {
+ value: string;
+ className?: string;
+ icon?: LucideIcon;
+ successMessage?: string;
+} & VariantProps<typeof copyButtonVariants>) {
+ const [copied, copyToClipboard] = useCopyToClipboard();
+ const Comp = icon || CopyIcon;
+ return (
+ <button
+ onClick={(e) => {
+ e.stopPropagation();
+ toast.promise(copyToClipboard(value), {
+ success: successMessage || "Copied to clipboard!",
+ });
+ }}
+ className={cn(copyButtonVariants({ variant }), className)}
+ type="button"
+ >
+ <span className="sr-only">Copy</span>
+ {copied ? (
+ <CheckIcon className="h-3.5 w-3.5" />
+ ) : (
+ <Comp className="h-3.5 w-3.5" />
+ )}
+ </button>
+ );
+}
diff --git a/lib/domains.ts b/lib/domains.ts
index 4cb29966..dd7e28a4 100644
--- a/lib/domains.ts
+++ b/lib/domains.ts
@@ -6,14 +6,16 @@ import {
export const addDomainToVercel = async (domain: string) => {
return await fetch(
- `https://api.vercel.com/v9/projects/${process.env.PROJECT_ID_VERCEL}/domains?teamId=${process.env.TEAM_ID_VERCEL}`,
+ `https://api.vercel.com/v10/projects/${process.env.PROJECT_ID_VERCEL}/domains?teamId=${process.env.TEAM_ID_VERCEL}`,
{
- body: `{\n "name": "${domain}"\n}`,
+ method: "POST",
headers: {
Authorization: `Bearer ${process.env.AUTH_BEARER_TOKEN}`,
"Content-Type": "application/json",
},
- method: "POST",
+ body: JSON.stringify({
+ name: domain.toLowerCase(),
+ }),
},
).then((res) => res.json());
};
diff --git a/lib/hooks/use-copy-to-clipboard.ts b/lib/hooks/use-copy-to-clipboard.ts
new file mode 100644
index 00000000..7b047f25
--- /dev/null
+++ b/lib/hooks/use-copy-to-clipboard.ts
@@ -0,0 +1,58 @@
+import { useCallback, useEffect, useRef, useState } from "react";
+
+export const useCopyToClipboard = (
+ timeout: number = 3000,
+): [
+ boolean,
+ (
+ value: string | ClipboardItem,
+ options?: { onSuccess?: () => void; throwOnError?: boolean },
+ ) => Promise<void>,
+] => {
+ const timer = useRef<ReturnType<typeof setTimeout> | null>(null);
+ const [copied, setCopied] = useState(false);
+
+ const clearTimer = () => {
+ if (timer.current) {
+ clearTimeout(timer.current);
+ timer.current = null;
+ }
+ };
+
+ const copyToClipboard = useCallback(
+ async (
+ value: string | ClipboardItem,
+ {
+ onSuccess,
+ throwOnError,
+ }: { onSuccess?: () => void; throwOnError?: boolean } = {},
+ ) => {
+ clearTimer();
+ try {
+ if (typeof value === "string") {
+ await navigator.clipboard.writeText(value);
+ } else if (value instanceof ClipboardItem) {
+ await navigator.clipboard.write([value]);
+ }
+ setCopied(true);
+ onSuccess?.();
+
+ // Ensure timeout is a non-negative finite number
+ if (Number.isFinite(timeout) && timeout >= 0) {
+ timer.current = setTimeout(() => setCopied(false), timeout);
+ }
+ } catch (error) {
+ console.error("Failed to copy: ", error);
+ if (throwOnError) throw error;
+ }
+ },
+ [timeout],
+ );
+
+ // Cleanup the timer when the component unmounts
+ useEffect(() => {
+ return () => clearTimer();
+ }, []);
+
+ return [copied, copyToClipboard];
+};