From 9dcf6f4c04bf0108833e45d0eb2fb4e44fc675c1 Mon Sep 17 00:00:00 2001 From: kami Date: Mon, 25 May 2026 16:26:03 +0400 Subject: [PATCH] refactor(tui): extract selectedPendingApproval() extension and unify call sites Add TuiState.selectedPendingApproval() extension and replace the three inline session-lookup expressions in InputReducer and RouterPanel with it. --- .../kotlin/com/correx/apps/tui/components/RouterPanel.kt | 3 ++- .../main/kotlin/com/correx/apps/tui/reducer/InputReducer.kt | 5 +++-- .../src/main/kotlin/com/correx/apps/tui/state/TuiState.kt | 3 +++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/apps/tui/src/main/kotlin/com/correx/apps/tui/components/RouterPanel.kt b/apps/tui/src/main/kotlin/com/correx/apps/tui/components/RouterPanel.kt index d24f636e..f7bf7deb 100644 --- a/apps/tui/src/main/kotlin/com/correx/apps/tui/components/RouterPanel.kt +++ b/apps/tui/src/main/kotlin/com/correx/apps/tui/components/RouterPanel.kt @@ -1,6 +1,7 @@ package com.correx.apps.tui.components import com.correx.apps.tui.state.TuiState +import com.correx.apps.tui.state.selectedPendingApproval import dev.tamboui.style.Style import dev.tamboui.text.Line import dev.tamboui.text.Span @@ -23,7 +24,7 @@ fun routerPanelWidget(state: TuiState): Paragraph { } } - val activeApproval = state.sessions.sessions.find { it.id == state.sessions.selectedId }?.pendingApproval + val activeApproval = state.selectedPendingApproval() val overlayLines: List = when { state.approvalOverlayVisible && activeApproval != null -> { buildList { diff --git a/apps/tui/src/main/kotlin/com/correx/apps/tui/reducer/InputReducer.kt b/apps/tui/src/main/kotlin/com/correx/apps/tui/reducer/InputReducer.kt index 8410b910..8a708094 100644 --- a/apps/tui/src/main/kotlin/com/correx/apps/tui/reducer/InputReducer.kt +++ b/apps/tui/src/main/kotlin/com/correx/apps/tui/reducer/InputReducer.kt @@ -5,6 +5,7 @@ import com.correx.apps.server.protocol.ClientMessage import com.correx.apps.tui.input.Action import com.correx.apps.tui.state.InputMode import com.correx.apps.tui.state.TuiState +import com.correx.apps.tui.state.selectedPendingApproval import com.correx.core.events.types.ApprovalRequestId object InputReducer { @@ -25,7 +26,7 @@ object InputReducer { is Action.OpenNewSessionPrompt -> state.copy(inputMode = InputMode.ROUTER, inputBuffer = "") to emptyList() is Action.OpenSteeringPrompt -> { - val active = state.sessions.sessions.find { it.id == state.sessions.selectedId }?.pendingApproval + val active = state.selectedPendingApproval() if (active != null) { state.copy(inputMode = InputMode.STEER, inputBuffer = "") to emptyList() } else { @@ -52,7 +53,7 @@ object InputReducer { InputMode.STEER -> { val text = state.inputBuffer.trim() - val active = state.sessions.sessions.find { it.id == state.sessions.selectedId }?.pendingApproval + val active = state.selectedPendingApproval() if (active != null && text.isNotBlank()) { state.copy(inputMode = InputMode.ROUTER, inputBuffer = "") to listOf( Effect.SendWs( diff --git a/apps/tui/src/main/kotlin/com/correx/apps/tui/state/TuiState.kt b/apps/tui/src/main/kotlin/com/correx/apps/tui/state/TuiState.kt index 5d75c72b..041635d7 100644 --- a/apps/tui/src/main/kotlin/com/correx/apps/tui/state/TuiState.kt +++ b/apps/tui/src/main/kotlin/com/correx/apps/tui/state/TuiState.kt @@ -75,3 +75,6 @@ data class ApprovalInfo( val toolName: String?, val preview: String?, ) + +fun TuiState.selectedPendingApproval(): ApprovalInfo? = + sessions.sessions.find { it.id == sessions.selectedId }?.pendingApproval