diff --git a/apps/server/src/process/utils.ts b/apps/server/src/process/utils.ts index d59b6b3eaa..f0a7c24987 100644 --- a/apps/server/src/process/utils.ts +++ b/apps/server/src/process/utils.ts @@ -1,11 +1,6 @@ -export const MAX_PORT_NUMBER = 65_535; +import { MAX_PORT_NUMBER, normalizeRunningPorts } from "@t3tools/shared/port"; -export function normalizeRunningPorts(ports: number[]): number[] { - if (ports.length === 0) return []; - return [...new Set(ports)] - .filter((port) => Number.isInteger(port) && port > 0 && port <= MAX_PORT_NUMBER) - .toSorted((left, right) => left - right); -} +export { MAX_PORT_NUMBER, normalizeRunningPorts }; export function parsePidList(stdout: string): number[] { const pids: number[] = []; diff --git a/apps/server/src/terminal/Layers/Manager.ts b/apps/server/src/terminal/Layers/Manager.ts index 1fa751bdfb..3c6a51e96f 100644 --- a/apps/server/src/terminal/Layers/Manager.ts +++ b/apps/server/src/terminal/Layers/Manager.ts @@ -613,12 +613,17 @@ export const makeTerminalManagerWithOptions = Effect.fn("makeTerminalManagerWith return cached.isWeb; } - const isWeb = yield* webPortInspector(port).pipe(Effect.catch(() => Effect.succeed(false))); - webPortProbeCache.set(port, { - isWeb, - checkedAt: Date.now(), - }); - return isWeb; + const isWeb = yield* webPortInspector(port).pipe( + Effect.map((result) => ({ result, probeSucceeded: true }) as const), + Effect.catch(() => Effect.succeed({ result: false, probeSucceeded: false } as const)), + ); + if (isWeb.probeSucceeded) { + webPortProbeCache.set(port, { + isWeb: isWeb.result, + checkedAt: Date.now(), + }); + } + return isWeb.result; }); const detectWebPorts = (runningPorts: number[]) => diff --git a/apps/web/src/components/Sidebar.tsx b/apps/web/src/components/Sidebar.tsx index 280d87bedb..74b99a8387 100644 --- a/apps/web/src/components/Sidebar.tsx +++ b/apps/web/src/components/Sidebar.tsx @@ -61,11 +61,8 @@ import { APP_STAGE_LABEL, APP_VERSION } from "../branding"; import { isTerminalFocused } from "../lib/terminalFocus"; import { isLinuxPlatform, isMacPlatform, newCommandId, newProjectId } from "../lib/utils"; import { useStore } from "../store"; -import { - normalizeRunningPorts, - selectThreadTerminalState, - useTerminalStateStore, -} from "../terminalStateStore"; +import { normalizeRunningPorts, portStatusLabel } from "@t3tools/shared/port"; +import { selectThreadTerminalState, useTerminalStateStore } from "../terminalStateStore"; import { useUiStateStore } from "../uiStateStore"; import { resolveShortcutCommand, @@ -219,15 +216,10 @@ function terminalStatusFromTerminalState( const runningPorts = normalizeRunningPorts( runningTerminalIds.flatMap((terminalId) => runningTerminalPorts[terminalId] ?? []), ); - const primaryWebPort = runningPorts[0] ?? null; + const { label, primaryWebPort } = portStatusLabel(runningPorts); return { - label: - primaryWebPort === null - ? "Terminal process running" - : runningPorts.length === 1 - ? `Open web server: http://localhost:${primaryWebPort}` - : `Open web server: http://localhost:${primaryWebPort} (detected web ports: ${runningPorts.join(", ")})`, + label, colorClass: primaryWebPort === null ? "text-teal-600 dark:text-teal-300/90" diff --git a/apps/web/src/components/ThreadTerminalDrawer.tsx b/apps/web/src/components/ThreadTerminalDrawer.tsx index a0df8a725f..553bc73f97 100644 --- a/apps/web/src/components/ThreadTerminalDrawer.tsx +++ b/apps/web/src/components/ThreadTerminalDrawer.tsx @@ -33,11 +33,8 @@ import { type ThreadTerminalGroup, } from "../types"; import { readNativeApi } from "~/nativeApi"; -import { - normalizeRunningPorts, - selectTerminalEventEntries, - useTerminalStateStore, -} from "../terminalStateStore"; +import { normalizeRunningPorts, portStatusLabel, type PortStatusLabel } from "@t3tools/shared/port"; +import { selectTerminalEventEntries, useTerminalStateStore } from "../terminalStateStore"; const MIN_DRAWER_HEIGHT = 180; const MAX_DRAWER_HEIGHT_RATIO = 0.75; @@ -79,33 +76,17 @@ export function selectPendingTerminalEventEntries( return entries.filter((entry) => entry.id > lastAppliedTerminalEventId); } -interface TerminalRuntimeStatus { - label: string; - primaryWebPort: number | null; -} - function terminalRuntimeStatus( terminalId: string, runningTerminalIds: Set, runningTerminalPorts: Record, -): TerminalRuntimeStatus | null { +): PortStatusLabel | null { if (!runningTerminalIds.has(terminalId)) { return null; } const runningPorts = normalizeRunningPorts(runningTerminalPorts[terminalId]); - const primaryWebPort = runningPorts[0] ?? null; - const label = - runningPorts.length === 0 - ? "Terminal process running" - : runningPorts.length === 1 - ? `Open web server: http://localhost:${primaryWebPort}` - : `Open web server: http://localhost:${primaryWebPort} (detected web ports: ${runningPorts.join(", ")})`; - - return { - label, - primaryWebPort, - }; + return portStatusLabel(runningPorts); } function terminalThemeFromApp(): ITheme { diff --git a/apps/web/src/terminalStateStore.ts b/apps/web/src/terminalStateStore.ts index 60ad5c5a1d..46a57b63eb 100644 --- a/apps/web/src/terminalStateStore.ts +++ b/apps/web/src/terminalStateStore.ts @@ -6,6 +6,8 @@ */ import { ThreadId, type TerminalEvent } from "@t3tools/contracts"; +import { normalizeRunningPorts } from "@t3tools/shared/port"; +export { normalizeRunningPorts } from "@t3tools/shared/port"; import { create } from "zustand"; import { createJSONStorage, persist } from "zustand/middleware"; import { resolveStorage } from "./lib/storage"; @@ -62,13 +64,6 @@ function normalizeRunningTerminalIds( .filter((id) => id.length > 0 && validTerminalIdSet.has(id)); } -export function normalizeRunningPorts(ports: readonly number[] | undefined): number[] { - if (!ports || ports.length === 0) return []; - return [...new Set(ports)] - .filter((port) => Number.isInteger(port) && port > 0 && port <= 65_535) - .toSorted((left, right) => left - right); -} - function normalizeRunningTerminalPorts( runningTerminalPorts: Record | undefined, terminalIds: string[], diff --git a/packages/shared/package.json b/packages/shared/package.json index b71d52d700..c3607979b7 100644 --- a/packages/shared/package.json +++ b/packages/shared/package.json @@ -51,6 +51,10 @@ "./projectScripts": { "types": "./src/projectScripts.ts", "import": "./src/projectScripts.ts" + }, + "./port": { + "types": "./src/port.ts", + "import": "./src/port.ts" } }, "scripts": { diff --git a/packages/shared/src/port.ts b/packages/shared/src/port.ts new file mode 100644 index 0000000000..8b3cb85b52 --- /dev/null +++ b/packages/shared/src/port.ts @@ -0,0 +1,24 @@ +export const MAX_PORT_NUMBER = 65_535; + +export function normalizeRunningPorts(ports: readonly number[] | undefined): number[] { + if (!ports || ports.length === 0) return []; + return [...new Set(ports)] + .filter((port) => Number.isInteger(port) && port > 0 && port <= MAX_PORT_NUMBER) + .toSorted((left, right) => left - right); +} + +export interface PortStatusLabel { + label: string; + primaryWebPort: number | null; +} + +export function portStatusLabel(runningPorts: readonly number[]): PortStatusLabel { + const primaryWebPort = runningPorts[0] ?? null; + const label = + runningPorts.length === 0 + ? "Terminal process running" + : runningPorts.length === 1 + ? `Open web server: http://localhost:${primaryWebPort}` + : `Open web server: http://localhost:${primaryWebPort} (detected web ports: ${runningPorts.join(", ")})`; + return { label, primaryWebPort }; +}