feat(shell): connect CommunitySync for cross-browser tab sync on all pages

Tab list sync via Automerge previously only worked on canvas pages.
Init CommunitySync + OfflineStore in the shell entry point so
community-sync-ready fires on every non-demo shell page, enabling
real-time tab sync across all browsers via the existing inline handler.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
Jeff Emmett 2026-03-24 16:14:12 -07:00
parent 32524fdf00
commit cd6d979b5a
1 changed files with 27 additions and 0 deletions

View File

@ -24,6 +24,8 @@ import { RStackUserDashboard } from "../shared/components/rstack-user-dashboard"
import { rspaceNavUrl } from "../shared/url-helpers"; import { rspaceNavUrl } from "../shared/url-helpers";
import { TabCache } from "../shared/tab-cache"; import { TabCache } from "../shared/tab-cache";
import { RSpaceOfflineRuntime } from "../shared/local-first/runtime"; import { RSpaceOfflineRuntime } from "../shared/local-first/runtime";
import { CommunitySync } from "../lib/community-sync";
import { OfflineStore } from "../lib/offline-store";
// Expose URL helper globally (used by shell inline scripts + components) // Expose URL helper globally (used by shell inline scripts + components)
(window as any).__rspaceNavUrl = rspaceNavUrl; (window as any).__rspaceNavUrl = rspaceNavUrl;
@ -77,6 +79,31 @@ if (spaceSlug && spaceSlug !== "demo") {
window.addEventListener("beforeunload", () => { window.addEventListener("beforeunload", () => {
runtime.flush(); runtime.flush();
}); });
// ── CommunitySync (tab list Automerge sync) ──
const offlineStore = new OfflineStore();
const communitySync = new CommunitySync(spaceSlug, offlineStore);
(window as any).__communitySync = communitySync;
(async () => {
try {
await offlineStore.open();
await communitySync.initFromCache();
} catch (e) {
console.warn("[shell] CommunitySync cache init failed:", e);
}
document.dispatchEvent(new CustomEvent("community-sync-ready", {
detail: { sync: communitySync, communitySlug: spaceSlug },
}));
const proto = location.protocol === "https:" ? "wss:" : "ws:";
const wsUrl = `${proto}//${location.host}/ws/${spaceSlug}`;
communitySync.connect(wsUrl);
})();
window.addEventListener("beforeunload", () => {
communitySync.saveBeforeUnload();
});
} }
// ── Track space visits for dashboard recency sorting ── // ── Track space visits for dashboard recency sorting ──