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.

simplify referrals

Steven Teydfe0b19dda4a
diff --git a/apps/web/app/app.dub.co/(dashboard)/account/settings/referrals/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/account/settings/referrals/page-client.tsx
new file mode 100644
index 0000000000..8c5b029e24
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/account/settings/referrals/page-client.tsx
@@ -0,0 +1,47 @@
+"use client";
+
+import LayoutLoader from "@/ui/layout/layout-loader";
+import { AnimatedEmptyState } from "@/ui/shared/animated-empty-state";
+import { DubWidget } from "@dub/embed-react";
+import { CursorRays, Hyperlink, InvoiceDollar, UserCheck } from "@dub/ui/icons";
+import { fetcher } from "@dub/utils";
+import { useSession } from "next-auth/react";
+import useSWRImmutable from "swr/immutable";
+
+export function ReferralsPageClient() {
+ const { data: session, status } = useSession();
+ const referralLinkId = session?.user?.["referralLinkId"];
+
+ const { data: { publicToken } = {}, isLoading } = useSWRImmutable<{
+ publicToken: string;
+ }>(referralLinkId && "/api/user/embed-tokens", fetcher, {
+ keepPreviousData: true,
+ });
+
+ if (status === "loading" || isLoading) {
+ return <LayoutLoader />;
+ }
+
+ if (!referralLinkId || !publicToken) {
+ return (
+ <AnimatedEmptyState
+ title="Refer a friend"
+ description="Activate your referral link to share the word about Dub and earn cash rewards"
+ cardContent={
+ <>
+ <Hyperlink className="size-4 text-neutral-700" />
+ <div className="h-2.5 w-24 min-w-0 rounded-sm bg-neutral-200" />
+ <div className="xs:flex hidden grow items-center justify-end gap-1.5 text-gray-500">
+ <CursorRays className="size-3.5" />
+ <UserCheck className="size-3.5" />
+ <InvoiceDollar className="size-3.5" />
+ </div>
+ </>
+ }
+ pillContent="Coming soon"
+ />
+ );
+ }
+
+ return <DubWidget token={publicToken} variant="inline" />;
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/account/settings/referrals/page.tsx b/apps/web/app/app.dub.co/(dashboard)/account/settings/referrals/page.tsx
index 5d39813daf..f823bc7d92 100644
--- a/apps/web/app/app.dub.co/(dashboard)/account/settings/referrals/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/account/settings/referrals/page.tsx
@@ -1,47 +1,5 @@
-import { getSession } from "@/lib/auth/utils";
-import { dub } from "@/lib/dub";
-import LayoutLoader from "@/ui/layout/layout-loader";
-import { AnimatedEmptyState } from "@/ui/shared/animated-empty-state";
-import { DubWidget } from "@dub/embed-react";
-import { CursorRays, Hyperlink, InvoiceDollar, UserCheck } from "@dub/ui/icons";
-import { Suspense } from "react";
-
-export const dynamic = "auto";
+import { ReferralsPageClient } from "./page-client";
export default function ReferralsPage() {
- return (
- <Suspense fallback={<LayoutLoader />}>
- <ReferralsPageRSC />
- </Suspense>
- );
-}
-async function ReferralsPageRSC() {
- const session = await getSession();
- const referralLinkId = session?.user.referralLinkId;
-
- if (!referralLinkId)
- return (
- <AnimatedEmptyState
- title="Refer a friend"
- description="Activate your referral link to share the word about Dub and earn cash rewards"
- cardContent={
- <>
- <Hyperlink className="size-4 text-neutral-700" />
- <div className="h-2.5 w-24 min-w-0 rounded-sm bg-neutral-200" />
- <div className="xs:flex hidden grow items-center justify-end gap-1.5 text-gray-500">
- <CursorRays className="size-3.5" />
- <UserCheck className="size-3.5" />
- <InvoiceDollar className="size-3.5" />
- </div>
- </>
- }
- pillContent="Coming soon"
- />
- );
-
- const { publicToken } = await dub.embedTokens.create({
- linkId: referralLinkId,
- });
-
- return <DubWidget token={publicToken} variant="inline" />;
+ return <ReferralsPageClient />;
}
diff --git a/apps/web/ui/layout/sidebar/refer-button.tsx b/apps/web/ui/layout/sidebar/refer-button.tsx
index f9d0a9d2ff..f4d644ede5 100644
--- a/apps/web/ui/layout/sidebar/refer-button.tsx
+++ b/apps/web/ui/layout/sidebar/refer-button.tsx
@@ -1,40 +1,41 @@
"use client";
-import { DubWidget } from "@dub/embed-react";
import { Gift } from "@dub/ui/icons";
-import { cn, fetcher } from "@dub/utils";
+import { cn } from "@dub/utils";
import { AnimatePresence, motion } from "framer-motion";
-import useSWRImmutable from "swr/immutable";
+import { useSession } from "next-auth/react";
+import Link from "next/link";
+import { usePathname } from "next/navigation";
export function ReferButton() {
- const { data: { publicToken } = {} } = useSWRImmutable<{
- publicToken: string;
- }>("/api/user/embed-tokens", fetcher, {
- keepPreviousData: true,
- });
+ const pathname = usePathname();
+ const { data: session } = useSession();
- if (!publicToken) return null;
+ if (
+ !session?.user?.["referralLinkId"] ||
+ pathname === "/account/settings/referrals"
+ ) {
+ return null;
+ }
return (
- <DubWidget token={publicToken} options={{ trigger: "manual" }}>
- <button
- type="button"
- className={cn(
- "font-lg relative size-4 shrink-0 overflow-hidden rounded-full transition-all active:scale-90 active:bg-gray-50",
- "outline-none ring-offset-2 ring-offset-neutral-100 focus-visible:ring-2 focus-visible:ring-black/50",
- )}
- >
- <AnimatePresence>
- <motion.div
- className="absolute inset-0 flex items-center justify-center font-medium text-neutral-500 hover:text-neutral-700"
- initial={{ opacity: 0, scale: 0 }}
- animate={{ opacity: 1, scale: 1 }}
- exit={{ opacity: 0, scale: 0 }}
- >
- <Gift className="size-4" />
- </motion.div>
- </AnimatePresence>
- </button>
- </DubWidget>
+ <Link
+ href="/account/settings/referrals"
+ className={cn(
+ "font-lg relative size-4 shrink-0 overflow-hidden rounded-full transition-all active:scale-90 active:bg-gray-50",
+ "outline-none ring-offset-2 ring-offset-neutral-100 focus-visible:ring-2 focus-visible:ring-black/50",
+ )}
+ >
+ <AnimatePresence>
+ <motion.div
+ className="absolute inset-0 flex items-center justify-center font-medium text-neutral-500 hover:text-neutral-700"
+ initial={{ opacity: 0, scale: 0 }}
+ animate={{ opacity: 1, scale: 1 }}
+ exit={{ opacity: 0, scale: 0 }}
+ >
+ <Gift className="size-4" />
+ </motion.div>
+ </AnimatePresence>
+ </Link>
);
}
diff --git a/apps/web/ui/layout/sidebar/user-dropdown.tsx b/apps/web/ui/layout/sidebar/user-dropdown.tsx
index fa8c36ee0b..47f9fb0755 100644
--- a/apps/web/ui/layout/sidebar/user-dropdown.tsx
+++ b/apps/web/ui/layout/sidebar/user-dropdown.tsx
@@ -1,6 +1,6 @@
"use client";
-import { Avatar, Icon, Popover, User } from "@dub/ui";
+import { Avatar, Gift, Icon, Popover, User } from "@dub/ui";
import { cn } from "@dub/utils";
import { LogOut } from "lucide-react";
import { signOut, useSession } from "next-auth/react";
@@ -37,6 +37,15 @@ export default function UserDropdown() {
href="/account/settings"
onClick={() => setOpenPopover(false)}
/>
+ {session?.user?.["referralLinkId"] && (
+ <UserOption
+ as={Link}
+ label="Refer and earn"
+ icon={Gift}
+ href="/account/settings/referrals"
+ onClick={() => setOpenPopover(false)}
+ />
+ )}
<UserOption
as="button"
type="button"