TypeScript
fix(web): allow folder references on saved local desktop servers (#27785)
shivame925dd681904
diff --git a/apps/web/src/components/chat/ChatComposer.tsx b/apps/web/src/components/chat/ChatComposer.tsxindex aa2c52443a5..9ad9158cd4d 100644--- a/apps/web/src/components/chat/ChatComposer.tsx+++ b/apps/web/src/components/chat/ChatComposer.tsx@@ -261,7 +261,7 @@ import { useMediaQuery } from "../../hooks/useMediaQuery";import { useSoftwareKeyboardOpen } from "../../hooks/useSoftwareKeyboardOpen";import { useStableCallback } from "../../hooks/useStableCallback";import { usePanelAnimationSettings } from "../../panelAnimations";-import { readEnvironmentScope } from "../../state/session";+import { readEnvironmentScope, readPreparedConnection } from "../../state/session";import type { ReviewCommentContext } from "../../reviewCommentContext";import { useComposerRestingTransition } from "./useComposerRestingTransition";import { ComposerCommandMenuLayer } from "./ComposerCommandMenuLayer";@@ -2522,11 +2522,13 @@ export const ChatComposer = memo(function ChatComposer(props: ChatComposerProps)localEnvironmentDisabled: isLocalEnvironmentDisabled(),environmentId,primaryEnvironmentId,+ connection: readPreparedConnection(environmentId),+ isDesktopRenderer: window.desktopBridge !== undefined,});if (target === "remote") {toastManager.add({type: "error",- title: "Folders can only be referenced in the local desktop environment",+ title: "Folders can only be referenced on a server running on this computer",});return;}diff --git a/apps/web/src/components/chat/folderDrop.test.ts b/apps/web/src/components/chat/folderDrop.test.tsnew file mode 100644index 00000000000..6c42bf42cb6--- /dev/null+++ b/apps/web/src/components/chat/folderDrop.test.ts@@ -0,0 +1,82 @@+import {+ BearerConnectionTarget,+ RelayConnectionTarget,+ SshConnectionTarget,+} from "@t3tools/client-runtime/connection";+import { EnvironmentId } from "@t3tools/contracts";+import { describe, expect, it } from "vite-plus/test";++import { folderDropTarget } from "./folderDrop";++const environmentId = EnvironmentId.make("saved-local");+const base = { environmentId, label: "server" };+const savedServer = (httpBaseUrl: string, connectionId = "saved-1") => ({+ target: new BearerConnectionTarget({ ...base, connectionId }),+ httpBaseUrl,+});+const thinClient = {+ localEnvironmentDisabled: true,+ environmentId,+ primaryEnvironmentId: null,+ isDesktopRenderer: true,+};++describe("folderDropTarget", () => {+ it.each([+ [+ "the bundled primary",+ {+ ...thinClient,+ localEnvironmentDisabled: false,+ primaryEnvironmentId: environmentId,+ connection: null,+ },+ "local",+ ],+ [+ "a saved loopback server on desktop",+ { ...thinClient, connection: savedServer("http://127.0.0.1:3773") },+ "local",+ ],+ [+ "a saved LAN server",+ { ...thinClient, connection: savedServer("http://192.168.1.20:3773") },+ "remote",+ ],+ [+ "a saved loopback server in a browser",+ { ...thinClient, isDesktopRenderer: false, connection: savedServer("http://localhost:3773") },+ "remote",+ ],+ [+ "a desktop WSL backend",+ { ...thinClient, connection: savedServer("http://127.0.0.1:3774", "local:wsl-ubuntu") },+ "remote",+ ],+ [+ "an SSH forward on loopback",+ {+ ...thinClient,+ connection: {+ target: new SshConnectionTarget({ ...base, connectionId: "ssh-1" }),+ httpBaseUrl: "http://127.0.0.1:41000",+ },+ },+ "remote",+ ],+ [+ "a relay connection",+ {+ ...thinClient,+ connection: {+ target: new RelayConnectionTarget(base),+ httpBaseUrl: "https://relay.t3.codes",+ },+ },+ "remote",+ ],+ ["an unprepared connection", { ...thinClient, connection: null }, "remote"],+ ] as const)("targets %s", (_name, input, expected) => {+ expect(folderDropTarget(input)).toBe(expected);+ });+});diff --git a/apps/web/src/components/chat/folderDrop.ts b/apps/web/src/components/chat/folderDrop.tsindex 8f462394e8f..df4089589aa 100644--- a/apps/web/src/components/chat/folderDrop.ts+++ b/apps/web/src/components/chat/folderDrop.ts@@ -1,18 +1,43 @@+import type { PreparedConnection } from "@t3tools/client-runtime/connection";import type { EnvironmentId } from "@t3tools/contracts";+import { isDesktopLocalConnectionTarget } from "~/connection/desktopLocal";+import { isLoopbackHostname } from "~/environments/primary/target";+export function folderDropTarget(input: {localEnvironmentDisabled: boolean;environmentId: EnvironmentId;primaryEnvironmentId: EnvironmentId | null;+ connection: Pick<PreparedConnection, "target" | "httpBaseUrl"> | null;+ isDesktopRenderer: boolean;}): "local" | "remote" {if (- input.localEnvironmentDisabled ||- input.primaryEnvironmentId === null ||- input.environmentId !== input.primaryEnvironmentId+ !input.localEnvironmentDisabled &&+ input.primaryEnvironmentId !== null &&+ input.environmentId === input.primaryEnvironmentId) {- return "remote";+ return "local";+ }+ return input.isDesktopRenderer &&+ input.connection !== null &&+ isSavedLoopbackConnection(input.connection)+ ? "local"+ : "remote";+}++// A direct saved connection to loopback shares the desktop's filesystem; WSL backends and SSH forwards do not.+function isSavedLoopbackConnection({+ target,+ httpBaseUrl,+}: Pick<PreparedConnection, "target" | "httpBaseUrl">): boolean {+ if (target._tag !== "BearerConnectionTarget" || isDesktopLocalConnectionTarget(target)) {+ return false;+ }+ try {+ return isLoopbackHostname(new URL(httpBaseUrl).hostname);+ } catch {+ return false;}- return "local";}export function resolveDroppedFolderPath(