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: properly construct WS URL

tiran13313d2eccca3ce
diff --git a/apps/web/src/hooks/use-project-websocket.test.ts b/apps/web/src/hooks/use-project-websocket.test.ts
new file mode 100644
index 000000000..3f9c677c8
--- /dev/null
+++ b/apps/web/src/hooks/use-project-websocket.test.ts
@@ -0,0 +1,50 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+vi.mock("@kaneo/libs", () => ({
+ windowId: "test-window-id",
+}));
+
+import { getWsUrl } from "./use-project-websocket";
+
+describe("getWsUrl", () => {
+ beforeEach(() => {
+ vi.stubEnv("VITE_API_URL", "http://localhost:1337");
+ });
+
+ afterEach(() => {
+ vi.unstubAllEnvs();
+ });
+
+ it("builds a ws:// URL from an http API base", () => {
+ expect(getWsUrl("project-123")).toBe(
+ "ws://localhost:1337/api/ws/project-123?windowId=test-window-id",
+ );
+ });
+
+ it("builds a wss:// URL from an https API base", () => {
+ vi.stubEnv("VITE_API_URL", "https://example.com");
+ expect(getWsUrl("project-123")).toBe(
+ "wss://example.com/api/ws/project-123?windowId=test-window-id",
+ );
+ });
+
+ it("does not append /api when the base already ends with /api", () => {
+ vi.stubEnv("VITE_API_URL", "https://example.com/api");
+ expect(getWsUrl("p1")).toBe(
+ "wss://example.com/api/ws/p1?windowId=test-window-id",
+ );
+ });
+
+ it("trims trailing slashes from the API base", () => {
+ vi.stubEnv("VITE_API_URL", "http://localhost:1337///");
+ expect(getWsUrl("p1")).toBe(
+ "ws://localhost:1337/api/ws/p1?windowId=test-window-id",
+ );
+ });
+
+ it("URL-encodes the projectId", () => {
+ expect(getWsUrl("a b/c?d")).toBe(
+ "ws://localhost:1337/api/ws/a%20b%2Fc%3Fd?windowId=test-window-id",
+ );
+ });
+});
diff --git a/apps/web/src/hooks/use-project-websocket.ts b/apps/web/src/hooks/use-project-websocket.ts
index f16a7cf1b..649191663 100644
--- a/apps/web/src/hooks/use-project-websocket.ts
+++ b/apps/web/src/hooks/use-project-websocket.ts
@@ -1,14 +1,13 @@
import { windowId } from "@kaneo/libs";
import { useQueryClient } from "@tanstack/react-query";
import { useEffect, useRef } from "react";
+import { getApiUrl } from "@/fetchers/get-api-url.ts";
import { authClient } from "@/lib/auth-client";
-function getWsUrl(projectId: string) {
- const base = (
- import.meta.env.VITE_API_URL || "http://localhost:1337"
- ).replace(/\/+$/, "");
+export function getWsUrl(projectId: string) {
+ const base = getApiUrl("ws");
const wsBase = base.replace(/^http/, "ws");
- return `${wsBase}/ws/${encodeURIComponent(projectId)}?windowId=${encodeURIComponent(windowId)}`;
+ return `${wsBase}/${encodeURIComponent(projectId)}?windowId=${encodeURIComponent(windowId)}`;
}
const MAX_RETRIES = 5;