Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions apps/web/src/lib/pullRequestsReactQuery.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ vi.mock("../environmentApi", () => ({

import {
markPullRequestListSettled,
orderPullRequestEnvironments,
PULL_REQUEST_ON_SCREEN_POLL_INTERVAL_MS,
pullRequestDetailQueryOptions,
pullRequestDiffQueryOptions,
Expand Down Expand Up @@ -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);
Expand Down
54 changes: 35 additions & 19 deletions apps/web/src/lib/pullRequestsReactQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<EnvironmentId>();

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;
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Loading