Skip to content

Commit 1e68c6c

Browse files
wpfleger96npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7
andauthored
feat(desktop): add drag-to-reorder for community rail (#2549)
Signed-off-by: Will Pfleger <[email protected]> Co-authored-by: npub1mn7jgtj4w2pd0g0zeuhxsa6jy6p0rewxz4kujt98my82ahfmp72sxjexk7 <dcfd242e557282d7a1e2cf2e6877522682f1e5c6156dc92ca7d90eaedd3b0f95@buzz.block.builderlab.xyz>
1 parent d0ab3fd commit 1e68c6c

6 files changed

Lines changed: 493 additions & 38 deletions

File tree

‎desktop/playwright.config.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ export default defineConfig({
7171
"**/home-collapsed-top-chrome.spec.ts",
7272
"**/top-chrome-zoom-clearance.spec.ts",
7373
"**/thread-unread.spec.ts",
74-
"**/workspace-rail.spec.ts",
74+
"**/community-rail.spec.ts",
7575
"**/boot-splash.spec.ts",
7676
"**/thread-reply-anchor-roleplay.spec.ts",
7777
"**/threadpane-ultrawide.spec.ts",

‎desktop/src/app/AppShell.tsx‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,6 +714,7 @@ export function AppShell() {
714714
}
715715
onAddCommunity={addCommunityDialog.openDialog}
716716
onRemoveCommunity={communitiesHook.removeCommunity}
717+
onReorderCommunities={communitiesHook.reorderCommunities}
717718
onSwitchCommunity={handleSwitchCommunity}
718719
onUpdateCommunity={communitiesHook.updateCommunity}
719720
communities={communitiesHook.communities}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
/**
2+
* Unit tests for applyCommunitiesOrder — the pure permutation helper that
3+
* drives community-rail drag-to-reorder.
4+
*/
5+
import assert from "node:assert/strict";
6+
import { describe, it } from "node:test";
7+
8+
import { applyCommunitiesOrder } from "./useCommunities.tsx";
9+
10+
const A = {
11+
id: "ws-a",
12+
name: "Alpha",
13+
relayUrl: "wss://a.example.com",
14+
addedAt: "2024-01-01",
15+
};
16+
const B = {
17+
id: "ws-b",
18+
name: "Bravo",
19+
relayUrl: "wss://b.example.com",
20+
addedAt: "2024-01-02",
21+
};
22+
const C = {
23+
id: "ws-c",
24+
name: "Charlie",
25+
relayUrl: "wss://c.example.com",
26+
addedAt: "2024-01-03",
27+
};
28+
29+
describe("applyCommunitiesOrder", () => {
30+
it("reorders communities to match orderedIds", () => {
31+
const result = applyCommunitiesOrder([A, B, C], ["ws-c", "ws-a", "ws-b"]);
32+
assert.deepEqual(
33+
result.map((c) => c.id),
34+
["ws-c", "ws-a", "ws-b"],
35+
);
36+
});
37+
38+
it("returns same order when orderedIds matches current order", () => {
39+
const result = applyCommunitiesOrder([A, B, C], ["ws-a", "ws-b", "ws-c"]);
40+
assert.deepEqual(
41+
result.map((c) => c.id),
42+
["ws-a", "ws-b", "ws-c"],
43+
);
44+
});
45+
46+
it("appends communities not mentioned in orderedIds at the end in original relative order", () => {
47+
// C was added after drag — not in orderedIds — should tail-append
48+
const result = applyCommunitiesOrder([A, B, C], ["ws-b", "ws-a"]);
49+
assert.deepEqual(
50+
result.map((c) => c.id),
51+
["ws-b", "ws-a", "ws-c"],
52+
);
53+
});
54+
55+
it("handles orderedIds that contain stale IDs not present in communities", () => {
56+
// "ws-gone" is a stale id — silent skip, no crash
57+
const result = applyCommunitiesOrder([A, B], ["ws-b", "ws-gone", "ws-a"]);
58+
assert.deepEqual(
59+
result.map((c) => c.id),
60+
["ws-b", "ws-a"],
61+
);
62+
});
63+
64+
it("returns the full list when orderedIds is empty — original order preserved", () => {
65+
const result = applyCommunitiesOrder([A, B, C], []);
66+
assert.deepEqual(
67+
result.map((c) => c.id),
68+
["ws-a", "ws-b", "ws-c"],
69+
);
70+
});
71+
72+
it("handles a single-element list (no-op reorder)", () => {
73+
const result = applyCommunitiesOrder([A], ["ws-a"]);
74+
assert.deepEqual(
75+
result.map((c) => c.id),
76+
["ws-a"],
77+
);
78+
});
79+
80+
it("handles an empty communities list", () => {
81+
const result = applyCommunitiesOrder([], ["ws-a", "ws-b"]);
82+
assert.deepEqual(result, []);
83+
});
84+
85+
it("does not mutate the original array", () => {
86+
const original = [A, B, C];
87+
applyCommunitiesOrder(original, ["ws-c", "ws-a", "ws-b"]);
88+
assert.deepEqual(
89+
original.map((c) => c.id),
90+
["ws-a", "ws-b", "ws-c"],
91+
);
92+
});
93+
94+
it("preserves object identity of each community (no clone)", () => {
95+
const result = applyCommunitiesOrder([A, B, C], ["ws-c", "ws-b", "ws-a"]);
96+
assert.equal(result[0], C);
97+
assert.equal(result[1], B);
98+
assert.equal(result[2], A);
99+
});
100+
101+
it("handles duplicate IDs in orderedIds — first occurrence wins", () => {
102+
// Defensive: dnd-kit should never produce duplicates, but guard anyway.
103+
const result = applyCommunitiesOrder([A, B, C], ["ws-b", "ws-b", "ws-a"]);
104+
// ws-b appears once, ws-a once, ws-c appended
105+
assert.deepEqual(
106+
result.map((c) => c.id),
107+
["ws-b", "ws-a", "ws-c"],
108+
);
109+
});
110+
});

‎desktop/src/features/communities/useCommunities.tsx‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,42 @@ export function resolveCommunityUpdateResult(
7272
return { kind: "updated", requiresReinit: backendFieldsChanged };
7373
}
7474

75+
/**
76+
* Permute `communities` so that its order matches `orderedIds`.
77+
*
78+
* - Communities whose id appears in `orderedIds` are placed first, in the
79+
* order given by `orderedIds`.
80+
* - Communities not mentioned in `orderedIds` (e.g. added after the drag
81+
* completed) are appended at the end in their original relative order.
82+
*
83+
* Pure and side-effect-free — extracted so it can be unit-tested without
84+
* a DOM or React.
85+
*/
86+
export function applyCommunitiesOrder(
87+
communities: Community[],
88+
orderedIds: string[],
89+
): Community[] {
90+
const byId = new Map(communities.map((c) => [c.id, c]));
91+
const seen = new Set<string>();
92+
const reordered: Community[] = [];
93+
94+
for (const id of orderedIds) {
95+
const c = byId.get(id);
96+
if (c && !seen.has(id)) {
97+
reordered.push(c);
98+
seen.add(id);
99+
}
100+
}
101+
102+
for (const c of communities) {
103+
if (!seen.has(c.id)) {
104+
reordered.push(c);
105+
}
106+
}
107+
108+
return reordered;
109+
}
110+
75111
export type UseCommunitiesReturn = {
76112
communities: Community[];
77113
activeCommunity: Community | null;
@@ -90,6 +126,8 @@ export type UseCommunitiesReturn = {
90126
Pick<Community, "name" | "relayUrl" | "token" | "pubkey" | "reposDir">
91127
>,
92128
) => UpdateCommunityResult;
129+
/** Persist a new display order for the rail. IDs not in orderedIds keep their relative position at the end. */
130+
reorderCommunities: (orderedIds: string[]) => void;
93131
};
94132

95133
const CommunitiesContext = createContext<UseCommunitiesReturn | null>(null);
@@ -242,6 +280,14 @@ function useCommunitiesInternal(): UseCommunitiesReturn {
242280
[activeId],
243281
);
244282

283+
const reorderCommunities = useCallback((orderedIds: string[]) => {
284+
setCommunitiesState((prev) => {
285+
const next = applyCommunitiesOrder(prev, orderedIds);
286+
saveCommunities(next);
287+
return next;
288+
});
289+
}, []);
290+
245291
return {
246292
communities,
247293
activeCommunity,
@@ -252,5 +298,6 @@ function useCommunitiesInternal(): UseCommunitiesReturn {
252298
switchCommunity,
253299
reconnectCommunity,
254300
updateCommunity,
301+
reorderCommunities,
255302
};
256303
}

0 commit comments

Comments
 (0)