diff --git a/apps/web/src/components/issue/IssuesPage.tsx b/apps/web/src/components/issue/IssuesPage.tsx index d1c9f77aa2917..94e7860c350de 100644 --- a/apps/web/src/components/issue/IssuesPage.tsx +++ b/apps/web/src/components/issue/IssuesPage.tsx @@ -9,7 +9,8 @@ import { 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 { issueEntryKey, type MergedIssueList } from "@t3tools/client-runtime/state/issues"; +import type { EnvironmentProject } from "@t3tools/client-runtime/state/models"; import type { EnvironmentId, IssueInvolvement, @@ -81,6 +82,7 @@ import { SidebarInset } from "~/components/ui/sidebar"; import { isCommandPaletteOpen } from "~/commandPaletteBus"; import { isElectron } from "~/env"; import { useLiveRefresh } from "~/hooks/useLiveRefresh"; +import { useStableCallback } from "~/hooks/useStableCallback"; import { resolveShortcutCommand, shortcutLabelForCommand } from "~/keybindings"; import { isTerminalFocused } from "~/lib/terminalFocus"; import { usePanelAnimationSettings, usePanelPresence } from "~/panelAnimations"; @@ -189,11 +191,75 @@ function hostOptionsFor( ]; } +// A change of selection should not rebuild the rows for the current list and filters. +function useIssueGroups({ + answers, + chosenHost, + hidden, + query, + projects, + order, +}: { + answers: MergedIssueList["answers"]; + chosenHost: string | undefined; + hidden: IssuesSearch["hideProjects"]; + query: string; + projects: ReadonlyArray; + order: IssueListSort; +}) { + const [pages, setPages] = useState>(new Map()); + const held = holdIssuePages(answers, pages); + const hiddenProjects = issueProjectKeys(hidden); + // 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. + // A server older than `excludeProjectIds` still answers for hidden projects. + const shown = + chosenHost || hiddenProjects.size > 0 + ? new Map( + [...held].map(([environmentId, page]) => [ + environmentId, + { + ...page, + entries: page.entries.filter( + (entry) => + !hiddenProjects.has( + pullRequestProjectKey({ environmentId, id: entry.projectId }), + ), + ), + errors: page.errors.filter( + (error) => + !hiddenProjects.has( + pullRequestProjectKey({ environmentId, id: error.projectId }), + ), + ), + answer: { + ...page.answer, + scopes: page.answer.scopes?.filter( + (scope) => + !hiddenProjects.has( + pullRequestProjectKey({ environmentId, id: scope.projectId }), + ), + ), + trackers: chosenHost + ? page.answer.trackers.filter( + (tracker) => tracker.host.toLowerCase() === chosenHost, + ) + : page.answer.trackers, + }, + }, + ]), + ) + : held; + const groups = groupIssues(shown, query, projects, order); + return { held, shown, groups, setPages }; +} + export function IssuesPage() { const gt = useGT(); const resolve = useMessages(); const navigate = useNavigate({ from: "/issues" }); - const search = useSearch({ from: "/_chat/issues" }); + const search = useSearch({ from: "/_chat/issues", structuralSharing: true }); const { environments } = useEnvironments(); const projects = useProjects(); const issuesSupported = useIssuesSupported(); @@ -242,8 +308,14 @@ export function IssuesPage() { { key: JSON.stringify(targets) }, ); - const [pages, setPages] = useState>(new Map()); - const held = holdIssuePages(list.answers, pages); + const { held, shown, groups, setPages } = useIssueGroups({ + answers: list.answers, + chosenHost: search.host?.toLowerCase(), + hidden: search.hideProjects, + query: debouncedQuery, + projects, + order: search.order ?? "updated", + }); const loadPage = useAtomCommand(issueEnvironment.listPage, { reportFailure: false }); const [loadingMore, setLoadingMore] = useState(null); // One tracker group's button carries on that tracker's scopes only. @@ -276,49 +348,6 @@ export function IssuesPage() { }); }; - // 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(); - // A server older than `excludeProjectIds` still answers for hidden projects. - const shown = - chosenHost || hiddenProjects.size > 0 - ? new Map( - [...held].map(([environmentId, page]) => [ - environmentId, - { - ...page, - entries: page.entries.filter( - (entry) => - !hiddenProjects.has( - pullRequestProjectKey({ environmentId, id: entry.projectId }), - ), - ), - errors: page.errors.filter( - (error) => - !hiddenProjects.has( - pullRequestProjectKey({ environmentId, id: error.projectId }), - ), - ), - answer: { - ...page.answer, - scopes: page.answer.scopes?.filter( - (scope) => - !hiddenProjects.has( - pullRequestProjectKey({ environmentId, id: scope.projectId }), - ), - ), - trackers: chosenHost - ? page.answer.trackers.filter( - (tracker) => tracker.host.toLowerCase() === chosenHost, - ) - : page.answer.trackers, - }, - }, - ]), - ) - : held; - const groups = groupIssues(shown, debouncedQuery, projects, search.order ?? "updated"); const filterScopes = issueFilterScopes( shown, projects.filter((project) => !hiddenProjects.has(pullRequestProjectKey(project))), @@ -419,18 +448,20 @@ export function IssuesPage() { : clearedSelection, ); - const selectEntry = (entry: Omit & { readonly host?: string }) => { - 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, - }); - }; + const selectEntry = useStableCallback( + (entry: Omit & { readonly host?: string }) => { + 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