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.

Extract isStagingEnvironment helper and replace inline staging checks

Kiran K12f2d0ec829f
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/discounts/group-discounts.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/discounts/group-discounts.tsx
index 79a5fb0a8e..2c9ea974f6 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/discounts/group-discounts.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/discounts/group-discounts.tsx
@@ -1,6 +1,7 @@
"use client";
import { useCopyDiscountToLiveModal } from "@/lib/sandbox/components/copy-discount-to-live-modal";
+import { isStagingEnvironment } from "@/lib/sandbox/workspace-guards";
import useGroup from "@/lib/swr/use-group";
import useWorkspace from "@/lib/swr/use-workspace";
import type { DiscountProps, GroupProps } from "@/lib/types";
@@ -9,7 +10,6 @@ import { useDiscountSheet } from "@/ui/partners/discounts/add-edit-discount-shee
import { ProgramRewardDescription } from "@/ui/partners/program-reward-description";
import { Button, useRouterStuff } from "@dub/ui";
import { cn, isClickOnInteractiveChild } from "@dub/utils";
-import { WorkspaceEnvironment } from "@prisma/client";
import { BadgePercent } from "lucide-react";
import Link from "next/link";
import { useParams } from "next/navigation";
@@ -80,7 +80,7 @@ const DiscountItem = ({
return (
<>
- {discount && environment === WorkspaceEnvironment.staging && (
+ {discount && isStagingEnvironment(environment) && (
<CopyDiscountToLiveModal />
)}
<As
@@ -126,7 +126,7 @@ const DiscountItem = ({
<CopyDefaultDiscountButton />
)}
- {discount && environment === WorkspaceEnvironment.staging && (
+ {discount && isStagingEnvironment(environment) && (
<Button
text="Copy to live"
variant="secondary"
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/rewards/group-rewards.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/rewards/group-rewards.tsx
index 7bab828c60..59d9cf9751 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/rewards/group-rewards.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/rewards/group-rewards.tsx
@@ -2,6 +2,7 @@
import { getPlanCapabilities } from "@/lib/plan-capabilities";
import { useCopyRewardToLiveModal } from "@/lib/sandbox/components/copy-reward-to-live-modal";
+import { isStagingEnvironment } from "@/lib/sandbox/workspace-guards";
import useGroup from "@/lib/swr/use-group";
import useWorkspace from "@/lib/swr/use-workspace";
import type { GroupProps, RewardProps } from "@/lib/types";
@@ -22,7 +23,7 @@ import {
useRouterStuff,
} from "@dub/ui";
import { cn, formatDate, isClickOnInteractiveChild } from "@dub/utils";
-import { EventType, WorkspaceEnvironment } from "@prisma/client";
+import { EventType } from "@prisma/client";
import Link from "next/link";
import { useParams } from "next/navigation";
import { useEffect, useState } from "react";
@@ -169,9 +170,7 @@ const RewardItem = ({
{partnersUpgradeModal}
{RewardSheet}
{rewardHistorySheet}
- {reward && environment === WorkspaceEnvironment.staging && (
- <CopyRewardToLiveModal />
- )}
+ {reward && isStagingEnvironment(environment) && <CopyRewardToLiveModal />}
<As
href={
reward
@@ -272,7 +271,7 @@ const RewardItem = ({
{reward ? (
<div className="flex items-center gap-2">
- {environment === WorkspaceEnvironment.staging && (
+ {isStagingEnvironment(environment) && (
<Button
text="Copy to live"
variant="secondary"
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/members/page.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/members/page.tsx
index aa88ebabef..aedae99723 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/members/page.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/settings/members/page.tsx
@@ -2,6 +2,7 @@
import { clientAccessCheck } from "@/lib/client-access-check";
import { exceededLimitError } from "@/lib/exceeded-limit-error";
+import { isStagingEnvironment } from "@/lib/sandbox/workspace-guards";
import useWorkspace from "@/lib/swr/use-workspace";
import useWorkspaceUsers from "@/lib/swr/use-workspace-users";
import { WorkspaceUserProps } from "@/lib/types";
@@ -38,7 +39,7 @@ import {
UserCheck,
} from "@dub/ui/icons";
import { cn, fetcher, timeAgo } from "@dub/utils";
-import { WorkspaceEnvironment, WorkspaceRole } from "@prisma/client";
+import { WorkspaceRole } from "@prisma/client";
import { ColumnDef, Row } from "@tanstack/react-table";
import { Command } from "cmdk";
import { UserMinus } from "lucide-react";
@@ -114,7 +115,6 @@ export default function WorkspaceMembersPage() {
}, [plan]);
const isCurrentUserOwner = role === "owner";
- const isStaging = environment === WorkspaceEnvironment.staging;
const filters = useMemo(
() => [
@@ -168,7 +168,10 @@ export default function WorkspaceMembersPage() {
}, [status, roleFilter]);
useKeyboardShortcut("m", () => setShowInviteWorkspaceUserModal(true), {
- enabled: !isStaging && !invitePermissionError && !isAtUserLimit,
+ enabled:
+ !isStagingEnvironment(environment) &&
+ !invitePermissionError &&
+ !isAtUserLimit,
});
const columns = useMemo<ColumnDef<WorkspaceUserProps>[]>(
@@ -371,7 +374,6 @@ function RoleCell({
isCurrentUserOwner: boolean;
}) {
const { plan, environment } = useWorkspace();
- const isStaging = environment === WorkspaceEnvironment.staging;
const [role, setRole] = useState<WorkspaceRole>(user.role);
useEffect(() => {
@@ -406,7 +408,7 @@ function RoleCell({
? "Only owners can change member roles"
: isCurrentUser
? "You cannot change your own role"
- : isStaging
+ : isStagingEnvironment(environment)
? "Roles cannot be updated in staging workspaces (automatically synced from your production workspace)"
: undefined;
@@ -463,7 +465,6 @@ function RowMenuButton({
const [isOpen, setIsOpen] = useState(false);
const { data: session } = useSession();
const { environment } = useWorkspace();
- const isStaging = environment === WorkspaceEnvironment.staging;
const user = row.original;
const searchParams = useSearchParams();
@@ -487,7 +488,7 @@ function RowMenuButton({
const isCurrentUser = session?.user?.email === user.email;
// In staging, only show menu for self (leave workspace)
- if (isStaging && !isCurrentUser) {
+ if (isStagingEnvironment(environment) && !isCurrentUser) {
return null;
}
diff --git a/apps/web/lib/client-access-check.ts b/apps/web/lib/client-access-check.ts
index 83e720178c..cb4f48d266 100644
--- a/apps/web/lib/client-access-check.ts
+++ b/apps/web/lib/client-access-check.ts
@@ -1,6 +1,7 @@
import { combineWords } from "@dub/utils";
import { WorkspaceEnvironment, WorkspaceRole } from "@prisma/client";
import { PermissionAction, ROLE_PERMISSIONS } from "./api/rbac/permissions";
+import { isStagingEnvironment } from "./sandbox/workspace-guards";
export const clientAccessCheck = ({
action,
@@ -15,7 +16,7 @@ export const clientAccessCheck = ({
environment?: WorkspaceEnvironment | null;
stagingBehavior?: "blocked" | "production-only";
}) => {
- if (environment === WorkspaceEnvironment.staging) {
+ if (isStagingEnvironment(environment)) {
switch (stagingBehavior) {
case "blocked":
return {
diff --git a/apps/web/lib/sandbox/components/program-environment-banner.tsx b/apps/web/lib/sandbox/components/program-environment-banner.tsx
index fea430e21c..755b7b72cc 100644
--- a/apps/web/lib/sandbox/components/program-environment-banner.tsx
+++ b/apps/web/lib/sandbox/components/program-environment-banner.tsx
@@ -1,6 +1,7 @@
import { IsolatedCube } from "@dub/ui/icons";
import { capitalize, cn } from "@dub/utils";
import { WorkspaceEnvironment } from "@prisma/client";
+import { isStagingEnvironment } from "../workspace-guards";
export function ProgramEnvironmentBanner({
environment,
@@ -15,9 +16,7 @@ export function ProgramEnvironmentBanner({
<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",
- environment === WorkspaceEnvironment.staging
- ? "bg-amber-200"
- : "bg-blue-200",
+ isStagingEnvironment(environment) ? "bg-amber-200" : "bg-blue-200",
)}
>
<div className="flex shrink-0 items-center gap-2">
diff --git a/apps/web/lib/sandbox/components/workspace-environment-banner.tsx b/apps/web/lib/sandbox/components/workspace-environment-banner.tsx
index e5177e3cc9..2cb459d496 100644
--- a/apps/web/lib/sandbox/components/workspace-environment-banner.tsx
+++ b/apps/web/lib/sandbox/components/workspace-environment-banner.tsx
@@ -10,6 +10,7 @@ import { WorkspaceEnvironment } from "@prisma/client";
import { motion } from "motion/react";
import Link from "next/link";
import { usePathname } from "next/navigation";
+import { isStagingEnvironment } from "../workspace-guards";
export function WorkspaceEnvironmentBanner() {
const pathname = usePathname();
@@ -39,7 +40,7 @@ export function WorkspaceEnvironmentBanner() {
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",
- currentWorkspace.environment === WorkspaceEnvironment.staging
+ isStagingEnvironment(currentWorkspace.environment)
? "bg-amber-200"
: "bg-blue-200",
)}
diff --git a/apps/web/lib/sandbox/components/workspace-environment-switcher.tsx b/apps/web/lib/sandbox/components/workspace-environment-switcher.tsx
index a608466b1a..dcbf550061 100644
--- a/apps/web/lib/sandbox/components/workspace-environment-switcher.tsx
+++ b/apps/web/lib/sandbox/components/workspace-environment-switcher.tsx
@@ -4,6 +4,7 @@ import { WorkspaceProps } from "@/lib/types";
import { IsolatedCube } from "@dub/ui/icons";
import { WorkspaceEnvironment } from "@prisma/client";
import Link from "next/link";
+import { 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";
@@ -31,7 +32,7 @@ export function WorkspaceEnvironmentSwitcher({
let label: string;
// Exit staging
- if (currentWorkspace.environment === WorkspaceEnvironment.staging) {
+ if (isStagingEnvironment(currentWorkspace.environment)) {
targetWorkspace = workspaces.find(
(workspace) => workspace.stagingWorkspaceId === currentWorkspace.id,
);
diff --git a/apps/web/lib/sandbox/workspace-guards.ts b/apps/web/lib/sandbox/workspace-guards.ts
index aa8b94d117..57ad23cbc2 100644
--- a/apps/web/lib/sandbox/workspace-guards.ts
+++ b/apps/web/lib/sandbox/workspace-guards.ts
@@ -10,7 +10,7 @@ export function assertNotStagingWorkspace(
) {
const shouldCheck = options?.when ?? true;
- if (shouldCheck && workspace.environment === WorkspaceEnvironment.staging) {
+ if (shouldCheck && isStagingEnvironment(workspace.environment)) {
throw new DubApiError({
code: "forbidden",
message:
@@ -41,3 +41,9 @@ export function assertProductionWorkspace(
});
}
}
+
+export function isStagingEnvironment(
+ environment: WorkspaceEnvironment | null | undefined,
+) {
+ return Boolean(environment && environment === WorkspaceEnvironment.staging);
+}
diff --git a/apps/web/ui/layout/sidebar/workspace-dropdown.tsx b/apps/web/ui/layout/sidebar/workspace-dropdown.tsx
index 1f557c6c85..aa95704022 100644
--- a/apps/web/ui/layout/sidebar/workspace-dropdown.tsx
+++ b/apps/web/ui/layout/sidebar/workspace-dropdown.tsx
@@ -1,6 +1,7 @@
"use client";
import { WorkspaceEnvironmentSwitcher } from "@/lib/sandbox/components/workspace-environment-switcher";
+import { isStagingEnvironment } from "@/lib/sandbox/workspace-guards";
import useWorkspaceUsers from "@/lib/swr/use-workspace-users";
import useWorkspaces from "@/lib/swr/use-workspaces";
import { PlanProps, WorkspaceProps } from "@/lib/types";
@@ -165,7 +166,7 @@ function WorkspaceList({
const current = workspaces.find((w) => w.slug === selected.slug);
const activeSlug =
- current?.environment === WorkspaceEnvironment.staging
+ current && isStagingEnvironment(current.environment)
? workspaces.find((w) => w.stagingWorkspaceId === current.id)?.slug ??
selected.slug
: selected.slug;