import { useAtomValue } from "@effect/atom-react"; import { continueIssuePage, groupIssues, holdIssuePages, issueCursorsForHost, issueFilterScopes, issueTrackerKey, type HeldIssuePage, } from "@t3tools/client-runtime/issue-list"; import { issueTrackerDetailText } from "@t3tools/client-runtime/state/issue-trackers"; import { issueEntryKey } from "@t3tools/client-runtime/state/issues"; import type { EnvironmentId, IssueInvolvement, IssueListInput, IssueListSort, IssueListState, IssueTrackerKind, IssueTrackerSummary, ProjectId, } from "@t3tools/contracts"; import { useNavigate, useSearch } from "@tanstack/react-router"; import { ArrowDownUpIcon, AtSignIcon, CalendarArrowDownIcon, CircleCheckIcon, ClockIcon, LayersIcon, PenLineIcon, Plug2Icon, UserCheckIcon, UsersIcon, } from "lucide-react"; import { msg, T, useGT, useMessages, Var } from "gt-react"; import { Fragment, useEffect, useEffectEvent, useState, type ReactNode } from "react"; import { RefreshIcon } from "~/components/ui/refresh-icon"; import { IssueDetailPanel } from "~/components/issue/IssueDetailPanel"; import { IssueRow, type IssueRowTarget } from "~/components/issue/IssueRow"; import { issueListPreferences, writeIssueListPreferences, type IssueListPreferences, } from "~/components/issue/issueListPreferences"; import { IssueGlyph, issueTrackerPresentation } from "~/components/issue/issuePresentation"; import { PullRequestDetailPanel } from "~/components/pullRequest/PullRequestDetailPanel"; import { PullRequestListGhost } from "~/components/pullRequest/PullRequestGhosts"; import { PullRequestFilterOptionIcon, PullRequestSearchInput, type PullRequestFilterOption, } from "~/components/pullRequest/PullRequestListFilters"; import { m, type MessageResolver } from "~/i18n/messages"; import { resolveFilterOptions } from "../pullRequest/pullRequestListOptions"; import { IssueFiltersMenu } from "./IssueListFilterPickers"; import { issueFilterProjects } from "./issuesSearch.logic"; import { ISSUE_SAVED_VIEWS } from "./issueSavedViews"; import { SavedViewsMenu } from "~/components/SavedViewsMenu"; import { PullRequestCompactFilterMenu } from "../pullRequest/PullRequestCompactFilterMenu"; import { PullRequestsUnavailableState } from "~/components/pullRequest/PullRequestsUnavailableState"; import { RightPanelTabs } from "~/components/RightPanelTabs"; import { WorkspaceBreadcrumb, WorkspaceBreadcrumbItem } from "~/components/WorkspaceBreadcrumb"; import { WorkspacePageContainer } from "~/components/WorkspacePageContainer"; import { WorkspacePageHeader } from "~/components/WorkspacePageHeader"; import { PanelLayoutControls } from "~/components/chat/PanelLayoutControls"; import { Button } from "~/components/ui/button"; import { Empty, EmptyDescription, EmptyHeader, EmptyMedia, EmptyTitle, } from "~/components/ui/empty"; import { Menu, MenuPopup, MenuRadioGroup, MenuRadioItem, MenuTrigger } from "~/components/ui/menu"; import { SidebarInset } from "~/components/ui/sidebar"; import { isCommandPaletteOpen } from "~/commandPaletteBus"; import { isElectron } from "~/env"; import { useLiveRefresh } from "~/hooks/useLiveRefresh"; import { resolveShortcutCommand, shortcutLabelForCommand } from "~/keybindings"; import { isTerminalFocused } from "~/lib/terminalFocus"; import { usePanelAnimationSettings, usePanelPresence } from "~/panelAnimations"; import { ISSUES_PANEL_REF, selectActiveRightPanelSurface, selectSelectedRightPanelSurface, selectThreadRightPanelState, useRightPanelStore, type PullRequestSurface, type RightPanelSurface, } from "~/rightPanelStore"; import { useAtomCommand } from "~/state/use-atom-command"; import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; import { useProjects } from "~/state/entities"; import { useEnvironments, useIssuesSupported } from "~/state/environments"; import { issueEnvironment, useIssueList, type IssueListTarget } from "~/state/issues"; import { useDebouncedValue } from "~/state/queries"; import { primaryServerKeybindingsAtom } from "~/state/server"; export interface IssuesSearch extends IssueListPreferences { readonly selectedEnvironmentId?: EnvironmentId | undefined; readonly selectedProjectId?: ProjectId | undefined; /** Part of the panel tab's identity, so a link reopens the tab a row press opened. */ readonly selectedHost?: string | undefined; readonly tracker?: string | undefined; readonly scope?: string | undefined; readonly key?: string | undefined; } const STATE_TABS = [ { value: "open", label: msg("Open", { $context: "issue state" }), Icon: IssueGlyph.issue }, { value: "closed", label: msg("Closed", { $context: "issue state" }), Icon: CircleCheckIcon }, { value: "all", label: msg("All", { $context: "issue filter" }), Icon: LayersIcon }, ] as const satisfies ReadonlyArray>; const INVOLVEMENT_TABS = [ { value: "all", label: msg("Everyone", { $context: "issue filter" }), Icon: UsersIcon }, { value: "assigned", label: msg("Assigned to me"), Icon: UserCheckIcon }, { value: "authored", label: msg("Opened by me"), Icon: PenLineIcon }, { value: "mentioned", label: msg("Mentioning me"), Icon: AtSignIcon }, ] as const satisfies ReadonlyArray>; const SORT_OPTIONS = [ { value: "updated", label: msg("Recently updated"), Icon: ClockIcon }, { value: "created", label: msg("Newest", { $context: "sort order" }), Icon: CalendarArrowDownIcon, }, ] as const satisfies ReadonlyArray>; const PAGE_SIZE = 50; const EMPTY_SET: ReadonlySet = new Set(); const EMPTY_RECORD = {} as const; const EMPTY_MAP = new Map(); function getShortcutContext() { return { terminalFocus: isTerminalFocused(), terminalOpen: false, previewFocus: false, previewOpen: false, modelPickerOpen: false, isWeb: !isElectron, isDesktop: isElectron, }; } function hostOptionsFor( trackers: ReadonlyArray, chosen: string | undefined, resolve: MessageResolver, ) { const hosts = new Map(); const ordered = [...trackers].sort((a, b) => issueTrackerKey(a).localeCompare(issueTrackerKey(b)), ); for (const tracker of ordered) { const existing = hosts.get(tracker.host); if (existing === undefined || (!existing.configured && tracker.configured)) hosts.set(tracker.host, tracker); } return [ { value: "", label: resolve(msg("All", { $context: "issue filter" })), Icon: Plug2Icon }, // A remembered host no project is on any more stays listed, so the filter can be taken off. ...(chosen !== undefined && !hosts.has(chosen) ? [{ value: chosen, label: chosen, Icon: Plug2Icon }] : []), ...[...hosts.values()].map((tracker) => ({ value: tracker.host, label: hosts.size > 1 && issueTrackerPresentation(tracker.tracker).providerName === tracker.host ? tracker.host : `${issueTrackerPresentation(tracker.tracker).providerName} (${tracker.host})`, Icon: issueTrackerPresentation(tracker.tracker).Icon, ...(tracker.configured ? {} : { unavailable: tracker.detail === null ? resolve(msg("Unavailable")) : issueTrackerDetailText(tracker.detail, resolve), }), })), ]; } export function IssuesPage() { const gt = useGT(); const resolve = useMessages(); const navigate = useNavigate({ from: "/issues" }); const search = useSearch({ from: "/_chat/issues" }); const { environments } = useEnvironments(); const projects = useProjects(); const issuesSupported = useIssuesSupported(); const keybindings = useAtomValue(primaryServerKeybindingsAtom); const capableEnvironments = environments.filter( (environment) => environment.serverConfig?.environment.capabilities.issues === true, ); const environmentLabels = new Map(environments.map((e) => [e.environmentId, e.label] as const)); const filterProjects = issueFilterProjects( projects.filter((project) => capableEnvironments.some((e) => e.environmentId === project.environmentId), ), environmentLabels, ); const scopedProject = search.issueServer && search.issueProject ? { environmentId: search.issueServer, projectId: search.issueProject } : undefined; const debouncedQuery = useDebouncedValue(search.q ?? "", 300); const listInput: IssueListInput = { state: search.status, involvement: search.who, limit: PAGE_SIZE, ...(scopedProject ? { projectId: scopedProject.projectId } : {}), ...(search.host ? { host: search.host } : {}), ...(debouncedQuery ? { query: debouncedQuery } : {}), ...(search.label?.length ? { labels: search.label } : {}), ...(search.assignee ? { assignee: search.assignee } : {}), ...(search.order ? { sort: search.order } : {}), }; const targets: ReadonlyArray = capableEnvironments .filter((e) => scopedProject === undefined || e.environmentId === scopedProject.environmentId) .map((e) => ({ environmentId: e.environmentId, input: listInput })); const list = useIssueList(targets); useLiveRefresh(list.refresh, { key: JSON.stringify(targets) }); const [pages, setPages] = useState>(new Map()); const held = holdIssuePages(list.answers, pages); const loadPage = useAtomCommand(issueEnvironment.listPage, { reportFailure: false }); const [loadingMore, setLoadingMore] = useState(null); // One tracker group's button carries on that tracker's scopes only. const loadMore = async (environmentId: EnvironmentId, host: string) => { const page = held.get(environmentId); const cursors = page ? issueCursorsForHost(page.nextCursors, host) : {}; if (!page || Object.keys(cursors).length === 0 || loadingMore !== null) return; setLoadingMore(`${environmentId} ${host}`); const result = await loadPage({ environmentId, input: { ...listInput, cursors } }); setLoadingMore(null); const failure = result._tag === "Failure" ? squashAtomCommandFailure(result) : null; setPages((current) => { const next = new Map(current); next.set( environmentId, continueIssuePage( page, result._tag === "Failure" ? { failure: failure instanceof Error ? failure.message : m(msg("The next page failed.")), } : result.value, cursors, ), ); return next; }); }; // Rows grouped per tracker account, so a tracker that cannot be read says so where its rows // would be, and each tracker's rows carry on from their own cursors. // The server names every tracker so the menu can switch hosts, but reads only the chosen one. const chosenHost = search.host?.toLowerCase(); const shown = chosenHost ? new Map( [...held].map(([environmentId, page]) => [ environmentId, { ...page, answer: { ...page.answer, trackers: page.answer.trackers.filter( (tracker) => tracker.host.toLowerCase() === chosenHost, ), }, }, ]), ) : held; const groups = groupIssues(shown, debouncedQuery, projects, search.order ?? "updated"); const filterScopes = issueFilterScopes(shown, projects, scopedProject); const hostOptions = hostOptionsFor( [...held.values()].flatMap((page) => page.answer.trackers), search.host, resolve, ); const updateSearch = (patch: { [Key in keyof IssuesSearch]?: IssuesSearch[Key] | undefined }) => { const merged = { ...search, ...patch }; const next: IssuesSearch = { who: merged.who ?? search.who, status: merged.status ?? search.status, ...(merged.issueServer && merged.issueProject ? { issueServer: merged.issueServer, issueProject: merged.issueProject } : {}), ...(merged.host ? { host: merged.host } : {}), q: merged.q || undefined, ...(merged.label?.length ? { label: merged.label } : {}), ...(merged.assignee ? { assignee: merged.assignee } : {}), ...(merged.order ? { order: merged.order } : {}), ...(merged.selectedEnvironmentId ? { selectedEnvironmentId: merged.selectedEnvironmentId } : {}), ...(merged.selectedProjectId ? { selectedProjectId: merged.selectedProjectId } : {}), ...(merged.selectedHost ? { selectedHost: merged.selectedHost } : {}), ...(merged.tracker ? { tracker: merged.tracker } : {}), ...(merged.scope ? { scope: merged.scope } : {}), ...(merged.key ? { key: merged.key } : {}), }; writeIssueListPreferences(issueListPreferences(next)); void navigate({ search: next }); }; const clearedSelection = { selectedEnvironmentId: undefined, selectedProjectId: undefined, selectedHost: undefined, tracker: undefined, scope: undefined, key: undefined, } as const; // One shared panel for the page, with a fixed sentinel ref, as the pull-request page does. const rightPanelRef = capableEnvironments.length === 0 ? null : ISSUES_PANEL_REF; const rightPanelState = useRightPanelStore((state) => selectThreadRightPanelState(state.byThreadKey, rightPanelRef), ); const selectedSurface = useRightPanelStore((state) => selectSelectedRightPanelSurface(state.byThreadKey, rightPanelRef), ); const panelSurface = selectedSurface?.kind === "issue" || selectedSurface?.kind === "pull-request" ? selectedSurface : null; const { active: panelAnimationsActive, durationMs } = usePanelAnimationSettings(); const presenceValue = { activeSurface: panelSurface, surfaces: rightPanelState.surfaces }; const presence = usePanelPresence( rightPanelState.isOpen && panelSurface !== null, presenceValue, panelAnimationsActive, rightPanelRef?.threadId ?? null, durationMs, ); const renderedSurface = presence.value?.activeSurface ?? null; const panelEnvironmentId = (renderedSurface?.environmentId as EnvironmentId | undefined) ?? capableEnvironments[0]?.environmentId ?? null; const panelListEntry = renderedSurface?.kind === "issue" ? (groups .flatMap((group) => group.rows) .find( (row) => row.environmentId === panelEnvironmentId && issueEntryKey(row) === issueEntryKey({ ...renderedSurface, host: renderedSurface.host ?? row.host, projectId: renderedSurface.projectId as ProjectId, }), ) ?? null) : null; const selectSurfaceInUrl = (surface: RightPanelSurface | null) => updateSearch( surface?.kind === "issue" ? { selectedEnvironmentId: surface.environmentId as EnvironmentId | undefined, selectedProjectId: surface.projectId as ProjectId, selectedHost: surface.host, tracker: surface.tracker, scope: surface.scope, key: surface.key, } : clearedSelection, ); const selectEntry = (entry: IssueRowTarget) => { if (rightPanelRef === null) return; useRightPanelStore.getState().openIssue(rightPanelRef, entry); updateSearch({ selectedEnvironmentId: entry.environmentId, selectedProjectId: entry.projectId, selectedHost: entry.host, tracker: entry.tracker, scope: entry.scope, key: entry.key, }); }; // A link into the page names an issue before any row was pressed. // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- opens the URL-linked issue in the right panel store useEffect(() => { if ( rightPanelRef === null || !search.tracker || !search.scope || !search.key || !search.selectedProjectId ) return; useRightPanelStore.getState().openIssue(rightPanelRef, { ...(search.selectedEnvironmentId === undefined ? {} : { environmentId: search.selectedEnvironmentId }), projectId: search.selectedProjectId, ...(search.selectedHost === undefined ? {} : { host: search.selectedHost }), tracker: search.tracker, scope: search.scope, key: search.key, }); }, [ rightPanelRef, search.tracker, search.scope, search.key, search.selectedProjectId, search.selectedHost, search.selectedEnvironmentId, ]); const closeSurface = (surface: RightPanelSurface) => { if (rightPanelRef === null) return; useRightPanelStore.getState().closeSurface(rightPanelRef, surface.id); selectSurfaceInUrl( selectActiveRightPanelSurface(useRightPanelStore.getState().byThreadKey, rightPanelRef), ); }; const toggleRightPanel = () => { if (rightPanelRef === null) return; if (rightPanelState.isOpen) { useRightPanelStore.getState().close(rightPanelRef); updateSearch(clearedSelection); return; } if (panelSurface === null) return; useRightPanelStore.getState().show(rightPanelRef); selectSurfaceInUrl(panelSurface); }; const onRightPanelKeyDown = useEffectEvent((event: KeyboardEvent) => { if (event.defaultPrevented || isCommandPaletteOpen() || panelSurface === null) return; const command = resolveShortcutCommand(event, keybindings, { context: { ...getShortcutContext(), rightPanelOpen: rightPanelState.isOpen }, }); if (command !== "rightPanel.toggle") return; event.preventDefault(); event.stopPropagation(); if (!event.repeat) toggleRightPanel(); }); // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- subscribes to the document right panel shortcut useEffect(() => { document.addEventListener("keydown", onRightPanelKeyDown); return () => document.removeEventListener("keydown", onRightPanelKeyDown); }, []); // Every writable scope the workspace has, for the composer. const showTracker = groups.length > 1; const noneAnswered = list.answers.length === 0; const listBody: ReactNode = !issuesSupported ? ( ) : noneAnswered && list.isPending ? ( ) : noneAnswered && list.error ? ( ) : groups.length === 0 ? ( No trackers to read Add a project whose remote is on a supported tracker. ) : (
{list.error ? (
{list.error} Showing the issues that loaded.
) : null} {groups.map((group) => { const presentation = issueTrackerPresentation(group.tracker.tracker); const showScope = new Set(group.rows.map((row) => row.scope.toLowerCase())).size > 1; return (

{presentation.providerName} {presentation.providerName.toLowerCase() !== group.tracker.host ? ( · {group.tracker.host} ) : null} ·{" "} {gt("{count, plural, one {{count} project} other {{count} projects}}", { count: group.tracker.projectCount, })}

{!group.tracker.configured ? (

{group.tracker.detail === null ? gt("This tracker cannot be read right now.") : issueTrackerDetailText(group.tracker.detail, resolve)}

) : null} {group.errors.map((error) => (

{error.projectTitle}: {error.message}

))} {group.tracker.configured && group.rows.length === 0 && group.errors.length === 0 ? (

{debouncedQuery ? ( Nothing matches that. ) : ( No issues under these filters. )}

) : null} {group.rows.map((row) => { const key = issueEntryKey(row); const selected = renderedSurface?.kind === "issue" && renderedSurface.environmentId === row.environmentId && issueEntryKey({ ...renderedSurface, host: renderedSurface.host ?? "", projectId: renderedSurface.projectId as ProjectId, }) === key; return ( 1 && environmentLabels.has(row.environmentId) ? { environmentLabel: environmentLabels.get(row.environmentId) } : {})} onSelect={selectEntry} /> ); })} {group.more.map((environmentId) => { const moreFailure = held.get(environmentId)?.moreFailure ?? null; return ( {moreFailure === null ? null : (

{moreFailure}

)}
); })}
); })}
); return (

Issues

undefined} rightPanelAvailable={panelSurface !== null} rightPanelOpen={rightPanelState.isOpen} rightPanelShortcutLabel={shortcutLabelForCommand(keybindings, "rightPanel.toggle")} rightPanelUnavailableLabel={gt("Select an issue first")} onToggleTerminal={() => undefined} onToggleRightPanel={toggleRightPanel} />
0 && list.isPending) } onChange={(q) => updateSearch({ q: q || undefined })} onSubmit={(q) => updateSearch({ q: q || undefined })} />
} triggerLabel={gt("Sort", { $context: "verb, button that sorts a list" })} outlined value={search.order ?? "updated"} options={resolveFilterOptions(SORT_OPTIONS, resolve)} onChange={(order) => updateSearch({ order: order === "created" ? "created" : undefined }) } /> updateSearch({ status })} involvement={search.who} involvementOptions={resolveFilterOptions(INVOLVEMENT_TABS, resolve)} onInvolvement={(who) => updateSearch({ who })} scopes={filterScopes} labels={search.label ?? []} onLabels={(label) => updateSearch({ label })} assignee={search.assignee} onAssignee={(assignee) => updateSearch({ assignee })} projects={filterProjects} project={scopedProject} onProject={(project) => updateSearch({ issueServer: project?.environmentId, issueProject: project?.projectId, }) } /> // Explicit clears replace the old list scope; the selected issue stays open. updateSearch({ who: view.who, status: view.status, issueServer: view.issueServer, issueProject: view.issueProject, host: view.host, q: view.q, label: view.label, assignee: view.assignee, order: view.order, }) } /> {hostOptions.length > 1 ? ( updateSearch({ host: host || undefined })} /> ) : null}
{listBody}
{presence.present && renderedSurface && panelEnvironmentId !== null && rightPanelRef !== null ? ( { useRightPanelStore.getState().activateSurface(rightPanelRef, surface.id); selectSurfaceInUrl(surface); }} onCloseSurface={closeSurface} onCloseOtherSurfaces={(surface) => { useRightPanelStore.getState().closeOtherSurfaces(rightPanelRef, surface.id); selectSurfaceInUrl(surface); }} onCloseSurfacesToRight={(surface) => { useRightPanelStore.getState().closeSurfacesToRight(rightPanelRef, surface.id); selectSurfaceInUrl( selectActiveRightPanelSurface( useRightPanelStore.getState().byThreadKey, rightPanelRef, ), ); }} onCloseAllSurfaces={() => { useRightPanelStore.getState().closeAllSurfaces(rightPanelRef); selectSurfaceInUrl(null); }} onMoveSurface={(surfaceId, toIndex) => useRightPanelStore.getState().moveSurface(rightPanelRef, surfaceId, toIndex) } onCopyFilePath={() => undefined} onAddBrowser={() => undefined} onAddBrowserInProfile={() => undefined} onAddTerminal={() => undefined} onAddDiff={() => undefined} onAddFiles={() => undefined} onAddPullRequest={() => undefined} onAddPullRequests={() => undefined} onAddDevice={() => undefined} browserAvailable={false} terminalAvailable={false} diffAvailable={false} filesAvailable={false} pullRequestAvailable={false} pullRequestsAvailable={false} deviceAvailable={false} > {renderedSurface.kind === "issue" ? ( { useRightPanelStore.getState().openPullRequest(rightPanelRef, { projectId: reference.projectId, repository: reference.repository, number: reference.number, ...(reference.host === undefined ? {} : { host: reference.host }), environmentId: panelEnvironmentId, }); }} /> ) : ( )} ) : null}
); } function FilterMenu({ label, value, options, onChange, }: { label: string; value: Value; options: ReadonlyArray>; onChange: (value: Value) => void; }) { const current = options.find((option) => option.value === value) ?? options[0]; if (!current) return null; return ( }> {current.label} onChange(next as Value)}> {options.map((option) => ( {option.label} ))} ); }