The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
add WorkspaceSelector
Steven Teyac2d14cef164
diff --git a/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsx b/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsxindex 6b4d5f1638..c9048077aa 100644--- a/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsx+++ b/apps/web/app/app.dub.co/(auth)/oauth/authorize/authorize-form.tsx@@ -5,11 +5,10 @@ import useWorkspaces from "@/lib/swr/use-workspaces";import z from "@/lib/zod";import { authorizeRequestSchema } from "@/lib/zod/schemas/oauth";import { useAddWorkspaceModal } from "@/ui/modals/add-workspace-modal";-import { Button, InputSelect, InputSelectItemProps } from "@dub/ui";-import { OfficeBuilding } from "@dub/ui/icons";-import { OG_AVATAR_URL } from "@dub/utils";+import { WorkspaceSelector } from "@/ui/workspaces/workspace-selector";+import { Button } from "@dub/ui";import { useSession } from "next-auth/react";-import { useEffect, useMemo, useState } from "react";+import { useEffect, useState } from "react";import { toast } from "sonner";interface AuthorizeFormProps extends z.infer<typeof authorizeRequestSchema> {@@ -30,29 +29,16 @@ export const AuthorizeForm = ({const { AddWorkspaceModal, setShowAddWorkspaceModal } =useAddWorkspaceModal();const [submitting, setSubmitting] = useState(false);- const [selectedWorkspace, setSelectedWorkspace] =- useState<InputSelectItemProps | null>(null);+ const [selectedWorkspace, setSelectedWorkspace] = useState<string | null>(+ null,+ );// missing scopes for the user's role on the workspace selectedconst [missingScopes, setMissingScopes] = useState<string[]>([]);- const selectOptions = useMemo(() => {- return workspaces- ? workspaces.map((workspace) => ({- id: workspace.slug,- value: workspace.name,- image: workspace.logo || `${OG_AVATAR_URL}${workspace.name}`,- }))- : [];- }, [workspaces]);-useEffect(() => {- setSelectedWorkspace(- selectOptions.find(- (option) => option.id === session?.user?.["defaultWorkspace"],- ) || null,- );- }, [selectOptions, session]);+ setSelectedWorkspace(session?.user?.["defaultWorkspace"] || null);+ }, [session]);// Check if the user has the required scopes for the workspace selecteduseEffect(() => {@@ -61,7 +47,7 @@ export const AuthorizeForm = ({}const workspace = workspaces?.find(- (workspace) => workspace.slug === selectedWorkspace.id,+ (workspace) => workspace.slug === selectedWorkspace,);if (!workspace) {@@ -99,7 +85,7 @@ export const AuthorizeForm = ({setSubmitting(true);const workspaceId = workspaces?.find(- (workspace) => workspace.slug === selectedWorkspace.id,+ (workspace) => workspace.slug === selectedWorkspace,)?.id;const response = await fetch(@@ -144,21 +130,9 @@ export const AuthorizeForm = ({Select a workspace to grant API access to</p><div className="max-w-md py-2">- <InputSelect- items={selectOptions}- selectedItem={selectedWorkspace}- setSelectedItem={setSelectedWorkspace}- adjustForMobile- disabled={submitting}- noItemsElement={- <Button- icon={<OfficeBuilding className="size-4" />}- text="Create new workspace"- variant="outline"- onClick={() => setShowAddWorkspaceModal(true)}- className="justify-start text-neutral-700"- />- }+ <WorkspaceSelector+ selectedWorkspace={selectedWorkspace || ""}+ setSelectedWorkspace={setSelectedWorkspace}/></div><div className="mt-4 flex justify-between gap-4">diff --git a/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/domain/domain-selector.tsx b/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/domain/default-domain-selector.tsxsimilarity index 99%rename from apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/domain/domain-selector.tsxrename to apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/domain/default-domain-selector.tsxindex 5db6abc237..96e0f39562 100644--- a/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/domain/domain-selector.tsx+++ b/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/domain/default-domain-selector.tsx@@ -11,7 +11,7 @@ import { ReactNode, useEffect, useState } from "react";import { LaterButton } from "../../later-button";import { useOnboardingProgress } from "../../use-onboarding-progress";-export function DomainSelector() {+export function DefaultDomainSelector() {const { loading: isWorkspaceLoading } = useWorkspace();const { continueTo, isLoading, isSuccessful } = useOnboardingProgress();diff --git a/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/domain/page.tsx b/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/domain/page.tsxindex a817deea6b..22e59a6553 100644--- a/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/domain/page.tsx+++ b/apps/web/app/app.dub.co/(onboarding)/onboarding/(steps)/domain/page.tsx@@ -1,6 +1,6 @@import { Globe } from "@dub/ui/icons";import { StepPage } from "../step-page";-import { DomainSelector } from "./domain-selector";+import { DefaultDomainSelector } from "./default-domain-selector";export default function Domain() {return (@@ -10,7 +10,7 @@ export default function Domain() {description="Take your links to the next level with your own branding."className="max-w-none">- <DomainSelector />+ <DefaultDomainSelector /></StepPage>);}diff --git a/apps/web/ui/account/update-default-workspace.tsx b/apps/web/ui/account/update-default-workspace.tsxindex 1cf63ff475..e4f34bcc8f 100644--- a/apps/web/ui/account/update-default-workspace.tsx+++ b/apps/web/ui/account/update-default-workspace.tsx@@ -1,41 +1,20 @@"use client";-import useWorkspaces from "@/lib/swr/use-workspaces";-import { Button, InputSelect, InputSelectItemProps } from "@dub/ui";-import { OG_AVATAR_URL } from "@dub/utils";+import { WorkspaceSelector } from "@/ui/workspaces/workspace-selector";+import { Button } from "@dub/ui";import { useSession } from "next-auth/react";-import { useEffect, useMemo, useState } from "react";+import { useEffect, useState } from "react";import { toast } from "sonner";export default function UpdateDefaultWorkspace() {const { data: session, update } = useSession();- const { workspaces } = useWorkspaces();-- const selectOptions = useMemo(() => {- return workspaces- ? workspaces.map((workspace) => ({- id: workspace.slug,- value: workspace.name,- image: workspace.logo || `${OG_AVATAR_URL}${workspace.name}`,- disabled: workspace.slug === session?.user?.["defaultWorkspace"],- label:- workspace.slug === session?.user?.["defaultWorkspace"]- ? "Current"- : "",- }))- : [];- }, [workspaces, session]);-- const [selectedWorkspace, setSelectedWorkspace] =- useState<InputSelectItemProps | null>(null);+ const [selectedWorkspace, setSelectedWorkspace] = useState<string | null>(+ null,+ );useEffect(() => {- setSelectedWorkspace(- selectOptions.find(- (option) => option.id === session?.user?.["defaultWorkspace"],- ) || null,- );- }, [selectOptions, session]);+ setSelectedWorkspace(session?.user?.["defaultWorkspace"] || null);+ }, [session]);const [saving, setSaving] = useState(false);@@ -47,7 +26,7 @@ export default function UpdateDefaultWorkspace() {"Content-Type": "application/json",},body: JSON.stringify({- defaultWorkspace: selectedWorkspace?.id || undefined,+ defaultWorkspace: selectedWorkspace || undefined,}),});@@ -79,11 +58,9 @@ export default function UpdateDefaultWorkspace() {Choose the workspace to show by default when you sign in.</p><div className="mt-1 max-w-md">- <InputSelect- items={selectOptions}- selectedItem={selectedWorkspace}- setSelectedItem={setSelectedWorkspace}- adjustForMobile+ <WorkspaceSelector+ selectedWorkspace={selectedWorkspace || ""}+ setSelectedWorkspace={setSelectedWorkspace}/></div></div>@@ -102,7 +79,7 @@ export default function UpdateDefaultWorkspace() {loading={saving}disabled={!selectedWorkspace ||- selectedWorkspace.id === session?.user?.["defaultWorkspace"]+ selectedWorkspace === session?.user?.["defaultWorkspace"]}/></div>diff --git a/apps/web/ui/workspaces/workspace-selector.tsx b/apps/web/ui/workspaces/workspace-selector.tsxnew file mode 100644index 0000000000..d0db229f62--- /dev/null+++ b/apps/web/ui/workspaces/workspace-selector.tsx@@ -0,0 +1,109 @@+import useWorkspaces from "@/lib/swr/use-workspaces";+import { Button, Combobox } from "@dub/ui";+import { OG_AVATAR_URL, cn } from "@dub/utils";+import { useMemo, useState } from "react";+import { useAddWorkspaceModal } from "../modals/add-workspace-modal";++interface WorkspaceSelectorProps {+ selectedWorkspace: string;+ setSelectedWorkspace: (workspace: string) => void;+}++export function WorkspaceSelector({+ selectedWorkspace,+ setSelectedWorkspace,+}: WorkspaceSelectorProps) {+ const [openPopover, setOpenPopover] = useState(false);+ const { workspaces, loading } = useWorkspaces();++ 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 selectedOption = useMemo(() => {+ if (!selectedWorkspace) {+ return null;+ }++ const workspace = workspaces?.find((w) => w.slug === selectedWorkspace);++ if (!workspace) {+ return null;+ }++ return {+ 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, selectedWorkspace]);++ return (+ <>+ <AddWorkspaceModal />+ <Combobox+ options={loading ? undefined : workspaceOptions}+ setSelected={(option) => {+ setSelectedWorkspace(option.value);+ }}+ selected={selectedOption}+ icon={loading ? null : selectedOption?.icon}+ caret={true}+ placeholder={loading ? "" : "Select workspace"}+ searchPlaceholder="Search workspaces..."+ matchTriggerWidth+ open={openPopover}+ onOpenChange={setOpenPopover}+ buttonProps={{+ className: cn(+ "w-full justify-start border-neutral-300 px-3",+ "data-[state=open]:ring-1 data-[state=open]:ring-neutral-500 data-[state=open]:border-neutral-500",+ "focus:ring-1 focus:ring-neutral-500 focus:border-neutral-500 transition-none",+ ),+ }}+ 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"+ />+ </div>+ }+ >+ {loading ? (+ <div className="flex items-center gap-3">+ <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+ )}+ </Combobox>+ </>+ );+}