(initialSelection);
const {
draft: customQuery,
committed: committedQuery,
setDraft: setCustomQuery,
setImmediate: setQueryImmediate,
commit: commitCustomQuery,
} = useCommittedQuery("");
const [projectFilter, setProjectFilter] = useState("");
const [saveDialogOpen, setSaveDialogOpen] = useState(false);
const {
presets: savedPresets,
save: saveSavedPreset,
remove: removeSavedPreset,
} = useSavedPresets();
const presets = selection.kind === "mr" ? MR_PRESETS : ISSUE_PRESETS;
const search = useGitLabSearch({
workspaceId: workspaceId ?? "",
kind: selection.kind,
presets,
preset: selection.source === "preset" ? selection.id : "",
customQuery: committedQuery,
projectFilter,
enabled: searchEnabled && Boolean(workspaceId),
});
const projectOptions = useProjectOptions(
selection,
committedQuery,
search.rawItems,
projectFilter,
);
const title = useMemo(
() => resolveTitle(selection, savedPresets, t),
[selection, savedPresets, t],
);
const onSelect = useCallback(
(s: SidebarSelection) => {
setSelection(s);
if (s.source === "saved") {
const found = savedPresets.find((p) => p.id === s.id);
setQueryImmediate(found?.customQuery ?? "");
setProjectFilter(found?.projectFilter ?? "");
return;
}
setQueryImmediate("");
setProjectFilter("");
},
[savedPresets, setQueryImmediate],
);
// Use committedQuery (not the unflushed draft) so the saved preset always
// matches what is currently displayed in the list.
const canSaveCurrent = committedQuery.trim().length > 0 || projectFilter.length > 0;
const suggestedLabel =
committedQuery.trim() || (projectFilter ? `In ${projectFilter}` : "Saved query");
const onOpenSaveDialog = () => {
if (canSaveCurrent) setSaveDialogOpen(true);
};
const onConfirmSave = (label: string) => {
const created = saveSavedPreset({
kind: selection.kind,
label,
customQuery: committedQuery,
projectFilter,
});
setSelection({ kind: selection.kind, source: "saved", id: created.id });
};
const onDeleteSaved = (id: string) => {
removeSavedPreset(id);
if (selection.source === "saved" && selection.id === id) {
const fallbackPresets = selection.kind === "mr" ? MR_PRESETS : ISSUE_PRESETS;
setSelection({ kind: selection.kind, source: "preset", id: fallbackPresets[0]?.value ?? "" });
setQueryImmediate("");
setProjectFilter("");
}
};
return {
selection,
customQuery,
committedQuery,
setCustomQuery,
commitCustomQuery,
projectFilter,
setProjectFilter,
savedPresets,
search,
projectOptions,
title,
onSelect,
canSaveCurrent,
suggestedLabel,
saveDialogOpen,
setSaveDialogOpen,
onOpenSaveDialog,
onConfirmSave,
onDeleteSaved,
};
}
function AuthenticatedLayout({
workspaceId,
state,
mrPresets,
issuePresets,
onStartTask,
host,
}: {
workspaceId?: string;
state: GitLabPageState;
mrPresets: GitLabTaskPreset[];
issuePresets: GitLabTaskPreset[];
onStartTask: (payload: GitLabLaunchPayload) => void;
host: string;
}) {
const { selection, search, projectOptions, title } = state;
const mrKeyToTasks = useMRKeyToTasks(workspaceId ?? null);
useAllWorkflowSnapshots(workspaceId ?? null);
// When the user narrows by project, the toolbar badge shows how many of the
// current page actually match (so it never reads "47" while three rows
// render). Pagination still uses search.total so the user can navigate to
// later pages that may contain more matches.
const displayedCount = state.projectFilter ? search.items.length : search.total;
return (
);
}
function useGitLabStatusFetch(workspaceId: string | undefined, enabled: boolean) {
const [result, setResult] = useState<{ workspaceId?: string; status: GitLabStatus | null }>({
workspaceId,
status: null,
});
const [loading, setLoading] = useState(true);
useEffect(() => {
if (!enabled || !workspaceId) {
setLoading(true);
return;
}
let cancelled = false;
setLoading(true);
fetchGitLabStatus({ cache: "no-store", workspaceId })
.then((s) => {
if (!cancelled) setResult({ workspaceId, status: s });
})
.catch(() => {
if (!cancelled) setResult({ workspaceId, status: null });
})
.finally(() => {
if (!cancelled) setLoading(false);
});
return () => {
cancelled = true;
};
}, [enabled, workspaceId]);
const current = enabled && result.workspaceId === workspaceId;
return { status: current ? result.status : null, loading: !current || loading };
}
function GitLabPageBody({
statusLoading,
connected,
reconnect,
workspaceId,
state,
mrPresets,
issuePresets,
onStartTask,
host,
}: {
statusLoading: boolean;
connected: boolean;
reconnect: boolean;
workspaceId?: string;
state: GitLabPageState;
mrPresets: GitLabTaskPreset[];
issuePresets: GitLabTaskPreset[];
onStartTask: (payload: GitLabLaunchPayload) => void;
host: string;
}) {
const { t } = useTranslation();
if (statusLoading) {
return (
{t("gitlab:checkingGitlabStatus")}
);
}
if (!connected) {
return (
);
}
return (
);
}
function GitLabPageOverlays({
state,
mobileSidebarOpen,
setMobileSidebarOpen,
workspaceId,
workflows,
steps,
repositories,
configuredHost,
launchPayload,
setLaunchPayload,
}: {
state: GitLabPageState;
mobileSidebarOpen: boolean;
setMobileSidebarOpen: (open: boolean) => void;
workspaceId?: string;
workflows: Workflow[];
steps: WorkflowStep[];
repositories: Repository[];
configuredHost: string;
launchPayload: GitLabLaunchPayload | null;
setLaunchPayload: (payload: GitLabLaunchPayload | null) => void;
}) {
const { t } = useTranslation();
const onMobileSidebarSelect = (selection: Parameters[0]) => {
state.onSelect(selection);
setMobileSidebarOpen(false);
};
const onMobileSaveCurrent = () => {
setMobileSidebarOpen(false);
state.onOpenSaveDialog();
};
return (
<>
{t("gitlab:filters")}
setLaunchPayload(null)}
/>
>
);
}
function useGitLabWorkspaceScope(serverWorkspaceId?: string) {
const activeWorkspaceId = useAppStore((state) => state.workspaces.activeId);
const workspaceId = activeWorkspaceId ?? serverWorkspaceId;
const switching = Boolean(serverWorkspaceId && workspaceId && serverWorkspaceId !== workspaceId);
const { status, loading } = useGitLabStatusFetch(workspaceId, !switching);
useEffect(() => {
if (switching) window.location.reload();
}, [switching]);
return {
workspaceId,
switching,
status,
statusLoading: switching || loading,
connected: Boolean(status?.authenticated),
reconnect: Boolean(status?.token_configured && !status.authenticated),
};
}
function useGitLabTaskPresets(workspaceId: string | undefined, switching: boolean) {
const { presets } = useGitLabActionPresets(switching ? null : workspaceId);
const mrPresets = useMemo(() => (presets?.mr ?? []).map(toGitLabTaskPreset), [presets?.mr]);
const issuePresets = useMemo(
() => (presets?.issue ?? []).map(toGitLabTaskPreset),
[presets?.issue],
);
return { mrPresets, issuePresets };
}
export function GitLabPageClient({
workspaceId,
workflows = [],
steps = [],
repositories = [],
}: GitLabPageClientProps = {}) {
const scope = useGitLabWorkspaceScope(workspaceId);
const { status, statusLoading, connected, reconnect } = scope;
const host = status?.host ?? "https://gitlab.com";
const state = useGitLabPageState(!statusLoading && connected, scope.workspaceId);
const [mobileSidebarOpen, setMobileSidebarOpen] = useState(false);
const [launchPayload, setLaunchPayload] = useState(null);
const { mrPresets, issuePresets } = useGitLabTaskPresets(scope.workspaceId, scope.switching);
useEffect(() => resetKnownProjectsStore, []);
const onOpenMobileSidebar = useCallback(() => setMobileSidebarOpen(true), []);
return (
);
}