|
| 1 | +import { Match } from "effect" |
| 2 | +import { Text } from "ink" |
| 3 | +import type React from "react" |
| 4 | + |
| 5 | +import type { ProjectItem } from "@effect-template/lib/usecases/projects" |
| 6 | +import type { SelectProjectRuntime } from "./menu-types.js" |
| 7 | + |
| 8 | +export type SelectPurpose = "Connect" | "Down" | "Info" | "Delete" |
| 9 | + |
| 10 | +const formatRepoRef = (repoRef: string): string => { |
| 11 | + const trimmed = repoRef.trim() |
| 12 | + const prPrefix = "refs/pull/" |
| 13 | + if (trimmed.startsWith(prPrefix)) { |
| 14 | + const rest = trimmed.slice(prPrefix.length) |
| 15 | + const number = rest.split("/")[0] ?? rest |
| 16 | + return `PR#${number}` |
| 17 | + } |
| 18 | + return trimmed.length > 0 ? trimmed : "main" |
| 19 | +} |
| 20 | + |
| 21 | +const stoppedRuntime = (): SelectProjectRuntime => ({ running: false, sshSessions: 0 }) |
| 22 | + |
| 23 | +const runtimeForProject = ( |
| 24 | + runtimeByProject: Readonly<Record<string, SelectProjectRuntime>>, |
| 25 | + item: ProjectItem |
| 26 | +): SelectProjectRuntime => runtimeByProject[item.projectDir] ?? stoppedRuntime() |
| 27 | + |
| 28 | +const renderRuntimeLabel = (runtime: SelectProjectRuntime): string => |
| 29 | + `${runtime.running ? "running" : "stopped"}, ssh=${runtime.sshSessions}` |
| 30 | + |
| 31 | +export const selectTitle = (purpose: SelectPurpose): string => |
| 32 | + Match.value(purpose).pipe( |
| 33 | + Match.when("Connect", () => "docker-git / Select project"), |
| 34 | + Match.when("Down", () => "docker-git / Stop container"), |
| 35 | + Match.when("Info", () => "docker-git / Show connection info"), |
| 36 | + Match.when("Delete", () => "docker-git / Delete project"), |
| 37 | + Match.exhaustive |
| 38 | + ) |
| 39 | + |
| 40 | +export const selectHint = ( |
| 41 | + purpose: SelectPurpose, |
| 42 | + connectEnableMcpPlaywright: boolean |
| 43 | +): string => |
| 44 | + Match.value(purpose).pipe( |
| 45 | + Match.when( |
| 46 | + "Connect", |
| 47 | + () => `Enter = select + SSH, P = toggle Playwright MCP (${connectEnableMcpPlaywright ? "on" : "off"}), Esc = back` |
| 48 | + ), |
| 49 | + Match.when("Down", () => "Enter = stop container, Esc = back"), |
| 50 | + Match.when("Info", () => "Use arrows to browse details, Enter = set active, Esc = back"), |
| 51 | + Match.when("Delete", () => "Enter = ask/confirm delete, Esc = cancel"), |
| 52 | + Match.exhaustive |
| 53 | + ) |
| 54 | + |
| 55 | +export const buildSelectLabels = ( |
| 56 | + items: ReadonlyArray<ProjectItem>, |
| 57 | + selected: number, |
| 58 | + purpose: SelectPurpose, |
| 59 | + runtimeByProject: Readonly<Record<string, SelectProjectRuntime>> |
| 60 | +): ReadonlyArray<string> => |
| 61 | + items.map((item, index) => { |
| 62 | + const prefix = index === selected ? ">" : " " |
| 63 | + const refLabel = formatRepoRef(item.repoRef) |
| 64 | + const runtimeSuffix = purpose === "Down" || purpose === "Delete" |
| 65 | + ? ` [${renderRuntimeLabel(runtimeForProject(runtimeByProject, item))}]` |
| 66 | + : "" |
| 67 | + return `${prefix} ${index + 1}. ${item.displayName} (${refLabel})${runtimeSuffix}` |
| 68 | + }) |
| 69 | + |
| 70 | +type SelectDetailsContext = { |
| 71 | + readonly item: ProjectItem |
| 72 | + readonly refLabel: string |
| 73 | + readonly authSuffix: string |
| 74 | + readonly runtime: SelectProjectRuntime |
| 75 | + readonly sshSessionsLabel: string |
| 76 | +} |
| 77 | + |
| 78 | +const buildDetailsContext = ( |
| 79 | + item: ProjectItem, |
| 80 | + runtimeByProject: Readonly<Record<string, SelectProjectRuntime>> |
| 81 | +): SelectDetailsContext => { |
| 82 | + const runtime = runtimeForProject(runtimeByProject, item) |
| 83 | + return { |
| 84 | + item, |
| 85 | + refLabel: formatRepoRef(item.repoRef), |
| 86 | + authSuffix: item.authorizedKeysExists ? "" : " (missing)", |
| 87 | + runtime, |
| 88 | + sshSessionsLabel: runtime.sshSessions === 1 |
| 89 | + ? "1 active SSH session" |
| 90 | + : `${runtime.sshSessions} active SSH sessions` |
| 91 | + } |
| 92 | +} |
| 93 | + |
| 94 | +const titleRow = (el: typeof React.createElement, value: string): React.ReactElement => |
| 95 | + el(Text, { color: "cyan", bold: true, wrap: "truncate" }, value) |
| 96 | + |
| 97 | +const commonRows = ( |
| 98 | + el: typeof React.createElement, |
| 99 | + context: SelectDetailsContext |
| 100 | +): ReadonlyArray<React.ReactElement> => [ |
| 101 | + el(Text, { wrap: "wrap" }, `Project directory: ${context.item.projectDir}`), |
| 102 | + el(Text, { wrap: "wrap" }, `Container: ${context.item.containerName}`), |
| 103 | + el(Text, { wrap: "wrap" }, `State: ${context.runtime.running ? "running" : "stopped"}`), |
| 104 | + el(Text, { wrap: "wrap" }, `SSH sessions now: ${context.sshSessionsLabel}`) |
| 105 | +] |
| 106 | + |
| 107 | +const renderInfoDetails = ( |
| 108 | + el: typeof React.createElement, |
| 109 | + context: SelectDetailsContext, |
| 110 | + common: ReadonlyArray<React.ReactElement> |
| 111 | +): ReadonlyArray<React.ReactElement> => [ |
| 112 | + titleRow(el, "Connection info"), |
| 113 | + ...common, |
| 114 | + el(Text, { wrap: "wrap" }, `Service: ${context.item.serviceName}`), |
| 115 | + el(Text, { wrap: "wrap" }, `SSH command: ${context.item.sshCommand}`), |
| 116 | + el(Text, { wrap: "wrap" }, `Repo: ${context.item.repoUrl} (${context.refLabel})`), |
| 117 | + el(Text, { wrap: "wrap" }, `Workspace: ${context.item.targetDir}`), |
| 118 | + el(Text, { wrap: "wrap" }, `Authorized keys: ${context.item.authorizedKeysPath}${context.authSuffix}`), |
| 119 | + el(Text, { wrap: "wrap" }, `Env global: ${context.item.envGlobalPath}`), |
| 120 | + el(Text, { wrap: "wrap" }, `Env project: ${context.item.envProjectPath}`), |
| 121 | + el(Text, { wrap: "wrap" }, `Codex auth: ${context.item.codexAuthPath} -> ${context.item.codexHome}`) |
| 122 | +] |
| 123 | + |
| 124 | +const renderDefaultDetails = ( |
| 125 | + el: typeof React.createElement, |
| 126 | + context: SelectDetailsContext |
| 127 | +): ReadonlyArray<React.ReactElement> => [ |
| 128 | + titleRow(el, "Details"), |
| 129 | + el(Text, { wrap: "truncate" }, `Repo: ${context.item.repoUrl}`), |
| 130 | + el(Text, { wrap: "truncate" }, `Ref: ${context.item.repoRef}`), |
| 131 | + el(Text, { wrap: "truncate" }, `Project dir: ${context.item.projectDir}`), |
| 132 | + el(Text, { wrap: "truncate" }, `Workspace: ${context.item.targetDir}`), |
| 133 | + el(Text, { wrap: "truncate" }, `SSH: ${context.item.sshCommand}`) |
| 134 | +] |
| 135 | + |
| 136 | +const renderConnectDetails = ( |
| 137 | + el: typeof React.createElement, |
| 138 | + context: SelectDetailsContext, |
| 139 | + common: ReadonlyArray<React.ReactElement>, |
| 140 | + connectEnableMcpPlaywright: boolean |
| 141 | +): ReadonlyArray<React.ReactElement> => [ |
| 142 | + titleRow(el, "Connect + SSH"), |
| 143 | + ...common, |
| 144 | + el( |
| 145 | + Text, |
| 146 | + { color: connectEnableMcpPlaywright ? "green" : "gray", wrap: "wrap" }, |
| 147 | + connectEnableMcpPlaywright |
| 148 | + ? "Playwright MCP: will be enabled before SSH (P to disable)." |
| 149 | + : "Playwright MCP: keep current project setting (P to enable before SSH)." |
| 150 | + ), |
| 151 | + el(Text, { wrap: "wrap" }, `Repo: ${context.item.repoUrl} (${context.refLabel})`), |
| 152 | + el(Text, { wrap: "wrap" }, `SSH command: ${context.item.sshCommand}`) |
| 153 | +] |
| 154 | + |
| 155 | +export const renderSelectDetails = ( |
| 156 | + el: typeof React.createElement, |
| 157 | + purpose: SelectPurpose, |
| 158 | + item: ProjectItem | undefined, |
| 159 | + runtimeByProject: Readonly<Record<string, SelectProjectRuntime>>, |
| 160 | + connectEnableMcpPlaywright: boolean |
| 161 | +): ReadonlyArray<React.ReactElement> => { |
| 162 | + if (!item) { |
| 163 | + return [el(Text, { color: "gray", wrap: "truncate" }, "No project selected.")] |
| 164 | + } |
| 165 | + const context = buildDetailsContext(item, runtimeByProject) |
| 166 | + const common = commonRows(el, context) |
| 167 | + |
| 168 | + return Match.value(purpose).pipe( |
| 169 | + Match.when("Connect", () => renderConnectDetails(el, context, common, connectEnableMcpPlaywright)), |
| 170 | + Match.when("Info", () => renderInfoDetails(el, context, common)), |
| 171 | + Match.when("Down", () => [ |
| 172 | + titleRow(el, "Stop container"), |
| 173 | + ...common, |
| 174 | + el(Text, { wrap: "wrap" }, `Repo: ${context.item.repoUrl} (${context.refLabel})`) |
| 175 | + ]), |
| 176 | + Match.when("Delete", () => [ |
| 177 | + titleRow(el, "Delete project"), |
| 178 | + ...common, |
| 179 | + context.runtime.sshSessions > 0 |
| 180 | + ? el(Text, { color: "yellow", wrap: "wrap" }, "Warning: project has active SSH sessions.") |
| 181 | + : el(Text, { color: "gray", wrap: "wrap" }, "No active SSH sessions detected."), |
| 182 | + el(Text, { wrap: "wrap" }, `Repo: ${context.item.repoUrl} (${context.refLabel})`), |
| 183 | + el(Text, { wrap: "wrap" }, "Removes the project folder (no git history rewrite).") |
| 184 | + ]), |
| 185 | + Match.orElse(() => renderDefaultDetails(el, context)) |
| 186 | + ) |
| 187 | +} |
0 commit comments