⚡ 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
feat(web): add invitation link and clipboard helpers
David Escobar8b7d01eb730e
diff --git a/apps/web/src/lib/copy-to-clipboard.test.ts b/apps/web/src/lib/copy-to-clipboard.test.tsnew file mode 100644index 000000000..9625d05f7--- /dev/null+++ b/apps/web/src/lib/copy-to-clipboard.test.ts@@ -0,0 +1,137 @@+import { afterEach, describe, expect, it, vi } from "vitest";+import { copyToClipboard } from "./copy-to-clipboard";++const originalClipboard = navigator.clipboard;+const originalExecCommand = document.execCommand;++afterEach(() => {+ Object.defineProperty(navigator, "clipboard", {+ value: originalClipboard,+ configurable: true,+ writable: true,+ });+ Object.defineProperty(document, "execCommand", {+ value: originalExecCommand,+ configurable: true,+ writable: true,+ });+ vi.restoreAllMocks();+});++function setClipboard(value: unknown) {+ Object.defineProperty(navigator, "clipboard", {+ value,+ configurable: true,+ writable: true,+ });+}++describe("copyToClipboard", () => {+ it("uses the async clipboard API when it is available", async () => {+ const writeText = vi.fn().mockResolvedValue(undefined);+ setClipboard({ writeText });++ await expect(copyToClipboard("hello")).resolves.toBe(true);+ expect(writeText).toHaveBeenCalledWith("hello");+ });++ it("falls back to execCommand when the clipboard API is missing", async () => {+ // Plain-HTTP self-hosted instances land here: not a secure context.+ setClipboard(undefined);+ const textareaState: {+ value?: string;+ selectionStart?: number;+ selectionEnd?: number;+ } = {};+ const execCommand = vi.fn(function (this: Document, _cmd: string) {+ // Capture textarea state at the moment execCommand is called (before removal)+ const textarea = document.querySelector("textarea");+ if (textarea) {+ textareaState.value = textarea.value;+ textareaState.selectionStart = textarea.selectionStart;+ textareaState.selectionEnd = textarea.selectionEnd;+ }+ return true;+ });+ Object.defineProperty(document, "execCommand", {+ value: execCommand,+ configurable: true,+ writable: true,+ });++ await expect(copyToClipboard("hello")).resolves.toBe(true);+ expect(execCommand).toHaveBeenCalledWith("copy");+ // Verify the textarea had the correct text and was selected at copy time+ expect(textareaState.value).toBe("hello");+ expect(textareaState.selectionStart).toBe(0);+ expect(textareaState.selectionEnd).toBe("hello".length);+ });++ it("falls back to execCommand when the clipboard API rejects", async () => {+ setClipboard({ writeText: vi.fn().mockRejectedValue(new Error("denied")) });+ const textareaState: {+ value?: string;+ selectionStart?: number;+ selectionEnd?: number;+ } = {};+ const execCommand = vi.fn(function (this: Document, _cmd: string) {+ // Capture textarea state at the moment execCommand is called+ const textarea = document.querySelector("textarea");+ if (textarea) {+ textareaState.value = textarea.value;+ textareaState.selectionStart = textarea.selectionStart;+ textareaState.selectionEnd = textarea.selectionEnd;+ }+ return true;+ });+ Object.defineProperty(document, "execCommand", {+ value: execCommand,+ configurable: true,+ writable: true,+ });++ await expect(copyToClipboard("hello")).resolves.toBe(true);+ // Verify the textarea had the correct text and was selected at copy time+ expect(textareaState.value).toBe("hello");+ expect(textareaState.selectionStart).toBe(0);+ expect(textareaState.selectionEnd).toBe("hello".length);+ });++ it("reports failure when neither path works", async () => {+ setClipboard(undefined);+ Object.defineProperty(document, "execCommand", {+ value: vi.fn().mockReturnValue(false),+ configurable: true,+ writable: true,+ });++ await expect(copyToClipboard("hello")).resolves.toBe(false);+ });++ it("leaves no scratch node behind", async () => {+ setClipboard(undefined);+ Object.defineProperty(document, "execCommand", {+ value: vi.fn().mockReturnValue(true),+ configurable: true,+ writable: true,+ });++ await copyToClipboard("hello");+ expect(document.querySelectorAll("textarea")).toHaveLength(0);+ });++ it("cleans up the scratch node when execCommand throws", async () => {+ setClipboard(undefined);+ Object.defineProperty(document, "execCommand", {+ value: vi.fn(() => {+ throw new Error("copy operation failed");+ }),+ configurable: true,+ writable: true,+ });++ await expect(copyToClipboard("hello")).resolves.toBe(false);+ // Verify the scratch node was removed despite the exception+ expect(document.querySelectorAll("textarea")).toHaveLength(0);+ });+});diff --git a/apps/web/src/lib/copy-to-clipboard.ts b/apps/web/src/lib/copy-to-clipboard.tsnew file mode 100644index 000000000..eb48c197a--- /dev/null+++ b/apps/web/src/lib/copy-to-clipboard.ts@@ -0,0 +1,39 @@+/**+ * Copies text to the clipboard, degrading gracefully outside secure contexts.+ *+ * navigator.clipboard is only defined over HTTPS or on localhost. A self-hosted+ * instance reached over plain HTTP — a normal deployment for this project — has+ * no such API, so the deprecated execCommand path is the only one that works+ * there and is kept deliberately.+ *+ * Returns whether the text made it to the clipboard by either route.+ */+export async function copyToClipboard(text: string): Promise<boolean> {+ if (navigator.clipboard?.writeText) {+ try {+ await navigator.clipboard.writeText(text);+ return true;+ } catch {+ // Permission denied or a transient failure — fall through to the legacy path.+ }+ }++ const scratch = document.createElement("textarea");+ scratch.value = text;+ scratch.setAttribute("readonly", "");+ // Kept off-screen rather than hidden: execCommand ignores nodes that are not+ // rendered, so display:none or visibility:hidden would break the copy.+ scratch.style.position = "fixed";+ scratch.style.top = "-9999px";+ scratch.style.opacity = "0";+ document.body.appendChild(scratch);++ try {+ scratch.select();+ return document.execCommand("copy");+ } catch {+ return false;+ } finally {+ scratch.remove();+ }+}diff --git a/apps/web/src/lib/invitation-link.test.ts b/apps/web/src/lib/invitation-link.test.tsnew file mode 100644index 000000000..3a5c8e785--- /dev/null+++ b/apps/web/src/lib/invitation-link.test.ts@@ -0,0 +1,29 @@+import { describe, expect, it } from "vitest";+import { buildInvitationLink } from "./invitation-link";++describe("buildInvitationLink", () => {+ it("builds the accept route from the given origin", () => {+ expect(buildInvitationLink("inv-1", "https://kaneo.example.com")).toBe(+ "https://kaneo.example.com/invitation/accept/inv-1",+ );+ });++ it("keeps a non-standard port", () => {+ expect(buildInvitationLink("inv-2", "http://192.168.1.10:5173")).toBe(+ "http://192.168.1.10:5173/invitation/accept/inv-2",+ );+ });++ it("does not double the separator when the origin has a trailing slash", () => {+ expect(buildInvitationLink("inv-3", "https://kaneo.example.com/")).toBe(+ "https://kaneo.example.com/invitation/accept/inv-3",+ );+ });++ it("falls back to the browser origin when none is given", () => {+ // jsdom serves the suite from http://localhost:3000+ expect(buildInvitationLink("inv-4")).toBe(+ `${window.location.origin}/invitation/accept/inv-4`,+ );+ });+});diff --git a/apps/web/src/lib/invitation-link.ts b/apps/web/src/lib/invitation-link.tsnew file mode 100644index 000000000..b2033a87b--- /dev/null+++ b/apps/web/src/lib/invitation-link.ts@@ -0,0 +1,16 @@+/**+ * Builds the public URL an invitee opens to accept a workspace invitation.+ *+ * The origin defaults to the browser's own origin rather than KANEO_CLIENT_URL:+ * whoever copies the link is already reaching the app through a URL that works,+ * while KANEO_CLIENT_URL is a server-side setting that can be stale or wrong on+ * a self-hosted instance — producing a link the admin cannot notice is broken.+ *+ * The `origin` parameter exists so this stays testable without a DOM.+ */+export function buildInvitationLink(+ invitationId: string,+ origin: string = window.location.origin,+): string {+ return `${origin.replace(/\/+$/, "")}/invitation/accept/${invitationId}`;+}