Files
kandev/apps/web/components/gitlab/my-gitlab/list-toolbar.tsx
T
João Salavisa cf76a27488 feat(web): normalize /gitlab page to match /github layout (#1082)
* feat(web): normalize /gitlab page to match /github layout

Adds a presets sidebar (MR/Issue toggle, Inbox/Created groups, Saved
section), a top toolbar with project filter and custom-query input,
paginated MR/Issue lists, and a save-preset dialog backed by a
localStorage store at kandev:gitlab-presets:v1.

Replaces the bespoke tabbed layout and supersedes
hooks/domains/gitlab/use-user-search with the pagination-aware
useGitLabSearch under components/gitlab/my-gitlab/. Project filtering is
applied client-side since GitLab's global MR/issue endpoints have no
path_with_namespace qualifier. No backend changes — the controller
already accepts filter / custom_query / page / per_page.

* fix(web): address /gitlab review feedback

- Merge duplicate cn/formatRelativeTime imports in list-toolbar.
- Drop the generic T from useGitLabSearch — it always resolved to MR | Issue,
  so the as-unknown-as double-cast was just hiding that. Now returns
  Array<MR | Issue> directly and exposes rawTotal alongside total.
- When projectFilter is active, total reflects the client-side filtered
  count instead of the server's unfiltered total so the toolbar badge and
  pagination don't claim "47 results" while only 3 rows render.
- Save flow uses committedQuery, not the draft customQuery, so a preset
  saved without pressing Enter matches what's actually displayed.
- Wrap mobile sidebar handlers in useCallback to keep props stable.
- Drop the unused refresh callback in useGitLabStatusFetch (no caller).
- Add hook-level tests for useSavedPresets save/remove/sync; use
  resetKnownProjectsStore directly in the known-projects test.

* fix(web): address round-2 /gitlab review feedback

- useSavedPresets save/remove now read from localStorage on every call so a
  concurrent write from another tab can't be silently overwritten by a stale
  in-memory snapshot.
- Tighten persisted shape validation: customQuery, projectFilter, and
  createdAt must all be strings (plus 3 regression tests).
- ResultsPagination clamps page to [1, totalPages] before deriving start/end
  and prev/next state, so a stale out-of-range page doesn't render an
  invalid range like "101–30 of 30" after a refresh that shrinks results.
- PresetsSidebar: ignore keydown bubbling from the nested delete button so
  Enter/Space on delete doesn't also trigger row selection, and add an
  aria-label to the icon-only delete control.
- Revert the hook's filtered-total policy: useGitLabSearch always returns
  the raw server total now (so pagination can still navigate to later
  pages). The page client owns the toolbar's displayedCount, computing
  filtered.length only when a project filter is active.

* fix(web): drop dead gitlab preset re-exports, tighten save dialog deps

- Remove unused IconBug / IconPencil imports and the re-export from
  presets.ts — no consumer imports them from this module.
- save-preset-dialog: inline the trimmed check inside handleSubmit so
  canSubmit no longer needs to be in the deps array. ESLint exhaustive-
  deps forced the previous duplication; inlining removes it cleanly.

* fix(web): null-safe response unwrap in useGitLabSearch

If the API returns null, the cast `(response as {mrs: ...}).mrs` would
crash before the `?? []` fallback could run. Optional-chain the cast so a
null payload yields an empty list, matching the existing total_count guard.

---------

Co-authored-by: Kandev Agent <agent@kandev.dev>
2026-05-26 16:00:55 +01:00

151 lines
4.4 KiB
TypeScript

"use client";
import { IconRefresh } from "@tabler/icons-react";
import { Button } from "@kandev/ui/button";
import { Input } from "@kandev/ui/input";
import { Select, SelectContent, SelectItem, SelectTrigger, SelectValue } from "@kandev/ui/select";
import { cn, formatRelativeTime } from "@/lib/utils";
const ALL_PROJECTS = "__all__";
type ListToolbarProps = {
title: string;
count: number;
loading: boolean;
lastFetchedAt: Date | null;
customQuery: string;
committedQuery: string;
onCustomQueryChange: (value: string) => void;
onCommitCustomQuery: () => void;
projectFilter: string;
onProjectFilterChange: (value: string) => void;
projectOptions: string[];
onRefresh: () => void;
};
function RefreshControls({
loading,
lastFetchedAt,
onRefresh,
showUpdatedPrefix,
}: {
loading: boolean;
lastFetchedAt: Date | null;
onRefresh: () => void;
showUpdatedPrefix: boolean;
}) {
return (
<>
{lastFetchedAt && !loading && (
<span className="text-xs text-muted-foreground whitespace-nowrap">
{showUpdatedPrefix ? "Updated " : ""}
{formatRelativeTime(lastFetchedAt.toISOString())}
</span>
)}
<Button
variant="ghost"
size="icon"
className="h-8 w-8 cursor-pointer"
onClick={onRefresh}
disabled={loading}
title="Refresh"
data-testid="gitlab-list-toolbar-refresh"
>
<IconRefresh className={cn("h-4 w-4", loading && "animate-spin")} />
</Button>
</>
);
}
export function ListToolbar({
title,
count,
loading,
lastFetchedAt,
customQuery,
committedQuery,
onCustomQueryChange,
onCommitCustomQuery,
projectFilter,
onProjectFilterChange,
projectOptions,
onRefresh,
}: ListToolbarProps) {
const selectValue = projectFilter || ALL_PROJECTS;
const dirty = customQuery !== committedQuery;
return (
<div className="px-4 sm:px-6 py-2.5 border-b shrink-0 flex flex-col md:flex-row md:items-center md:flex-wrap gap-2 md:gap-3">
<div className="flex items-center gap-2 min-w-0">
<div className="flex items-baseline gap-2 min-w-0 flex-1 md:flex-initial">
<h2 className="text-sm font-semibold truncate" data-testid="gitlab-list-toolbar-title">
{title}
</h2>
<span className="text-xs text-muted-foreground tabular-nums">
{loading ? "…" : count}
</span>
</div>
<div className="flex items-center gap-2 md:hidden">
<RefreshControls
loading={loading}
lastFetchedAt={lastFetchedAt}
onRefresh={onRefresh}
showUpdatedPrefix={false}
/>
</div>
</div>
<Select
value={selectValue}
onValueChange={(v) => onProjectFilterChange(v === ALL_PROJECTS ? "" : v)}
>
<SelectTrigger
className="w-full md:w-[220px] h-8 cursor-pointer"
data-testid="gitlab-project-filter-trigger"
>
<SelectValue placeholder="All projects" />
</SelectTrigger>
<SelectContent>
<SelectItem value={ALL_PROJECTS} className="cursor-pointer">
All projects
</SelectItem>
{projectOptions.map((key) => (
<SelectItem key={key} value={key} className="cursor-pointer">
{key}
</SelectItem>
))}
</SelectContent>
</Select>
<div className="w-full md:flex-1 md:min-w-[240px] relative">
<Input
value={customQuery}
onChange={(e) => onCustomQueryChange(e.target.value)}
onKeyDown={(e) => {
if (e.key === "Enter") {
e.preventDefault();
onCommitCustomQuery();
}
}}
onBlur={() => {
if (dirty) onCommitCustomQuery();
}}
placeholder='Custom query — press Enter. e.g. "labels=bug&state=opened"'
className="h-8 pr-20"
data-testid="gitlab-list-toolbar-custom-query"
/>
{dirty && (
<span className="pointer-events-none absolute right-2 top-1/2 -translate-y-1/2 text-[10px] uppercase tracking-wider text-muted-foreground hidden sm:inline">
Press Enter
</span>
)}
</div>
<div className="hidden md:flex items-center gap-2 md:ml-auto">
<RefreshControls
loading={loading}
lastFetchedAt={lastFetchedAt}
onRefresh={onRefresh}
showUpdatedPrefix
/>
</div>
</div>
);
}