The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useAtomValue } from "@effect/atom-react"; |
| 2 | import { errorDisplayMessage } from "@t3tools/client-runtime/errors"; |
| 3 | import { |
| 4 | isAtomCommandInterrupted, |
| 5 | squashAtomCommandFailure, |
| 6 | } from "@t3tools/client-runtime/state/runtime"; |
| 7 | import type { EnvironmentId } from "@t3tools/contracts"; |
| 8 | import { proposedPlanTitle, stripDisplayedPlanMarkdown } from "@t3tools/shared/proposedPlanText"; |
| 9 | import { useGT } from "gt-react-native"; |
| 10 | import { m } from "../../i18n/messages"; |
| 11 | import { useRef, useState, type ReactNode } from "react"; |
| 12 | import { View } from "react-native"; |
| 13 | |
| 14 | import { AppText as Text } from "../../components/ui/AppText"; |
| 15 | import { Button } from "../../components/ui/Button"; |
| 16 | import * as Dialog from "../../components/ui/Dialog"; |
| 17 | import { projectEnvironment } from "../../state/projects"; |
| 18 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 19 | import { CopyTextButton } from "./CopyTextButton"; |
| 20 | |
| 21 | export function ProposedPlanCard(props: { |
| 22 | readonly environmentId: EnvironmentId; |
| 23 | readonly workspaceRoot: string | null | undefined; |
| 24 | readonly markdown: string; |
| 25 | readonly renderMarkdown: (text: string) => ReactNode; |
| 26 | }) { |
| 27 | const gt = useGT(); |
| 28 | const canWrite = useAtomValue(projectEnvironment.writeFile.permissionAtom(props.environmentId)); |
| 29 | const writeFile = useAtomCommand(projectEnvironment.writeFile, { reportFailure: false }); |
| 30 | const inFlight = useRef(false); |
| 31 | const [saving, setSaving] = useState(false); |
| 32 | const [lastPath, setLastPath] = useState("plan.md"); |
| 33 | const contents = `${props.markdown.trimEnd()}\n`; |
| 34 | |
| 35 | const save = async (path: string) => { |
| 36 | const relativePath = path.trim(); |
| 37 | if (!canWrite || !props.workspaceRoot || inFlight.current) return; |
| 38 | if (!relativePath) { |
| 39 | Dialog.alert(gt("Enter a workspace path")); |
| 40 | return; |
| 41 | } |
| 42 | inFlight.current = true; |
| 43 | setSaving(true); |
| 44 | setLastPath(relativePath); |
| 45 | const result = await writeFile({ |
| 46 | environmentId: props.environmentId, |
| 47 | input: { cwd: props.workspaceRoot, relativePath, contents }, |
| 48 | }).finally(() => { |
| 49 | inFlight.current = false; |
| 50 | setSaving(false); |
| 51 | }); |
| 52 | if (result._tag === "Success") { |
| 53 | Dialog.alert(gt("Plan saved to workspace"), result.value.relativePath); |
| 54 | } else if (!isAtomCommandInterrupted(result)) { |
| 55 | const error = squashAtomCommandFailure(result); |
| 56 | Dialog.alert(gt("Could not save plan"), errorDisplayMessage(error, m) ?? String(error)); |
| 57 | } |
| 58 | }; |
| 59 | |
| 60 | return ( |
| 61 | <View className="mb-3 gap-3 rounded-[20px] border border-border bg-card-alt p-4"> |
| 62 | <View className="flex-row items-center gap-2"> |
| 63 | <Text accessibilityRole="header" className="flex-1 font-t3-bold text-base text-foreground"> |
| 64 | {proposedPlanTitle(props.markdown) ?? gt("Proposed plan")} |
| 65 | </Text> |
| 66 | <CopyTextButton accessibilityLabel={gt("Copy plan")} text={contents} /> |
| 67 | </View> |
| 68 | {props.renderMarkdown(stripDisplayedPlanMarkdown(props.markdown))} |
| 69 | <View className="flex-row"> |
| 70 | <Button |
| 71 | label={saving ? gt("Saving...") : gt("Save to workspace")} |
| 72 | tone="secondary" |
| 73 | disabled={!canWrite || !props.workspaceRoot || saving} |
| 74 | onPress={() => |
| 75 | Dialog.prompt({ |
| 76 | title: gt("Save plan to workspace"), |
| 77 | // The write replaces an existing file, and the RPC has no create-only mode. |
| 78 | message: gt( |
| 79 | "Enter a path relative to the thread's workspace. A file already at that path is replaced.", |
| 80 | ), |
| 81 | initialValue: lastPath, |
| 82 | confirmText: gt("Save", { $context: "verb, confirm button" }), |
| 83 | onConfirm: (path) => void save(path), |
| 84 | }) |
| 85 | } |
| 86 | /> |
| 87 | </View> |
| 88 | </View> |
| 89 | ); |
| 90 | } |