fix(web,mobile): pull request project filter follows the sidebar order (#19180)
This commit is contained in:
1 parent
cb395ee13b
commit
464fad9a43
6 files changed
+162
-45
No files matched your search
@@ -1,6 +1,11 @@
|
||||
import { sortProjectsByGroupOrder } from "@t3tools/client-runtime/state/project-grouping";
|
||||
import { useMemo } from "react";
|
||||
import { ScrollView } from "react-native";
|
||||
import type { ControlPillMenuProps } from "../../components/ControlPillMenu.types";
|
||||
import { ListFilterMenu } from "../workspace/ListFilterMenu";
|
||||
import { useProjects, useThreadShells } from "../../state/entities";
|
||||
import { useMobileProjectGroupingSettings } from "../../state/project-grouping";
|
||||
import { buildHomeProjectScopes, sortHomeProjectScopes } from "../home/homeThreadList";
|
||||
import { DEFAULT_PR_LIST_CONTROLS, type PullRequestListControls } from "./pullRequestList";
|
||||
|
||||
const STATES = { open: "Open", closed: "Closed", merged: "Merged", all: "All states" } as const;
|
||||
@@ -28,6 +33,30 @@ function choices(field: string, options: Readonly<Record<string, string>>, selec
|
||||
}));
|
||||
}
|
||||
|
||||
/** The project list's own order, which is the order its sidebar and new-task picker show. */
|
||||
function useProjectsInSidebarOrder<Project extends { id: string; environmentId: string }>(
|
||||
projects: readonly Project[],
|
||||
) {
|
||||
const allProjects = useProjects();
|
||||
const threads = useThreadShells();
|
||||
const { sidebarProjectGroupingMode } = useMobileProjectGroupingSettings();
|
||||
const scopes = useMemo(
|
||||
() =>
|
||||
sortHomeProjectScopes({
|
||||
scopes: buildHomeProjectScopes({
|
||||
projects: allProjects,
|
||||
environmentId: null,
|
||||
projectGroupingMode: sidebarProjectGroupingMode,
|
||||
}),
|
||||
threads,
|
||||
pendingTasks: [],
|
||||
projectSortOrder: "updated_at",
|
||||
}).map((scope) => ({ memberProjectRefs: scope.projectRefs })),
|
||||
[allProjects, sidebarProjectGroupingMode, threads],
|
||||
);
|
||||
return useMemo(() => sortProjectsByGroupOrder(projects, scopes), [projects, scopes]);
|
||||
}
|
||||
|
||||
export function PullRequestListFilters(props: {
|
||||
readonly environments: readonly { id: string; label: string }[];
|
||||
readonly environmentId: string | null;
|
||||
@@ -39,6 +68,7 @@ export function PullRequestListFilters(props: {
|
||||
readonly projects: readonly { id: string; title: string; environmentId: string }[];
|
||||
}) {
|
||||
const { value } = props;
|
||||
const projects = useProjectsInSidebarOrder(props.projects);
|
||||
const selectedLabels = value.filters.labels?.flat() ?? [];
|
||||
const selectedLabelSet = new Set(selectedLabels);
|
||||
const author = value.filters.author;
|
||||
@@ -76,7 +106,7 @@ export function PullRequestListFilters(props: {
|
||||
props.onChange({ ...value, sort: next as PullRequestListControls["sort"] });
|
||||
break;
|
||||
case "project": {
|
||||
const project = props.projects.find(
|
||||
const project = projects.find(
|
||||
(project) => JSON.stringify([project.environmentId, project.id]) === next,
|
||||
);
|
||||
props.onChange({
|
||||
@@ -165,7 +195,7 @@ export function PullRequestListFilters(props: {
|
||||
{
|
||||
id: "projects",
|
||||
title:
|
||||
props.projects.find(
|
||||
projects.find(
|
||||
(project) =>
|
||||
project.id === value.projectId && project.environmentId === value.projectEnvironmentId,
|
||||
)?.title ?? "Project",
|
||||
@@ -173,7 +203,7 @@ export function PullRequestListFilters(props: {
|
||||
"project",
|
||||
Object.fromEntries([
|
||||
["", "All projects"],
|
||||
...props.projects.map((project) => [
|
||||
...projects.map((project) => [
|
||||
JSON.stringify([project.environmentId, project.id]),
|
||||
project.title,
|
||||
]),
|
||||
|
||||
@@ -128,21 +128,21 @@ import { isMacPlatform } from "~/lib/utils";
|
||||
import { useOpenPrLink } from "../lib/openPullRequestLink";
|
||||
import { readLocalApi } from "../localApi";
|
||||
import { useSidebarPendingFileDropStore } from "../sidebarPendingFileDropStore";
|
||||
import { getProjectOrderKey, selectProjectGroupingSettings } from "../logicalProject";
|
||||
import { selectProjectGroupingSettings } from "../logicalProject";
|
||||
import {
|
||||
buildSidebarProjectSnapshots,
|
||||
projectGroupsSpanEnvironments,
|
||||
resolveSidebarProjectScopeKey,
|
||||
getSidebarProjectSettingsKey,
|
||||
type SidebarProjectSnapshot,
|
||||
} from "../sidebarProjectGrouping";
|
||||
import { legacyProjectCwdPreferenceKey, useUiStateStore } from "../uiStateStore";
|
||||
import { useUiStateStore } from "../uiStateStore";
|
||||
import {
|
||||
getThreadKeysToDeselectAfterDelete,
|
||||
useThreadSelectionStore,
|
||||
} from "../threadSelectionStore";
|
||||
import { useAcknowledgeThreadWoke, useThreadActions } from "../hooks/useThreadActions";
|
||||
import { useHandleNewThread } from "../hooks/useHandleNewThread";
|
||||
import { useSidebarProjectGroups } from "../hooks/useSidebarProjectGroups";
|
||||
import { useTerminalFocus } from "../hooks/useTerminalFocus";
|
||||
import { isCommandPaletteOpen, openCommandPalette } from "../commandPaletteBus";
|
||||
import { registerSettleAndAdvance } from "../settleAndAdvance";
|
||||
@@ -224,7 +224,6 @@ import {
|
||||
sidebarThreadKeyAtY,
|
||||
sortInboxThreadsByReturn,
|
||||
sortPinnedThreadsForSidebar,
|
||||
sortSidebarV2ProjectGroups,
|
||||
sortThreadsForSidebar,
|
||||
sortWorkingThreadsBySend,
|
||||
useThreadJumpHintVisibility,
|
||||
@@ -2420,14 +2419,12 @@ const SidebarSearchResultRow = memo(function SidebarSearchResultRow(props: {
|
||||
|
||||
export default function Sidebar() {
|
||||
const projects = useProjects();
|
||||
const projectOrder = useUiStateStore((store) => store.projectOrder);
|
||||
const threads = useThreadShells();
|
||||
const router = useRouter();
|
||||
const { isMobile, setOpenMobile } = useSidebar();
|
||||
const keybindings = useAtomValue(primaryServerKeybindingsAtom);
|
||||
const confirmThreadDelete = useClientSettings((s) => s.confirmThreadDelete);
|
||||
const confirmThreadArchive = useClientSettings((s) => s.confirmThreadArchive);
|
||||
const sidebarProjectSortOrder = useClientSettings((s) => s.sidebarProjectSortOrder);
|
||||
const timestampFormat = useClientSettings((s) => s.timestampFormat);
|
||||
const workingShelfEnabled = useClientSettings((s) => s.sidebarWorkingShelfEnabled);
|
||||
const projectGroupingSettings = useClientSettings(selectProjectGroupingSettings);
|
||||
@@ -2545,41 +2542,7 @@ export default function Sidebar() {
|
||||
[environments],
|
||||
);
|
||||
const environmentMachineById = useEnvironmentMachines();
|
||||
const orderedProjects = useMemo(
|
||||
() =>
|
||||
orderItemsByPreferredIds({
|
||||
items: projects,
|
||||
preferredIds: projectOrder,
|
||||
getId: getProjectOrderKey,
|
||||
getPreferenceIds: (project) => [
|
||||
getProjectOrderKey(project),
|
||||
legacyProjectCwdPreferenceKey(project.workspaceRoot),
|
||||
],
|
||||
}),
|
||||
[projectOrder, projects],
|
||||
);
|
||||
const unsortedProjectGroups = useMemo(
|
||||
() =>
|
||||
buildSidebarProjectSnapshots({
|
||||
projects: sidebarProjectSortOrder === "manual" ? orderedProjects : projects,
|
||||
groupScratchProjects: true,
|
||||
settings: projectGroupingSettings,
|
||||
primaryEnvironmentId,
|
||||
resolveEnvironmentLabel: (environmentId) => environmentLabelById.get(environmentId) ?? null,
|
||||
}),
|
||||
[
|
||||
environmentLabelById,
|
||||
orderedProjects,
|
||||
primaryEnvironmentId,
|
||||
projectGroupingSettings,
|
||||
projects,
|
||||
sidebarProjectSortOrder,
|
||||
],
|
||||
);
|
||||
const projectGroups = useMemo(
|
||||
() => sortSidebarV2ProjectGroups(unsortedProjectGroups, threads, sidebarProjectSortOrder),
|
||||
[sidebarProjectSortOrder, threads, unsortedProjectGroups],
|
||||
);
|
||||
const projectGroups = useSidebarProjectGroups();
|
||||
const projectGroupsRef = useRef(projectGroups);
|
||||
projectGroupsRef.current = projectGroups;
|
||||
// Threads on non-primary environments (T3 Connect, hosted) resolve their
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
import { useMemo } from "react";
|
||||
|
||||
import { orderItemsByPreferredIds, sortSidebarV2ProjectGroups } from "../components/Sidebar.logic";
|
||||
import { getProjectOrderKey, selectProjectGroupingSettings } from "../logicalProject";
|
||||
import { buildSidebarProjectSnapshots } from "../sidebarProjectGrouping";
|
||||
import { useEnvironmentIdentities, usePrimaryEnvironmentId } from "../state/environments";
|
||||
import { useProjects, useThreadShells } from "../state/entities";
|
||||
import { legacyProjectCwdPreferenceKey, useUiStateStore } from "../uiStateStore";
|
||||
import { useClientSettings } from "./useSettings";
|
||||
|
||||
/** The sidebar's project groups, in the order the sidebar shows them. */
|
||||
export function useSidebarProjectGroups() {
|
||||
const projects = useProjects();
|
||||
const threads = useThreadShells();
|
||||
const projectOrder = useUiStateStore((store) => store.projectOrder);
|
||||
const sortOrder = useClientSettings((s) => s.sidebarProjectSortOrder);
|
||||
const groupingSettings = useClientSettings(selectProjectGroupingSettings);
|
||||
const environments = useEnvironmentIdentities();
|
||||
const primaryEnvironmentId = usePrimaryEnvironmentId();
|
||||
const orderedProjects = useMemo(
|
||||
() =>
|
||||
orderItemsByPreferredIds({
|
||||
items: projects,
|
||||
preferredIds: projectOrder,
|
||||
getId: getProjectOrderKey,
|
||||
getPreferenceIds: (project) => [
|
||||
getProjectOrderKey(project),
|
||||
legacyProjectCwdPreferenceKey(project.workspaceRoot),
|
||||
],
|
||||
}),
|
||||
[projectOrder, projects],
|
||||
);
|
||||
const unsortedGroups = useMemo(() => {
|
||||
const labels = new Map(
|
||||
environments.map((environment) => [environment.environmentId, environment.label] as const),
|
||||
);
|
||||
return buildSidebarProjectSnapshots({
|
||||
projects: sortOrder === "manual" ? orderedProjects : projects,
|
||||
groupScratchProjects: true,
|
||||
settings: groupingSettings,
|
||||
primaryEnvironmentId,
|
||||
resolveEnvironmentLabel: (environmentId) => labels.get(environmentId) ?? null,
|
||||
});
|
||||
}, [environments, groupingSettings, orderedProjects, primaryEnvironmentId, projects, sortOrder]);
|
||||
return useMemo(
|
||||
() => sortSidebarV2ProjectGroups(unsortedGroups, threads, sortOrder),
|
||||
[sortOrder, threads, unsortedGroups],
|
||||
);
|
||||
}
|
||||
@@ -5,6 +5,7 @@ import type { PullRequestSpeedActionResult } from "~/components/pullRequest/Pull
|
||||
import { usePullRequestCloseBatch } from "~/components/pullRequest/usePullRequestActions";
|
||||
import { SidebarPointerSensor } from "~/components/Sidebar.pointer";
|
||||
import { resolveSidebarSweepKeys } from "~/components/Sidebar.logic";
|
||||
import { sortProjectsByGroupOrder } from "@t3tools/client-runtime/state/project-grouping";
|
||||
import { pullRequestHostOf, resolveEnvironmentMachineKind } from "@t3tools/contracts";
|
||||
import type {
|
||||
EnvironmentId,
|
||||
@@ -45,6 +46,7 @@ import {
|
||||
useMemo,
|
||||
useRef,
|
||||
useState,
|
||||
type ComponentProps,
|
||||
type ReactNode,
|
||||
type RefObject,
|
||||
} from "react";
|
||||
@@ -154,6 +156,7 @@ import {
|
||||
} from "../rightPanelStore";
|
||||
import { useDebouncedValue } from "../state/queries";
|
||||
import { useAllEnvironmentShellsBootstrapped, useProjects } from "../state/entities";
|
||||
import { useSidebarProjectGroups } from "../hooks/useSidebarProjectGroups";
|
||||
import { useEnvironments } from "../state/environments";
|
||||
import {
|
||||
pullRequestEnvironment,
|
||||
@@ -2061,7 +2064,7 @@ function PullRequestsRouteView() {
|
||||
/>
|
||||
);
|
||||
const filtersMenu = (
|
||||
<PullRequestFiltersMenu
|
||||
<PullRequestFiltersMenuInSidebarOrder
|
||||
onOpenChange={setFiltersOpen}
|
||||
state={search.state}
|
||||
stateOptions={STATE_TABS}
|
||||
@@ -2389,6 +2392,18 @@ function PullRequestsRouteView() {
|
||||
}
|
||||
|
||||
/** A compact stand-in for one pill group when the header is narrow. */
|
||||
/** Lists the projects in the sidebar's order; it re-renders with thread activity, so the page does not. */
|
||||
function PullRequestFiltersMenuInSidebarOrder(
|
||||
props: ComponentProps<typeof PullRequestFiltersMenu>,
|
||||
) {
|
||||
const sidebarProjectGroups = useSidebarProjectGroups();
|
||||
const projects = useMemo(
|
||||
() => sortProjectsByGroupOrder(props.projects, sidebarProjectGroups),
|
||||
[props.projects, sidebarProjectGroups],
|
||||
);
|
||||
return <PullRequestFiltersMenu {...props} projects={projects} />;
|
||||
}
|
||||
|
||||
function CompactFilterMenu<Value extends string>({
|
||||
label,
|
||||
triggerIcon,
|
||||
|
||||
@@ -8,6 +8,7 @@ import {
|
||||
deriveLogicalProjectKeyFromSettings,
|
||||
derivePhysicalProjectKey,
|
||||
resolveScratchProjectScopeKey,
|
||||
sortProjectsByGroupOrder,
|
||||
type ProjectGroupingSettings,
|
||||
} from "./projectGrouping.ts";
|
||||
|
||||
@@ -445,3 +446,40 @@ describe("buildProjectGroups", () => {
|
||||
expect(groups[0]?.members.map((member) => member.project.id)).toEqual(["winner", "sibling"]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("sortProjectsByGroupOrder", () => {
|
||||
const remote = EnvironmentId.make("remote");
|
||||
const ref = (projectId: string, env = environmentId) => ({
|
||||
environmentId: env,
|
||||
projectId: ProjectId.make(projectId),
|
||||
});
|
||||
const project = (id: string, env: string = environmentId) => ({ id, environmentId: env });
|
||||
|
||||
it("follows the group order, keeping ungrouped projects last in their own order", () => {
|
||||
const sorted = sortProjectsByGroupOrder(
|
||||
[
|
||||
project("alpha"),
|
||||
project("zeta"),
|
||||
project("orphan-b"),
|
||||
project("beta"),
|
||||
project("orphan-a"),
|
||||
],
|
||||
[{ memberProjectRefs: [ref("zeta")] }, { memberProjectRefs: [ref("beta"), ref("alpha")] }],
|
||||
);
|
||||
expect(sorted.map((entry) => entry.id)).toEqual([
|
||||
"zeta",
|
||||
"alpha",
|
||||
"beta",
|
||||
"orphan-b",
|
||||
"orphan-a",
|
||||
]);
|
||||
});
|
||||
|
||||
it("tells the same project id on two environments apart", () => {
|
||||
const sorted = sortProjectsByGroupOrder(
|
||||
[project("shared"), project("shared", remote)],
|
||||
[{ memberProjectRefs: [ref("shared", remote)] }, { memberProjectRefs: [ref("shared")] }],
|
||||
);
|
||||
expect(sorted.map((entry) => entry.environmentId)).toEqual([remote, environmentId]);
|
||||
});
|
||||
});
|
||||
@@ -376,3 +376,25 @@ export function buildProjectGroups<TProject extends EnvironmentProject>(input: {
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Orders projects the way a sorted group list shows them: by the first group holding each
|
||||
* project. Projects in no group follow, and ties keep their incoming order.
|
||||
*/
|
||||
export function sortProjectsByGroupOrder<
|
||||
TProject extends { readonly id: string; readonly environmentId: string },
|
||||
>(
|
||||
projects: ReadonlyArray<TProject>,
|
||||
groups: ReadonlyArray<{ readonly memberProjectRefs: ReadonlyArray<ScopedProjectRef> }>,
|
||||
): TProject[] {
|
||||
const rankByProject = new Map<string, number>();
|
||||
groups.forEach((group, rank) => {
|
||||
for (const ref of group.memberProjectRefs) {
|
||||
const key = `${ref.environmentId}\0${ref.projectId}`;
|
||||
if (!rankByProject.has(key)) rankByProject.set(key, rank);
|
||||
}
|
||||
});
|
||||
const rankOf = (project: TProject) =>
|
||||
rankByProject.get(`${project.environmentId}\0${project.id}`) ?? groups.length;
|
||||
return [...projects].sort((left, right) => rankOf(left) - rankOf(right));
|
||||
}
|
||||
Reference in new issue
Block a user