66} from "@t3tools/client-runtime/state/runtime" ;
77import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell" ;
88import type { EnvironmentId } from "@t3tools/contracts" ;
9- import { useCallback } from "react" ;
109
1110import { stackedThreadToast , toastManager } from "~/components/ui/toast" ;
1211import { useEnvironments } from "~/state/environments" ;
@@ -29,54 +28,39 @@ export function useScratchProject() {
2928 const openScratch = useAtomCommand ( projectEnvironment . openScratch , { reportFailure : false } ) ;
3029 const handleNewThread = useNewThreadHandler ( ) ;
3130
32- const scratchWorkspaceRootFor = useCallback (
33- ( environmentId : EnvironmentId | null ) : string | null => {
34- const environment = environments . find ( ( entry ) => entry . environmentId === environmentId ) ;
35- return availableScratchWorkspaceRoot (
36- environment ?. connection . phase ,
37- environment ?. serverConfig ,
38- ) ;
39- } ,
40- [ environments ] ,
41- ) ;
31+ const scratchWorkspaceRootFor = ( environmentId : EnvironmentId | null ) : string | null => {
32+ const environment = environments . find ( ( entry ) => entry . environmentId === environmentId ) ;
33+ return availableScratchWorkspaceRoot ( environment ?. connection . phase , environment ?. serverConfig ) ;
34+ } ;
4235
43- const scratchEnvironmentId = useCallback (
44- ( current : EnvironmentId | null ) : EnvironmentId | null => {
45- if ( current !== null ) return scratchWorkspaceRootFor ( current ) !== null ? current : null ;
46- const offering = environments . filter (
47- ( entry ) => scratchWorkspaceRootFor ( entry . environmentId ) !== null ,
48- ) ;
49- return offering . length === 1 ? ( offering [ 0 ] ?. environmentId ?? null ) : null ;
50- } ,
51- [ environments , scratchWorkspaceRootFor ] ,
52- ) ;
36+ const scratchEnvironmentId = ( current : EnvironmentId | null ) : EnvironmentId | null => {
37+ if ( current !== null ) return scratchWorkspaceRootFor ( current ) !== null ? current : null ;
38+ const offering = environments . filter (
39+ ( entry ) => scratchWorkspaceRootFor ( entry . environmentId ) !== null ,
40+ ) ;
41+ return offering . length === 1 ? ( offering [ 0 ] ?. environmentId ?? null ) : null ;
42+ } ;
5343
54- const openScratchProject = useCallback (
55- async (
56- environmentId : EnvironmentId ,
57- failureTitle = "Could not start without a project" ,
58- ) : Promise < EnvironmentProject | null > => {
59- const result = await openScratch ( { environmentId, input : { } } ) ;
60- if ( result . _tag === "Success" ) return result . value ;
61- if ( ! isAtomCommandInterrupted ( result ) ) {
62- reportScratchFailure ( failureTitle , squashAtomCommandFailure ( result ) ) ;
63- }
64- return null ;
65- } ,
66- [ openScratch ] ,
67- ) ;
44+ const openScratchProject = async (
45+ environmentId : EnvironmentId ,
46+ failureTitle = "Could not start without a project" ,
47+ ) : Promise < EnvironmentProject | null > => {
48+ const result = await openScratch ( { environmentId, input : { } } ) ;
49+ if ( result . _tag === "Success" ) return result . value ;
50+ if ( ! isAtomCommandInterrupted ( result ) ) {
51+ reportScratchFailure ( failureTitle , squashAtomCommandFailure ( result ) ) ;
52+ }
53+ return null ;
54+ } ;
6855
69- const startScratchThread = useCallback (
70- async ( environmentId : EnvironmentId ) => {
71- const project = await openScratchProject ( environmentId ) ;
72- if ( project ) {
73- await handleNewThread ( scopeProjectRef ( project . environmentId , project . id ) ) . catch (
74- ( error : unknown ) => reportScratchFailure ( "Could not start without a project" , error ) ,
75- ) ;
76- }
77- } ,
78- [ handleNewThread , openScratchProject ] ,
79- ) ;
56+ const startScratchThread = async ( environmentId : EnvironmentId ) => {
57+ const project = await openScratchProject ( environmentId ) ;
58+ if ( project ) {
59+ await handleNewThread ( scopeProjectRef ( project . environmentId , project . id ) ) . catch (
60+ ( error : unknown ) => reportScratchFailure ( "Could not start without a project" , error ) ,
61+ ) ;
62+ }
63+ } ;
8064
8165 return { scratchWorkspaceRootFor, scratchEnvironmentId, openScratchProject, startScratchThread } ;
8266}
0 commit comments