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.

Improve template combobox

Tim Wilson9eafcaf3066f
diff --git a/apps/web/ui/modals/link-builder/utm-templates-combo.tsx b/apps/web/ui/modals/link-builder/utm-templates-combo.tsx
index 09fcf7a4bc..b0cc43b9ea 100644
--- a/apps/web/ui/modals/link-builder/utm-templates-combo.tsx
+++ b/apps/web/ui/modals/link-builder/utm-templates-combo.tsx
@@ -2,9 +2,11 @@
import useWorkspace from "@/lib/swr/use-workspace";
import { UtmTemplateProps } from "@/lib/types";
-import { Combobox } from "@dub/ui";
+import { UTM_PARAMETERS } from "@/ui/links/utm-builder";
+import { Combobox, Tooltip } from "@dub/ui";
import { Note } from "@dub/ui/src/icons";
import { fetcher, getParamsFromURL } from "@dub/utils";
+import { Fragment } from "react";
import { useFormContext } from "react-hook-form";
import { toast } from "sonner";
import useSWR, { mutate } from "swr";
@@ -26,7 +28,7 @@ export function UTMTemplatesCombo({
},
);
- return data && data.length > 0 ? (
+ return data ? (
<Combobox
selected={null}
setSelected={(option) => {
@@ -48,8 +50,40 @@ export function UTMTemplatesCombo({
label: template.name,
value: template.id,
}))}
+ optionRight={({ value }) => {
+ const template = data.find((template) => template.id === value);
+ if (!template) return null;
+
+ const includedParams = UTM_PARAMETERS.filter(
+ ({ key }) => template[key],
+ );
+
+ return (
+ <Tooltip
+ content={
+ <div className="grid max-w-[225px] grid-cols-[1fr,minmax(0,min-content)] gap-x-2 gap-y-1 whitespace-nowrap p-2 text-sm sm:min-w-[150px]">
+ {includedParams.map(({ key, label, icon: Icon }) => (
+ <Fragment key={key}>
+ <span className="font-medium text-gray-600">{label}</span>
+ <span className="truncate text-gray-500">
+ {template[key]}
+ </span>
+ </Fragment>
+ ))}
+ </div>
+ }
+ >
+ <div className="ml-4 flex shrink-0 items-center gap-1 text-gray-500">
+ {includedParams.map(({ icon: Icon }) => (
+ <Icon className="size-3.5" />
+ ))}
+ </div>
+ </Tooltip>
+ );
+ }}
placeholder="Templates"
searchPlaceholder="Load or save a template..."
+ emptyState="No templates found"
icon={Note}
createLabel={(search) => `Save new template: "${search}"`}
onCreate={async (search) => {
@@ -81,7 +115,8 @@ export function UTMTemplatesCombo({
return false;
}}
buttonProps={{ className: "w-fit px-2" }}
- optionClassName="sm:min-w-[200px] sm:max-w-[350px] animate-fade-in"
+ inputClassName="md:min-w-[200px]"
+ optionClassName="md:min-w-[250px] md:max-w-[350px] animate-fade-in"
caret
/>
) : null;
diff --git a/packages/ui/src/combobox/index.tsx b/packages/ui/src/combobox/index.tsx
index af7694aa71..891d832a11 100644
--- a/packages/ui/src/combobox/index.tsx
+++ b/packages/ui/src/combobox/index.tsx
@@ -1,7 +1,9 @@
import { cn } from "@dub/utils";
-import { Command, CommandEmpty, CommandInput, CommandItem } from "cmdk";
+import { Command, CommandInput, CommandItem, useCommandState } from "cmdk";
import { ChevronDown } from "lucide-react";
import {
+ forwardRef,
+ HTMLProps,
isValidElement,
PropsWithChildren,
ReactNode,
@@ -56,6 +58,8 @@ export type ComboboxProps<
onOpenChange?: (open: boolean) => void;
onSearchChange?: (search: string) => void;
shouldFilter?: boolean;
+ inputClassName?: string;
+ optionRight?: (option: ComboboxOption) => ReactNode;
optionClassName?: string;
matchTriggerWidth?: boolean;
}>;
@@ -86,6 +90,8 @@ export function Combobox({
onOpenChange,
onSearchChange,
shouldFilter = true,
+ inputClassName,
+ optionRight,
optionClassName,
matchTriggerWidth,
children,
@@ -193,7 +199,10 @@ export function Combobox({
placeholder={searchPlaceholder}
value={search}
onValueChange={setSearch}
- className="grow border-0 py-3 pl-4 pr-2 outline-none placeholder:text-gray-400 focus:ring-0 sm:text-sm"
+ className={cn(
+ "grow border-0 py-3 pl-4 pr-2 outline-none placeholder:text-gray-400 focus:ring-0 sm:text-sm",
+ inputClassName,
+ )}
onKeyDown={(e) => {
if (
e.key === "Escape" ||
@@ -226,6 +235,7 @@ export function Combobox({
({ value }) => value === option.value,
)}
onSelect={() => handleSelect(option)}
+ right={optionRight?.(option)}
className={optionClassName}
/>
))}
@@ -257,9 +267,9 @@ export function Combobox({
</CommandItem>
)}
{shouldFilter ? (
- <CommandEmpty className="flex h-12 items-center justify-center text-sm text-gray-500">
+ <Empty className="flex h-12 items-center justify-center text-sm text-gray-500">
{emptyState ? emptyState : "No matches"}
- </CommandEmpty>
+ </Empty>
) : sortedOptions.length === 0 ? (
<div className="flex h-12 items-center justify-center text-sm text-gray-500">
{emptyState ? emptyState : "No matches"}
@@ -344,12 +354,14 @@ function Option({
onSelect,
multiple,
selected,
+ right,
className,
}: {
option: ComboboxOption;
onSelect: () => void;
multiple: boolean;
selected: boolean;
+ right?: ReactNode;
className?: string;
}) {
return (
@@ -383,6 +395,7 @@ function Option({
)}
<span className="grow truncate">{option.label}</span>
</div>
+ {right}
{!multiple && selected && (
<Check2 className="size-4 shrink-0 text-gray-600" />
)}
@@ -392,3 +405,15 @@ function Option({
const isReactNode = (element: any): element is ReactNode =>
isValidElement(element);
+
+// Custom Empty component because our current cmdk version has an issue with first render (https://github.com/pacocoursey/cmdk/issues/149)
+const Empty = forwardRef<HTMLDivElement, HTMLProps<HTMLDivElement>>(
+ (props, forwardedRef) => {
+ const render = useCommandState((state) => state.filtered.count === 0);
+
+ if (!render) return null;
+ return (
+ <div ref={forwardedRef} cmdk-empty="" role="presentation" {...props} />
+ );
+ },
+);