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.

add useCurrentSubdomain hook

Steven Teya297ee5cb7cc
diff --git a/apps/web/app/app.dub.co/(dashboard)/account/settings/page-client.tsx b/apps/web/app/app.dub.co/(dashboard)/account/settings/page-client.tsx
index ce9e1aa7ca..4b7fa816bd 100644
--- a/apps/web/app/app.dub.co/(dashboard)/account/settings/page-client.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/account/settings/page-client.tsx
@@ -6,19 +6,14 @@ import UpdateSubscription from "@/ui/account/update-subscription";
import UploadAvatar from "@/ui/account/upload-avatar";
import UserId from "@/ui/account/user-id";
import { PageWidthWrapper } from "@/ui/layout/page-width-wrapper";
-import { Form } from "@dub/ui";
+import { Form, useCurrentSubdomain } from "@dub/ui";
import { APP_NAME } from "@dub/utils";
import { useSession } from "next-auth/react";
-import { useEffect, useState } from "react";
import { toast } from "sonner";
export function SettingsPageClient() {
const { data: session, update, status } = useSession();
- const [isPartnerPage, setIsPartnerPage] = useState(false);
-
- useEffect(() => {
- setIsPartnerPage(window.location.hostname.startsWith("partners."));
- }, []);
+ const { subdomain } = useCurrentSubdomain();
return (
<PageWidthWrapper className="mb-8 grid gap-8">
@@ -82,7 +77,7 @@ export function SettingsPageClient() {
/>
<UploadAvatar />
<UserId />
- {!isPartnerPage && <UpdateDefaultWorkspace />}
+ {subdomain === "app" && <UpdateDefaultWorkspace />}
<DeleteAccountSection />
</PageWidthWrapper>
);
diff --git a/apps/web/lib/swr/use-links-count.ts b/apps/web/lib/swr/use-links-count.ts
index 9e3661f334..3f75ec1bc5 100644
--- a/apps/web/lib/swr/use-links-count.ts
+++ b/apps/web/lib/swr/use-links-count.ts
@@ -1,6 +1,5 @@
-import { useRouterStuff } from "@dub/ui";
+import { useCurrentSubdomain, useRouterStuff } from "@dub/ui";
import { fetcher } from "@dub/utils";
-import { useEffect, useState } from "react";
import useSWR from "swr";
import z from "../zod";
import { getLinksCountQuerySchema } from "../zod/schemas/links";
@@ -20,14 +19,8 @@ export default function useLinksCount<T = any>({
} = {}) {
const { id: workspaceId } = useWorkspace();
const { getQueryString } = useRouterStuff();
-
- const [admin, setAdmin] = useState(false);
- useEffect(() => {
- if (window.location.host.startsWith("admin.")) {
- setAdmin(true);
- }
- }, []);
const { isMegaFolder } = useIsMegaFolder();
+ const { subdomain } = useCurrentSubdomain();
const { data, error } = useSWR<any>(
workspaceId && !isMegaFolder && enabled
@@ -42,7 +35,7 @@ export default function useLinksCount<T = any>({
exclude: ["import", "upgrade", "newLink"],
},
)}`
- : admin
+ : subdomain === "admin"
? `/api/admin/links/count${getQueryString({
...query,
})}`
diff --git a/apps/web/lib/swr/use-links.ts b/apps/web/lib/swr/use-links.ts
index 9ffd7ae52e..d3c20cf179 100644
--- a/apps/web/lib/swr/use-links.ts
+++ b/apps/web/lib/swr/use-links.ts
@@ -1,6 +1,5 @@
-import { useRouterStuff } from "@dub/ui";
+import { useCurrentSubdomain, useRouterStuff } from "@dub/ui";
import { fetcher } from "@dub/utils";
-import { useEffect, useState } from "react";
import useSWR, { SWRConfiguration } from "swr";
import { z } from "zod";
import { ExpandedLinkProps, UserProps } from "../types";
@@ -18,12 +17,7 @@ export default function useLinks(
const { getQueryString } = useRouterStuff();
const { isMegaFolder } = useIsMegaFolder();
- const [admin, setAdmin] = useState(false);
- useEffect(() => {
- if (window.location.host.startsWith("admin.")) {
- setAdmin(true);
- }
- }, []);
+ const { subdomain } = useCurrentSubdomain();
const {
data: links,
@@ -62,7 +56,7 @@ export default function useLinks(
],
},
)}`
- : admin
+ : subdomain === "admin"
? `/api/admin/links${getQueryString(opts)}`
: null,
fetcher,
diff --git a/apps/web/ui/layout/page-content/page-content-old.tsx b/apps/web/ui/layout/page-content/page-content-old.tsx
index c5f5e4b5fe..cde4b21339 100644
--- a/apps/web/ui/layout/page-content/page-content-old.tsx
+++ b/apps/web/ui/layout/page-content/page-content-old.tsx
@@ -4,7 +4,7 @@ import { ChevronLeft } from "lucide-react";
import Link from "next/link";
import { PropsWithChildren, ReactNode } from "react";
import { HelpButtonRSC } from "../sidebar/help-button-rsc";
-import UserDropdownOld from "../sidebar/user-dropdown-old";
+import UserDropdown from "../sidebar/user-dropdown";
import { NavButton } from "./nav-button";
/**
@@ -74,7 +74,7 @@ export function PageContentOld({
{showControls && (
<div className="flex items-center gap-4 md:hidden">
<HelpButtonRSC />
- <UserDropdownOld />
+ <UserDropdown />
</div>
)}
</div>
diff --git a/apps/web/ui/layout/sidebar/user-dropdown-old.tsx b/apps/web/ui/layout/sidebar/user-dropdown-old.tsx
deleted file mode 100644
index cf14473edc..0000000000
--- a/apps/web/ui/layout/sidebar/user-dropdown-old.tsx
+++ /dev/null
@@ -1,143 +0,0 @@
-"use client";
-
-import usePartnerProfile from "@/lib/swr/use-partner-profile";
-import {
- ArrowsOppositeDirectionX,
- 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";
-import Link from "next/link";
-import {
- ComponentPropsWithoutRef,
- ElementType,
- useEffect,
- useState,
-} from "react";
-
-/**
- * @deprecated use UserDropdown instead
- */
-export default function UserDropdownOld() {
- const { data: session } = useSession();
- const { partner } = usePartnerProfile();
- const [openPopover, setOpenPopover] = useState(false);
- const [isPartnerPage, setIsPartnerPage] = useState(false);
- useEffect(() => {
- setIsPartnerPage(window.location.hostname.startsWith("partners."));
- }, []);
-
- return (
- <Popover
- content={
- <div className="flex w-full flex-col space-y-px rounded-md bg-white p-2 sm:min-w-56">
- {session?.user ? (
- <div className="p-2">
- <p className="truncate text-sm font-medium text-neutral-900">
- {session.user.name || session.user.email?.split("@")[0]}
- </p>
- <p className="truncate text-sm text-neutral-500">
- {session.user.email}
- </p>
- </div>
- ) : (
- <div className="grid gap-2 px-2 py-3">
- <div className="h-3 w-12 animate-pulse rounded-full bg-neutral-200" />
- <div className="h-3 w-20 animate-pulse rounded-full bg-neutral-200" />
- </div>
- )}
- <UserOption
- as={Link}
- label="Account"
- icon={User}
- href="/account/settings"
- onClick={() => setOpenPopover(false)}
- />
- {...!isPartnerPage
- ? [
- <UserOption
- as={Link}
- label="Refer and earn"
- icon={Gift}
- href="/account/settings/referrals"
- onClick={() => setOpenPopover(false)}
- />,
- partner ? (
- <UserOption
- as={Link}
- label="Switch to partner account"
- icon={ArrowsOppositeDirectionX}
- href="https://partners.dub.co"
- />
- ) : null,
- ].filter(Boolean)
- : []}
- <UserOption
- as="button"
- type="button"
- label="Logout"
- icon={LogOut}
- onClick={() =>
- signOut({
- callbackUrl: "/login",
- })
- }
- />
- </div>
- }
- align="start"
- openPopover={openPopover}
- setOpenPopover={setOpenPopover}
- >
- <button
- onClick={() => setOpenPopover(!openPopover)}
- className={cn(
- "group relative rounded-full ring-offset-1 ring-offset-neutral-100 transition-all hover:ring-2 hover:ring-black/10 active:ring-black/15 data-[state='open']:ring-black/15",
- "outline-none focus-visible:ring-2 focus-visible:ring-black/50",
- )}
- >
- {session?.user ? (
- <Avatar
- user={session.user}
- className="size-6 border-none duration-75 sm:size-6"
- />
- ) : (
- <div className="size-6 animate-pulse rounded-full bg-neutral-100 sm:size-6" />
- )}
- </button>
- </Popover>
- );
-}
-
-type UserOptionProps<T extends ElementType> = {
- as?: T;
- label: string;
- icon: Icon;
-};
-
-function UserOption<T extends ElementType = "button">({
- as,
- label,
- icon: Icon,
- children,
- ...rest
-}: UserOptionProps<T> &
- Omit<ComponentPropsWithoutRef<T>, keyof UserOptionProps<T>>) {
- const Component = as ?? "button";
-
- return (
- <Component
- className="flex items-center gap-x-4 rounded-md px-2.5 py-1.5 text-sm transition-all duration-75 hover:bg-neutral-200/50 active:bg-neutral-200/80"
- {...rest}
- >
- <Icon className="size-4 text-neutral-500" />
- <span className="block truncate text-neutral-600">{label}</span>
- {children}
- </Component>
- );
-}
diff --git a/apps/web/ui/layout/sidebar/user-dropdown.tsx b/apps/web/ui/layout/sidebar/user-dropdown.tsx
index 166cedf2b6..19804ccdde 100644
--- a/apps/web/ui/layout/sidebar/user-dropdown.tsx
+++ b/apps/web/ui/layout/sidebar/user-dropdown.tsx
@@ -7,16 +7,17 @@ import {
Gift,
Icon,
Popover,
+ useCurrentSubdomain,
User,
} from "@dub/ui";
-import { cn } from "@dub/utils";
+import { APP_DOMAIN, cn, PARTNERS_DOMAIN } from "@dub/utils";
import { LogOut } from "lucide-react";
import { signOut, useSession } from "next-auth/react";
import Link from "next/link";
import {
ComponentPropsWithoutRef,
ElementType,
- useEffect,
+ useMemo,
useState,
} from "react";
@@ -24,10 +25,63 @@ export default function UserDropdown() {
const { data: session } = useSession();
const { partner } = usePartnerProfile();
const [openPopover, setOpenPopover] = useState(false);
- const [isPartnerPage, setIsPartnerPage] = useState(false);
- useEffect(() => {
- setIsPartnerPage(window.location.hostname.startsWith("partners."));
- }, []);
+ const { subdomain } = useCurrentSubdomain();
+
+ const menuOptions = useMemo(() => {
+ const options: Array<{
+ label: string;
+ icon: any;
+ href?: string;
+ type?: string;
+ onClick?: () => void;
+ }> = [
+ {
+ label: "Account settings",
+ icon: User,
+ href: "/account/settings",
+ onClick: () => setOpenPopover(false),
+ },
+ ];
+
+ // Add subdomain-specific options
+ if (subdomain === "partners") {
+ options.push({
+ label: "Switch to workspace",
+ icon: ArrowsOppositeDirectionX,
+ href: APP_DOMAIN,
+ });
+ }
+
+ if (subdomain === "app") {
+ options.push({
+ label: "Refer and earn",
+ icon: Gift,
+ href: "/account/settings/referrals",
+ onClick: () => setOpenPopover(false),
+ });
+
+ if (partner) {
+ options.push({
+ label: "Switch to partner account",
+ icon: ArrowsOppositeDirectionX,
+ href: PARTNERS_DOMAIN,
+ });
+ }
+ }
+
+ // Add logout option
+ options.push({
+ type: "button",
+ label: "Log out",
+ icon: LogOut,
+ onClick: () =>
+ signOut({
+ callbackUrl: "/login",
+ }),
+ });
+
+ return options;
+ }, [subdomain, partner, setOpenPopover]);
return (
<Popover
@@ -48,43 +102,13 @@ export default function UserDropdown() {
<div className="h-3 w-20 animate-pulse rounded-full bg-neutral-200" />
</div>
)}
- <UserOption
- as={Link}
- label="Account settings"
- icon={User}
- href="/account/settings"
- onClick={() => setOpenPopover(false)}
- />
- {...!isPartnerPage
- ? [
- <UserOption
- as={Link}
- label="Refer and earn"
- icon={Gift}
- href="/account/settings/referrals"
- onClick={() => setOpenPopover(false)}
- />,
- partner ? (
- <UserOption
- as={Link}
- label="Switch to partner account"
- icon={ArrowsOppositeDirectionX}
- href="https://partners.dub.co"
- />
- ) : null,
- ].filter(Boolean)
- : []}
- <UserOption
- as="button"
- type="button"
- label="Log out"
- icon={LogOut}
- onClick={() =>
- signOut({
- callbackUrl: "/login",
- })
- }
- />
+ {menuOptions.map((menuOption, idx) => (
+ <UserOption
+ key={idx}
+ as={menuOption.href ? Link : "button"}
+ {...menuOption}
+ />
+ ))}
</div>
}
align="start"
diff --git a/packages/ui/src/hooks/index.ts b/packages/ui/src/hooks/index.ts
index 4325472aeb..7f543b6e1f 100644
--- a/packages/ui/src/hooks/index.ts
+++ b/packages/ui/src/hooks/index.ts
@@ -2,6 +2,7 @@ export * from "./use-column-visibility";
export * from "./use-cookies";
export * from "./use-copy-to-clipboard";
export * from "./use-current-anchor";
+export * from "./use-current-subdomain";
export * from "./use-enter-submit";
export * from "./use-in-viewport";
export * from "./use-input-focused";
diff --git a/packages/ui/src/hooks/use-current-subdomain.ts b/packages/ui/src/hooks/use-current-subdomain.ts
new file mode 100644
index 0000000000..bcd67478da
--- /dev/null
+++ b/packages/ui/src/hooks/use-current-subdomain.ts
@@ -0,0 +1,20 @@
+import { ADMIN_HOSTNAMES, APP_HOSTNAMES, PARTNERS_HOSTNAMES } from "@dub/utils";
+import { useEffect, useState } from "react";
+
+export function useCurrentSubdomain() {
+ const [subdomain, setSubdomain] = useState<
+ "app" | "partners" | "admin" | null
+ >(null);
+ useEffect(() => {
+ const hostname = window.location.hostname;
+ if (APP_HOSTNAMES.has(hostname)) {
+ setSubdomain("app");
+ } else if (PARTNERS_HOSTNAMES.has(hostname)) {
+ setSubdomain("partners");
+ } else if (ADMIN_HOSTNAMES.has(hostname)) {
+ setSubdomain("admin");
+ }
+ }, []);
+
+ return { subdomain };
+}
diff --git a/packages/utils/src/constants/main.ts b/packages/utils/src/constants/main.ts
index 7436b9a267..c5340acd20 100644
--- a/packages/utils/src/constants/main.ts
+++ b/packages/utils/src/constants/main.ts
@@ -8,6 +8,7 @@ export const APP_HOSTNAMES = new Set([
`preview.${process.env.NEXT_PUBLIC_APP_DOMAIN}`,
"localhost:8888",
"localhost",
+ "app.localhost",
]);
export const APP_DOMAIN =
@@ -29,6 +30,7 @@ export const API_HOSTNAMES = new Set([
`api-staging.${process.env.NEXT_PUBLIC_APP_DOMAIN}`,
`api.${SHORT_DOMAIN}`,
"api.localhost:8888",
+ "api.localhost",
]);
export const API_DOMAIN =
@@ -41,12 +43,14 @@ export const API_DOMAIN =
export const ADMIN_HOSTNAMES = new Set([
`admin.${process.env.NEXT_PUBLIC_APP_DOMAIN}`,
"admin.localhost:8888",
+ "admin.localhost",
]);
export const PARTNERS_HOSTNAMES = new Set([
`partners.${process.env.NEXT_PUBLIC_APP_DOMAIN}`,
`partners-staging.${process.env.NEXT_PUBLIC_APP_DOMAIN}`,
"partners.localhost:8888",
+ "partners.localhost",
]);
export const PARTNERS_DOMAIN =