All files / src/components/collection Toolbar.tsx

100% Statements 15/15
96.66% Branches 29/30
100% Functions 6/6
100% Lines 15/15

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219                                  37x           300x             1x                           37x       24x                                           37x                                                     37x               553x   553x         553x                                                                                 37x 259x           259x                                                         37x                 329x                                        
import ViewToggle, {
  ViewMode,
} from "@/components/buttons/ViewToggle/ViewToggle";
import type { LucideIcon } from "lucide-react";
import { Plus, Search, Sparkles, X } from "lucide-react";
import React from "react";
import { Link } from "react-router-dom";
 
// ─── Search input ─────────────────────────────────────────────────────────────
 
export interface SearchInputProps {
  value: string;
  onChange: (value: string) => void;
  placeholder?: string;
  className?: string;
}
 
export const SearchInput: React.FC<SearchInputProps> = ({
  value,
  onChange,
  placeholder = "Search…",
  className = "w-full sm:w-64",
}) => (
  <div className={`relative shrink-0 ${className}`}>
    <Search className="absolute left-3 top-1/2 -translate-y-1/2 size-3.5 text-gray-400 pointer-events-none" />
    <input
      type="text"
      placeholder={placeholder}
      aria-label={placeholder}
      value={value}
      onChange={(e) => onChange(e.target.value)}
      className="w-full pl-8 pr-3 py-2 text-sm rounded-lg border border-gray-200 dark:border-gray-600 bg-white dark:bg-gray-800 text-gray-800 dark:text-gray-200 placeholder-gray-400 focus:outline-none focus:ring-2 focus:ring-gray-300 dark:focus:ring-gray-500 transition-colors"
    />
  </div>
);
 
// ─── Clear filters ────────────────────────────────────────────────────────────
 
export interface ClearFiltersButtonProps {
  onClick: () => void;
  /** Number of active filters — rendered as a small badge when > 0. */
  count?: number;
}
 
export const ClearFiltersButton: React.FC<ClearFiltersButtonProps> = ({
  onClick,
  count,
}) => (
  <button
    type="button"
    onClick={onClick}
    className="flex items-center gap-1 text-xs text-gray-500 dark:text-gray-400 hover:text-gray-800 dark:hover:text-gray-200 transition-colors px-1"
  >
    <X className="size-3" />
    Clear
    {count !== undefined && count > 0 && (
      <span className="ml-0.5 inline-flex items-center justify-center size-4 rounded-full bg-gray-200 dark:bg-gray-600 text-gray-700 dark:text-gray-200 text-[10px] font-semibold">
        {count}
      </span>
    )}
  </button>
);
 
// ─── Action buttons ───────────────────────────────────────────────────────────
 
/** Visual weight of a toolbar action. `primary` is the one obvious next step
 *  on the page (create); `secondary` sits beside it for the alternative route
 *  to the same resource (generate, import), so two actions never compete. */
export type ActionVariant = "primary" | "secondary";
 
const ACTION_CLS: Record<ActionVariant, string> = {
  primary:
    "bg-gray-900 dark:bg-white text-white dark:text-gray-900 hover:opacity-80",
  secondary:
    "border border-gray-200 dark:border-gray-600 text-gray-700 dark:text-gray-300 hover:bg-gray-50 dark:hover:bg-gray-700",
};
 
export type CreateButtonProps = {
  label: string;
  Icon?: LucideIcon;
  variant?: ActionVariant;
  disabled?: boolean;
} & (
  | {
      /** Navigate to a route… */
      to: string;
      /** …or run a handler (e.g. open a modal). */
      onClick?: never;
    }
  | {
      /** Navigate to a route… */
      to?: never;
      /** …or run a handler (e.g. open a modal). */
      onClick: () => void;
    }
);
 
export const CreateButton: React.FC<CreateButtonProps> = ({
  label,
  to,
  onClick,
  Icon = Plus,
  variant = "primary",
  disabled,
}) => {
  const cls = `flex items-center gap-1.5 px-3 py-1.5 rounded-lg text-xs font-medium transition-all shrink-0 ${ACTION_CLS[variant]}`;
  const content = (
    <>
      <Icon className="size-3.5" />
      {label}
    </>
  );
  return to && !disabled ? (
    <Link to={to} className={cls}>
      {content}
    </Link>
  ) : (
    <button
      type="button"
      onClick={onClick}
      disabled={disabled}
      className={cls + (disabled ? " opacity-40 cursor-not-allowed" : "")}
    >
      {content}
    </button>
  );
};
 
/** Same shape as `CreateButtonProps`: a generate action either opens a modal
 *  (most resources) or navigates to a setup flow of its own (agents), and the
 *  two are mutually exclusive. */
export type GenerateButtonProps = {
  label?: string;
  disabled?: boolean;
} & (
  | {
      /** Navigate to a generative setup route… */
      to: string;
      /** …or open a generate modal. */
      onClick?: never;
    }
  | {
      /** Navigate to a generative setup route… */
      to?: never;
      /** …or open a generate modal. */
      onClick: () => void;
    }
);
 
/** The generative sibling of `CreateButton`: same geometry, secondary weight,
 *  and the platform-wide Sparkles glyph every "describe it and we'll build
 *  it" entry point uses. Kept as its own component so a resource page cannot
 *  accidentally style its generate action differently from the next page. */
export const GenerateButton: React.FC<GenerateButtonProps> = (props) => {
  const shared = {
    label: props.label ?? "Generate",
    Icon: Sparkles,
    variant: "secondary" as const,
    disabled: props.disabled,
  };
  return props.to !== undefined ? (
    <CreateButton {...shared} to={props.to} />
  ) : (
    <CreateButton {...shared} onClick={props.onClick} />
  );
};
 
// ─── Toolbar ──────────────────────────────────────────────────────────────────
 
export interface ToolbarProps {
  search?: SearchInputProps;
  /** Filter/sort controls rendered after the search input. */
  children?: React.ReactNode;
  /** Number of active filters (incl. search). When > 0 a Clear button is
   *  shown, invoking `onClear`. */
  activeFilterCount?: number;
  onClear?: () => void;
  view?: ViewMode;
  onViewChange?: (view: ViewMode) => void;
  /** Right-aligned actions, e.g. a `CreateButton`. */
  actions?: React.ReactNode;
}
 
/**
 * The standard collection toolbar: search + filters on the left, primary
 * actions and the grid/list toggle on the right. Filter controls are passed
 * as children so each subpage composes its own set while the layout stays
 * consistent.
 */
const Toolbar: React.FC<ToolbarProps> = ({
  search,
  children,
  activeFilterCount = 0,
  onClear,
  view,
  onViewChange,
  actions,
}) => (
  <div className="flex flex-col sm:flex-row gap-2 items-start sm:items-center justify-between">
    <div className="flex items-center gap-2 flex-wrap flex-1 min-w-0">
      {search && <SearchInput {...search} />}
      {children}
      {activeFilterCount > 0 && onClear && (
        <ClearFiltersButton onClick={onClear} count={activeFilterCount} />
      )}
    </div>
    {(actions || (view && onViewChange)) && (
      <div className="flex items-center gap-2 shrink-0">
        {actions}
        {view && onViewChange && (
          <ViewToggle view={view} onViewChange={onViewChange} />
        )}
      </div>
    )}
  </div>
);
 
export default Toolbar;