Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions apps/web/src/components/chat/ChatComposer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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;
}
Expand Down
82 changes: 82 additions & 0 deletions apps/web/src/components/chat/folderDrop.test.ts
Original file line number Diff line number Diff line change
@@ -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);
});
});
35 changes: 30 additions & 5 deletions apps/web/src/components/chat/folderDrop.ts
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
Loading