|
1 | 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | +import * as Cause from "effect/Cause"; |
| 3 | +import { AsyncResult } from "effect/unstable/reactivity"; |
2 | 4 | import { clampFileAttachmentUploadBytes } from "@t3tools/client-runtime/state/attachments"; |
3 | 5 | import { |
4 | 6 | nextPastedTextFileName, |
@@ -103,6 +105,11 @@ import { |
103 | 105 | waitForComposerDraftsLoaded, |
104 | 106 | } from "../../state/use-composer-drafts"; |
105 | 107 | import { useEnvironmentServerConfig, useProjects } from "../../state/entities"; |
| 108 | +import { useProjectClone } from "../../state/projectClones"; |
| 109 | +import { projectEnvironment } from "../../state/projects"; |
| 110 | +import { sourceControlEnvironment } from "../../state/sourceControl"; |
| 111 | +import { useAtomCommand } from "../../state/use-atom-command"; |
| 112 | +import { ProjectCloneBanner } from "../../components/ProjectCloneBanner"; |
106 | 113 | import { |
107 | 114 | isModelSelectionUnavailable, |
108 | 115 | resolveSelectableModelSelection, |
@@ -165,6 +172,8 @@ export function NewTaskDraftScreen(props: { |
165 | 172 | readonly projectId?: string; |
166 | 173 | readonly branch?: string | null; |
167 | 174 | readonly worktreePath?: string | null; |
| 175 | + /** The project was just added by a clone that is still running. */ |
| 176 | + readonly cloning?: boolean; |
168 | 177 | }; |
169 | 178 | /** Queued outbox message id when editing an existing pending task. */ |
170 | 179 | readonly pendingTaskId?: string; |
@@ -198,6 +207,70 @@ export function NewTaskDraftScreen(props: { |
198 | 207 | (environment) => environment.environmentId === selectedProject.environmentId, |
199 | 208 | )?.connectionState === "connected"; |
200 | 209 | const modelUnavailable = environmentConnected && flow.selectedModelOption?.isUnavailable === true; |
| 210 | + // A project added by cloning exists before its files do: the prompt can be |
| 211 | + // written meanwhile, but Start waits for the clone. |
| 212 | + const projectCloneState = useProjectClone( |
| 213 | + selectedProject |
| 214 | + ? { environmentId: selectedProject.environmentId, projectId: selectedProject.id } |
| 215 | + : null, |
| 216 | + ); |
| 217 | + const projectClone = projectCloneState === "pending" ? null : projectCloneState; |
| 218 | + // Before the clone stream delivers, only the project this draft was opened |
| 219 | + // for by Add Project is known to be cloning; any other project (offline |
| 220 | + // included) must still be able to queue a task. |
| 221 | + const awaitingKnownClone = |
| 222 | + projectCloneState === "pending" && |
| 223 | + props.initialProjectRef?.cloning === true && |
| 224 | + selectedProject !== null && |
| 225 | + selectedProject.id === props.initialProjectRef.projectId && |
| 226 | + selectedProject.environmentId === props.initialProjectRef.environmentId; |
| 227 | + const cloneBlocksStart = |
| 228 | + awaitingKnownClone || (projectClone !== null && projectClone.phase !== "done"); |
| 229 | + const cancelProjectClone = useAtomCommand(sourceControlEnvironment.cancelProjectClone, { |
| 230 | + reportFailure: false, |
| 231 | + }); |
| 232 | + const retryProjectClone = useAtomCommand(sourceControlEnvironment.retryProjectClone, { |
| 233 | + reportFailure: false, |
| 234 | + }); |
| 235 | + // The banner only reflects the server's state, so a request that never got |
| 236 | + // there needs its own feedback. |
| 237 | + const runCloneAction = async ( |
| 238 | + title: string, |
| 239 | + action: () => Promise<AsyncResult.AsyncResult<unknown, unknown>>, |
| 240 | + ) => { |
| 241 | + const result = await action(); |
| 242 | + if (AsyncResult.isFailure(result)) { |
| 243 | + const error = Cause.squash(result.cause); |
| 244 | + Alert.alert(title, error instanceof Error ? error.message : "An error occurred."); |
| 245 | + } |
| 246 | + }; |
| 247 | + const deleteProject = useAtomCommand(projectEnvironment.delete, { reportFailure: false }); |
| 248 | + // The delete is awaited; by then the picker may point somewhere else, and |
| 249 | + // only the removed project's draft should leave the screen. |
| 250 | + const selectedProjectRef = useRef(selectedProject); |
| 251 | + useEffect(() => { |
| 252 | + selectedProjectRef.current = selectedProject; |
| 253 | + }, [selectedProject]); |
| 254 | + const removeClonedProject = async () => { |
| 255 | + if (!selectedProject) return; |
| 256 | + const removed = selectedProject; |
| 257 | + const result = await deleteProject({ |
| 258 | + environmentId: removed.environmentId, |
| 259 | + input: { projectId: removed.id }, |
| 260 | + }); |
| 261 | + if (AsyncResult.isFailure(result)) { |
| 262 | + const error = Cause.squash(result.cause); |
| 263 | + Alert.alert( |
| 264 | + "Failed to remove project", |
| 265 | + error instanceof Error ? error.message : "An error occurred.", |
| 266 | + ); |
| 267 | + return; |
| 268 | + } |
| 269 | + const current = selectedProjectRef.current; |
| 270 | + if (current?.id === removed.id && current.environmentId === removed.environmentId) { |
| 271 | + navigation.dispatch(StackActions.replace("Home")); |
| 272 | + } |
| 273 | + }; |
201 | 274 | const uploadStates = useAtomValue(composerAttachmentUploadsAtom); |
202 | 275 | const attachmentBlockReason = selectedProject |
203 | 276 | ? composerAttachmentUploadBlockReason({ |
@@ -1261,6 +1334,7 @@ export function NewTaskDraftScreen(props: { |
1261 | 1334 | const isAndroid = Platform.OS === "android"; |
1262 | 1335 | const canStart = |
1263 | 1336 | !isImportingContext && |
| 1337 | + !cloneBlocksStart && |
1264 | 1338 | attachmentBlockReason === null && |
1265 | 1339 | !modelUnavailable && |
1266 | 1340 | Boolean(flow.selectedProject) && |
@@ -1477,6 +1551,32 @@ export function NewTaskDraftScreen(props: { |
1477 | 1551 | /> |
1478 | 1552 | </View> |
1479 | 1553 | ) : null} |
| 1554 | + {/* Above the workspace controls so they keep their place relative to |
| 1555 | + the composer when the banner goes away once the clone lands. */} |
| 1556 | + {projectClone && projectClone.phase !== "done" && selectedProject ? ( |
| 1557 | + <View className="px-1 pb-2"> |
| 1558 | + <ProjectCloneBanner |
| 1559 | + clone={projectClone} |
| 1560 | + onCancel={() => |
| 1561 | + void runCloneAction("Failed to cancel clone", () => |
| 1562 | + cancelProjectClone({ |
| 1563 | + environmentId: selectedProject.environmentId, |
| 1564 | + input: { projectId: selectedProject.id }, |
| 1565 | + }), |
| 1566 | + ) |
| 1567 | + } |
| 1568 | + onRetry={() => |
| 1569 | + void runCloneAction("Failed to retry clone", () => |
| 1570 | + retryProjectClone({ |
| 1571 | + environmentId: selectedProject.environmentId, |
| 1572 | + input: { projectId: selectedProject.id }, |
| 1573 | + }), |
| 1574 | + ) |
| 1575 | + } |
| 1576 | + onRemove={() => void removeClonedProject()} |
| 1577 | + /> |
| 1578 | + </View> |
| 1579 | + ) : null} |
1480 | 1580 | <View className="pb-1">{workspaceControls}</View> |
1481 | 1581 |
|
1482 | 1582 | {modelUnavailable ? ( |
@@ -1618,15 +1718,19 @@ export function NewTaskDraftScreen(props: { |
1618 | 1718 | <ComposerActionButton |
1619 | 1719 | accessibilityLabel={ |
1620 | 1720 | attachmentBlockReason ?? |
1621 | | - (pendingPastedTextAttachmentCount > 0 |
1622 | | - ? "Attaching pasted text" |
1623 | | - : flow.submitting |
1624 | | - ? "Starting task" |
1625 | | - : attachmentsUploading |
1626 | | - ? "Queue task, sends when uploads finish" |
1627 | | - : environmentConnected |
1628 | | - ? "Start task" |
1629 | | - : "Queue task") |
| 1721 | + (cloneBlocksStart |
| 1722 | + ? projectClone === null || projectClone.phase === "running" |
| 1723 | + ? "Cloning repository" |
| 1724 | + : "Repository not cloned" |
| 1725 | + : pendingPastedTextAttachmentCount > 0 |
| 1726 | + ? "Attaching pasted text" |
| 1727 | + : flow.submitting |
| 1728 | + ? "Starting task" |
| 1729 | + : attachmentsUploading |
| 1730 | + ? "Queue task, sends when uploads finish" |
| 1731 | + : environmentConnected |
| 1732 | + ? "Start task" |
| 1733 | + : "Queue task") |
1630 | 1734 | } |
1631 | 1735 | disabled={!canStart} |
1632 | 1736 | icon={queuesInsteadOfStarting ? "tray.and.arrow.up" : "arrow.up"} |
|
0 commit comments