1- import { useCallback , useEffect , useEffectEvent , useMemo } from "react" ;
1+ import { useAtomMount } from "@effect/atom-react" ;
2+ import { useCallback , useMemo } from "react" ;
23
34import { EnvironmentProject , EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell" ;
45import type { AtomCommandResult } from "@t3tools/client-runtime/state/runtime" ;
56import {
7+ createVcsStatusRevalidationAtomFamily ,
68 type GitActionRequestInput ,
79 type VcsActionOperation ,
810 type VcsRef ,
@@ -26,12 +28,31 @@ import { vcsActionManager, vcsEnvironment } from "../state/vcs";
2628import { uuidv4 } from "../lib/uuid" ;
2729import { appAtomRegistry } from "./atom-registry" ;
2830import { readEnvironmentScope , useEnvironmentScope } from "./session" ;
29- import { setPendingConnectionError } from "./use-remote-environment-registry" ;
31+ import {
32+ environmentConnectedAtom ,
33+ setPendingConnectionError ,
34+ } from "./use-remote-environment-registry" ;
3035import { useAtomCommand } from "./use-atom-command" ;
3136import { showGitActionResult } from "./use-vcs-action-state" ;
3237import { useThreadSelection } from "./use-thread-selection" ;
3338import { useSelectedThreadWorktree } from "./use-selected-thread-worktree" ;
3439
40+ function refreshFailureMessage ( cause : Cause . Cause < unknown > ) : string {
41+ const error = Cause . squash ( cause ) ;
42+ return error instanceof Error ? error . message : "Failed to refresh git status." ;
43+ }
44+
45+ // Selecting a thread or reconnecting its environment refreshes the checkout's
46+ // status once, shared by every sheet that shows it.
47+ const selectedThreadStatusRevalidation = createVcsStatusRevalidationAtomFamily ( {
48+ refreshStatus : vcsEnvironment . refreshStatus ,
49+ connected : environmentConnectedAtom ,
50+ onRefreshed : ( result ) =>
51+ setPendingConnectionError (
52+ AsyncResult . isFailure ( result ) ? refreshFailureMessage ( result . cause ) : null ,
53+ ) ,
54+ } ) ;
55+
3556export function useSelectedThreadGitActions ( ) {
3657 const updateThreadMetadata = useAtomCommand ( threadEnvironment . updateMetadata , {
3758 reportFailure : false ,
@@ -41,8 +62,7 @@ export function useSelectedThreadGitActions() {
4162 const createRef = useAtomCommand ( vcsEnvironment . createRef , { reportFailure : false } ) ;
4263 const createWorktree = useAtomCommand ( vcsEnvironment . createWorktree , { reportFailure : false } ) ;
4364 const pull = useAtomCommand ( vcsEnvironment . pull , { reportFailure : false } ) ;
44- const { selectedThread, selectedThreadProject, selectedEnvironmentRuntime } =
45- useThreadSelection ( ) ;
65+ const { selectedThread, selectedThreadProject } = useThreadSelection ( ) ;
4666 const canWriteSourceControl = useEnvironmentScope (
4767 selectedThread ?. environmentId ?? null ,
4868 AuthSourceControlWriteScope ,
@@ -130,9 +150,7 @@ export function useSelectedThreadGitActions() {
130150 execute ,
131151 ) ;
132152 if ( AsyncResult . isFailure ( result ) ) {
133- const error = Cause . squash ( result . cause ) ;
134- const message = error instanceof Error ? error . message : "Failed to refresh git status." ;
135- setPendingConnectionError ( message ) ;
153+ setPendingConnectionError ( refreshFailureMessage ( result . cause ) ) ;
136154 return null ;
137155 }
138156 setPendingConnectionError ( null ) ;
@@ -141,32 +159,13 @@ export function useSelectedThreadGitActions() {
141159 [ refreshStatus , selectedThread , selectedThreadCwd , selectedThreadProject ] ,
142160 ) ;
143161
144- // Key refreshes on primitives; streaming frequently replaces the selected thread object.
145- const selectedEnvironmentId = selectedThread ?. environmentId ?? null ;
146- const selectedThreadId = selectedThread ?. id ?? null ;
147- const hasSelectedThreadProject = selectedThreadProject !== null ;
148- const isEnvironmentConnected = selectedEnvironmentRuntime ?. connectionState === "connected" ;
149- const refreshOnSelection = useEffectEvent ( ( ) => {
150- void refreshSelectedThreadGitStatus ( { quiet : true } ) ;
151- } ) ;
152- useEffect ( ( ) => {
153- if (
154- selectedEnvironmentId === null ||
155- selectedThreadId === null ||
156- ! hasSelectedThreadProject ||
157- selectedThreadCwd === null ||
158- ! isEnvironmentConnected
159- ) {
160- return ;
161- }
162- refreshOnSelection ( ) ;
163- } , [
164- selectedEnvironmentId ,
165- selectedThreadId ,
166- hasSelectedThreadProject ,
167- selectedThreadCwd ,
168- isEnvironmentConnected ,
169- ] ) ;
162+ useAtomMount (
163+ selectedThreadStatusRevalidation (
164+ selectedThread && selectedThreadProject && selectedThreadCwd
165+ ? { environmentId : selectedThread . environmentId , cwd : selectedThreadCwd }
166+ : null ,
167+ ) ,
168+ ) ;
170169
171170 const runSelectedThreadGitMutation = useCallback (
172171 async < T , E > (
0 commit comments