"use client";
import { useCallback, useEffect, useMemo, useState } from "react";
import Link from "next/link";
import {
IconBrandGitlab,
IconBug,
IconCheck,
IconClock,
IconExternalLink,
IconGitMerge,
IconRefresh,
IconX,
} from "@tabler/icons-react";
import { Badge } from "@kandev/ui/badge";
import { Button } from "@kandev/ui/button";
import { Card, CardContent } from "@kandev/ui/card";
import { Input } from "@kandev/ui/input";
import { Spinner } from "@kandev/ui/spinner";
import { Tabs, TabsList, TabsTrigger, TabsContent } from "@kandev/ui/tabs";
import { PageTopbar } from "@/components/page-topbar";
import { fetchGitLabStatus } from "@/lib/api/domains/gitlab-api";
import { useGitLabUserIssues, useGitLabUserMRs } from "@/hooks/domains/gitlab/use-user-search";
import type { GitLabStatus, Issue, MR } from "@/lib/types/gitlab";
// Filter values map to GitLab's "scope" query param semantics.
const MR_FILTERS = [
{ value: "assigned_to_me", label: "Assigned" },
{ value: "created_by_me", label: "Authored" },
{ value: "review_requested", label: "Review requested" },
] as const;
const ISSUE_FILTERS = [
{ value: "assigned_to_me", label: "Assigned" },
{ value: "created_by_me", label: "Created" },
] as const;
function MRStateBadge({ mr }: { mr: MR }) {
const state = mr.state === "opened" ? "open" : mr.state;
if (state === "merged") {
return (
Merged
);
}
if (state === "closed") {
return (
Closed
);
}
if (mr.draft) {
return (
Draft
);
}
return (
Open
);
}
function IssueStateBadge({ issue }: { issue: Issue }) {
if (issue.state === "closed") {
return (
Closed
);
}
return (
Open
);
}
function NotConnectedNotice() {
return (
GitLab not connected
Connect your GitLab account to browse merge requests and issues from this page.
);
}
function MRList({ filter, query }: { filter: string; query: string }) {
const { items: mrs, loading, error } = useGitLabUserMRs(filter, query);
if (loading) {
return (
);
}
if (error) {
return {error}
;
}
if (mrs.length === 0) {
return (
No merge requests found.
);
}
return (
{mrs.map((mr) => (
-
{mr.title}
{mr.project_path} !{mr.iid} · {mr.head_branch} → {mr.base_branch}
))}
);
}
function IssueList({ filter, query }: { filter: string; query: string }) {
const { items: issues, loading, error } = useGitLabUserIssues(filter, query);
if (loading) {
return (
);
}
if (error) {
return {error}
;
}
if (issues.length === 0) {
return No issues found.
;
}
return (
{issues.map((issue) => (
-
{issue.title}
{issue.project_path} #{issue.iid}
{issue.labels.length > 0 && ` · ${issue.labels.slice(0, 3).join(", ")}`}
))}
);
}
export function GitLabPageClient({ workspaceId: _workspaceId }: { workspaceId?: string }) {
const [status, setStatus] = useState(null);
const [statusLoading, setStatusLoading] = useState(true);
const [mrFilter, setMrFilter] = useState(MR_FILTERS[0].value);
const [issueFilter, setIssueFilter] = useState(ISSUE_FILTERS[0].value);
const [search, setSearch] = useState("");
const [debounced, setDebounced] = useState("");
useEffect(() => {
const t = setTimeout(() => setDebounced(search), 200);
return () => clearTimeout(t);
}, [search]);
const reloadStatus = useCallback(() => {
setStatusLoading(true);
fetchGitLabStatus({ cache: "no-store" })
.then(setStatus)
.catch(() => setStatus(null))
.finally(() => setStatusLoading(false));
}, []);
useEffect(() => {
let cancelled = false;
fetchGitLabStatus({ cache: "no-store" })
.then((s) => {
if (!cancelled) setStatus(s);
})
.catch(() => {
if (!cancelled) setStatus(null);
})
.finally(() => {
if (!cancelled) setStatusLoading(false);
});
return () => {
cancelled = true;
};
}, []);
// Pass debounced search through as the custom_query. GitLab understands
// free-text search via the `search` URL param; mapping the raw text to it
// keeps the surface simple — power users can still type the param-style
// string like "labels=bug&author_username=me" and the backend forwards
// it as customQuery.
const customQuery = useMemo(() => {
const q = debounced.trim();
if (!q) return "";
if (q.includes("=")) return q; // assume raw key=value
return `search=${encodeURIComponent(q)}`;
}, [debounced]);
const connected = status?.authenticated || status?.token_configured;
return (
}
actions={
}
/>
{!statusLoading && !connected ? (
) : (
)}
);
}
function BrowseTabs({
search,
setSearch,
mrFilter,
setMrFilter,
issueFilter,
setIssueFilter,
customQuery,
}: {
search: string;
setSearch: (v: string) => void;
mrFilter: string;
setMrFilter: (v: string) => void;
issueFilter: string;
setIssueFilter: (v: string) => void;
customQuery: string;
}) {
return (
<>
setSearch(e.target.value)}
className="font-mono text-sm"
/>
Merge requests
Issues
>
);
}
function FilterButtons({
value,
onChange,
options,
}: {
value: string;
onChange: (v: string) => void;
options: readonly { value: string; label: string }[];
}) {
return (
{options.map((f) => (
))}
);
}