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.
This commit is contained in:
2026-05-25 16:26:03 +04:00
parent 7d4468f292
commit 9dcf6f4c04
3 changed files with 8 additions and 3 deletions
@@ -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<Line> = when {
state.approvalOverlayVisible && activeApproval != null -> {
buildList {
@@ -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(
@@ -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