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.

Adds group fast switching

Ian MacCallumafff99a87de4
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/group-header-selector.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/group-header-selector.tsx
new file mode 100644
index 0000000000..dfdd327e25
--- /dev/null
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/group-header-selector.tsx
@@ -0,0 +1,144 @@
+import useGroups from "@/lib/swr/use-groups";
+import { GroupProps } from "@/lib/types";
+import { GROUPS_MAX_PAGE_SIZE } from "@/lib/zod/schemas/groups";
+import { GroupColorCircle } from "@/ui/partners/groups/group-color-circle";
+import { Combobox } from "@dub/ui";
+import { useCallback, useEffect, useMemo, useState } from "react";
+import { useDebounce } from "use-debounce";
+
+export type Group = Pick<GroupProps, "id" | "slug" | "name" | "color">;
+
+interface GroupSelectorProps {
+ selectedGroup: Group | null;
+ setSelectedGroup: (group: Group) => void;
+ disabled?: boolean;
+}
+
+export function GroupHeaderSelector({
+ selectedGroup,
+ setSelectedGroup,
+ 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: selectedGroup ? { groupIds: [selectedGroup.id] } : undefined,
+ });
+
+ // Determine if we should use async loading
+ useEffect(
+ () =>
+ setUseAsync(
+ Boolean(groups && !useAsync && groups.length >= GROUPS_MAX_PAGE_SIZE),
+ ),
+ [groups, useAsync],
+ );
+
+ const getGroupById = useCallback(
+ (groupId: string) => {
+ return (
+ groups?.find((group) => group.id === groupId) ??
+ selectedGroups?.find((group) => group.id === groupId)
+ );
+ },
+ [groups, selectedGroups],
+ );
+
+ const groupOptions = useMemo(() => {
+ if (!groups && selectedGroup) {
+ return [
+ {
+ value: selectedGroup.id,
+ label: selectedGroup.name,
+ icon: <GroupColorCircle group={selectedGroup} />,
+ },
+ ];
+ }
+
+ return groups?.map((group) => ({
+ value: group.id,
+ label: group.name,
+ icon: <GroupColorCircle group={group} />,
+ }));
+ }, [groups, selectedGroup]);
+
+ const selectedOption = useMemo(() => {
+ if (!selectedGroup) return null;
+
+ return {
+ value: selectedGroup.id,
+ label: selectedGroup.name,
+ icon: <GroupColorCircle group={selectedGroup} />,
+ };
+ }, [selectedGroup]);
+
+ let label;
+
+ if (selectedOption?.label) {
+ label = selectedOption?.label;
+ } else if (loading) {
+ label = (
+ <div className="my-0.5 h-5 w-1/3 animate-pulse rounded bg-neutral-200" />
+ );
+ }
+
+ const onChange = useCallback(
+ (option: { value: string }) => {
+ if (!option) {
+ return;
+ }
+
+ console.log(`Selected option: ${option.value}`);
+
+ const group = getGroupById(option.value);
+
+ console.log(`Selected group: ${group}`);
+ if (!group) {
+ return;
+ }
+
+ setSelectedGroup(group);
+ },
+ [setSelectedGroup, getGroupById],
+ );
+
+ return (
+ <Combobox
+ options={groupOptions}
+ setSelected={(option) => onChange(option)}
+ selected={selectedOption}
+ icon={selectedOption?.icon}
+ caret={true}
+ placeholder={selectedGroupsLoading ? "" : "Select group"}
+ searchPlaceholder="Search groups..."
+ onSearchChange={setSearch}
+ shouldFilter={!useAsync}
+ matchTriggerWidth
+ open={openPopover}
+ onOpenChange={setOpenPopover}
+ popoverProps={{
+ contentClassName: "min-w-[200px]",
+ }}
+ labelProps={{
+ className: "text-lg font-semibold leading-7 text-neutral-900",
+ }}
+ iconProps={{
+ className: "size-6",
+ }}
+ buttonProps={{
+ disabled,
+ className:
+ "w-full justify-start px-3 transition-none max-md:bg-bg-subtle hover:bg-bg-emphasis md:hover:bg-neutral-50 border-none",
+ }}
+ >
+ {label}
+ </Combobox>
+ );
+}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/group-header.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/group-header.tsx
index 4dfcb5707f..21c63a2dd1 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/group-header.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/groups/[groupSlug]/group-header.tsx
@@ -1,10 +1,8 @@
"use client";
import useGroup from "@/lib/swr/use-group";
-import useProgram from "@/lib/swr/use-program";
import useWorkspace from "@/lib/swr/use-workspace";
import { GroupProps } from "@/lib/types";
-import { GroupColorCircle } from "@/ui/partners/groups/group-color-circle";
import {
ArrowUpRight2,
Brush,
@@ -18,9 +16,20 @@ import {
} from "@dub/ui";
import { cn } from "@dub/utils";
import Link from "next/link";
-import { redirect, useParams, usePathname } from "next/navigation";
+import {
+ redirect,
+ useParams,
+ usePathname,
+ useRouter,
+ useSearchParams,
+} from "next/navigation";
+import { Group, GroupHeaderSelector } from "./group-header-selector";
export function GroupHeaderTitle() {
+ const router = useRouter();
+ const params = useParams<{ slug: string; groupSlug: string }>();
+ const pathname = usePathname();
+ const searchParams = useSearchParams();
const { group, loading } = useGroup();
const { slug: workspaceSlug } = useWorkspace();
@@ -32,6 +41,14 @@ export function GroupHeaderTitle() {
redirect(`/${workspaceSlug}/program/groups`);
}
+ const switchToGroup = (newGroup: Group) => {
+ if (group.id === newGroup.id || params.groupSlug === newGroup.slug) return;
+
+ const url = `${pathname.replace(`/groups/${params.groupSlug}`, `/groups/${newGroup.slug}`)}?${searchParams.toString()}`;
+
+ router.push(url);
+ };
+
return (
<div className="flex items-center gap-1.5">
<Link
@@ -43,12 +60,11 @@ export function GroupHeaderTitle() {
<Users className="size-4" />
</Link>
<ChevronRight className="text-content-muted size-2.5 shrink-0 [&_*]:stroke-2" />
- <div className="flex items-center gap-1.5">
- <GroupColorCircle group={group} />
- <span className="text-lg font-semibold leading-7 text-neutral-900">
- {group.name}
- </span>
- </div>
+
+ <GroupHeaderSelector
+ selectedGroup={group}
+ setSelectedGroup={switchToGroup}
+ />
</div>
);
}
@@ -56,7 +72,6 @@ export function GroupHeaderTitle() {
export function GroupHeaderTabs() {
const pathname = usePathname();
const { slug } = useParams<{ slug: string }>();
- const { program } = useProgram();
const { group, loading } = useGroup();
const GROUP_NAVIGATION_TABS = [
diff --git a/packages/ui/src/combobox/index.tsx b/packages/ui/src/combobox/index.tsx
index 0735e9058b..077c69dcc5 100644
--- a/packages/ui/src/combobox/index.tsx
+++ b/packages/ui/src/combobox/index.tsx
@@ -57,6 +57,9 @@ export type ComboboxProps<
createLabel?: (search: string) => ReactNode;
onCreate?: (search: string) => Promise<boolean>;
buttonProps?: ButtonProps;
+ labelProps?: { className?: string };
+ iconProps?: { className?: string };
+ popoverProps?: { contentClassName?: string };
shortcutHint?: string;
caret?: boolean | ReactNode;
side?: PopoverProps["side"];
@@ -93,6 +96,9 @@ export function Combobox({
createLabel,
onCreate,
buttonProps,
+ labelProps,
+ iconProps,
+ popoverProps,
shortcutHint,
caret,
side,
@@ -204,6 +210,7 @@ export function Combobox({
}}
popoverContentClassName={cn(
matchTriggerWidth && "sm:w-[var(--radix-popover-trigger-width)]",
+ popoverProps?.contentClassName,
)}
content={
<AnimatedSizeContainer
@@ -324,7 +331,12 @@ export function Combobox({
)}
text={
<>
- <div className="min-w-0 grow truncate text-left">
+ <div
+ className={cn(
+ "min-w-0 grow truncate text-left",
+ labelProps?.className,
+ )}
+ >
{children ||
selected.map((option) => option.label).join(", ") ||
placeholder}
@@ -344,7 +356,7 @@ export function Combobox({
isReactNode(Icon) ? (
Icon
) : (
- <Icon className="size-4 shrink-0" />
+ <Icon className={cn("size-4 shrink-0", iconProps?.className)} />
)
) : undefined
}