@@ -7,7 +7,11 @@ import {
77 type StaticScreenProps ,
88} from "@react-navigation/native" ;
99import { SymbolView } from "../../components/AppSymbol" ;
10+ import { canCreateProjectInEnvironment } from "@t3tools/client-runtime/operations/projects" ;
11+ import { isScratchProject } from "@t3tools/client-runtime/state/projects" ;
1012import type { EnvironmentProject } from "@t3tools/client-runtime/state/shell" ;
13+ import * as Cause from "effect/Cause" ;
14+ import { AsyncResult } from "effect/unstable/reactivity" ;
1115import { useEffect , useRef , useState } from "react" ;
1216import { ActivityIndicator , Alert , Platform , Pressable , View } from "react-native" ;
1317import { useSafeAreaInsets } from "react-native-safe-area-context" ;
@@ -17,7 +21,10 @@ import { MaterialButton } from "../../components/MaterialButton";
1721import { ScreenScrollView as ScrollView } from "../../components/ScreenScrollView" ;
1822import { AppText as Text } from "../../components/AppText" ;
1923import { ProjectFavicon } from "../../components/ProjectFavicon" ;
20- import { useProjects } from "../../state/entities" ;
24+ import { useProjects , useServerConfigs , waitForProject } from "../../state/entities" ;
25+ import { projectEnvironment } from "../../state/projects" ;
26+ import { useAtomCommand } from "../../state/use-atom-command" ;
27+ import { useRemoteConnectionStatus } from "../../state/use-remote-environment-registry" ;
2128import type { WorkspaceState } from "../../state/workspaceModel" ;
2229import { useWorkspaceState } from "../../state/workspace" ;
2330import { useAdaptiveWorkspaceLayout } from "../layout/AdaptiveWorkspaceLayout" ;
@@ -156,6 +163,44 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
156163 project . id === incomingShare . destination ?. projectId ,
157164 ) ?? null )
158165 : null ;
166+ const serverConfigs = useServerConfigs ( ) ;
167+ const { connectedEnvironments } = useRemoteConnectionStatus ( ) ;
168+ const ensureScratch = useAtomCommand ( projectEnvironment . ensureScratch , {
169+ reportFailure : false ,
170+ } ) ;
171+ // Threads without a project need a connected environment that offers them.
172+ // The selected environment wins when it has one; otherwise the first that
173+ // does, and the entry names that machine whenever there is a choice.
174+ const scratchEnvironments = connectedEnvironments . filter (
175+ ( environment ) =>
176+ canCreateProjectInEnvironment ( environment . connectionState ) &&
177+ serverConfigs . get ( environment . environmentId ) ?. scratchWorkspaceRoot !== undefined ,
178+ ) ;
179+ const scratchEnvironment =
180+ scratchEnvironments . find (
181+ ( environment ) => environment . environmentId === selectedEnvironmentId ,
182+ ) ??
183+ scratchEnvironments [ 0 ] ??
184+ null ;
185+ const scratchWorkspaceRoot = scratchEnvironment
186+ ? ( serverConfigs . get ( scratchEnvironment . environmentId ) ?. scratchWorkspaceRoot ?? null )
187+ : null ;
188+ // Once the Scratch project exists it is an ordinary row in the list.
189+ const scratchProjectExists = projects . some (
190+ ( project ) =>
191+ project . environmentId === scratchEnvironment ?. environmentId &&
192+ isScratchProject ( project , scratchWorkspaceRoot ) ,
193+ ) ;
194+ const canStartScratch = scratchWorkspaceRoot !== null && reservedDestinationProject === null ;
195+ const scratchMachineLabel =
196+ connectedEnvironments . length > 1 ? ( scratchEnvironment ?. environmentLabel ?? null ) : null ;
197+ const startScratchLabel = scratchMachineLabel
198+ ? `Start without a project on ${ scratchMachineLabel } `
199+ : "Start without a project" ;
200+ const scratchRowSubtitle = scratchMachineLabel
201+ ? `On ${ scratchMachineLabel } `
202+ : "Start a task without a project" ;
203+ const scratchStartInFlightRef = useRef ( false ) ;
159204
160205 async function selectProject ( project : EnvironmentProject ) : Promise < void > {
161206 if ( incomingShare ?. destination && ! reservedDestinationProject ) {
@@ -189,6 +234,36 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
189234 ) ;
190235 }
191236
237+ async function startScratch ( ) : Promise < void > {
238+ if ( ! scratchEnvironment || scratchStartInFlightRef . current ) return ;
239+ const environmentId = scratchEnvironment . environmentId ;
240+ scratchStartInFlightRef . current = true ;
241+ try {
242+ const result = await ensureScratch ( { environmentId, input : { } } ) ;
243+ if ( AsyncResult . isFailure ( result ) ) {
244+ const error = Cause . squash ( result . cause ) ;
245+ Alert . alert (
246+ "Could not start without a project" ,
247+ error instanceof Error
248+ ? error . message
249+ : "The folder for threads without a project could not be created." ,
250+ ) ;
251+ return ;
252+ }
253+ const project = await waitForProject ( { environmentId, projectId : result . value . projectId } ) ;
254+ if ( project === null ) {
255+ Alert . alert (
256+ "Could not start without a project" ,
257+ "It has not reached this device yet. Pick No project from the list once it appears." ,
258+ ) ;
259+ return ;
260+ }
261+ await selectProject ( project ) ;
262+ } finally {
263+ scratchStartInFlightRef . current = false ;
264+ }
265+ }
266+
192267 useEffect ( ( ) => {
193268 const destination = incomingShare ?. destination ;
194269 if ( ! destination ) {
@@ -264,15 +339,24 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
264339 { projectEmptyState . detail }
265340 </ Text >
266341 { Platform . OS === "android" ? (
267- < MaterialButton
268- label = { catalogState . hasReadyEnvironment ? "Add new project" : "Add environment" }
269- tone = "primary"
270- onPress = { ( ) =>
271- catalogState . hasReadyEnvironment
272- ? navigation . dispatch ( StackActions . push ( "AddProject" ) )
273- : navigation . navigate ( "ConnectionsNew" )
274- }
275- />
342+ < >
343+ < MaterialButton
344+ label = { catalogState . hasReadyEnvironment ? "Add new project" : "Add environment" }
345+ tone = "primary"
346+ onPress = { ( ) =>
347+ catalogState . hasReadyEnvironment
348+ ? navigation . dispatch ( StackActions . push ( "AddProject" ) )
349+ : navigation . navigate ( "ConnectionsNew" )
350+ }
351+ />
352+ { canStartScratch ? (
353+ < MaterialButton
354+ label = { startScratchLabel }
355+ tone = "secondary"
356+ onPress = { ( ) => void startScratch ( ) }
357+ />
358+ ) : null }
359+ </ >
276360 ) : ! catalogState . hasReadyEnvironment ? (
277361 < Pressable
278362 className = "mt-1 rounded-full bg-primary px-4 py-2.5 active:opacity-70"
@@ -283,14 +367,26 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
283367 </ Text >
284368 </ Pressable >
285369 ) : (
286- < Pressable
287- className = "mt-1 rounded-full bg-primary px-4 py-2.5 active:opacity-70"
288- onPress = { ( ) => navigation . dispatch ( StackActions . push ( "AddProject" ) ) }
289- >
290- < Text className = "text-sm font-t3-bold text-primary-foreground" >
291- Add new project
292- </ Text >
293- </ Pressable >
370+ < >
371+ < Pressable
372+ className = "mt-1 rounded-full bg-primary px-4 py-2.5 active:opacity-70"
373+ onPress = { ( ) => navigation . dispatch ( StackActions . push ( "AddProject" ) ) }
374+ >
375+ < Text className = "text-sm font-t3-bold text-primary-foreground" >
376+ Add new project
377+ </ Text >
378+ </ Pressable >
379+ { canStartScratch ? (
380+ < Pressable
381+ className = "rounded-full bg-subtle px-4 py-2.5 active:opacity-70"
382+ onPress = { ( ) => void startScratch ( ) }
383+ >
384+ < Text className = "text-sm font-t3-bold text-foreground" >
385+ { startScratchLabel }
386+ </ Text >
387+ </ Pressable >
388+ ) : null }
389+ </ >
294390 ) }
295391 </ View >
296392 ) : visibleScopes . length === 0 ? (
@@ -390,6 +486,55 @@ export function NewTaskRouteScreen({ route }: StaticScreenProps<NewTaskRoutePara
390486 } ) }
391487 </ View >
392488 ) }
489+ { canStartScratch && ! scratchProjectExists && projectScopes . length > 0 ? (
490+ Platform . OS === "android" ? (
491+ < View collapsable = { false } className = "overflow-hidden rounded-[28px] bg-card" >
492+ < MaterialListRow
493+ title = "No project"
494+ subtitle = { scratchRowSubtitle }
495+ onPress = { ( ) => void startScratch ( ) }
496+ leading = {
497+ < SymbolView
498+ name = "text.bubble"
499+ size = { 22 }
500+ tintColorClassName = "accent-icon-muted"
501+ type = "monochrome"
502+ />
503+ }
504+ />
505+ </ View >
506+ ) : (
507+ < View collapsable = { false } className = "overflow-hidden rounded-[24px] bg-card" >
508+ < Pressable
509+ accessibilityRole = "button"
510+ accessibilityLabel = "No project"
511+ onPress = { ( ) => void startScratch ( ) }
512+ className = "flex-row items-center gap-3 bg-card px-4 py-3.5"
513+ >
514+ < View className = "h-7 w-7 items-center justify-center" >
515+ < SymbolView
516+ name = "text.bubble"
517+ size = { 18 }
518+ tintColorClassName = "accent-icon-muted"
519+ type = "monochrome"
520+ />
521+ </ View >
522+ < View className = "min-w-0 flex-1" >
523+ < Text className = "text-base font-t3-bold leading-snug" > No project</ Text >
524+ < Text className = "text-xs leading-snug text-foreground-muted" numberOfLines = { 1 } >
525+ { scratchRowSubtitle }
526+ </ Text >
527+ </ View >
528+ < SymbolView
529+ name = "chevron.right"
530+ size = { 14 }
531+ tintColorClassName = "accent-chevron"
532+ type = "monochrome"
533+ />
534+ </ Pressable >
535+ </ View >
536+ )
537+ ) : null }
393538 </ ScrollView >
394539 </ MaterialScreenContent >
395540 </ View >
0 commit comments