88 squashAtomCommandFailure ,
99} from "@t3tools/client-runtime/state/runtime" ;
1010import type { EnvironmentId } from "@t3tools/contracts" ;
11- import { useCallback } from "react" ;
1211
1312import { stackedThreadToast , toastManager } from "~/components/ui/toast" ;
1413import { waitForProject } from "~/state/entities" ;
@@ -30,113 +29,107 @@ export function useNewProject() {
3029 } ) ;
3130 const handleNewThread = useNewThreadHandler ( ) ;
3231
33- const publishNewRepository = useCallback (
34- async ( input : {
35- readonly environmentId : EnvironmentId ;
36- readonly workspaceRoot : string ;
37- readonly target : NewProjectPublishTarget ;
38- } ) => {
39- const { label } = input . target . definition ;
40- const result = await publishRepository ( {
41- environmentId : input . environmentId ,
42- input : {
43- cwd : input . workspaceRoot ,
44- provider : input . target . definition . kind ,
45- repository : getNewProjectRepository ( input . target , input . workspaceRoot ) ,
46- visibility : "private" ,
47- } ,
48- } ) ;
49- if ( result . _tag === "Failure" ) {
50- if ( ! isAtomCommandInterrupted ( result ) ) {
51- toastManager . add (
52- stackedThreadToast ( {
53- type : "error" ,
54- title : `Could not create the ${ label } repository` ,
55- description : `${ errorMessage ( squashAtomCommandFailure ( result ) ) } Use Publish Repository in the Git menu to try again.` ,
56- } ) ,
57- ) ;
58- }
59- return ;
60- }
61- toastManager . add (
62- stackedThreadToast ( {
63- type : "success" ,
64- title : `Published to ${ label } ` ,
65- description : result . value . repository . nameWithOwner ,
66- } ) ,
67- ) ;
68- } ,
69- [ publishRepository ] ,
70- ) ;
71-
72- return useCallback (
73- async ( input : {
74- readonly environmentId : EnvironmentId ;
75- readonly name : string ;
76- readonly publishTo : NewProjectPublishTarget | null ;
77- } ) : Promise < boolean > => {
78- const result = await createNew ( {
79- environmentId : input . environmentId ,
80- input : { name : input . name } ,
81- } ) ;
82- if ( result . _tag === "Failure" ) {
83- if ( ! isAtomCommandInterrupted ( result ) ) {
84- toastManager . add (
85- stackedThreadToast ( {
86- type : "error" ,
87- title : "Could not create the project" ,
88- description : errorMessage ( squashAtomCommandFailure ( result ) ) ,
89- } ) ,
90- ) ;
91- }
92- return false ;
93- }
94-
95- const { projectId, workspaceRoot, commitError } = result . value ;
96- toastManager . add (
97- stackedThreadToast (
98- commitError === undefined
99- ? { type : "success" , title : `Created ${ input . name } ` , description : workspaceRoot }
100- : {
101- type : "warning" ,
102- title : `Created ${ input . name } without a first commit` ,
103- description : `${ commitError } The project is in ${ workspaceRoot } .` ,
104- } ,
105- ) ,
106- ) ;
107- if ( input . publishTo ) {
108- void publishNewRepository ( {
109- environmentId : input . environmentId ,
110- workspaceRoot,
111- target : input . publishTo ,
112- } ) ;
113- }
114-
115- const projectRef = scopeProjectRef ( input . environmentId , projectId ) ;
116- // Drafts key off the project's stored path, so wait for the create event
117- // to reach the store before opening one.
118- const project = await waitForProject ( projectRef ) . catch ( ( error : unknown ) => {
32+ const publishNewRepository = async ( input : {
33+ readonly environmentId : EnvironmentId ;
34+ readonly workspaceRoot : string ;
35+ readonly target : NewProjectPublishTarget ;
36+ } ) => {
37+ const { label } = input . target . definition ;
38+ const result = await publishRepository ( {
39+ environmentId : input . environmentId ,
40+ input : {
41+ cwd : input . workspaceRoot ,
42+ provider : input . target . definition . kind ,
43+ repository : getNewProjectRepository ( input . target , input . workspaceRoot ) ,
44+ visibility : "private" ,
45+ } ,
46+ } ) ;
47+ if ( result . _tag === "Failure" ) {
48+ if ( ! isAtomCommandInterrupted ( result ) ) {
11949 toastManager . add (
12050 stackedThreadToast ( {
12151 type : "error" ,
122- title : "Failed to open project" ,
123- description : `${ errorMessage ( error ) } It will appear in the sidebar once this client catches up .` ,
52+ title : `Could not create the ${ label } repository` ,
53+ description : `${ errorMessage ( squashAtomCommandFailure ( result ) ) } Use Publish Repository in the Git menu to try again .` ,
12454 } ) ,
12555 ) ;
126- return null ;
127- } ) ;
128- if ( project === null ) return true ;
129- await handleNewThread ( projectRef ) . catch ( ( error : unknown ) => {
56+ }
57+ return ;
58+ }
59+ toastManager . add (
60+ stackedThreadToast ( {
61+ type : "success" ,
62+ title : `Published to ${ label } ` ,
63+ description : result . value . repository . nameWithOwner ,
64+ } ) ,
65+ ) ;
66+ } ;
67+
68+ return async ( input : {
69+ readonly environmentId : EnvironmentId ;
70+ readonly name : string ;
71+ readonly publishTo : NewProjectPublishTarget | null ;
72+ } ) : Promise < boolean > => {
73+ const result = await createNew ( {
74+ environmentId : input . environmentId ,
75+ input : { name : input . name } ,
76+ } ) ;
77+ if ( result . _tag === "Failure" ) {
78+ if ( ! isAtomCommandInterrupted ( result ) ) {
13079 toastManager . add (
13180 stackedThreadToast ( {
13281 type : "error" ,
133- title : "Failed to open project" ,
134- description : errorMessage ( error ) ,
82+ title : "Could not create the project" ,
83+ description : errorMessage ( squashAtomCommandFailure ( result ) ) ,
13584 } ) ,
13685 ) ;
86+ }
87+ return false ;
88+ }
89+
90+ const { projectId, workspaceRoot, commitError } = result . value ;
91+ toastManager . add (
92+ stackedThreadToast (
93+ commitError === undefined
94+ ? { type : "success" , title : `Created ${ input . name } ` , description : workspaceRoot }
95+ : {
96+ type : "warning" ,
97+ title : `Created ${ input . name } without a first commit` ,
98+ description : `${ commitError } The project is in ${ workspaceRoot } .` ,
99+ } ,
100+ ) ,
101+ ) ;
102+ if ( input . publishTo ) {
103+ void publishNewRepository ( {
104+ environmentId : input . environmentId ,
105+ workspaceRoot,
106+ target : input . publishTo ,
137107 } ) ;
138- return true ;
139- } ,
140- [ createNew , handleNewThread , publishNewRepository ] ,
141- ) ;
108+ }
109+
110+ const projectRef = scopeProjectRef ( input . environmentId , projectId ) ;
111+ // Drafts key off the project's stored path, so wait for the create event
112+ // to reach the store before opening one.
113+ const project = await waitForProject ( projectRef ) . catch ( ( error : unknown ) => {
114+ toastManager . add (
115+ stackedThreadToast ( {
116+ type : "error" ,
117+ title : "Failed to open project" ,
118+ description : `${ errorMessage ( error ) } It will appear in the sidebar once this client catches up.` ,
119+ } ) ,
120+ ) ;
121+ return null ;
122+ } ) ;
123+ if ( project === null ) return true ;
124+ await handleNewThread ( projectRef ) . catch ( ( error : unknown ) => {
125+ toastManager . add (
126+ stackedThreadToast ( {
127+ type : "error" ,
128+ title : "Failed to open project" ,
129+ description : errorMessage ( error ) ,
130+ } ) ,
131+ ) ;
132+ } ) ;
133+ return true ;
134+ } ;
142135}
0 commit comments