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.

Show manual copy button when short link copy fails

Tim Wilson887172bb3b70
diff --git a/apps/web/ui/links/link-builder/use-link-builder-submit.tsx b/apps/web/ui/links/link-builder/use-link-builder-submit.tsx
index 474eb2e73b..785ac3ca0f 100644
--- a/apps/web/ui/links/link-builder/use-link-builder-submit.tsx
+++ b/apps/web/ui/links/link-builder/use-link-builder-submit.tsx
@@ -1,6 +1,6 @@
import { mutatePrefix } from "@/lib/swr/mutate";
import { UpgradeRequiredToast } from "@/ui/shared/upgrade-required-toast";
-import { useCopyToClipboard } from "@dub/ui";
+import { Button, useCopyToClipboard } from "@dub/ui";
import { useRouter } from "next/navigation";
import posthog from "posthog-js";
import { useCallback } from "react";
@@ -81,10 +81,37 @@ export function useLinkBuilderSubmit({
// copy shortlink to clipboard when adding a new link
if (!props) {
try {
- await copyToClipboard(data.shortLink);
- toast.success("Copied short link to clipboard!");
+ await copyToClipboard(data.shortLink, { throwOnError: true });
+ toast.success("Copied short link to clipboard!", {
+ duration: 100000,
+ });
} catch (err) {
- toast.success("Successfully created link!");
+ toast.success(
+ <div className="flex grow items-center justify-between gap-4">
+ <p className="text-[0.8125rem] text-neutral-900">
+ Link created successfully!
+ </p>
+ <Button
+ type="button"
+ className="-my-1 h-7 w-fit"
+ text="Copy link"
+ onClick={async () => {
+ try {
+ await copyToClipboard(data.shortLink, {
+ throwOnError: true,
+ });
+ toast.success("Copied short link to clipboard!");
+ } catch (e) {
+ toast.error("Failed to copy short link to clipboard.");
+ console.error("Failed to copy with manual button", e);
+ }
+ }}
+ />
+ </div>,
+ {
+ duration: 100000,
+ },
+ );
}
} else toast.success("Successfully updated short link!");
diff --git a/packages/ui/src/hooks/use-copy-to-clipboard.tsx b/packages/ui/src/hooks/use-copy-to-clipboard.tsx
index 279ed83ee6..c330e6a53a 100644
--- a/packages/ui/src/hooks/use-copy-to-clipboard.tsx
+++ b/packages/ui/src/hooks/use-copy-to-clipboard.tsx
@@ -22,7 +22,10 @@ export const useCopyToClipboard = (
const copyToClipboard = useCallback(
async (
value: string | ClipboardItem,
- { onSuccess }: { onSuccess?: () => void } = {},
+ {
+ onSuccess,
+ throwOnError,
+ }: { onSuccess?: () => void; throwOnError?: boolean } = {},
) => {
clearTimer();
try {
@@ -40,6 +43,7 @@ export const useCopyToClipboard = (
}
} catch (error) {
console.error("Failed to copy: ", error);
+ if (throwOnError) throw error;
}
},
[timeout],