From 9531885405544c27bd8374c266d3d6edd2e39b0a Mon Sep 17 00:00:00 2001 From: Theo Browne Date: Sat, 4 Jul 2026 04:16:31 -0700 Subject: [PATCH] Pull uncommitted work: new-task draft flow changes (worktree 4673f128) Co-Authored-By: Claude Fable 5 --- .../features/threads/NewTaskDraftScreen.tsx | 26 ++++-- .../threads/new-task-flow-provider.tsx | 88 +++++++++++-------- 2 files changed, 67 insertions(+), 47 deletions(-) diff --git a/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx b/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx index e9043f6447..fd0e37988e 100644 --- a/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx +++ b/apps/mobile/src/features/threads/NewTaskDraftScreen.tsx @@ -66,28 +66,36 @@ export function NewTaskDraftScreen(props: { const { logicalProjects, selectedProject, setProject } = flow; const promptInputRef = useRef(null); const loadedBranchesProjectKeyRef = useRef(null); + const appliedInitialRefKeyRef = useRef(null); const borderColor = useThemeColor("--color-border"); const sheetFadeOpaque = colorScheme === "dark" ? "rgba(14,14,14,0.98)" : "rgba(242,242,247,0.98)"; const sheetFadeTransparent = colorScheme === "dark" ? "rgba(14,14,14,0)" : "rgba(242,242,247,0)"; useEffect(() => { - if (props.initialProjectRef?.environmentId && props.initialProjectRef?.projectId) { + const initialEnvironmentId = props.initialProjectRef?.environmentId; + const initialProjectId = props.initialProjectRef?.projectId; + if (initialEnvironmentId && initialProjectId) { const directProject = projects.find( (project) => - project.environmentId === props.initialProjectRef?.environmentId && - project.id === props.initialProjectRef?.projectId, + project.environmentId === initialEnvironmentId && project.id === initialProjectId, ) ?? null; if (directProject) { - if ( - selectedProject?.environmentId === directProject.environmentId && - selectedProject.id === directProject.id - ) { - return; + // Honor the route's project ref only once. Re-applying it on every + // selection change would immediately revert a manual computer switch, + // silently snapping back to the machine the draft was opened from. + const initialKey = `${initialEnvironmentId}:${initialProjectId}`; + if (appliedInitialRefKeyRef.current !== initialKey) { + appliedInitialRefKeyRef.current = initialKey; + if ( + selectedProject?.environmentId !== directProject.environmentId || + selectedProject.id !== directProject.id + ) { + setProject(directProject); + } } - setProject(directProject); return; } } diff --git a/apps/mobile/src/features/threads/new-task-flow-provider.tsx b/apps/mobile/src/features/threads/new-task-flow-provider.tsx index 9a8dde3429..47862f4519 100644 --- a/apps/mobile/src/features/threads/new-task-flow-provider.tsx +++ b/apps/mobile/src/features/threads/new-task-flow-provider.tsx @@ -171,40 +171,6 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) { setExpandedProvider(null); }, []); - const environments = useMemo( - () => - pipe( - [ - ...new Set( - pipe( - projects, - Arr.map((project) => project.environmentId), - ), - ), - ], - Arr.map((environmentId) => { - const environment = savedConnectionsById[environmentId]; - if (!environment) { - return null; - } - - return { - environmentId, - environmentLabel: environment.environmentLabel, - }; - }), - Arr.filter( - ( - entry, - ): entry is { - readonly environmentId: EnvironmentId; - readonly environmentLabel: string; - } => entry !== null, - ), - ), - [projects, savedConnectionsById], - ); - const projectsForEnvironment = useMemo( () => pipe( @@ -220,6 +186,40 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) { ) ?? projectsForEnvironment[0] ?? null; + + // Only offer machines that actually host the currently selected repository, so + // switching computers moves the same repo across machines instead of jumping to + // whatever unrelated project happens to be first on the other machine. + const selectedRepositoryKey = selectedProject?.repositoryIdentity?.canonicalKey ?? null; + const environments = useMemo(() => { + const seen = new Set(); + const result: Array<{ + readonly environmentId: EnvironmentId; + readonly environmentLabel: string; + }> = []; + for (const project of projects) { + if ( + selectedRepositoryKey !== null && + (project.repositoryIdentity?.canonicalKey ?? null) !== selectedRepositoryKey + ) { + continue; + } + if (seen.has(project.environmentId)) { + continue; + } + const environment = savedConnectionsById[project.environmentId]; + if (!environment) { + continue; + } + seen.add(project.environmentId); + result.push({ + environmentId: project.environmentId, + environmentLabel: environment.environmentLabel, + }); + } + return result; + }, [projects, savedConnectionsById, selectedRepositoryKey]); + const selectedEnvironmentServerConfig = useEnvironmentServerConfig( selectedProject?.environmentId ?? null, ); @@ -384,10 +384,22 @@ export function NewTaskFlowProvider(props: React.PropsWithChildren) { setSelectedProjectKey(nextProjectKey); }, []); - const selectEnvironment = useCallback((environmentId: EnvironmentId) => { - setSelectedEnvironmentId(environmentId); - setSelectedProjectKey(null); - }, []); + const selectEnvironment = useCallback( + (environmentId: EnvironmentId) => { + const repositoryKey = selectedProject?.repositoryIdentity?.canonicalKey ?? null; + const match = + repositoryKey === null + ? undefined + : projects.find( + (project) => + project.environmentId === environmentId && + (project.repositoryIdentity?.canonicalKey ?? null) === repositoryKey, + ); + setSelectedEnvironmentId(environmentId); + setSelectedProjectKey(match ? scopedProjectKey(match.environmentId, match.id) : null); + }, + [projects, selectedProject?.repositoryIdentity?.canonicalKey], + ); const setWorkspaceMode = useCallback( (mode: WorkspaceMode) => {