The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Consolidate workspace environment components into single file
Kiran Kda13332f259e
diff --git a/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/(default)/apply/page.tsx b/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/(default)/apply/page.tsxindex bef7412464..656921d7dd 100644--- a/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/(default)/apply/page.tsx+++ b/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/(default)/apply/page.tsx@@ -1,5 +1,5 @@import { getProgram } from "@/lib/fetchers/get-program";-import { ProgramEnvironmentBanner } from "@/lib/sandbox/components/program-environment-banner";+import { ProgramEnvironmentBanner } from "@/lib/sandbox/components/workspace-environment";import { isProductionEnvironment } from "@/lib/sandbox/workspace-guards";import { DEFAULT_PARTNER_GROUP } from "@/lib/zod/schemas/groups";import { programApplicationFormSchema } from "@/lib/zod/schemas/program-application-form";diff --git a/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/(default)/apply/success/page.tsx b/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/(default)/apply/success/page.tsxindex aaff3cade7..ded02cfdf4 100644--- a/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/(default)/apply/success/page.tsx+++ b/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/(default)/apply/success/page.tsx@@ -1,6 +1,6 @@import { getProgram } from "@/lib/fetchers/get-program";import { prisma } from "@/lib/prisma";-import { ProgramEnvironmentBanner } from "@/lib/sandbox/components/program-environment-banner";+import { ProgramEnvironmentBanner } from "@/lib/sandbox/components/workspace-environment";import { isProductionEnvironment } from "@/lib/sandbox/workspace-guards";import { DEFAULT_PARTNER_GROUP } from "@/lib/zod/schemas/groups";import { Logo } from "@dub/ui";diff --git a/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/(default)/page.tsx b/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/(default)/page.tsxindex 01cad89020..6850d64b34 100644--- a/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/(default)/page.tsx+++ b/apps/web/app/(ee)/partners.dub.co/(apply)/[programSlug]/(default)/page.tsx@@ -1,5 +1,5 @@import { getProgram } from "@/lib/fetchers/get-program";-import { ProgramEnvironmentBanner } from "@/lib/sandbox/components/program-environment-banner";+import { ProgramEnvironmentBanner } from "@/lib/sandbox/components/workspace-environment";import { isProductionEnvironment } from "@/lib/sandbox/workspace-guards";import { DEFAULT_PARTNER_GROUP } from "@/lib/zod/schemas/groups";import { programLanderSchema } from "@/lib/zod/schemas/program-lander";diff --git a/apps/web/app/app.dub.co/(dashboard)/layout.tsx b/apps/web/app/app.dub.co/(dashboard)/layout.tsxindex ec9cc4f4db..b2796adfb1 100644--- a/apps/web/app/app.dub.co/(dashboard)/layout.tsx+++ b/apps/web/app/app.dub.co/(dashboard)/layout.tsx@@ -1,4 +1,4 @@-import { WorkspaceEnvironmentBanner } from "@/lib/sandbox/components/workspace-environment-banner";+import { WorkspaceEnvironmentBanner } from "@/lib/sandbox/components/workspace-environment";import { MainNav } from "@/ui/layout/main-nav";import { AppSidebarNav } from "@/ui/layout/sidebar/app-sidebar-nav";import { HelpButton } from "@/ui/layout/sidebar/help-button";diff --git a/apps/web/lib/sandbox/components/program-environment-banner.tsx b/apps/web/lib/sandbox/components/program-environment-banner.tsxdeleted file mode 100644index 5c19874786..0000000000--- a/apps/web/lib/sandbox/components/program-environment-banner.tsx+++ /dev/null@@ -1,37 +0,0 @@-import { IsolatedCube } from "@dub/ui/icons";-import { capitalize, cn } from "@dub/utils";-import { WorkspaceEnvironment } from "@prisma/client";-import {- isProductionEnvironment,- isStagingEnvironment,-} from "../workspace-guards";--export function ProgramEnvironmentBanner({- environment,-}: {- environment: WorkspaceEnvironment;-}) {- if (isProductionEnvironment(environment)) {- return null;- }-- return (- <div- className={cn(- "fixed left-0 right-0 top-0 z-50 flex h-12 items-center justify-between gap-4 overflow-hidden px-6 text-neutral-800",- isStagingEnvironment(environment) ? "bg-amber-200" : "bg-blue-200",- )}- >- <div className="flex shrink-0 items-center gap-2">- <IsolatedCube className="size-4 shrink-0" />- <span className="truncate text-sm font-semibold">- {capitalize(environment)}- </span>- </div>-- <p className="text-sm font-medium">- No real money or payouts in {environment}- </p>- </div>- );-}diff --git a/apps/web/lib/sandbox/components/workspace-environment-banner.tsx b/apps/web/lib/sandbox/components/workspace-environment-banner.tsxdeleted file mode 100644index cce2262321..0000000000--- a/apps/web/lib/sandbox/components/workspace-environment-banner.tsx+++ /dev/null@@ -1,81 +0,0 @@-"use client";--import { useDashboardBannerVisible } from "@/lib/hooks/use-dashboard-banner-visible";-import useWorkspace from "@/lib/swr/use-workspace";-import useWorkspaces from "@/lib/swr/use-workspaces";-import { IsolatedCube } from "@dub/ui";-import { cn } from "@dub/utils";-import { capitalize } from "@dub/utils/src";-import { motion } from "motion/react";-import Link from "next/link";-import { usePathname } from "next/navigation";-import {- isProductionEnvironment,- isStagingEnvironment,-} from "../workspace-guards";--export function WorkspaceEnvironmentBanner() {- const pathname = usePathname();- const { loading: loadingWorkspace, ...currentWorkspace } = useWorkspace();- const { workspaces, loading: loadingWorkspaces } = useWorkspaces();- const { isEnvironmentBannerVisible } = useDashboardBannerVisible();-- if (- !isEnvironmentBannerVisible ||- isProductionEnvironment(currentWorkspace.environment) ||- loadingWorkspace ||- loadingWorkspaces ||- !currentWorkspace.slug- ) {- return null;- }-- const liveWorkspace = currentWorkspace.id- ? workspaces?.find(- (workspace) => workspace.stagingWorkspaceId === currentWorkspace.id,- )- : undefined;-- return (- <motion.div- initial={{ transform: "translateY(-100%)" }}- animate={{ transform: "translateY(0)" }}- className={cn(- "fixed left-0 right-0 top-0 z-30 flex h-12 items-center justify-between gap-4 overflow-hidden px-6 text-neutral-800",- isStagingEnvironment(currentWorkspace.environment)- ? "bg-amber-200"- : "bg-blue-200",- )}- >- <div className="flex min-w-0 shrink-0 items-center gap-2">- <IsolatedCube className="size-4 shrink-0" />- <span className="truncate text-sm font-semibold">- {capitalize(currentWorkspace.environment)} workspace- </span>- </div>-- <p className="hidden min-w-0 flex-1 text-center text-sm font-medium sm:block">- No real money or payouts in {currentWorkspace.environment}.{" "}- <a- href="https://dub.co/docs"- target="_blank"- rel="noopener noreferrer"- className="underline underline-offset-2"- >- Learn more- </a>- </p>-- {liveWorkspace ? (- <Link- href={pathname.replace(currentWorkspace.slug, liveWorkspace.slug)}- className="bg-bg-inverted text-content-inverted shrink-0 rounded-md p-2 text-xs font-medium"- >- Exit staging- </Link>- ) : (- <span className="w-[88px] shrink-0" />- )}- </motion.div>- );-}diff --git a/apps/web/lib/sandbox/components/workspace-environment-switcher.tsx b/apps/web/lib/sandbox/components/workspace-environment-switcher.tsxdeleted file mode 100644index 72d260eae9..0000000000--- a/apps/web/lib/sandbox/components/workspace-environment-switcher.tsx+++ /dev/null@@ -1,77 +0,0 @@-"use client";--import { WorkspaceProps } from "@/lib/types";-import { IsolatedCube } from "@dub/ui/icons";-import Link from "next/link";-import {- isProductionEnvironment,- isStagingEnvironment,-} from "../workspace-guards";--const buttonClassName =- "text-content-emphasis flex w-full items-center justify-center gap-x-2 rounded-lg border border-neutral-200 px-2 py-1 outline-none transition-all duration-75 hover:bg-neutral-100/50 focus-visible:ring-2 focus-visible:ring-black/50 active:bg-neutral-200/80";--export function WorkspaceEnvironmentSwitcher({- href,- onNavigate,- workspaces,- selectedWorkspace,-}: {- href: (slug: string) => string;- onNavigate: () => void;- workspaces: WorkspaceProps[];- selectedWorkspace: { slug?: string };-}) {- const currentWorkspace = workspaces.find(- (workspace) => workspace.slug === selectedWorkspace.slug,- );-- if (!currentWorkspace) {- return null;- }-- let targetWorkspace: WorkspaceProps | undefined;- let label: string;-- // Exit staging- if (isStagingEnvironment(currentWorkspace.environment)) {- targetWorkspace = workspaces.find(- (workspace) => workspace.stagingWorkspaceId === currentWorkspace.id,- );-- label = "Exit staging";- }-- // Switch to staging- else if (- isProductionEnvironment(currentWorkspace.environment) &&- currentWorkspace.stagingWorkspaceId- ) {- targetWorkspace = workspaces.find(- (workspace) => workspace.id === currentWorkspace.stagingWorkspaceId,- );-- label = "Switch to staging";- }-- // No action needed- else {- return null;- }-- if (!targetWorkspace) {- return null;- }-- return (- <Link- href={href(targetWorkspace.slug)}- shallow={false}- className={buttonClassName}- onClick={onNavigate}- >- <IsolatedCube className="text-content-emphasis size-4" />- <span className="block truncate text-sm">{label}</span>- </Link>- );-}diff --git a/apps/web/lib/sandbox/components/workspace-environment.tsx b/apps/web/lib/sandbox/components/workspace-environment.tsxnew file mode 100644index 0000000000..17f0be958a--- /dev/null+++ b/apps/web/lib/sandbox/components/workspace-environment.tsx@@ -0,0 +1,180 @@+"use client";++import { useDashboardBannerVisible } from "@/lib/hooks/use-dashboard-banner-visible";+import useWorkspace from "@/lib/swr/use-workspace";+import useWorkspaces from "@/lib/swr/use-workspaces";+import { WorkspaceProps } from "@/lib/types";+import { IsolatedCube } from "@dub/ui";+import { capitalize, cn } from "@dub/utils";+import { WorkspaceEnvironment } from "@prisma/client";+import { motion } from "motion/react";+import Link from "next/link";+import { usePathname } from "next/navigation";+import {+ isProductionEnvironment,+ isStagingEnvironment,+} from "../workspace-guards";++const buttonClassName =+ "text-content-emphasis flex w-full items-center justify-center gap-x-2 rounded-lg border border-neutral-200 px-2 py-1 outline-none transition-all duration-75 hover:bg-neutral-100/50 focus-visible:ring-2 focus-visible:ring-black/50 active:bg-neutral-200/80";++export function ProgramEnvironmentBanner({+ environment,+}: {+ environment: WorkspaceEnvironment;+}) {+ if (isProductionEnvironment(environment)) {+ return null;+ }++ return (+ <div+ className={cn(+ "fixed left-0 right-0 top-0 z-50 flex h-12 items-center justify-between gap-4 overflow-hidden px-6 text-neutral-800",+ isStagingEnvironment(environment) ? "bg-amber-200" : "bg-blue-200",+ )}+ >+ <div className="flex shrink-0 items-center gap-2">+ <IsolatedCube className="size-4 shrink-0" />+ <span className="truncate text-sm font-semibold">+ {capitalize(environment)}+ </span>+ </div>++ <p className="text-sm font-medium">+ No real money or payouts in {environment}+ </p>+ </div>+ );+}++export function WorkspaceEnvironmentBanner() {+ const pathname = usePathname();+ const { loading: loadingWorkspace, ...currentWorkspace } = useWorkspace();+ const { workspaces, loading: loadingWorkspaces } = useWorkspaces();+ const { isEnvironmentBannerVisible } = useDashboardBannerVisible();++ if (+ !isEnvironmentBannerVisible ||+ isProductionEnvironment(currentWorkspace.environment) ||+ loadingWorkspace ||+ loadingWorkspaces ||+ !currentWorkspace.slug+ ) {+ return null;+ }++ const liveWorkspace = currentWorkspace.id+ ? workspaces?.find(+ (workspace) => workspace.stagingWorkspaceId === currentWorkspace.id,+ )+ : undefined;++ return (+ <motion.div+ initial={{ transform: "translateY(-100%)" }}+ animate={{ transform: "translateY(0)" }}+ className={cn(+ "fixed left-0 right-0 top-0 z-30 flex h-12 items-center justify-between gap-4 overflow-hidden px-6 text-neutral-800",+ isStagingEnvironment(currentWorkspace.environment)+ ? "bg-amber-200"+ : "bg-blue-200",+ )}+ >+ <div className="flex min-w-0 shrink-0 items-center gap-2">+ <IsolatedCube className="size-4 shrink-0" />+ <span className="truncate text-sm font-semibold">+ {capitalize(currentWorkspace.environment)} workspace+ </span>+ </div>++ <p className="hidden min-w-0 flex-1 text-center text-sm font-medium sm:block">+ No real money or payouts in {currentWorkspace.environment}.{" "}+ <a+ href="https://dub.co/docs"+ target="_blank"+ rel="noopener noreferrer"+ className="underline underline-offset-2"+ >+ Learn more+ </a>+ </p>++ {liveWorkspace ? (+ <Link+ href={pathname.replace(currentWorkspace.slug, liveWorkspace.slug)}+ className="bg-bg-inverted text-content-inverted shrink-0 rounded-md p-2 text-xs font-medium"+ >+ Exit staging+ </Link>+ ) : (+ <span className="w-[88px] shrink-0" />+ )}+ </motion.div>+ );+}++export function WorkspaceEnvironmentSwitcher({+ href,+ onNavigate,+ workspaces,+ selectedWorkspace,+}: {+ href: (slug: string) => string;+ onNavigate: () => void;+ workspaces: WorkspaceProps[];+ selectedWorkspace: { slug?: string };+}) {+ const currentWorkspace = workspaces.find(+ (workspace) => workspace.slug === selectedWorkspace.slug,+ );++ if (!currentWorkspace) {+ return null;+ }++ let targetWorkspace: WorkspaceProps | undefined;+ let label: string;++ // Exit staging+ if (isStagingEnvironment(currentWorkspace.environment)) {+ targetWorkspace = workspaces.find(+ (workspace) => workspace.stagingWorkspaceId === currentWorkspace.id,+ );++ label = "Exit staging";+ }++ // Switch to staging+ else if (+ isProductionEnvironment(currentWorkspace.environment) &&+ currentWorkspace.stagingWorkspaceId+ ) {+ targetWorkspace = workspaces.find(+ (workspace) => workspace.id === currentWorkspace.stagingWorkspaceId,+ );++ label = "Switch to staging";+ }++ // No action needed+ else {+ return null;+ }++ if (!targetWorkspace) {+ return null;+ }++ return (+ <Link+ href={href(targetWorkspace.slug)}+ shallow={false}+ className={buttonClassName}+ onClick={onNavigate}+ >+ <IsolatedCube className="text-content-emphasis size-4" />+ <span className="block truncate text-sm">{label}</span>+ </Link>+ );+}diff --git a/apps/web/lib/sandbox/sync-workspace.ts b/apps/web/lib/sandbox/sync-workspace.tsindex ebe6e542cb..c629180e2e 100644--- a/apps/web/lib/sandbox/sync-workspace.ts+++ b/apps/web/lib/sandbox/sync-workspace.ts@@ -2,7 +2,6 @@ import { prisma } from "@/lib/prisma";import { Project, WorkspaceRole } from "@prisma/client";// We don't sync the machine user from the production workspace to the staging workspace-export async function addMemberToStaging({workspace,user,diff --git a/apps/web/ui/layout/sidebar/workspace-dropdown.tsx b/apps/web/ui/layout/sidebar/workspace-dropdown.tsxindex b0f3d74b12..49ccce08f1 100644--- a/apps/web/ui/layout/sidebar/workspace-dropdown.tsx+++ b/apps/web/ui/layout/sidebar/workspace-dropdown.tsx@@ -1,6 +1,6 @@"use client";-import { WorkspaceEnvironmentSwitcher } from "@/lib/sandbox/components/workspace-environment-switcher";+import { WorkspaceEnvironmentSwitcher } from "@/lib/sandbox/components/workspace-environment";import { isStagingEnvironment } from "@/lib/sandbox/workspace-guards";import useWorkspaceUsers from "@/lib/swr/use-workspace-users";import useWorkspaces from "@/lib/swr/use-workspaces";