Skip to content
Source
Sign inSign up
Open History to see the last commit

folderDrop.ts

49 lines1.6 KB

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