The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add group selection to partner application approval
Tim Wilson200d6e28d108
diff --git a/apps/web/lib/api/groups/get-groups.ts b/apps/web/lib/api/groups/get-groups.tsindex 45f3174341..f869a15147 100644--- a/apps/web/lib/api/groups/get-groups.ts+++ b/apps/web/lib/api/groups/get-groups.ts@@ -22,6 +22,7 @@ const sortColumnsMap = {export async function getGroups(filters: GroupFilters) {const {search,+ groupIds,page,pageSize,sortBy,@@ -84,6 +85,7 @@ export async function getGroups(filters: GroupFilters) {}WHERE pg.programId = ${programId}${search ? Prisma.sql`AND (pg.name LIKE ${`%${search}%`} OR pg.slug LIKE ${`%${search}%`})` : Prisma.sql``}+ ${groupIds && groupIds.length > 0 ? Prisma.sql`AND pg.id IN (${Prisma.join(groupIds)})` : Prisma.sql``}GROUP BY pg.idORDER BY ${Prisma.raw(sortColumnsMap[sortBy])} ${Prisma.raw(sortOrder)}LIMIT ${pageSize} OFFSET ${(page - 1) * pageSize}diff --git a/apps/web/lib/swr/use-groups.ts b/apps/web/lib/swr/use-groups.tsindex 413b9fb8ae..93749f3cd2 100644--- a/apps/web/lib/swr/use-groups.ts+++ b/apps/web/lib/swr/use-groups.ts@@ -22,7 +22,7 @@ export default function useGroups<T extends GroupProps>({const { data, isLoading, error } = useSWR<T[]>(queryEnabled- ? `/api/groups${getQueryString({ workspaceId, ...query })}`+ ? `/api/groups${getQueryString({ workspaceId, ...query }, { exclude: ["partnerId"] })}`: null,fetcher,{diff --git a/apps/web/lib/zod/schemas/groups.ts b/apps/web/lib/zod/schemas/groups.tsindex 3c1f09dc9c..0765861af4 100644--- a/apps/web/lib/zod/schemas/groups.ts+++ b/apps/web/lib/zod/schemas/groups.ts@@ -52,9 +52,15 @@ export const changeGroupSchema = z.object({partnerIds: z.array(z.string()).min(1),});+export const GROUPS_MAX_PAGE_SIZE = 100;+export const getGroupsQuerySchema = z.object({search: z.string().optional(),+ groupIds: z+ .union([z.string(), z.array(z.string())])+ .transform((v) => (Array.isArray(v) ? v : v.split(",")))+ .optional(),sortBy: z.enum(["createdAt",@@ -71,7 +77,7 @@ export const getGroupsQuerySchema = zsortOrder: z.enum(["asc", "desc"]).default("desc"),includeExpandedFields: booleanQuerySchema.optional(),})- .merge(getPaginationQuerySchema({ pageSize: 100 }));+ .merge(getPaginationQuerySchema({ pageSize: GROUPS_MAX_PAGE_SIZE }));export const getGroupsCountQuerySchema = z.object({search: z.string().optional(),diff --git a/apps/web/ui/partners/groups/group-selector.tsx b/apps/web/ui/partners/groups/group-selector.tsxnew file mode 100644index 0000000000..1cd36b8eb1--- /dev/null+++ b/apps/web/ui/partners/groups/group-selector.tsx@@ -0,0 +1,98 @@+import useGroups from "@/lib/swr/use-groups";+import { GROUPS_MAX_PAGE_SIZE } from "@/lib/zod/schemas/groups";+import { Combobox } from "@dub/ui";+import { cn } from "@dub/utils";+import { useEffect, useMemo, useState } from "react";+import { useDebounce } from "use-debounce";+import { GroupColorCircle } from "./group-color-circle";++interface GroupSelectorProps {+ selectedGroupId: string | null;+ setSelectedGroupId: (groupId: string) => void;+ disabled?: boolean;+}++export function GroupSelector({+ selectedGroupId,+ setSelectedGroupId,+ disabled,+}: GroupSelectorProps) {+ const [search, setSearch] = useState("");+ const [useAsync, setUseAsync] = useState(false);+ const [debouncedSearch] = useDebounce(search, 500);+ const [openPopover, setOpenPopover] = useState(false);++ const { groups, loading } = useGroups({+ query: useAsync ? { search: debouncedSearch } : undefined,+ });++ const { groups: selectedGroups, loading: selectedGroupsLoading } = useGroups({+ query: selectedGroupId ? { groupIds: [selectedGroupId] } : undefined,+ });++ // Determine if we should use async loading+ useEffect(+ () =>+ setUseAsync(+ Boolean(groups && !useAsync && groups.length >= GROUPS_MAX_PAGE_SIZE),+ ),+ [groups, useAsync],+ );++ const groupOptions = useMemo(() => {+ return groups?.map((group) => ({+ value: group.id,+ label: group.name,+ icon: <GroupColorCircle group={group} />,+ }));+ }, [groups]);++ const selectedOption = useMemo(() => {+ if (!selectedGroupId) return null;++ const group = [...(groups || []), ...(selectedGroups || [])].find(+ (p) => p.id === selectedGroupId,+ );++ if (!group) return null;++ return {+ value: group.id,+ label: group.name,+ icon: <GroupColorCircle group={group} />,+ };+ }, [groups, selectedGroups, selectedGroupId]);++ return (+ <Combobox+ options={loading ? undefined : groupOptions}+ setSelected={(option) => {+ setSelectedGroupId(option.value);+ }}+ selected={selectedOption}+ icon={selectedGroupsLoading ? null : selectedOption?.icon}+ caret={true}+ placeholder={selectedGroupsLoading ? "" : "Select group"}+ searchPlaceholder="Search groups..."+ onSearchChange={setSearch}+ shouldFilter={!useAsync}+ matchTriggerWidth+ open={openPopover}+ onOpenChange={setOpenPopover}+ buttonProps={{+ disabled,+ 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",+ ),+ }}+ >+ {selectedGroupsLoading ? (+ <div className="my-0.5 h-5 w-1/3 animate-pulse rounded bg-neutral-200" />+ ) : (+ selectedOption?.label+ )}+ </Combobox>+ );+}diff --git a/apps/web/ui/partners/partner-application-sheet.tsx b/apps/web/ui/partners/partner-application-sheet.tsxindex 26876d4af9..9beea15fdf 100644--- a/apps/web/ui/partners/partner-application-sheet.tsx+++ b/apps/web/ui/partners/partner-application-sheet.tsx@@ -15,6 +15,7 @@ import { useAction } from "next-safe-action/hooks";import { Dispatch, SetStateAction, useEffect, useState } from "react";import { toast } from "sonner";import useSWRImmutable from "swr/immutable";+import { GroupSelector } from "./groups/group-selector";import { OnlinePresenceSummary } from "./online-presence-summary";import { PartnerInfoSection } from "./partner-info-section";import { PartnerLinkSelector } from "./partner-link-selector";@@ -180,6 +181,11 @@ function PartnerApproval({const { program } = useProgram();const [isApproving, setIsApproving] = useState(false);++ const [selectedGroupId, setSelectedGroupId] = useState<string | undefined>(+ partner.group?.id ?? undefined,+ );+const [selectedLinkId, setSelectedLinkId] = useState<string | null>(null);const [linkError, setLinkError] = useState(false);@@ -249,6 +255,7 @@ function PartnerApproval({workspaceId: workspaceId!,partnerId: partner.id,linkId: selectedLinkId,+ groupId: selectedGroupId!,});};@@ -257,29 +264,87 @@ function PartnerApproval({setSelectedLinkId(null);};- if (isMobile) {- return (- <div className="flex flex-col gap-3">- {/* First row - Approve button */}- <div className="flex">- <div className="flex grow">- <Button- type="button"- variant="primary"- text="Approve"- loading={isPending}- className="w-full"- onClick={handleApproveClick}- />+ return (+ <div className="flex flex-col gap-3">+ <GroupSelector+ selectedGroupId={selectedGroupId ?? null}+ setSelectedGroupId={setSelectedGroupId}+ />+ {isMobile ? (+ <div className="flex flex-col gap-3">+ {/* First row - Approve button */}+ <div className="flex">+ <div className="flex grow">+ <Button+ type="button"+ variant="primary"+ text="Approve"+ loading={isPending}+ className="w-full"+ disabled={!selectedGroupId}+ onClick={handleApproveClick}+ />+ </div></div>- </div>- {/* Second row - Reject button and link selector */}+ {/* Second row - Reject button and link selector */}+ <div className="flex">+ <div+ className={cn(+ "transition-[width] duration-300",+ isApproving ? "w-10" : "w-full",+ )}+ >+ {isApproving ? (+ <Button+ type="button"+ variant="secondary"+ icon={<ChevronLeft className="size-4 shrink-0" />}+ onClick={handleBackClick}+ />+ ) : (+ <PartnerRejectButton partner={partner} setIsOpen={setIsOpen} />+ )}+ </div>++ <div+ className={cn(+ "overflow-hidden transition-[width] duration-300",+ isApproving ? "w-full pl-2" : "w-0",+ )}+ >+ <div+ className={cn(+ "w-0 transition-[width] duration-300",+ isApproving && "w-full",+ )}+ >+ <PartnerLinkSelector+ selectedLinkId={selectedLinkId}+ setSelectedLinkId={setSelectedLinkId}+ showDestinationUrl={false}+ onCreate={async (search) => {+ try {+ await createLink(search);+ return true;+ } catch (error) {+ toast.error(error?.message ?? "Failed to create link");+ }+ return false;+ }}+ error={linkError}+ optional+ />+ </div>+ </div>+ </div>+ </div>+ ) : (<div className="flex"><divclassName={cn("transition-[width] duration-300",- isApproving ? "w-10" : "w-full",+ isApproving ? "w-[52px]" : "w-[83px]",)}>{isApproving ? (@@ -294,98 +359,46 @@ function PartnerApproval({)}</div>- <div- className={cn(- "overflow-hidden transition-[width] duration-300",- isApproving ? "w-full pl-2" : "w-0",- )}- >+ <div className="flex grow pl-2"><divclassName={cn("w-0 transition-[width] duration-300",isApproving && "w-full",)}>- <PartnerLinkSelector- selectedLinkId={selectedLinkId}- setSelectedLinkId={setSelectedLinkId}- showDestinationUrl={false}- onCreate={async (search) => {- try {- await createLink(search);- return true;- } catch (error) {- toast.error(error?.message ?? "Failed to create link");- }- return false;- }}- error={linkError}- optional- />+ <div className="w-[calc(100%-8px)]">+ <PartnerLinkSelector+ selectedLinkId={selectedLinkId}+ setSelectedLinkId={setSelectedLinkId}+ showDestinationUrl={false}+ onCreate={async (search) => {+ try {+ await createLink(search);+ return true;+ } catch (error) {+ toast.error(error?.message ?? "Failed to create link");+ }+ return false;+ }}+ error={linkError}+ optional+ />+ </div></div>- </div>- </div>- </div>- );- }- return (- <div className="flex">- <div- className={cn(- "transition-[width] duration-300",- isApproving ? "w-[52px]" : "w-[83px]",- )}- >- {isApproving ? (- <Button- type="button"- variant="secondary"- icon={<ChevronLeft className="size-4 shrink-0" />}- onClick={handleBackClick}- />- ) : (- <PartnerRejectButton partner={partner} setIsOpen={setIsOpen} />- )}- </div>-- <div className="flex grow pl-2">- <div- className={cn(- "w-0 transition-[width] duration-300",- isApproving && "w-full",- )}- >- <div className="w-[calc(100%-8px)]">- <PartnerLinkSelector- selectedLinkId={selectedLinkId}- setSelectedLinkId={setSelectedLinkId}- showDestinationUrl={false}- onCreate={async (search) => {- try {- await createLink(search);- return true;- } catch (error) {- toast.error(error?.message ?? "Failed to create link");- }- return false;- }}- error={linkError}- optional- />+ <div className="grow">+ <Button+ type="button"+ variant="primary"+ text="Approve"+ loading={isPending}+ disabled={!selectedGroupId}+ onClick={handleApproveClick}+ />+ </div></div></div>-- <div className="grow">- <Button- type="button"- variant="primary"- text="Approve"- loading={isPending}- onClick={handleApproveClick}- />- </div>- </div>+ )}</div>);}