cf76a27488
* 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>
105 lines
3.0 KiB
TypeScript
105 lines
3.0 KiB
TypeScript
"use client";
|
|
|
|
import { useCallback, useSyncExternalStore } from "react";
|
|
|
|
const STORAGE_KEY = "kandev:gitlab-presets:v1";
|
|
|
|
export type SavedPreset = {
|
|
id: string;
|
|
kind: "mr" | "issue";
|
|
label: string;
|
|
customQuery: string;
|
|
projectFilter: string;
|
|
createdAt: string;
|
|
};
|
|
|
|
export function readStorage(): SavedPreset[] {
|
|
if (typeof window === "undefined") return [];
|
|
try {
|
|
const raw = window.localStorage.getItem(STORAGE_KEY);
|
|
if (!raw) return [];
|
|
const parsed = JSON.parse(raw) as unknown;
|
|
if (!Array.isArray(parsed)) return [];
|
|
return parsed.filter(
|
|
(p): p is SavedPreset =>
|
|
typeof p === "object" &&
|
|
p !== null &&
|
|
typeof (p as SavedPreset).id === "string" &&
|
|
((p as SavedPreset).kind === "mr" || (p as SavedPreset).kind === "issue") &&
|
|
typeof (p as SavedPreset).label === "string" &&
|
|
typeof (p as SavedPreset).customQuery === "string" &&
|
|
typeof (p as SavedPreset).projectFilter === "string" &&
|
|
typeof (p as SavedPreset).createdAt === "string",
|
|
);
|
|
} catch {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
function writeStorage(presets: SavedPreset[]) {
|
|
if (typeof window === "undefined") return;
|
|
try {
|
|
window.localStorage.setItem(STORAGE_KEY, JSON.stringify(presets));
|
|
} catch {
|
|
/* ignore quota / access errors */
|
|
}
|
|
}
|
|
|
|
const listeners = new Set<() => void>();
|
|
let snapshot: SavedPreset[] | null = null;
|
|
const emptySnapshot: SavedPreset[] = [];
|
|
|
|
function getSnapshot(): SavedPreset[] {
|
|
if (snapshot === null) snapshot = readStorage();
|
|
return snapshot;
|
|
}
|
|
|
|
function getServerSnapshot(): SavedPreset[] {
|
|
return emptySnapshot;
|
|
}
|
|
|
|
function subscribe(cb: () => void) {
|
|
listeners.add(cb);
|
|
return () => {
|
|
listeners.delete(cb);
|
|
};
|
|
}
|
|
|
|
function publish(next: SavedPreset[]) {
|
|
snapshot = next;
|
|
writeStorage(next);
|
|
for (const l of listeners) l();
|
|
}
|
|
|
|
// Test-only: drop the module-level snapshot so the next read goes through
|
|
// readStorage again. Used by the hook tests so each `it` starts from a known
|
|
// empty state independent of test execution order.
|
|
export function __resetSnapshotForTests() {
|
|
snapshot = null;
|
|
for (const l of listeners) l();
|
|
}
|
|
|
|
export function useSavedPresets() {
|
|
const presets = useSyncExternalStore(subscribe, getSnapshot, getServerSnapshot);
|
|
|
|
// Always merge against the latest localStorage read instead of the in-memory
|
|
// snapshot. With two tabs open, snapshot in tab A is stale the moment tab B
|
|
// writes — appending to it would silently drop B's preset. readStorage is
|
|
// cheap (small JSON, single key) so this is fine on the save/remove paths.
|
|
const save = useCallback((input: Omit<SavedPreset, "id" | "createdAt">) => {
|
|
const preset: SavedPreset = {
|
|
...input,
|
|
id: `g_${Date.now().toString(36)}_${Math.random().toString(36).slice(2, 8)}`,
|
|
createdAt: new Date().toISOString(),
|
|
};
|
|
publish([...readStorage(), preset]);
|
|
return preset;
|
|
}, []);
|
|
|
|
const remove = useCallback((id: string) => {
|
|
publish(readStorage().filter((p) => p.id !== id));
|
|
}, []);
|
|
|
|
return { presets, save, remove };
|
|
}
|