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 workspace environment switcher for staging workspaces

Kiran K1d3c39b97564
diff --git a/apps/web/lib/api/workspaces/staging-workspace.ts b/apps/web/lib/api/workspaces/staging-workspace.ts
index e9221a3b43..24afe2fc23 100644
--- a/apps/web/lib/api/workspaces/staging-workspace.ts
+++ b/apps/web/lib/api/workspaces/staging-workspace.ts
@@ -1,5 +1,6 @@
import { getPlanCapabilities } from "@/lib/plan-capabilities";
import { prisma } from "@dub/prisma";
+import { WorkspaceEnvironment } from "@dub/prisma/client";
import { nanoid } from "@dub/utils";
import { generateRandomString } from "../utils/generate-random-string";
import { createWorkspaceId } from "./create-workspace-id";
@@ -56,6 +57,7 @@ export async function createStagingWorkspace(workspaceId: string) {
name: `${workspace.name} [STAGING]`,
slug: `${workspace.slug}-staging`,
logo: workspace.logo,
+ environment: WorkspaceEnvironment.staging,
plan: workspace.plan,
billingCycleStart: new Date().getDate(),
invoicePrefix: generateRandomString(8),
@@ -78,19 +80,32 @@ export async function createStagingWorkspace(workspaceId: string) {
]);
// Copy the users to the staging workspace
- const { users } = workspace;
+ if (workspace.users.length > 0) {
+ await prisma.$transaction(async (tx) => {
+ await tx.projectUsers.createMany({
+ skipDuplicates: true,
+ data: workspace.users.map((user) => ({
+ projectId: stagingWorkspaceId,
+ role: user.role,
+ userId: user.userId,
+ })),
+ });
- if (users.length > 0) {
- await prisma.projectUsers.createMany({
- data: users.map((user) => ({
- projectId: stagingWorkspaceId,
- role: user.role,
- userId: user.userId,
- notificationPreference: {
- create: {},
+ const workspaceUsers = await tx.projectUsers.findMany({
+ where: {
+ projectId: stagingWorkspaceId,
+ },
+ select: {
+ id: true,
},
- })),
- skipDuplicates: true,
+ });
+
+ await tx.notificationPreference.createMany({
+ skipDuplicates: true,
+ data: workspaceUsers.map((user) => ({
+ projectUserId: user.id,
+ })),
+ });
});
}
}
diff --git a/apps/web/lib/zod/schemas/workspaces.ts b/apps/web/lib/zod/schemas/workspaces.ts
index c47244e96e..8313e0da15 100644
--- a/apps/web/lib/zod/schemas/workspaces.ts
+++ b/apps/web/lib/zod/schemas/workspaces.ts
@@ -185,6 +185,7 @@ export const WorkspaceSchema = z
ssoEmailDomain: z.string().nullable(),
ssoEnforcedAt: z.date().nullable(),
environment: z.enum(WorkspaceEnvironment).nullable(),
+ stagingWorkspaceId: z.string().nullable(),
})
.meta({
title: "Workspace",
diff --git a/apps/web/ui/layout/sidebar/workspace-dropdown.tsx b/apps/web/ui/layout/sidebar/workspace-dropdown.tsx
index fa696d8b11..b19d72587d 100644
--- a/apps/web/ui/layout/sidebar/workspace-dropdown.tsx
+++ b/apps/web/ui/layout/sidebar/workspace-dropdown.tsx
@@ -5,6 +5,7 @@ import useWorkspaces from "@/lib/swr/use-workspaces";
import { PlanProps, WorkspaceProps } from "@/lib/types";
import { ModalContext } from "@/ui/modals/modal-provider";
import { getUserAvatarUrl } from "@/ui/users/user-avatar";
+import { WorkspaceEnvironment } from "@dub/prisma/client";
import { BlurImage, Popover, useScrollProgress } from "@dub/ui";
import { Check2, Gear, Plus, UserPlus } from "@dub/ui/icons";
import { cn, isLegacyBusinessPlan, pluralize } from "@dub/utils";
@@ -19,11 +20,12 @@ import {
useRef,
useState,
} from "react";
+import { WorkspaceEnvironmentSwitcher } from "./workspace-environment-switcher";
export function WorkspaceDropdown() {
const { workspaces } = useWorkspaces();
const { data: session, status } = useSession();
- const { slug: currentSlug, key } = useParams() as {
+ const { slug: currentSlug } = useParams() as {
slug?: string;
key?: string;
};
@@ -203,23 +205,30 @@ function WorkspaceList({
<div className="flex flex-row gap-1">
<Link
href={`/${selected.slug ? selected.slug : "account"}/settings`}
- className="flex items-center justify-start gap-x-2 rounded-lg border border-neutral-200 px-2 py-1 text-neutral-700 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"
+ className="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"
onClick={() => setOpenPopover(false)}
>
- <Gear className="size-4 text-neutral-800" />
+ <Gear className="text-content-emphasis size-4" />
<span className="block truncate text-sm">Settings</span>
</Link>
{selected.slug && (
<Link
href={`/${selected.slug}/settings/people`}
- className="flex items-center justify-start gap-x-2 rounded-lg border border-neutral-200 px-2 py-1 text-neutral-700 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"
+ className="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"
onClick={() => setOpenPopover(false)}
>
- <UserPlus className="size-4 text-neutral-800" />
- <span className="block truncate text-sm">Invite members</span>
+ <UserPlus className="text-content-emphasis size-4" />
+ <span className="block truncate text-sm">Invite</span>
</Link>
)}
</div>
+
+ <WorkspaceEnvironmentSwitcher
+ href={href}
+ onNavigate={() => setOpenPopover(false)}
+ workspaces={workspaces}
+ selectedWorkspace={selected}
+ />
</div>
{/* Workspaces section */}
@@ -228,40 +237,45 @@ function WorkspaceList({
Workspaces
</p>
<div className="flex flex-col gap-0.5">
- {workspaces.map(({ id, name, slug, logo }) => {
- const isActive = selected.slug === slug;
- return (
- <Link
- key={slug}
- className={cn(
- "relative flex w-full items-center gap-x-2 rounded-md px-2 py-2 transition-all duration-75",
- "hover:bg-neutral-200/50 active:bg-neutral-200/80",
- "outline-none focus-visible:ring-2 focus-visible:ring-black/50",
- isActive && "bg-neutral-200/50",
- )}
- href={href(slug)}
- shallow={false}
- onClick={() => setOpenPopover(false)}
- >
- <BlurImage
- src={logo || `https://avatar.vercel.sh/${id}`}
- width={28}
- height={28}
- alt={id}
- className="size-5 shrink-0 overflow-hidden rounded-full"
- draggable={false}
- />
- <span className="block truncate text-base leading-5 text-neutral-900 sm:max-w-[140px] sm:text-sm">
- {name}
- </span>
- {selected.slug === slug ? (
- <span className="absolute inset-y-0 right-0 flex items-center pr-3 text-black">
- <Check2 className="size-4" aria-hidden="true" />
+ {workspaces
+ .filter(
+ (workspace) =>
+ workspace.environment !== WorkspaceEnvironment.staging,
+ )
+ .map(({ id, name, slug, logo }) => {
+ const isActive = selected.slug === slug;
+ return (
+ <Link
+ key={slug}
+ className={cn(
+ "relative flex w-full items-center gap-x-2 rounded-md px-2 py-2 transition-all duration-75",
+ "hover:bg-neutral-200/50 active:bg-neutral-200/80",
+ "outline-none focus-visible:ring-2 focus-visible:ring-black/50",
+ isActive && "bg-neutral-200/50",
+ )}
+ href={href(slug)}
+ shallow={false}
+ onClick={() => setOpenPopover(false)}
+ >
+ <BlurImage
+ src={logo || `https://avatar.vercel.sh/${id}`}
+ width={28}
+ height={28}
+ alt={id}
+ className="size-5 shrink-0 overflow-hidden rounded-full"
+ draggable={false}
+ />
+ <span className="block truncate text-base leading-5 text-neutral-900 sm:max-w-[140px] sm:text-sm">
+ {name}
</span>
- ) : null}
- </Link>
- );
- })}
+ {selected.slug === slug ? (
+ <span className="absolute inset-y-0 right-0 flex items-center pr-3 text-black">
+ <Check2 className="size-4" aria-hidden="true" />
+ </span>
+ ) : null}
+ </Link>
+ );
+ })}
<button
key="add"
onClick={() => {
diff --git a/apps/web/ui/layout/sidebar/workspace-environment-switcher.tsx b/apps/web/ui/layout/sidebar/workspace-environment-switcher.tsx
new file mode 100644
index 0000000000..15a5a39738
--- /dev/null
+++ b/apps/web/ui/layout/sidebar/workspace-environment-switcher.tsx
@@ -0,0 +1,74 @@
+"use client";
+
+import { WorkspaceProps } from "@/lib/types";
+import { WorkspaceEnvironment } from "@dub/prisma/client";
+import { Cube } from "@dub/ui/icons";
+import Link from "next/link";
+
+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 (currentWorkspace.environment === WorkspaceEnvironment.staging) {
+ targetWorkspace = workspaces.find(
+ (workspace) => workspace.stagingWorkspaceId === currentWorkspace.id,
+ );
+
+ label = "Exit staging";
+ }
+
+ // Switch to staging
+ else if (
+ currentWorkspace.environment === WorkspaceEnvironment.live &&
+ 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}
+ >
+ <Cube className="text-content-emphasis size-4" />
+ <span className="block truncate text-sm">{label}</span>
+ </Link>
+ );
+}
diff --git a/packages/prisma/client.ts b/packages/prisma/client.ts
index d0641ff247..7efd044004 100644
--- a/packages/prisma/client.ts
+++ b/packages/prisma/client.ts
@@ -39,5 +39,6 @@ export {
SubmittedLeadStatus,
WebhookReceiver,
WorkflowTrigger,
+ WorkspaceEnvironment,
WorkspaceRole,
} from "@prisma/client";
diff --git a/packages/prisma/schema/workspace.prisma b/packages/prisma/schema/workspace.prisma
index 212eb7ffa6..18f0cb568b 100644
--- a/packages/prisma/schema/workspace.prisma
+++ b/packages/prisma/schema/workspace.prisma
@@ -32,7 +32,6 @@ model Project {
environment WorkspaceEnvironment @default(live)
stagingWorkspaceId String? @unique
- sandboxId String? @unique
stripeConnectId String? @unique // for Stripe Integration
shopifyStoreId String? @unique // for Shopify Integration