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.

Only show nav item for workspaces w/ programs

Tim Wilsonb1682d7a9ec1
diff --git a/apps/web/app/api/workspaces/[idOrSlug]/route.ts b/apps/web/app/api/workspaces/[idOrSlug]/route.ts
index ed8f5f9d70..80ec5af0a3 100644
--- a/apps/web/app/api/workspaces/[idOrSlug]/route.ts
+++ b/apps/web/app/api/workspaces/[idOrSlug]/route.ts
@@ -4,31 +4,43 @@ import { withWorkspace } from "@/lib/auth";
import { dub } from "@/lib/dub";
import { getFeatureFlags } from "@/lib/edge-config";
import { prisma } from "@/lib/prisma";
+import { ProgramSchema } from "@/lib/zod/schemas/partners";
import {
WorkspaceSchema,
updateWorkspaceSchema,
} from "@/lib/zod/schemas/workspaces";
import { waitUntil } from "@vercel/functions";
import { NextResponse } from "next/server";
+import { z } from "zod";
// GET /api/workspaces/[idOrSlug] – get a specific workspace by id or slug
export const GET = withWorkspace(
async ({ workspace, headers }) => {
- const domains = await prisma.domain.findMany({
- where: {
- projectId: workspace.id,
- },
- select: {
- slug: true,
- primary: true,
- },
- });
+ const [domains, programs] = await Promise.all([
+ prisma.domain.findMany({
+ where: {
+ projectId: workspace.id,
+ },
+ select: {
+ slug: true,
+ primary: true,
+ },
+ }),
+ prisma.program.findMany({
+ where: {
+ workspaceId: workspace.id,
+ },
+ }),
+ ]);
return NextResponse.json(
- WorkspaceSchema.parse({
+ WorkspaceSchema.extend({
+ programs: z.array(ProgramSchema.pick({ id: true, name: true })),
+ }).parse({
...workspace,
id: `ws_${workspace.id}`,
domains,
+ programs,
flags: await getFeatureFlags({
workspaceId: workspace.id,
}),
diff --git a/apps/web/lib/swr/use-workspace.ts b/apps/web/lib/swr/use-workspace.ts
index b8a5479ef1..96979cde51 100644
--- a/apps/web/lib/swr/use-workspace.ts
+++ b/apps/web/lib/swr/use-workspace.ts
@@ -1,4 +1,4 @@
-import { WorkspaceProps } from "@/lib/types";
+import { ExpandedWorkspaceProps } from "@/lib/types";
import { PRO_PLAN, fetcher, getNextPlan } from "@dub/utils";
import { useParams, useSearchParams } from "next/navigation";
import useSWR from "swr";
@@ -14,15 +14,20 @@ export default function useWorkspace() {
data: workspace,
error,
mutate,
- } = useSWR<WorkspaceProps>(slug && `/api/workspaces/${slug}`, fetcher, {
- dedupingInterval: 60000,
- });
+ } = useSWR<ExpandedWorkspaceProps>(
+ slug && `/api/workspaces/${slug}`,
+ fetcher,
+ {
+ dedupingInterval: 60000,
+ },
+ );
return {
...workspace,
nextPlan: workspace?.plan ? getNextPlan(workspace.plan) : PRO_PLAN,
role: (workspace?.users && workspace.users[0].role) || "member",
isOwner: workspace?.users && workspace.users[0].role === "owner",
+ hasPrograms: workspace?.programs && workspace.programs.length > 0,
exceededClicks: workspace && workspace.usage >= workspace.usageLimit,
exceededLinks: workspace && workspace.linksUsage >= workspace.linksLimit,
exceededAI: workspace && workspace.aiUsage >= workspace.aiLimit,
diff --git a/apps/web/lib/types.ts b/apps/web/lib/types.ts
index 9ff137b37c..6a9f72f95a 100644
--- a/apps/web/lib/types.ts
+++ b/apps/web/lib/types.ts
@@ -99,6 +99,13 @@ export interface WorkspaceProps extends Project {
};
}
+export type ExpandedWorkspaceProps = WorkspaceProps & {
+ programs: {
+ id: string;
+ name: string;
+ }[];
+};
+
export type WorkspaceWithUsers = Omit<WorkspaceProps, "domains">;
export interface UserProps {
diff --git a/apps/web/ui/layout/sidebar/app-sidebar-nav.tsx b/apps/web/ui/layout/sidebar/app-sidebar-nav.tsx
index f6de1b721b..33f7c3e1cd 100644
--- a/apps/web/ui/layout/sidebar/app-sidebar-nav.tsx
+++ b/apps/web/ui/layout/sidebar/app-sidebar-nav.tsx
@@ -6,6 +6,7 @@ import {
Books2,
CircleInfo,
ConnectedDots,
+ ConnectedDots4,
CubeSettings,
Gear2,
Gift,
@@ -13,7 +14,6 @@ import {
Key,
Receipt2,
ShieldCheck,
- TableIcon,
Users6,
Webhook,
} from "@dub/ui/src/icons";
@@ -31,9 +31,10 @@ import { WorkspaceDropdown } from "./workspace-dropdown";
const NAV_AREAS: SidebarNavAreas<{
slug: string;
flags?: Record<BetaFeatures, boolean>;
+ hasPrograms: boolean;
}> = {
// Top-level
- default: ({ slug }) => ({
+ default: ({ slug, hasPrograms }) => ({
showSwitcher: true,
showNews: true,
direction: "left",
@@ -63,16 +64,20 @@ const NAV_AREAS: SidebarNavAreas<{
},
],
},
- {
- name: "Partnerships",
- items: [
- {
- name: "Affiliate Program",
- icon: TableIcon,
- href: `/${slug}/programs`,
- },
- ],
- },
+ ...(hasPrograms
+ ? [
+ {
+ name: "Partnerships",
+ items: [
+ {
+ name: "Affiliate Program",
+ icon: ConnectedDots4,
+ href: `/${slug}/programs`,
+ },
+ ],
+ },
+ ]
+ : []),
],
}),
@@ -202,7 +207,7 @@ export function AppSidebarNav({
}) {
const { slug } = useParams() as { slug?: string };
const pathname = usePathname();
- const { flags } = useWorkspace();
+ const { flags, hasPrograms } = useWorkspace();
const currentArea = useMemo(() => {
return pathname.startsWith("/account/settings")
@@ -216,7 +221,7 @@ export function AppSidebarNav({
<SidebarNav
areas={NAV_AREAS}
currentArea={currentArea}
- data={{ slug: slug || "", flags }}
+ data={{ slug: slug || "", flags, hasPrograms: hasPrograms || false }}
toolContent={toolContent}
newsContent={newsContent}
switcher={<WorkspaceDropdown />}