⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
fix(web): reconcile project access on resync
Andrejd1d3fe7d596c
diff --git a/apps/web/src/hooks/use-user-websocket-lifecycle.test.tsx b/apps/web/src/hooks/use-user-websocket-lifecycle.test.tsxindex c5e372072..294bc50a8 100644--- a/apps/web/src/hooks/use-user-websocket-lifecycle.test.tsx+++ b/apps/web/src/hooks/use-user-websocket-lifecycle.test.tsx@@ -301,6 +301,23 @@ describe("user WebSocket lifecycle", () => {expect(evictProject).not.toHaveBeenCalled();});+ it("reconciles cached projects when a reconnect snapshot arrives", async () => {+ cachedProjects.mockReturnValueOnce([{ id: "kept" }, { id: "revoked" }]);+ client.fetchQuery.mockResolvedValueOnce([{ id: "kept" }]);+ renderHook(useUserWebSocket);+ await act(async () =>+ TestSocket.instances[0].onmessage?.({+ data: JSON.stringify({+ type: "WORKSPACE_ACCESS_SYNC",+ workspaceIds: ["workspace"],+ }),+ }),+ );+ expect(cachedProjects).toHaveBeenCalledWith(client, "workspace");+ expect(evictProject).toHaveBeenCalledWith(client, "revoked");+ expect(evictProject).toHaveBeenCalledOnce();+ });+it("redirects after a reconnect snapshot reveals a missed workspace revocation", () => {renderHook(useUserWebSocket);act(() =>diff --git a/apps/web/src/hooks/use-user-websocket.ts b/apps/web/src/hooks/use-user-websocket.tsindex 377e497fc..18a27bdae 100644--- a/apps/web/src/hooks/use-user-websocket.ts+++ b/apps/web/src/hooks/use-user-websocket.ts@@ -3,15 +3,12 @@ import { useQueryClient } from "@tanstack/react-query";import { useLocation, useNavigate } from "@tanstack/react-router";import { useEffect, useRef } from "react";import { getApiUrl } from "@/fetchers/get-api-url";-import getProjects from "@/fetchers/project/get-projects";-import { collectCachedProjects } from "@/lib/collect-cached-projects";-import { evictProjectCache } from "@/lib/evict-project-cache";import {evictInaccessibleWorkspaceCache,evictWorkspaceCache,} from "@/lib/evict-workspace-cache";import { authClient } from "@/lib/auth-client";-import { findRemovedProjectIds } from "@/lib/find-removed-project-ids";+import { reconcileProjectAccess } from "@/lib/reconcile-project-access";export function getUserWsUrl() {const base = getApiUrl("ws");@@ -113,6 +110,8 @@ export function useUserWebSocket() {message.workspaceIds.every((id) => typeof id === "string")) {evictInaccessibleWorkspaceCache(queryClient, message.workspaceIds);+ for (const workspaceId of message.workspaceIds)+ reconcileProjectAccess(queryClient, workspaceId, () => !disposed);refreshOrganizationState();const path = pathnameRef.current;const current =@@ -141,7 +140,6 @@ export function useUserWebSocket() {message.workspaceId) {const { workspaceId } = message;- const before = collectCachedProjects(queryClient, workspaceId);for (const queryKey of [["projects", workspaceId],["assigned-tasks", workspaceId],@@ -152,19 +150,7 @@ export function useUserWebSocket() {["notifications"],])void queryClient.invalidateQueries({ queryKey });- if (before.length > 0)- void queryClient- .fetchQuery({- queryKey: ["projects", workspaceId, "including-archived"],- queryFn: () =>- getProjects({ workspaceId, includeArchived: "true" }),- })- .then((after) => {- if (disposed) return;- for (const projectId of findRemovedProjectIds(before, after))- evictProjectCache(queryClient, projectId);- })- .catch(() => {});+ reconcileProjectAccess(queryClient, workspaceId, () => !disposed);}if (message.type === "NOTIFICATION_CREATED") {queryClient.invalidateQueries({ queryKey: ["notifications"] });diff --git a/apps/web/src/lib/reconcile-project-access.ts b/apps/web/src/lib/reconcile-project-access.tsnew file mode 100644index 000000000..c9f2f9893--- /dev/null+++ b/apps/web/src/lib/reconcile-project-access.ts@@ -0,0 +1,29 @@+import type { QueryClient } from "@tanstack/react-query";+import getProjects from "@/fetchers/project/get-projects";+import { collectCachedProjects } from "./collect-cached-projects";+import { evictProjectCache } from "./evict-project-cache";+import { findRemovedProjectIds } from "./find-removed-project-ids";++export function reconcileProjectAccess(+ client: QueryClient,+ workspaceId: string,+ isActive: () => boolean,+) {+ const before = collectCachedProjects(client, workspaceId);+ if (before.length === 0) return;+ void client+ .fetchQuery({+ queryKey: ["projects", workspaceId, "including-archived"],+ queryFn: () => getProjects({ workspaceId, includeArchived: "true" }),+ })+ .then((after) => {+ if (!isActive()) return;+ const removed = findRemovedProjectIds(before, after);+ for (const projectId of removed) evictProjectCache(client, projectId);+ if (removed.length > 0) {+ void client.invalidateQueries({ queryKey: ["projects", workspaceId] });+ void client.invalidateQueries({ queryKey: ["notifications"] });+ }+ })+ .catch(() => {});+}