// The Settings destination's pure logic and copy (L1): the exact strings the // Android SettingsScreen.kt shows, the workspace-path validation rules // (AppViewModel.validateWorkspace), the connection status card's lines, and the // two localStorage keys that stand in for SettingsRepository's preferences. // // Deliberately free of DOM, sockets and timers, so every branch is unit-testable // in Node and renders identically in the page. Wording authority: // ui/screens/SettingsScreen.kt — the fields, helper lines, warning, status card // and the TrustWarning / Templates row // ui/AppViewModel.kt — validateWorkspace / saveWorkspace // data/SettingsRepository.kt — the two persisted preferences (server URL, workspace) // ui/screens/SettingsScreen.kt — the ConnectionStatusCard's connected build line // // The Android store persists the server URL and the workspace under `server_url` // and `workspace`; the browser's analogue is localStorage. Only these two keys // are ever written — never a credential, because the server has no authentication. import { connectingLabel, serverBuildLabel } from "./connection.js"; /** * The persisted server-URL key. It is the browser analogue of * SettingsRepository's `KEY_SERVER_URL` and the single source of truth the page * already used (`app.js`); kept here so the module owns both settings keys. */ export const SERVER_URL_STORAGE_KEY = "pipelines.web.serverUrl.v2"; /** The persisted workspace key (SettingsRepository's `KEY_WORKSPACE` analogue). */ export const WORKSPACE_STORAGE_KEY = "pipelines.web.workspace.v1"; /** Server-URL field copy, verbatim from SettingsScreen.kt. */ export const SERVER_URL_PLACEHOLDER = "ws://192.168.1.20:8081/connect"; export const SERVER_URL_HELPER = "ws:// on a trusted LAN/VPN, or wss:// through a proxy/VPN. No authentication is used."; /** Workspace field copy, verbatim from SettingsScreen.kt. */ export const WORKSPACE_PLACEHOLDER = "/Users/you/repos/pipelines"; export const WORKSPACE_HELPER = "An absolute directory that already exists on the execution server. For Docker, " + "mount the host directory at the same absolute path with its owner's UID/GID. " + "This is not the phone's storage."; /** The one workspace validation failure, verbatim from AppViewModel.validateWorkspace. */ export const WORKSPACE_ERROR = "The workspace must be an absolute path that exists on the execution server."; /** The trusted-network warning, verbatim from SettingsScreen.kt's TrustWarning. */ export const TRUST_TITLE = "Trusted LAN/VPN only"; export const TRUST_BODY = "The Pipelines API has no authentication and is not internet-safe. Do not " + "expose it to untrusted networks, and never enter provider credentials here \u2014 " + "the app stores none."; /** The templates entry, verbatim from SettingsScreen.kt. */ export const TEMPLATES_CARD_LABEL = "Browse templates"; export const TEMPLATES_CARD_SECONDARY = "Reusable pipeline definitions offered by the server."; /** * AppViewModel.validateWorkspace: a blank draft is not an error (the field is * simply untouched), a non-absolute path names the failure, an absolute path is * accepted. Returns the error string or null. */ export function validateWorkspacePath(value) { const trimmed = String(value ?? "").trim(); if (trimmed === "") return null; if (!trimmed.startsWith("/")) return WORKSPACE_ERROR; return null; } /** * The supporting text under the workspace field: the validation error when the * draft is invalid, otherwise the helper line (Android's `workspaceError ?: helper`). */ export function workspaceErrorText(value) { return validateWorkspacePath(value) ?? ""; } /** * Whether `Save workspace` is enabled. The page deliberately refuses a blank * field (there is nothing to save) as well as a relative path, so the only * enabled state is a trimmed absolute path. This is stricter than Android's * `workspaceError == null` (which also enables Save on a blank field) and is * recorded in web/PARITY.md. */ export function canSaveWorkspace(value) { const trimmed = String(value ?? "").trim(); return trimmed !== "" && validateWorkspacePath(trimmed) === null; } /** The `Saved workspace:` line (Android's `configuredWorkspace.ifBlank { "(none)" }`). */ export function savedWorkspaceLine(value) { const trimmed = String(value ?? "").trim(); return `Saved workspace: ${trimmed || "(none)"}`; } /** * Read the persisted workspace. Tolerant and blank-on-unreadable: a missing * storage, a throwing `getItem` or a non-string value all read as "". */ export function readWorkspace(storage) { try { const raw = storage?.getItem?.(WORKSPACE_STORAGE_KEY); return typeof raw === "string" ? raw : ""; } catch { return ""; } } /** * Persist the workspace trimmed, exactly as SettingsRepository.setWorkspace * does, and return the stored value so the caller can show the same line. Never * throws: an unavailable/full storage leaves the in-memory value authoritative. */ export function writeWorkspace(storage, value) { const trimmed = String(value ?? "").trim(); try { storage?.setItem?.(WORKSPACE_STORAGE_KEY, trimmed); } catch { // A read-only or full localStorage must not break saving in memory. } return trimmed; } /** * Read the persisted server URL. The same tolerant contract as `readWorkspace`: * a missing storage, a throwing `getItem` or a non-string value all read as "". * (The page treats "" as "use the default endpoint", exactly as before.) */ export function readServerUrl(storage) { try { const raw = storage?.getItem?.(SERVER_URL_STORAGE_KEY); return typeof raw === "string" ? raw : ""; } catch { return ""; } } /** * Persist the server URL trimmed, mirroring SettingsRepository.setServerUrl. * Never throws: an unavailable/full storage leaves the in-memory value * authoritative. Returns the stored value. */ export function writeServerUrl(storage, value) { const trimmed = String(value ?? "").trim(); try { storage?.setItem?.(SERVER_URL_STORAGE_KEY, trimmed); } catch { // A read-only or full localStorage must not break connecting. } return trimmed; } /** * The connection status card's lines, exactly per SettingsScreen.kt's * ConnectionStatusCard: * idle -> "Not connected" * connecting -> "Connecting (attempt N)…" * connected -> "Connected", the build line when present, "Server time t" when present * disconnected -> "Disconnected", the message, "Retrying with backoff…" only while retrying * * The connecting and build lines reuse `connection.js`, so each sentence has one * definition shared with the header banner. */ export function connectionCard(state, { version, commit, serverTime } = {}) { const c = state ?? {}; switch (c.kind) { case "connected": { const lines = ["Connected"]; const build = serverBuildLabel({ version, commit }); if (build) lines.push(build); const time = String(serverTime ?? "").trim(); if (time) lines.push(`Server time ${time}`); return lines; } case "connecting": return [connectingLabel(c.attempt)]; case "disconnected": { const lines = ["Disconnected"]; const reason = String(c.reason ?? "").trim(); if (reason) lines.push(reason); if (c.retrying) lines.push("Retrying with backoff\u2026"); return lines; } default: return ["Not connected"]; } } /** * The emphasised primary action and the de-emphasised secondary one, mirroring * SettingsScreen.kt's `active` branch: while connected (or connecting) the * meaningful action is Disconnect, otherwise Connect, with Reconnect offered * while idle/disconnected. The labels are the app's own. */ export function connectionActions(state) { const kind = (state ?? {}).kind; const active = kind === "connected" || kind === "connecting"; return active ? { primary: "Disconnect", secondary: "Connect" } : { primary: "Connect", secondary: "Reconnect" }; } /** * Which existing client operation an action label drives. The mapping is pure so * the page cannot accidentally send `Connect` through the header's toggle (which * disconnects while connected): Android's `AppViewModel.connect()` is * connect-only, `reconnect()` re-opens the saved endpoint and `disconnect()` is * the only thing that closes the socket. Returns one of * `"disconnect" | "reconnect" | "connect"`; anything that is not a known * disconnect/reconnect label is a connect. */ export function connectionActionKind(label) { if (label === "Disconnect") return "disconnect"; if (label === "Reconnect") return "reconnect"; return "connect"; }