Skip to content

Commit fbc1aa2

Browse files
committed
fix(web): keep unseen projects' placement when saving a reorder
1 parent c328b94 commit fbc1aa2

2 files changed

Lines changed: 33 additions & 2 deletions

File tree

‎apps/web/src/hooks/useProjectOrder.test.tsx‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,14 @@ it("keeps a second quick drag when the first save broadcasts first", () => {
147147
expect(displayed).toEqual(second);
148148
});
149149

150+
it("keeps projects this client cannot see next to the project they followed", async () => {
151+
environments = [environment("a", ["a:/1", "b:/hidden", "a:/2", "a:/3"])];
152+
act(() => renderer.update(<Probe />));
153+
await act(async () => reorder(["a:/1", "a:/2", "a:/3"], ["a:/3"], ["a:/1"]));
154+
const saved = mocks.persist.mock.calls[0]![0].input.patch.sidebarProjectOrder;
155+
expect(saved).toEqual(["a:/3", "a:/1", "b:/hidden", "a:/2"]);
156+
});
157+
150158
it("keeps a pending reorder through an unrelated settings broadcast", () => {
151159
const original = ["remote:/a", "remote:/b"];
152160
environments = [environment("remote", original)];

‎apps/web/src/hooks/useProjectOrder.ts‎

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,28 @@ export function reorderProjectKeys(
5555
return order;
5656
}
5757

58+
/**
59+
* Keeps keys missing from `next` (projects this client cannot see, such as a
60+
* server still loading) after the key they followed in `previous`, so a
61+
* reorder never drops another environment's placement.
62+
*/
63+
function keepUnseenKeys(next: readonly string[], previous: readonly string[]): string[] {
64+
const seen = new Set(next);
65+
const unseenAfter = new Map<string | null, string[]>();
66+
let anchor: string | null = null;
67+
for (const key of previous) {
68+
if (seen.has(key)) {
69+
anchor = key;
70+
} else {
71+
unseenAfter.set(anchor, [...(unseenAfter.get(anchor) ?? []), key]);
72+
}
73+
}
74+
return [
75+
...(unseenAfter.get(null) ?? []),
76+
...next.flatMap((key) => [key, ...(unseenAfter.get(key) ?? [])]),
77+
];
78+
}
79+
5880
function sameOrder(a: readonly string[] | null, b: readonly string[] | null) {
5981
return a === b || (!!a && !!b && a.length === b.length && a.every((key, i) => key === b[i]));
6082
}
@@ -92,13 +114,14 @@ export function useReorderProjects() {
92114
draggedKeys: readonly string[],
93115
targetKeys: readonly string[],
94116
) => {
95-
const order = reorderProjectKeys(currentOrder, draggedKeys, targetKeys);
96-
if (!order || !environment) return;
117+
const reordered = reorderProjectKeys(currentOrder, draggedKeys, targetKeys);
118+
if (!reordered || !environment) return;
97119
const server = environment.serverConfig?.settings.sidebarProjectOrder ?? null;
98120
const previous = usePendingOrder.getState().pending;
99121
const chained =
100122
previous?.environmentId === environment.environmentId &&
101123
previous.bases.some((base) => sameOrder(base, server));
124+
const order = keepUnseenKeys(reordered, chained ? previous.order : (server ?? []));
102125
const pending = {
103126
environmentId: environment.environmentId,
104127
order,

0 commit comments

Comments
 (0)