@@ -12,7 +12,11 @@ import type { AtomRegistry } from "effect/reactivity";
1212import { useContext } from "react" ;
1313
1414import { stackedThreadToast , toastManager } from "~/components/ui/toast" ;
15- import { type EnvironmentPresentation , useEnvironments } from "~/state/environments" ;
15+ import {
16+ type EnvironmentPresentation ,
17+ useEnvironments ,
18+ usePrimaryEnvironmentId ,
19+ } from "~/state/environments" ;
1620import { projectEnvironment } from "~/state/projects" ;
1721import { type NewThreadHandler , useNewThreadHandler } from "./useHandleNewThread" ;
1822
@@ -32,6 +36,7 @@ type ScratchEnvironment = Pick<EnvironmentPresentation, "connection" | "serverCo
3236
3337export function createScratchProjectActions ( input : {
3438 readonly environments : ( ) => Iterable < ScratchEnvironment > ;
39+ readonly primaryEnvironmentId : ( ) => EnvironmentId | null ;
3540 readonly registry : AtomRegistry . AtomRegistry ;
3641 readonly handleNewThread : NewThreadHandler ;
3742} ) {
@@ -43,8 +48,11 @@ export function createScratchProjectActions(input: {
4348 return availableScratchWorkspaceRoot ( environment ?. connection . phase , environment ?. serverConfig ) ;
4449 } ;
4550
46- const scratchEnvironmentId = ( current : EnvironmentId | null ) : EnvironmentId | null => {
47- if ( current !== null ) return scratchWorkspaceRootFor ( current ) !== null ? current : null ;
51+ // The primary (local) environment wins; `fallback` only applies when it offers no scratch root.
52+ const scratchEnvironmentId = ( fallback : EnvironmentId | null ) : EnvironmentId | null => {
53+ const primary = input . primaryEnvironmentId ( ) ;
54+ if ( primary !== null && scratchWorkspaceRootFor ( primary ) !== null ) return primary ;
55+ if ( fallback !== null && scratchWorkspaceRootFor ( fallback ) !== null ) return fallback ;
4856 const offering = [ ...input . environments ( ) ] . filter (
4957 ( entry ) => scratchWorkspaceRootFor ( entry . environmentId ) !== null ,
5058 ) ;
@@ -85,10 +93,12 @@ export function createScratchProjectActions(input: {
8593
8694export function useScratchProject ( ) {
8795 const { environments } = useEnvironments ( ) ;
96+ const primaryEnvironmentId = usePrimaryEnvironmentId ( ) ;
8897 const registry = useContext ( RegistryContext ) ;
8998 const handleNewThread = useNewThreadHandler ( ) ;
9099 return createScratchProjectActions ( {
91100 environments : ( ) => environments ,
101+ primaryEnvironmentId : ( ) => primaryEnvironmentId ,
92102 registry,
93103 handleNewThread,
94104 } ) ;
0 commit comments