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.

Restrict default workspace to live environments only

Kiran K6835904562e8
diff --git a/apps/web/app/api/user/route.ts b/apps/web/app/api/user/route.ts
index d604bc1a39..5e53120b50 100644
--- a/apps/web/app/api/user/route.ts
+++ b/apps/web/app/api/user/route.ts
@@ -4,6 +4,7 @@ import { confirmEmailChange } from "@/lib/auth/confirm-email-change";
import { storage } from "@/lib/storage";
import { uploadedImageSchema } from "@/lib/zod/schemas/images";
import { prisma } from "@dub/prisma";
+import { WorkspaceEnvironment } from "@dub/prisma/client";
import {
APP_DOMAIN,
APP_HOSTNAMES,
@@ -83,6 +84,13 @@ export const PATCH = withSession(async ({ req, session }) => {
slug: defaultWorkspace,
},
},
+ select: {
+ project: {
+ select: {
+ environment: true,
+ },
+ },
+ },
});
if (!workspaceUser) {
@@ -91,6 +99,13 @@ export const PATCH = withSession(async ({ req, session }) => {
message: `You don't have access to the workspace ${defaultWorkspace}.`,
});
}
+
+ if (workspaceUser.project.environment !== WorkspaceEnvironment.live) {
+ throw new DubApiError({
+ code: "bad_request",
+ message: `You can only set your default workspace to a live workspace.`,
+ });
+ }
}
// Verify email ownership if the email is being changed
diff --git a/apps/web/ui/account/update-default-workspace.tsx b/apps/web/ui/account/update-default-workspace.tsx
index e6bfeaffd0..6a40a040c3 100644
--- a/apps/web/ui/account/update-default-workspace.tsx
+++ b/apps/web/ui/account/update-default-workspace.tsx
@@ -1,6 +1,7 @@
"use client";
import { WorkspaceSelector } from "@/ui/workspaces/workspace-selector";
+import { WorkspaceEnvironment } from "@dub/prisma/client";
import { Button } from "@dub/ui";
import { useSession } from "next-auth/react";
import { useEffect, useState } from "react";
@@ -63,6 +64,8 @@ export default function UpdateDefaultWorkspace() {
<WorkspaceSelector
selectedWorkspace={selectedWorkspace || ""}
setSelectedWorkspace={setSelectedWorkspace}
+ environments={[WorkspaceEnvironment.live]}
+ showAddWorkspaceButton={false}
/>
</div>
</div>
diff --git a/apps/web/ui/workspaces/workspace-selector.tsx b/apps/web/ui/workspaces/workspace-selector.tsx
index d0db229f62..8661d61ba2 100644
--- a/apps/web/ui/workspaces/workspace-selector.tsx
+++ b/apps/web/ui/workspaces/workspace-selector.tsx
@@ -1,4 +1,5 @@
import useWorkspaces from "@/lib/swr/use-workspaces";
+import { WorkspaceEnvironment } from "@dub/prisma/client";
import { Button, Combobox } from "@dub/ui";
import { OG_AVATAR_URL, cn } from "@dub/utils";
import { useMemo, useState } from "react";
@@ -6,12 +7,16 @@ import { useAddWorkspaceModal } from "../modals/add-workspace-modal";
interface WorkspaceSelectorProps {
selectedWorkspace: string;
- setSelectedWorkspace: (workspace: string) => void;
+ setSelectedWorkspace: (workspace: string | null) => void;
+ environments?: WorkspaceEnvironment[]; // Only display workspaces that are in the environments
+ showAddWorkspaceButton?: boolean;
}
export function WorkspaceSelector({
selectedWorkspace,
setSelectedWorkspace,
+ environments,
+ showAddWorkspaceButton = true,
}: WorkspaceSelectorProps) {
const [openPopover, setOpenPopover] = useState(false);
const { workspaces, loading } = useWorkspaces();
@@ -19,26 +24,41 @@ export function WorkspaceSelector({
const { AddWorkspaceModal, setShowAddWorkspaceModal } =
useAddWorkspaceModal();
- const workspaceOptions = useMemo(() => {
- return workspaces?.map((workspace) => ({
- value: workspace.slug,
- label: workspace.name,
- icon: (
- <img
- src={workspace.logo || `${OG_AVATAR_URL}${workspace.name}`}
- alt={workspace.name}
- className="size-4 rounded-full"
- />
- ),
- }));
- }, [workspaces]);
+ const filteredWorkspaces = useMemo(() => {
+ if (!workspaces) {
+ return [];
+ }
+
+ return workspaces.filter(
+ (workspace) =>
+ !environments || environments.includes(workspace.environment),
+ );
+ }, [workspaces, environments]);
+
+ const workspaceOptions = useMemo(
+ () =>
+ filteredWorkspaces.map((workspace) => ({
+ value: workspace.slug,
+ label: workspace.name,
+ icon: (
+ <img
+ src={workspace.logo || `${OG_AVATAR_URL}${workspace.name}`}
+ alt={workspace.name}
+ className="size-4 rounded-full"
+ />
+ ),
+ })),
+ [filteredWorkspaces],
+ );
const selectedOption = useMemo(() => {
if (!selectedWorkspace) {
return null;
}
- const workspace = workspaces?.find((w) => w.slug === selectedWorkspace);
+ const workspace = filteredWorkspaces.find(
+ (w) => w.slug === selectedWorkspace,
+ );
if (!workspace) {
return null;
@@ -55,15 +75,15 @@ export function WorkspaceSelector({
/>
),
};
- }, [workspaces, selectedWorkspace]);
+ }, [filteredWorkspaces, selectedWorkspace]);
return (
<>
- <AddWorkspaceModal />
+ {showAddWorkspaceButton && <AddWorkspaceModal />}
<Combobox
options={loading ? undefined : workspaceOptions}
setSelected={(option) => {
- setSelectedWorkspace(option.value);
+ setSelectedWorkspace(option?.value ?? null);
}}
selected={selectedOption}
icon={loading ? null : selectedOption?.icon}
@@ -83,15 +103,17 @@ export function WorkspaceSelector({
emptyState={
<div className="flex w-full flex-col items-center gap-2 py-4">
No workspaces found
- <Button
- onClick={() => {
- setOpenPopover(false);
- setShowAddWorkspaceModal(true);
- }}
- variant="primary"
- className="h-7 w-fit px-2"
- text="Create new workspace"
- />
+ {showAddWorkspaceButton && (
+ <Button
+ onClick={() => {
+ setOpenPopover(false);
+ setShowAddWorkspaceModal(true);
+ }}
+ variant="primary"
+ className="h-7 w-fit px-2"
+ text="Create new workspace"
+ />
+ )}
</div>
}
>