diff --git a/apps/web/src/lib/pullRequestsReactQuery.test.ts b/apps/web/src/lib/pullRequestsReactQuery.test.ts index 2d7259023..e240f0b56 100644 --- a/apps/web/src/lib/pullRequestsReactQuery.test.ts +++ b/apps/web/src/lib/pullRequestsReactQuery.test.ts @@ -10,6 +10,7 @@ vi.mock("../environmentApi", () => ({ import { markPullRequestListSettled, + orderPullRequestEnvironments, PULL_REQUEST_ON_SCREEN_POLL_INTERVAL_MS, pullRequestDetailQueryOptions, pullRequestDiffQueryOptions, @@ -39,6 +40,22 @@ function pace(attended: boolean | undefined, state: PullRequestDetail["state"]) return refetchInterval({ state: { data, status: "success" } }); } +describe("the environments the page asks", () => { + // The page seats a pull request two computers both list with whichever is + // asked first, so an id that sorts first must not pull rows off this device. + it("asks this device first, however the other computers' ids sort", () => { + const here = { environmentId: EnvironmentId.make("zz-this-mac"), label: "MacBook" }; + const pc = { environmentId: EnvironmentId.make("aa-windows"), label: "DESKTOP" }; + const laptop = { environmentId: EnvironmentId.make("mm-laptop"), label: "Laptop" }; + + expect( + orderPullRequestEnvironments({ primary: here, others: [laptop, pc] }).map( + (environment) => environment.label, + ), + ).toEqual(["MacBook", "DESKTOP", "Laptop"]); + }); +}); + describe("pull request detail reads", () => { it("keep an open pull request current only while someone looks at it", () => { expect(pace(true, "open")).toBe(PULL_REQUEST_ON_SCREEN_POLL_INTERVAL_MS); diff --git a/apps/web/src/lib/pullRequestsReactQuery.ts b/apps/web/src/lib/pullRequestsReactQuery.ts index 6b36b55da..1989cddcb 100644 --- a/apps/web/src/lib/pullRequestsReactQuery.ts +++ b/apps/web/src/lib/pullRequestsReactQuery.ts @@ -838,10 +838,28 @@ export async function refreshPullRequestList( }); } +/** + * The order the page asks environments in: this device first, then the other + * computers by id so the order holds still. The page keeps one row per pull + * request and seats it with the first listing to carry it + * ({@link mergePullRequestListResults}), so this order decides which computer + * a pull request both list is shown under and acted on from. + */ +export function orderPullRequestEnvironments(input: { + readonly primary: PullRequestEnvironment | null; + readonly others: readonly PullRequestEnvironment[]; +}): readonly PullRequestEnvironment[] { + const others = input.others.toSorted((left, right) => + left.environmentId.localeCompare(right.environmentId), + ); + return input.primary === null ? others : [input.primary, ...others]; +} + /** * Every environment that can serve the page: this device plus each connected - * saved computer whose server reports the capability. A server too old to know - * about pull requests omits the key, and an absent capability is a no. + * saved computer whose server reports the capability, in + * {@link orderPullRequestEnvironments} order. A server too old to know about + * pull requests omits the key, and an absent capability is a no. */ export function usePullRequestEnvironments(): readonly PullRequestEnvironment[] { const primaryEnvironmentId = usePrimaryEnvironmentId(); @@ -852,20 +870,20 @@ export function usePullRequestEnvironments(): readonly PullRequestEnvironment[] const savedEnvironmentRuntimeById = useSavedEnvironmentRuntimeStore((state) => state.byId); return useMemo(() => { - const environments: PullRequestEnvironment[] = []; + const primary: PullRequestEnvironment | null = + primaryEnvironmentId && primarySupported + ? { + environmentId: primaryEnvironmentId, + label: resolveEnvironmentOptionLabel({ + isPrimary: true, + environmentId: primaryEnvironmentId, + runtimeLabel: primaryLabel, + }), + } + : null; + const others: PullRequestEnvironment[] = []; const seen = new Set(); - - if (primaryEnvironmentId && primarySupported) { - seen.add(primaryEnvironmentId); - environments.push({ - environmentId: primaryEnvironmentId, - label: resolveEnvironmentOptionLabel({ - isPrimary: true, - environmentId: primaryEnvironmentId, - runtimeLabel: primaryLabel, - }), - }); - } + if (primary) seen.add(primary.environmentId); for (const environmentId of Object.keys(savedEnvironmentRuntimeById) as EnvironmentId[]) { if (seen.has(environmentId)) continue; @@ -874,7 +892,7 @@ export function usePullRequestEnvironments(): readonly PullRequestEnvironment[] const descriptor = runtime.descriptor; if (!descriptor || descriptor.capabilities.pullRequests !== true) continue; seen.add(environmentId); - environments.push({ + others.push({ environmentId, label: resolveEnvironmentOptionLabel({ isPrimary: false, @@ -885,9 +903,7 @@ export function usePullRequestEnvironments(): readonly PullRequestEnvironment[] }); } - return environments.toSorted((left, right) => - left.environmentId.localeCompare(right.environmentId), - ); + return orderPullRequestEnvironments({ primary, others }); }, [ primaryEnvironmentId, primaryLabel,