@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 04:45 UTC

main
ts 51 lines 1.92 KB
Raw
1 import type { PreparedConnection } from "@t3tools/client-runtime/connection";
2 import { PRIMARY_LOCAL_ENVIRONMENT_ID, type EnvironmentId } from "@t3tools/contracts";
3
4 import { desktopLocalBackendId, isDesktopLocalConnectionTarget } from "~/connection/desktopLocal";
5 import { isLoopbackHostname } from "~/environments/primary/target";
6
7 export type FolderDropTarget = "local" | "remote" | { readonly wslBackendId: string };
8
9 export function folderDropTarget(input: {
10 localEnvironmentDisabled: boolean;
11 environmentId: EnvironmentId;
12 primaryEnvironmentId: EnvironmentId | null;
13 connection: Pick<PreparedConnection, "target" | "httpBaseUrl"> | null;
14 isDesktopRenderer: boolean;
15 primaryRunsInWsl: boolean;
16 }): FolderDropTarget {
17 if (
18 !input.localEnvironmentDisabled &&
19 input.primaryEnvironmentId !== null &&
20 input.environmentId === input.primaryEnvironmentId
21 ) {
22 return input.primaryRunsInWsl ? { wslBackendId: PRIMARY_LOCAL_ENVIRONMENT_ID } : "local";
23 }
24 if (!input.isDesktopRenderer || input.connection === null) return "remote";
25 const backendId = desktopLocalBackendId(input.connection.target);
26 if (backendId?.startsWith("wsl:")) return { wslBackendId: backendId };
27 return isSavedLoopbackConnection(input.connection) ? "local" : "remote";
28 }
29
30 // A direct saved connection to loopback shares the desktop's filesystem; SSH forwards do not.
31 function isSavedLoopbackConnection({
32 target,
33 httpBaseUrl,
34 }: Pick<PreparedConnection, "target" | "httpBaseUrl">): boolean {
35 if (target._tag !== "BearerConnectionTarget" || isDesktopLocalConnectionTarget(target)) {
36 return false;
37 }
38 try {
39 return isLoopbackHostname(new URL(httpBaseUrl).hostname);
40 } catch {
41 return false;
42 }
43 }
44
45 export function resolveDroppedFolderPath(
46 folder: File,
47 getPathForFile: ((file: File) => string) | undefined,
48 ): string | null {
49 const path = getPathForFile?.(folder);
50 return typeof path === "string" && path.length > 0 ? path : null;
51 }