11import { Outlet , createFileRoute , redirect } from "@tanstack/react-router" ;
22import { useAtomValue } from "@effect/atom-react" ;
3+ import { scopedThreadKey } from "@t3tools/client-runtime/environment" ;
4+ import {
5+ isAtomCommandInterrupted ,
6+ squashAtomCommandFailure ,
7+ } from "@t3tools/client-runtime/state/runtime" ;
8+ import { effectiveSettled } from "@t3tools/client-runtime/state/thread-settled" ;
39import { useEffect , useMemo } from "react" ;
410
511import { isCommandPaletteOpen } from "../commandPaletteBus" ;
612import { useClientSettings , useLegacySidebarEnabled } from "../hooks/useSettings" ;
713import { openCommandPalette } from "../commandPaletteBus" ;
8- import { useProjects } from "../state/entities" ;
14+ import { readThreadShell , useProjects } from "../state/entities" ;
915import { usePrimaryEnvironmentId } from "../state/environments" ;
1016import { selectProjectGroupingSettings } from "../logicalProject" ;
1117import { buildSidebarProjectSnapshots } from "../sidebarProjectGrouping" ;
1218import { dispatchPreviewAction } from "../components/preview/previewActionBus" ;
1319import { useHandleNewThread } from "../hooks/useHandleNewThread" ;
20+ import { useThreadActions } from "../hooks/useThreadActions" ;
1421import { startNewThreadFromContext } from "../lib/chatThreadActions" ;
1522import { isPreviewFocused } from "../lib/previewFocus" ;
1623import { isTerminalFocused } from "../lib/terminalFocus" ;
@@ -19,6 +26,8 @@ import { selectThreadTerminalUiState, useTerminalUiStateStore } from "../termina
1926import { isPreviewSupportedInRuntime } from "../previewStateStore" ;
2027import { selectActiveRightPanel , useRightPanelStore } from "../rightPanelStore" ;
2128import { useThreadSelectionStore } from "../threadSelectionStore" ;
29+ import { threadChangeRequestSnapshotsAtom } from "../components/ThreadStatusIndicators" ;
30+ import { appAtomRegistry } from "../rpc/atomRegistry" ;
2231import { stackedThreadToast , toastManager } from "~/components/ui/toast" ;
2332import { primaryServerKeybindingsAtom } from "~/state/server" ;
2433
@@ -27,9 +36,12 @@ function ChatRouteGlobalShortcuts() {
2736 const selectedThreadKeysSize = useThreadSelectionStore ( ( state ) => state . selectedThreadKeys . size ) ;
2837 const { activeDraftThread, activeThread, defaultProjectRef, handleNewThread, routeThreadRef } =
2938 useHandleNewThread ( ) ;
39+ const { settleThread, unsettleThread } = useThreadActions ( ) ;
3040 const keybindings = useAtomValue ( primaryServerKeybindingsAtom ) ;
3141 const legacySidebarEnabled = useLegacySidebarEnabled ( ) ;
3242 const projectGroupingSettings = useClientSettings ( selectProjectGroupingSettings ) ;
43+ const autoSettleAfterDays = useClientSettings ( ( settings ) => settings . sidebarAutoSettleAfterDays ) ;
44+ const autoSettleOnMerge = useClientSettings ( ( settings ) => settings . sidebarAutoSettleOnMerge ) ;
3345 const projects = useProjects ( ) ;
3446 const primaryEnvironmentId = usePrimaryEnvironmentId ( ) ;
3547 const projectGroupCount = useMemo (
@@ -108,6 +120,44 @@ function ChatRouteGlobalShortcuts() {
108120 return ;
109121 }
110122
123+ if ( command === "thread.settle" ) {
124+ event . preventDefault ( ) ;
125+ event . stopPropagation ( ) ;
126+ if ( ! routeThreadRef ) return ;
127+
128+ const thread = readThreadShell ( routeThreadRef ) ;
129+ if ( ! thread ) return ;
130+
131+ const snapshot = appAtomRegistry
132+ . get ( threadChangeRequestSnapshotsAtom )
133+ . get ( scopedThreadKey ( routeThreadRef ) ) ;
134+ const changeRequest =
135+ snapshot != null && ( thread . worktreePath === null || snapshot . branch === thread . branch )
136+ ? snapshot . pr
137+ : null ;
138+ const settled = effectiveSettled ( thread , {
139+ now : `${ new Date ( ) . toISOString ( ) . slice ( 0 , 16 ) } :00.000Z` ,
140+ autoSettleAfterDays,
141+ autoSettleOnMerge,
142+ changeRequest,
143+ } ) ;
144+
145+ void ( settled ? unsettleThread ( routeThreadRef ) : settleThread ( routeThreadRef ) ) . then (
146+ ( result ) => {
147+ if ( result . _tag !== "Failure" || isAtomCommandInterrupted ( result ) ) return ;
148+ const error = squashAtomCommandFailure ( result ) ;
149+ toastManager . add (
150+ stackedThreadToast ( {
151+ type : "error" ,
152+ title : settled ? "Failed to un-settle thread" : "Failed to settle thread" ,
153+ description : error instanceof Error ? error . message : "An error occurred." ,
154+ } ) ,
155+ ) ;
156+ } ,
157+ ) ;
158+ return ;
159+ }
160+
111161 if ( command === "preview.toggle" ) {
112162 event . preventDefault ( ) ;
113163 event . stopPropagation ( ) ;
@@ -159,6 +209,8 @@ function ChatRouteGlobalShortcuts() {
159209 } , [
160210 activeDraftThread ,
161211 activeThread ,
212+ autoSettleAfterDays ,
213+ autoSettleOnMerge ,
162214 clearSelection ,
163215 handleNewThread ,
164216 keybindings ,
@@ -167,8 +219,10 @@ function ChatRouteGlobalShortcuts() {
167219 projectGroupCount ,
168220 routeThreadRef ,
169221 selectedThreadKeysSize ,
222+ settleThread ,
170223 legacySidebarEnabled ,
171224 terminalOpen ,
225+ unsettleThread ,
172226 ] ) ;
173227
174228 return null ;
0 commit comments