Skip to content
Open
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
Original file line number Diff line number Diff line change
@@ -1,12 +1,83 @@
import assert from "node:assert/strict";
import test from "node:test";

import { swapSectionOrder } from "./channelSectionsHelpers.ts";
import {
isChannelSectionsAllowlistReady,
scopeChannelSectionsToKnownChannels,
swapSectionOrder,
} from "./channelSectionsHelpers.ts";

function makeStore(sections, assignments = {}) {
return { version: 1, sections, assignments };
}

test("isChannelSectionsAllowlistReady: explicit ready wins over empty set", () => {
assert.equal(isChannelSectionsAllowlistReady(new Set(), true), true);
assert.equal(isChannelSectionsAllowlistReady(new Set(["a"]), false), false);
assert.equal(isChannelSectionsAllowlistReady(new Set(["a"])), true);
assert.equal(isChannelSectionsAllowlistReady(new Set()), false);
});

test("scopeChannelSectionsToKnownChannels: strips foreign channel ids", () => {
const store = makeStore([{ id: "s1", name: "One", order: 0 }], {
"chan-a": "s1",
"chan-b-foreign": "s1",
});
const filtered = scopeChannelSectionsToKnownChannels(
store,
new Set(["chan-a"]),
);
assert.deepEqual(filtered.assignments, { "chan-a": "s1" });
assert.equal(filtered.sections.length, 1);
assert.equal(filtered.sections[0].id, "s1");
});

test("scopeChannelSectionsToKnownChannels: drops sections that only held foreign channels", () => {
const store = makeStore(
[
{ id: "s-local", name: "Local", order: 0 },
{ id: "s-foreign", name: "From B", order: 1 },
{ id: "s-empty", name: "Empty", order: 2 },
],
{
"chan-a": "s-local",
"chan-b-foreign": "s-foreign",
},
);
const scoped = scopeChannelSectionsToKnownChannels(
store,
new Set(["chan-a"]),
);
assert.deepEqual(scoped.assignments, { "chan-a": "s-local" });
assert.deepEqual(
scoped.sections.map((s) => s.id),
["s-local", "s-empty"],
);
});

test("scopeChannelSectionsToKnownChannels: empty allowlist is a no-op until channelsReady", () => {
const store = makeStore([{ id: "s1", name: "One", order: 0 }], {
"chan-a": "s1",
});
assert.equal(scopeChannelSectionsToKnownChannels(store, new Set()), store);
assert.equal(scopeChannelSectionsToKnownChannels(store, null), store);
assert.equal(scopeChannelSectionsToKnownChannels(store, undefined), store);
assert.deepEqual(
scopeChannelSectionsToKnownChannels(store, new Set(), true).assignments,
{},
);
});

test("scopeChannelSectionsToKnownChannels: returns same reference when nothing stripped", () => {
const store = makeStore([{ id: "s1", name: "One", order: 0 }], {
"chan-a": "s1",
});
assert.equal(
scopeChannelSectionsToKnownChannels(store, new Set(["chan-a", "chan-b"])),
store,
);
});

function makeSection(id, name, order) {
return { id, name, order };
}
Expand Down
70 changes: 70 additions & 0 deletions desktop/src/features/sidebar/lib/channelSectionsHelpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,75 @@
import type { ChannelSectionStore } from "./channelSectionsStorage";

/**
* Whether the active community's channel list has settled enough to scope
* section assignments. An explicit `channelsReady` flag distinguishes
* "loaded, zero channels" from "still loading" (both present as an empty Set).
* Without the flag, a non-empty allowlist is treated as ready for callers that
* only pass known ids (unit tests).
*/
export function isChannelSectionsAllowlistReady(
knownChannelIds: ReadonlySet<string> | null | undefined,
channelsReady?: boolean,
): boolean {
if (channelsReady === true) return true;
if (channelsReady === false) return false;
return Boolean(knownChannelIds && knownChannelIds.size > 0);
}

/**
* Drop assignments whose channel id is not in the active community, and drop
* section folders that only referenced those foreign channels.
*
* Used before local persist / relay publish so a stale in-memory store from a
* previous workspace cannot write foreign channel UUIDs (or the section objects
* that only existed to hold them) into another community's `kind:30078` blob
* (#7207). When the allowlist is not ready, returns `store` unchanged so a
* still-loading channel list cannot wipe every assignment.
*
* Intentionally empty sections (no assignments at all) are kept — they cannot
* be distinguished from user-created empty folders on the active community.
*/
export function scopeChannelSectionsToKnownChannels(
store: ChannelSectionStore,
knownChannelIds: ReadonlySet<string> | null | undefined,
channelsReady?: boolean,
): ChannelSectionStore {
if (!isChannelSectionsAllowlistReady(knownChannelIds, channelsReady)) {
return store;
}
const allow = knownChannelIds ?? new Set<string>();
let assignmentsChanged = false;
const assignments: Record<string, string> = {};
for (const [channelId, sectionId] of Object.entries(store.assignments)) {
if (allow.has(channelId)) {
assignments[channelId] = sectionId;
} else {
assignmentsChanged = true;
}
}

const retainedSectionIds = new Set(Object.values(assignments));
const previouslyAssignedSectionIds = new Set(
Object.values(store.assignments),
);
const sections = store.sections.filter((section) => {
if (retainedSectionIds.has(section.id)) return true;
// Drop sections that only held foreign-channel assignments.
if (previouslyAssignedSectionIds.has(section.id)) return false;
return true;
});
const sectionsChanged = sections.length !== store.sections.length;

if (!assignmentsChanged && !sectionsChanged) {
return store;
}
return {
...store,
sections,
assignments,
};
}

export function swapSectionOrder(
prev: ChannelSectionStore,
sectionId: string,
Expand Down
Loading