The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add `WorkspaceEnvironmentIndicator`
Steven Teyd9b85448e8ec
diff --git a/apps/web/app/app.dub.co/(onboarding)/workspaces/page.tsx b/apps/web/app/app.dub.co/(onboarding)/workspaces/page.tsxindex bb55f05a9e..b8014b537f 100644--- a/apps/web/app/app.dub.co/(onboarding)/workspaces/page.tsx+++ b/apps/web/app/app.dub.co/(onboarding)/workspaces/page.tsx@@ -1,5 +1,6 @@"use client";+import { WorkspaceEnvironmentIndicator } from "@/lib/sandbox/components/workspace-environment-indicator";import { isStagingEnvironment } from "@/lib/sandbox/environment";import useWorkspaces from "@/lib/swr/use-workspaces";import { WorkspaceProps } from "@/lib/types";@@ -160,7 +161,7 @@ function WorkspaceList({ workspaces }: { workspaces: WorkspaceProps[] }) {}function WorkspaceRow({- workspace: { id, name, slug, logo, plan, disabledAt },+ workspace: { id, name, slug, logo, plan, disabledAt, environment },onFocus,}: {workspace: WorkspaceProps;@@ -185,8 +186,11 @@ function WorkspaceRow({className="size-6 shrink-0 overflow-hidden rounded-full"draggable={false}/>- <span className="min-w-0 flex-1 truncate text-sm font-medium text-neutral-900">- {name}+ <span className="flex min-w-0 flex-1 items-center gap-1.5">+ <span className="min-w-0 truncate text-sm font-medium text-neutral-900">+ {name}+ </span>+ <WorkspaceEnvironmentIndicator environment={environment} /></span><div className="flex shrink-0 items-center gap-1.5">{disabledAt && (diff --git a/apps/web/lib/sandbox/components/workspace-environment-indicator.tsx b/apps/web/lib/sandbox/components/workspace-environment-indicator.tsxnew file mode 100644index 0000000000..6d7132cd03--- /dev/null+++ b/apps/web/lib/sandbox/components/workspace-environment-indicator.tsx@@ -0,0 +1,56 @@+"use client";++import { IsolatedCube, Tooltip } from "@dub/ui";+import { cn } from "@dub/utils";+import { WorkspaceEnvironment } from "@prisma/client";+import { isSandboxEnvironment, isStagingEnvironment } from "../environment";++const INDICATORS = {+ sandbox: {+ className: "bg-blue-200",+ content:+ "This is a [sandbox workspace](https://dub.co/docs/testing). No real money or payouts are processed.",+ },+ staging: {+ className: "bg-amber-200",+ content:+ "This is a [staging workspace](https://dub.co/docs/testing). No real money or payouts are processed.",+ },+} as const;++export function WorkspaceEnvironmentIndicator({+ environment,+ className,+}: {+ environment: WorkspaceEnvironment | null | undefined;+ className?: string;+}) {+ const indicator = isSandboxEnvironment(environment)+ ? INDICATORS.sandbox+ : isStagingEnvironment(environment)+ ? INDICATORS.staging+ : null;++ if (!indicator) {+ return null;+ }++ return (+ <Tooltip content={indicator.content}>+ <span+ className={cn(+ "flex size-5 shrink-0 cursor-help items-center justify-center rounded-md",+ indicator.className,+ className,+ )}+ >+ {/* Glyph is drawn around (8, 8) inside the 18×18 viewBox, so shift it to center in the badge */}+ <IsolatedCube+ viewBox="-1 -1 18 18"+ className="size-3.5"+ aria-hidden="true"+ />+ </span>+ </Tooltip>+ );+}diff --git a/apps/web/lib/sandbox/environment.ts b/apps/web/lib/sandbox/environment.tsindex b79a2a3d7e..7adb814868 100644--- a/apps/web/lib/sandbox/environment.ts+++ b/apps/web/lib/sandbox/environment.ts@@ -17,6 +17,12 @@ export function isProductionEnvironment();}+export function isSandboxEnvironment(+ environment: WorkspaceEnvironment | null | undefined,+) {+ return Boolean(environment && environment === WorkspaceEnvironment.sandbox);+}+export function getPublishableKeyPrefix(environment: WorkspaceEnvironment | null | undefined,) {diff --git a/apps/web/ui/account/update-default-workspace.tsx b/apps/web/ui/account/update-default-workspace.tsxindex 69ffd4becd..51e6ff2089 100644--- a/apps/web/ui/account/update-default-workspace.tsx+++ b/apps/web/ui/account/update-default-workspace.tsx@@ -2,7 +2,6 @@import { WorkspaceSelector } from "@/ui/workspaces/workspace-selector";import { Button } from "@dub/ui";-import { WorkspaceEnvironment } from "@prisma/client";import { useSession } from "next-auth/react";import { useEffect, useState } from "react";import { toast } from "sonner";@@ -64,7 +63,6 @@ export default function UpdateDefaultWorkspace() {<WorkspaceSelectorselectedWorkspace={selectedWorkspace || ""}setSelectedWorkspace={setSelectedWorkspace}- environments={[WorkspaceEnvironment.production]}showAddWorkspaceButton={false}/></div>diff --git a/apps/web/ui/layout/sidebar/workspace-dropdown.tsx b/apps/web/ui/layout/sidebar/workspace-dropdown.tsxindex 30cf1bdc64..af0ac5e0b2 100644--- a/apps/web/ui/layout/sidebar/workspace-dropdown.tsx+++ b/apps/web/ui/layout/sidebar/workspace-dropdown.tsx@@ -1,5 +1,6 @@"use client";+import { WorkspaceEnvironmentIndicator } from "@/lib/sandbox/components/workspace-environment-indicator";import { WorkspaceEnvironmentSwitcher } from "@/lib/sandbox/components/workspace-environment";import { isStagingEnvironment } from "@/lib/sandbox/environment";import useWorkspaceUsers from "@/lib/swr/use-workspace-users";@@ -16,6 +17,7 @@ import {} from "@dub/ui";import { Check2, Gear, Plus, UserPlus } from "@dub/ui/icons";import { cn, isLegacyBusinessPlan, pluralize } from "@dub/utils";+import { WorkspaceEnvironment } from "@prisma/client";import { useSession } from "next-auth/react";import Link from "next/link";import { useParams, usePathname } from "next/navigation";@@ -69,6 +71,7 @@ export function WorkspaceDropdown() {image: string;plan: PlanProps;disabledAt?: Date | string | null;+ environment?: WorkspaceEnvironment | null;};const [openPopover, setOpenPopover] = useState(false);@@ -132,6 +135,7 @@ function WorkspaceList({image: string;plan: PlanProps;disabledAt?: Date | string | null;+ environment?: WorkspaceEnvironment | null;};workspaces: WorkspaceProps[];setOpenPopover: (open: boolean) => void;@@ -266,7 +270,7 @@ function WorkspaceList({.filter((workspace) => !isStagingEnvironment(workspace.environment),)- .map(({ id, name, slug, logo, disabledAt }) => {+ .map(({ id, name, slug, logo, disabledAt, environment }) => {const isActive = activeSlug === slug;return (<Link@@ -292,6 +296,7 @@ function WorkspaceList({<span className="min-w-0 flex-1 truncate text-base leading-5 text-neutral-900 sm:text-sm">{name}</span>+ <WorkspaceEnvironmentIndicator environment={environment} />{disabledAt && (<StatusBadgevariant="neutral"diff --git a/apps/web/ui/support/chat-interface.tsx b/apps/web/ui/support/chat-interface.tsxindex eef0c8ad32..b874c7670a 100644--- a/apps/web/ui/support/chat-interface.tsx+++ b/apps/web/ui/support/chat-interface.tsx@@ -6,6 +6,7 @@ import { useChat } from "@ai-sdk/react";import { Combobox } from "@dub/ui";import { OfficeBuilding, PaperPlane, Users2, Xmark } from "@dub/ui/icons";import { cn, fetcher, OG_AVATAR_URL } from "@dub/utils";+import { WorkspaceEnvironment } from "@prisma/client";import { DefaultChatTransport, isFileUIPart, UIMessage } from "ai";import { Paperclip } from "lucide-react";import { useSession } from "next-auth/react";@@ -27,7 +28,11 @@ import { extractSources, SourceCitations } from "./source-citations";import { StarterQuestions } from "./starter-questions";import { StatusIndicator } from "./status-indicator";import { TicketUpload } from "./ticket-upload";-import { WorkspaceCombobox, WorkspaceSummary } from "./workspace-combobox";+import {+ DEFAULT_WORKSPACE_ENVIRONMENTS,+ WorkspaceCombobox,+ WorkspaceSummary,+} from "./workspace-combobox";type AccountType = "workspace" | "partner";@@ -91,10 +96,29 @@ export function ChatInterface({const effectiveAccountType = selection.accountType;- const { data: workspaces } = useSWR<WorkspaceSummary[]>(+ const { data: workspaces } = useSWR<+ (WorkspaceSummary & { environment: WorkspaceEnvironment })[]+ >(effectiveAccountType === "workspace" ? "/api/workspaces" : null,fetcher,);+ useEffect(() => {+ if (!workspaces || !selection.selectedWorkspace) return;++ const stillVisible = workspaces.some(+ (workspace) =>+ workspace.slug === selection.selectedWorkspace?.slug &&+ DEFAULT_WORKSPACE_ENVIRONMENTS.includes(workspace.environment),+ );++ if (!stillVisible) {+ setSelection((current) =>+ current.selectedWorkspace+ ? { ...current, selectedWorkspace: undefined }+ : current,+ );+ }+ }, [workspaces, selection.selectedWorkspace]);const { programEnrollments, isLoading: isLoadingPrograms } =useProgramEnrollments();@@ -555,6 +579,7 @@ export function ChatInterface({<div className="mt-3 w-full max-w-72"><WorkspaceComboboxworkspaces={workspaces}+ environments={DEFAULT_WORKSPACE_ENVIRONMENTS}selectedSlug={selection.selectedWorkspace?.slug}onSelect={handleWorkspaceSelect}/>diff --git a/apps/web/ui/support/workspace-combobox.tsx b/apps/web/ui/support/workspace-combobox.tsxindex f91d33e106..29632157c1 100644--- a/apps/web/ui/support/workspace-combobox.tsx+++ b/apps/web/ui/support/workspace-combobox.tsx@@ -1,30 +1,54 @@"use client";+import { WorkspaceEnvironmentIndicator } from "@/lib/sandbox/components/workspace-environment-indicator";+import { isStagingEnvironment } from "@/lib/sandbox/environment";import { Combobox } from "@dub/ui";import { cn, OG_AVATAR_URL } from "@dub/utils";+import { WorkspaceEnvironment } from "@prisma/client";import { useMemo, useState } from "react";+export const DEFAULT_WORKSPACE_ENVIRONMENTS: WorkspaceEnvironment[] = [+ WorkspaceEnvironment.production,+ WorkspaceEnvironment.sandbox,+];+export type WorkspaceSummary = {id: string;name: string;slug: string;logo?: string | null;+ environment?: WorkspaceEnvironment;};+// TODO: consolidate with WorkspaceSelectorexport function WorkspaceCombobox({workspaces,selectedSlug,onSelect,+ environments = DEFAULT_WORKSPACE_ENVIRONMENTS,}: {workspaces: WorkspaceSummary[] | undefined;selectedSlug?: string;onSelect: (ws: WorkspaceSummary) => void;+ environments?: WorkspaceEnvironment[]; // only display workspaces in these environments (defaults to production and sandbox)}) {const [open, setOpen] = useState(false);+ const filteredWorkspaces = useMemo(() => {+ if (!workspaces) {+ return workspaces;+ }++ return workspaces.filter((workspace) =>+ environments+ ? workspace.environment && environments.includes(workspace.environment)+ : !isStagingEnvironment(workspace.environment),+ );+ }, [workspaces, environments]);+const options = useMemo(() =>- workspaces?.map((ws) => ({+ filteredWorkspaces?.map((ws) => ({value: ws.slug,label: ws.name,icon: (@@ -35,7 +59,7 @@ export function WorkspaceCombobox({/>),})),- [workspaces],+ [filteredWorkspaces],);const selected = useMemo(@@ -43,19 +67,30 @@ export function WorkspaceCombobox({[options, selectedSlug],);+ const environmentBySlug = useMemo(+ () =>+ new Map(+ (filteredWorkspaces ?? []).map((workspace) => [+ workspace.slug,+ workspace.environment,+ ]),+ ),+ [filteredWorkspaces],+ );+return (<ComboboxforceDropdown- options={workspaces === undefined ? undefined : options}+ options={filteredWorkspaces === undefined ? undefined : options}setSelected={(opt) => {if (!opt) return;- const ws = workspaces?.find((w) => w.slug === opt.value);+ const ws = filteredWorkspaces?.find((w) => w.slug === opt.value);if (ws) onSelect(ws);}}selected={selected}icon={selected?.icon}caret- placeholder={workspaces === undefined ? "" : "Select workspace"}+ placeholder={filteredWorkspaces === undefined ? "" : "Select workspace"}searchPlaceholder="Search workspaces..."matchTriggerWidthpopoverProps={{@@ -75,20 +110,30 @@ export function WorkspaceCombobox({}}inputClassName="text-sm h-10"optionClassName="h-8"+ optionRight={(option) => (+ <WorkspaceEnvironmentIndicator+ environment={environmentBySlug.get(option.value)}+ />+ )}emptyState={<div className="flex w-full flex-col items-center gap-2 py-3 text-xs text-neutral-500">No workspaces found</div>}>- {workspaces === undefined ? (+ {filteredWorkspaces === undefined ? (<div className="flex items-center gap-1.5 text-sm"><div className="size-3.5 animate-pulse rounded-full bg-neutral-200" /><div className="h-3.5 w-24 animate-pulse rounded bg-neutral-200" /></div>- ) : (- selected?.label- )}+ ) : selected ? (+ <span className="flex w-full min-w-0 items-center gap-1.5">+ <span className="min-w-0 truncate">{selected.label}</span>+ <WorkspaceEnvironmentIndicator+ environment={environmentBySlug.get(selected.value)}+ />+ </span>+ ) : null}</Combobox>);}diff --git a/apps/web/ui/workspaces/workspace-selector.tsx b/apps/web/ui/workspaces/workspace-selector.tsxindex 0c8e2ee6d0..8a9c45df9b 100644--- a/apps/web/ui/workspaces/workspace-selector.tsx+++ b/apps/web/ui/workspaces/workspace-selector.tsx@@ -1,3 +1,4 @@+import { WorkspaceEnvironmentIndicator } from "@/lib/sandbox/components/workspace-environment-indicator";import { isStagingEnvironment } from "@/lib/sandbox/environment";import useWorkspaces from "@/lib/swr/use-workspaces";import { BlurImage, Button, Combobox } from "@dub/ui";@@ -9,14 +10,17 @@ import { useAddWorkspaceModal } from "../modals/add-workspace-modal";interface WorkspaceSelectorProps {selectedWorkspace: string;setSelectedWorkspace: (workspace: string | null) => void;- environments?: WorkspaceEnvironment[]; // Only display workspaces that are in the environments+ environments?: WorkspaceEnvironment[]; // only display workspaces in these environments (defaults to production and sandbox)showAddWorkspaceButton?: boolean;}export function WorkspaceSelector({selectedWorkspace,setSelectedWorkspace,- environments,+ environments = [+ WorkspaceEnvironment.production,+ WorkspaceEnvironment.sandbox,+ ],showAddWorkspaceButton = true,}: WorkspaceSelectorProps) {const [openPopover, setOpenPopover] = useState(false);@@ -55,6 +59,17 @@ export function WorkspaceSelector({[filteredWorkspaces],);+ const environmentBySlug = useMemo(+ () =>+ new Map(+ filteredWorkspaces.map((workspace) => [+ workspace.slug,+ workspace.environment,+ ]),+ ),+ [filteredWorkspaces],+ );+const selectedOption = useMemo(() => {if (!selectedWorkspace) {return null;@@ -106,6 +121,11 @@ export function WorkspaceSelector({"focus:ring-1 focus:ring-neutral-500 focus:border-neutral-500 transition-none",),}}+ optionRight={(option) => (+ <WorkspaceEnvironmentIndicator+ environment={environmentBySlug.get(option.value)}+ />+ )}emptyState={<div className="flex w-full flex-col items-center gap-2 py-4">No workspaces found@@ -128,9 +148,14 @@ export function WorkspaceSelector({<div className="size-4 animate-pulse rounded-full bg-neutral-200" /><div className="h-4 w-32 animate-pulse rounded-md bg-neutral-200" /></div>- ) : (- selectedOption?.label- )}+ ) : selectedOption ? (+ <span className="flex w-full min-w-0 items-center gap-1.5">+ <span className="min-w-0 truncate">{selectedOption.label}</span>+ <WorkspaceEnvironmentIndicator+ environment={environmentBySlug.get(selectedOption.value)}+ />+ </span>+ ) : null}</Combobox></>);