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.

fix: improve tag selector search

flooorianflo773cce05024d
diff --git a/apps/web/ui/analytics/tag-selector.tsx b/apps/web/ui/analytics/tag-selector.tsx
index 7dc0d843cb..2f709ce961 100644
--- a/apps/web/ui/analytics/tag-selector.tsx
+++ b/apps/web/ui/analytics/tag-selector.tsx
@@ -1,13 +1,14 @@
import useTags from "@/lib/swr/use-tags";
-import { IconMenu, Popover, Tick, useRouterStuff } from "@dub/ui";
+import { IconMenu, InputSelect, Popover, Tick, useRouterStuff } from "@dub/ui";
import { Command } from "cmdk";
-import { ChevronDown, Search, Tag } from "lucide-react";
+import { ChevronDown, Tag } from "lucide-react";
import Link from "next/link";
-import { useSearchParams } from "next/navigation";
+import { useRouter, useSearchParams } from "next/navigation";
import { useState } from "react";
import TagBadge from "../links/tag-badge";
export default function TagSelector() {
+ const router = useRouter();
const { queryParams } = useRouterStuff();
const { tags } = useTags();
@@ -15,48 +16,61 @@ export default function TagSelector() {
const selectedTagId = searchParams?.get("tagId");
const [openPopover, setOpenPopover] = useState(false);
- const [filterValue, setFilterValue] = useState("");
return tags && tags.length > 0 ? (
- <Popover
- content={
- <Command className="w-full md:w-48">
- <div className="grid w-full p-2 md:w-48">
- {tags.length > 4 && (
- <div className="relative mb-2">
- <div className="pointer-events-none absolute inset-y-0 left-3 flex items-center">
- <Search className="h-4 w-4 text-gray-400" />
- </div>
- <Command.Input
- value={filterValue}
- autoFocus
- onValueChange={(search: string) => setFilterValue(search)}
- placeholder="Filter tags"
- className="w-full rounded-md border border-gray-300 py-1.5 pl-9 text-black placeholder:text-gray-400 focus:border-black focus:outline-none focus:ring-0 focus:ring-gray-600 sm:text-sm"
- />
- </div>
- )}
- <Command.List className="dub-scrollbar max-h-96 overflow-y-auto">
- {/* All tags selection should be shown all the time */}
- <Link
- href={
- queryParams({
- del: "tagId",
- getNewPath: true,
- }) as string
- }
- className="flex w-full items-center justify-between space-x-2 rounded-md p-2 hover:bg-gray-100 active:bg-gray-200"
- >
- <p className="text-sm">All tags</p>
- {!selectedTagId && (
- <Tick className="h-4 w-4" aria-hidden="true" />
- )}
- </Link>
- {tags
- .filter((tag) =>
- tag.name.toLowerCase().includes(filterValue.toLowerCase()),
- )
- .map((tag) => (
+ tags.length > 10 ? (
+ <InputSelect
+ items={tags.map(({ id, name, color }) => ({
+ id,
+ color,
+ value: name,
+ }))}
+ icon={<Tag className="h-4 w-4 text-black" />}
+ selectedItem={{
+ id: selectedTagId!,
+ value: tags.find(({ id }) => id === selectedTagId)?.name || "",
+ color: tags.find(({ id }) => id === selectedTagId)?.color,
+ }}
+ setSelectedItem={(tag) => {
+ if (tag && typeof tag !== "function" && tag.id)
+ router.push(
+ queryParams({
+ set: { tagId: tag.id },
+ getNewPath: true,
+ }) as string,
+ );
+ else
+ router.push(
+ queryParams({ del: "tagId", getNewPath: true }) as string,
+ );
+ }}
+ inputAttrs={{
+ placeholder: "Filter tags",
+ }}
+ className="border-none shadow transition-all hover:shadow-md md:w-48"
+ />
+ ) : (
+ <Popover
+ content={
+ <Command className="w-full md:w-48">
+ <div className="grid w-full p-2 md:w-48">
+ <Command.List className="dub-scrollbar max-h-96 overflow-y-auto">
+ {/* All tags selection should be shown all the time */}
+ <Link
+ href={
+ queryParams({
+ del: "tagId",
+ getNewPath: true,
+ }) as string
+ }
+ className="flex w-full items-center justify-between space-x-2 rounded-md p-2 hover:bg-gray-100 active:bg-gray-200"
+ >
+ <p className="text-sm">All tags</p>
+ {!selectedTagId && (
+ <Tick className="h-4 w-4" aria-hidden="true" />
+ )}
+ </Link>
+ {tags.map((tag) => (
<Link
key={tag.id}
href={
@@ -73,27 +87,30 @@ export default function TagSelector() {
)}
</Link>
))}
- </Command.List>
- </div>
- </Command>
- }
- openPopover={openPopover}
- setOpenPopover={setOpenPopover}
- >
- <button
- onClick={(o) => setOpenPopover(!o)}
- className="flex w-full items-center justify-between space-x-2 truncate rounded-md bg-white px-3 py-2.5 shadow transition-all hover:shadow-md md:w-48"
+ </Command.List>
+ </div>
+ </Command>
+ }
+ openPopover={openPopover}
+ setOpenPopover={setOpenPopover}
>
- <IconMenu
- text={tags.find(({ id }) => id === selectedTagId)?.name || "All tags"}
- icon={<Tag className="h-4 w-4" />}
- />
- <ChevronDown
- className={`h-5 w-5 text-gray-400 ${
- openPopover ? "rotate-180 transform" : ""
- } transition-all duration-75`}
- />
- </button>
- </Popover>
+ <button
+ onClick={(o) => setOpenPopover(!o)}
+ className="flex w-full items-center justify-between space-x-2 truncate rounded-md bg-white px-3 py-2.5 shadow transition-all hover:shadow-md md:w-48"
+ >
+ <IconMenu
+ text={
+ tags.find(({ id }) => id === selectedTagId)?.name || "All tags"
+ }
+ icon={<Tag className="h-4 w-4" />}
+ />
+ <ChevronDown
+ className={`h-5 w-5 text-gray-400 ${
+ openPopover ? "rotate-180 transform" : ""
+ } transition-all duration-75`}
+ />
+ </button>
+ </Popover>
+ )
) : undefined;
}
diff --git a/packages/ui/src/input-select.tsx b/packages/ui/src/input-select.tsx
index a386a4fb93..7e346ce875 100644
--- a/packages/ui/src/input-select.tsx
+++ b/packages/ui/src/input-select.tsx
@@ -11,11 +11,13 @@ import {
useState,
} from "react";
import { Badge } from "./badge";
+import { cn } from "@dub/utils";
export interface InputSelectItemProps {
id: string;
value: string;
- image: string;
+ color?: string;
+ image?: string;
disabled?: boolean;
label?: string;
}
@@ -24,18 +26,20 @@ export function InputSelect({
items,
selectedItem,
setSelectedItem,
+ className,
icon,
inputAttrs,
}: {
- items: InputSelectItemProps[];
+ items: InputSelectItemProps[] | [];
selectedItem: InputSelectItemProps | null;
setSelectedItem: Dispatch<SetStateAction<InputSelectItemProps | null>>;
+ className?: string;
icon?: ReactNode;
inputAttrs?: InputHTMLAttributes<HTMLInputElement>;
}) {
const commandRef = useRef<HTMLDivElement | null>(null);
const [openCommandList, setOpenCommandList] = useState(false);
- const [inputValue, setInputValue] = useState("");
+ const [inputValue, setInputValue] = useState(selectedItem?.value || "");
useEffect(() => {
const handleClickOutside = (e: MouseEvent) => {
@@ -55,36 +59,43 @@ export function InputSelect({
const CommandInput = () => {
const isEmpty = useCommandState((state: any) => state.filtered.count === 0);
return (
- <Command.Input
- placeholder={inputAttrs?.placeholder || "Search..."}
- // hack to focus on the input when the dropdown opens
- autoFocus={openCommandList}
- // when focus on the input. only show the dropdown if there are tags and the tagValue is not empty
- onFocus={() => setOpenCommandList(true)}
- value={inputValue}
- onValueChange={setInputValue}
- onKeyDown={(e) => {
- if (e.key === "Escape") {
- e.preventDefault();
- setOpenCommandList(false);
- // listen for cases where empty results and enter is pressed
- } else if (e.key === "Enter" && isEmpty) {
- setOpenCommandList(false);
- // if it's a letter or a number and there's no meta key pressed, openCommandList dropdown
- } else if (e.key.match(/^[a-z0-9]$/i) && !e.metaKey) {
- setOpenCommandList(true);
- }
- }}
- className="block w-full rounded-md border-none px-0 text-gray-900 placeholder-gray-400 focus:outline-none focus:ring-0 sm:text-sm"
- />
+ <>
+ <Command.Input
+ placeholder={inputAttrs?.placeholder || "Search..."}
+ // hack to focus on the input when the dropdown opens
+ autoFocus={openCommandList}
+ // when focus on the input. only show the dropdown if there are tags and the tagValue is not empty
+ onFocus={() => setOpenCommandList(true)}
+ value={selectedItem?.value === "" ? "" : inputValue}
+ onValueChange={setInputValue}
+ onKeyDown={(e) => {
+ if (e.key === "Escape") {
+ e.preventDefault();
+ setOpenCommandList(false);
+ // listen for cases where empty results and enter is pressed
+ } else if (e.key === "Enter" && isEmpty) {
+ setOpenCommandList(false);
+ // if it's a letter or a number and there's no meta key pressed, openCommandList dropdown
+ } else if (e.key.match(/^[a-z0-9]$/i) && !e.metaKey) {
+ setOpenCommandList(true);
+ }
+ }}
+ className="block w-full rounded-md border-none px-0 text-gray-900 placeholder-gray-400 focus:outline-none focus:ring-0 sm:text-sm"
+ />
+ </>
);
};
return (
- <Command ref={commandRef} className="relative w-full" loop>
- <div className="group rounded-md border border-gray-300 bg-white px-1 focus-within:border-gray-500 focus-within:ring-1 focus-within:ring-gray-500">
+ <Command ref={commandRef} className="relative" loop>
+ <div
+ className={cn(
+ "group rounded-md border border-gray-300 bg-white px-1 focus-within:border-gray-500 focus-within:ring-1 focus-within:ring-gray-500",
+ className,
+ )}
+ >
<div className="absolute inset-y-0 left-0 flex items-center justify-center pl-3 text-gray-400">
- {selectedItem ? (
+ {selectedItem && selectedItem.image ? (
<img
src={selectedItem.image}
alt={selectedItem.value}
@@ -94,9 +105,9 @@ export function InputSelect({
icon || <Search className="h-4 w-4 text-gray-400" />
)}
</div>
- <div className="flex h-9 px-8">
+ <div className="flex h-10 px-8">
<CommandInput />
- {inputValue ? (
+ {inputValue && selectedItem?.value !== "" ? (
<button
onClick={() => {
setSelectedItem(null);
@@ -112,7 +123,7 @@ export function InputSelect({
</div>
</div>
{openCommandList && (
- <Command.List className="absolute z-20 mt-2 h-[calc(var(--cmdk-list-height)+17px)] max-h-[300px] w-full overflow-auto rounded-md border border-gray-200 bg-white p-2 shadow-md transition-all">
+ <Command.List className="dub-scrollbar absolute z-20 mt-2 h-[calc(var(--cmdk-list-height)+17px)] max-h-[300px] w-full overflow-auto rounded-md border border-gray-200 bg-white p-2 shadow-md transition-all">
<Command.Empty className="px-4 py-2 text-sm text-gray-600">
No results found for "{inputValue}"
</Command.Empty>
@@ -129,18 +140,33 @@ export function InputSelect({
className="group flex cursor-pointer items-center justify-between rounded-md px-4 py-2 text-sm text-gray-900 hover:bg-gray-100 hover:text-gray-900 active:bg-gray-200 aria-disabled:cursor-not-allowed aria-disabled:opacity-75 aria-disabled:hover:bg-white aria-selected:bg-gray-100 aria-selected:text-gray-900"
>
<div className="flex items-center space-x-2">
- <img
- src={item.image}
- alt={item.value}
- className="h-4 w-4 rounded-full"
- />
- <p>{item.value}</p>
+ {item.image && (
+ <img
+ src={item.image}
+ alt={item.value}
+ className="h-4 w-4 rounded-full"
+ />
+ )}
+ <p
+ className={cn(
+ "my-auto whitespace-nowrap rounded-md px-2 py-0.5 text-sm",
+ item.color === "red" && "bg-red-100 text-red-600",
+ item.color === "yellow" && "bg-yellow-100 text-yellow-600",
+ item.color === "green" && "bg-green-100 text-green-600",
+ item.color === "blue" && "bg-blue-100 text-blue-600",
+ item.color === "purple" && "bg-purple-100 text-purple-600",
+ item.color === "brown" && "bg-brown-100 text-brown-600",
+ )}
+ >
+ {item.value}
+ </p>
{item.label && (
<Badge className="text-xs" variant="neutral">
{item.label}
</Badge>
)}
</div>
+
<Check className="invisible h-5 w-5 text-gray-500 aria-selected:visible" />
</Command.Item>
))}