Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(web): evict archived projects on revocation

Andrejbee254065d06
diff --git a/apps/web/src/hooks/use-user-websocket-lifecycle.test.tsx b/apps/web/src/hooks/use-user-websocket-lifecycle.test.tsx
index ff3d781e8..c5e372072 100644
--- a/apps/web/src/hooks/use-user-websocket-lifecycle.test.tsx
+++ b/apps/web/src/hooks/use-user-websocket-lifecycle.test.tsx
@@ -54,6 +54,13 @@ vi.mock("@kaneo/libs", () => ({ windowId: "local-test" }));
vi.mock("@/lib/evict-project-cache", () => ({
evictProjectCache: evictProject,
}));
+const cachedProjects = vi.fn(
+ (_client: unknown, _workspaceId: string): { id: string }[] => [],
+);
+vi.mock("@/lib/collect-cached-projects", () => ({
+ collectCachedProjects: (client: unknown, workspaceId: string) =>
+ cachedProjects(client, workspaceId),
+}));
function projectAccessChanged(socket: TestSocket) {
socket.onmessage?.({
data: JSON.stringify({
@@ -252,16 +259,15 @@ describe("user WebSocket lifecycle", () => {
});
it("evicts cached data for projects that left the refreshed list", async () => {
- client.getQueryData.mockReturnValueOnce([
- { id: "kept" },
- { id: "removed" },
- ]);
+ cachedProjects.mockReturnValueOnce([{ id: "kept" }, { id: "removed" }]);
client.fetchQuery.mockResolvedValueOnce([{ id: "kept" }, { id: "added" }]);
renderHook(useUserWebSocket);
await act(async () => projectAccessChanged(TestSocket.instances[0]));
- expect(client.getQueryData).toHaveBeenCalledWith(["projects", "workspace"]);
+ expect(cachedProjects).toHaveBeenCalledWith(client, "workspace");
expect(client.fetchQuery).toHaveBeenCalledWith(
- expect.objectContaining({ queryKey: ["projects", "workspace"] }),
+ expect.objectContaining({
+ queryKey: ["projects", "workspace", "including-archived"],
+ }),
);
expect(evictProject).toHaveBeenCalledOnce();
expect(evictProject).toHaveBeenCalledWith(client, "removed");
@@ -272,7 +278,7 @@ describe("user WebSocket lifecycle", () => {
});
it("keeps project caches when the project list refresh fails", async () => {
- client.getQueryData.mockReturnValueOnce([{ id: "project" }]);
+ cachedProjects.mockReturnValueOnce([{ id: "project" }]);
client.fetchQuery.mockRejectedValueOnce(new Error("Network unavailable"));
renderHook(useUserWebSocket);
await act(async () => projectAccessChanged(TestSocket.instances[0]));
@@ -282,7 +288,7 @@ describe("user WebSocket lifecycle", () => {
it("ignores a project list refresh that finishes after the session ends", async () => {
let finish!: (projects: { id: string }[]) => void;
- client.getQueryData.mockReturnValueOnce([{ id: "project" }]);
+ cachedProjects.mockReturnValueOnce([{ id: "project" }]);
client.fetchQuery.mockReturnValueOnce(
new Promise((resolve) => {
finish = resolve;
diff --git a/apps/web/src/hooks/use-user-websocket.ts b/apps/web/src/hooks/use-user-websocket.ts
index 322df3f95..377e497fc 100644
--- a/apps/web/src/hooks/use-user-websocket.ts
+++ b/apps/web/src/hooks/use-user-websocket.ts
@@ -4,6 +4,7 @@ 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,
@@ -140,13 +141,9 @@ export function useUserWebSocket() {
message.workspaceId
) {
const { workspaceId } = message;
- const projectsKey = ["projects", workspaceId];
- const before =
- queryClient.getQueryData<
- NonNullable<Awaited<ReturnType<typeof getProjects>>>
- >(projectsKey);
+ const before = collectCachedProjects(queryClient, workspaceId);
for (const queryKey of [
- projectsKey,
+ ["projects", workspaceId],
["assigned-tasks", workspaceId],
["search", { workspaceId }],
["workspace-activity", workspaceId],
@@ -155,11 +152,12 @@ export function useUserWebSocket() {
["notifications"],
])
void queryClient.invalidateQueries({ queryKey });
- if (before)
+ if (before.length > 0)
void queryClient
.fetchQuery({
- queryKey: projectsKey,
- queryFn: () => getProjects({ workspaceId }),
+ queryKey: ["projects", workspaceId, "including-archived"],
+ queryFn: () =>
+ getProjects({ workspaceId, includeArchived: "true" }),
})
.then((after) => {
if (disposed) return;
diff --git a/apps/web/src/lib/collect-cached-projects.test.ts b/apps/web/src/lib/collect-cached-projects.test.ts
new file mode 100644
index 000000000..113331419
--- /dev/null
+++ b/apps/web/src/lib/collect-cached-projects.test.ts
@@ -0,0 +1,22 @@
+import { QueryClient } from "@tanstack/react-query";
+import { describe, expect, it } from "vite-plus/test";
+import { collectCachedProjects } from "./collect-cached-projects";
+
+describe("collectCachedProjects", () => {
+ it("gathers projects from every cached list and single project query", () => {
+ const client = new QueryClient();
+ client.setQueryData(["projects", "ws"], [{ id: "active" }]);
+ client.setQueryData(
+ ["projects", "ws", "including-archived"],
+ [{ id: "active" }, { id: "archived" }],
+ );
+ client.setQueryData(["projects", "ws", "visited"], { id: "visited" });
+ client.setQueryData(["projects", "other"], [{ id: "elsewhere" }]);
+
+ expect(
+ collectCachedProjects(client, "ws")
+ .map((project) => project.id)
+ .sort(),
+ ).toEqual(["active", "archived", "visited"]);
+ });
+});
diff --git a/apps/web/src/lib/collect-cached-projects.ts b/apps/web/src/lib/collect-cached-projects.ts
new file mode 100644
index 000000000..5697f1d53
--- /dev/null
+++ b/apps/web/src/lib/collect-cached-projects.ts
@@ -0,0 +1,21 @@
+import type { QueryClient } from "@tanstack/react-query";
+
+export function collectCachedProjects(
+ client: QueryClient,
+ workspaceId: string,
+): { id: string }[] {
+ const ids = new Set<string>();
+ for (const query of client
+ .getQueryCache()
+ .findAll({ queryKey: ["projects", workspaceId] })) {
+ const data = query.state.data;
+ const scope = query.queryKey[2];
+ if (Array.isArray(data)) {
+ for (const project of data)
+ if (typeof project?.id === "string") ids.add(project.id);
+ } else if (typeof scope === "string" && scope !== "including-archived") {
+ ids.add(scope);
+ }
+ }
+ return [...ids].map((id) => ({ id }));
+}