The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { proposedPlanTitle, stripDisplayedPlanMarkdown } from "@t3tools/shared/proposedPlanText"; |
| 2 | import { msg, T, useGT, useMessages, Var } from "gt-react"; |
| 3 | import { memo, useState, useId } from "react"; |
| 4 | import { findRevealRef } from "./markdownFindContext"; |
| 5 | import { |
| 6 | isAtomCommandInterrupted, |
| 7 | squashAtomCommandFailure, |
| 8 | } from "@t3tools/client-runtime/state/runtime"; |
| 9 | import { |
| 10 | AuthFilesystemWriteScope, |
| 11 | type EnvironmentId, |
| 12 | type ScopedThreadRef, |
| 13 | } from "@t3tools/contracts"; |
| 14 | import { |
| 15 | buildCollapsedProposedPlanPreviewMarkdown, |
| 16 | buildProposedPlanMarkdownFilename, |
| 17 | downloadPlanAsTextFile, |
| 18 | normalizePlanMarkdownForExport, |
| 19 | } from "../../proposedPlan"; |
| 20 | import ChatMarkdown from "../ChatMarkdown"; |
| 21 | import { EllipsisIcon } from "lucide-react"; |
| 22 | import { Button } from "../ui/button"; |
| 23 | import { Input } from "../ui/input"; |
| 24 | import { Menu, MenuItem, MenuPopup, MenuTrigger } from "../ui/menu"; |
| 25 | import { cn } from "~/lib/utils"; |
| 26 | import { Badge } from "../ui/badge"; |
| 27 | import { |
| 28 | Dialog, |
| 29 | DialogDescription, |
| 30 | DialogFooter, |
| 31 | DialogHeader, |
| 32 | DialogPanel, |
| 33 | DialogPopup, |
| 34 | DialogTitle, |
| 35 | } from "../ui/dialog"; |
| 36 | import { stackedThreadToast, toastManager } from "../ui/toast"; |
| 37 | import { projectEnvironment } from "~/state/projects"; |
| 38 | import { useCopyToClipboard } from "~/hooks/useCopyToClipboard"; |
| 39 | import { useAtomCommand } from "~/state/use-atom-command"; |
| 40 | import { readEnvironmentScope, useEnvironmentScope } from "~/state/session"; |
| 41 | |
| 42 | export const ProposedPlanCard = memo(function ProposedPlanCard({ |
| 43 | planMarkdown, |
| 44 | environmentId, |
| 45 | threadRef, |
| 46 | cwd, |
| 47 | workspaceRoot, |
| 48 | findActive = false, |
| 49 | }: { |
| 50 | planMarkdown: string; |
| 51 | environmentId: EnvironmentId; |
| 52 | threadRef?: ScopedThreadRef | undefined; |
| 53 | cwd: string | undefined; |
| 54 | workspaceRoot: string | undefined; |
| 55 | findActive?: boolean; |
| 56 | }) { |
| 57 | const gt = useGT(); |
| 58 | const messages = useMessages(); |
| 59 | const [expanded, setExpanded] = useState(false); |
| 60 | const canWriteFiles = useEnvironmentScope(environmentId, AuthFilesystemWriteScope); |
| 61 | const [isSaveDialogOpen, setIsSaveDialogOpen] = useState(false); |
| 62 | const [lastSavePath, setLastSavePath] = useState(""); |
| 63 | const [isSavingToWorkspace, setIsSavingToWorkspace] = useState(false); |
| 64 | const writeProjectFile = useAtomCommand(projectEnvironment.writeFile, { |
| 65 | reportFailure: false, |
| 66 | }); |
| 67 | const { copyToClipboard, isCopied } = useCopyToClipboard({ |
| 68 | target: { |
| 69 | target: "plan", |
| 70 | unavailableMessage: msg("Clipboard API is unavailable while copying plan."), |
| 71 | failureMessage: msg("Failed to copy plan to the clipboard."), |
| 72 | }, |
| 73 | onError: (error) => { |
| 74 | toastManager.add( |
| 75 | stackedThreadToast({ |
| 76 | type: "error", |
| 77 | title: gt("Could not copy plan"), |
| 78 | description: |
| 79 | error instanceof Error ? error.message : gt("An error occurred while copying."), |
| 80 | }), |
| 81 | ); |
| 82 | }, |
| 83 | }); |
| 84 | const title = proposedPlanTitle(planMarkdown) ?? gt("Proposed plan"); |
| 85 | const lineCount = planMarkdown.split("\n").length; |
| 86 | const canCollapse = planMarkdown.length > 900 || lineCount > 20; |
| 87 | const displayedPlanMarkdown = stripDisplayedPlanMarkdown(planMarkdown); |
| 88 | const collapsedPreview = canCollapse |
| 89 | ? buildCollapsedProposedPlanPreviewMarkdown(planMarkdown, { maxLines: 10 }, messages) |
| 90 | : null; |
| 91 | const isCollapsed = canCollapse && !expanded; |
| 92 | // Mount clipped plans during find to count hidden matches without revealing them. |
| 93 | const showPreview = isCollapsed && !findActive; |
| 94 | const revealRef = findRevealRef(() => setExpanded(true)); |
| 95 | const downloadFilename = buildProposedPlanMarkdownFilename(planMarkdown); |
| 96 | const saveContents = normalizePlanMarkdownForExport(planMarkdown); |
| 97 | |
| 98 | const handleDownload = () => { |
| 99 | downloadPlanAsTextFile(downloadFilename, saveContents); |
| 100 | }; |
| 101 | |
| 102 | const handleCopyPlan = () => { |
| 103 | copyToClipboard(saveContents); |
| 104 | }; |
| 105 | |
| 106 | const openSaveDialog = () => { |
| 107 | if (!canWriteFiles) return; |
| 108 | if (!workspaceRoot) { |
| 109 | toastManager.add( |
| 110 | stackedThreadToast({ |
| 111 | type: "error", |
| 112 | title: gt("Workspace path is unavailable"), |
| 113 | description: gt("This thread does not have a workspace path to save into."), |
| 114 | }), |
| 115 | ); |
| 116 | return; |
| 117 | } |
| 118 | setIsSaveDialogOpen(true); |
| 119 | }; |
| 120 | |
| 121 | const handleSaveToWorkspace = (savePath: string) => { |
| 122 | const relativePath = savePath.trim(); |
| 123 | if (!workspaceRoot || !readEnvironmentScope(environmentId, AuthFilesystemWriteScope)) { |
| 124 | return; |
| 125 | } |
| 126 | if (!relativePath) { |
| 127 | toastManager.add({ |
| 128 | type: "warning", |
| 129 | title: gt("Enter a workspace path"), |
| 130 | }); |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | setLastSavePath(savePath); |
| 135 | setIsSavingToWorkspace(true); |
| 136 | void (async () => { |
| 137 | const result = await writeProjectFile({ |
| 138 | environmentId, |
| 139 | input: { |
| 140 | cwd: workspaceRoot, |
| 141 | relativePath, |
| 142 | contents: saveContents, |
| 143 | }, |
| 144 | }); |
| 145 | setIsSavingToWorkspace(false); |
| 146 | if (result._tag === "Success") { |
| 147 | setIsSaveDialogOpen(false); |
| 148 | toastManager.add({ |
| 149 | type: "success", |
| 150 | title: gt("Plan saved to workspace"), |
| 151 | description: result.value.relativePath, |
| 152 | }); |
| 153 | return; |
| 154 | } |
| 155 | if (!isAtomCommandInterrupted(result)) { |
| 156 | const error = squashAtomCommandFailure(result); |
| 157 | toastManager.add( |
| 158 | stackedThreadToast({ |
| 159 | type: "error", |
| 160 | title: gt("Could not save plan"), |
| 161 | description: |
| 162 | error instanceof Error ? error.message : gt("An error occurred while saving."), |
| 163 | }), |
| 164 | ); |
| 165 | } |
| 166 | })(); |
| 167 | }; |
| 168 | |
| 169 | return ( |
| 170 | <div className="rounded-2xl border border-border/80 bg-card/70 p-4 sm:p-5"> |
| 171 | <div className="flex flex-wrap items-center justify-between gap-3"> |
| 172 | <div className="flex min-w-0 items-center gap-2"> |
| 173 | <Badge variant="secondary">{gt("Plan", { $context: "badge, noun" })}</Badge> |
| 174 | {} |
| 175 | <h3 data-thread-find-text="true" className="truncate text-sm font-medium text-foreground"> |
| 176 | {title} |
| 177 | </h3> |
| 178 | </div> |
| 179 | <Menu> |
| 180 | <MenuTrigger |
| 181 | render={<Button aria-label={gt("Plan actions")} size="icon-xs" variant="outline" />} |
| 182 | > |
| 183 | <EllipsisIcon aria-hidden="true" className="size-4" /> |
| 184 | </MenuTrigger> |
| 185 | <MenuPopup align="end"> |
| 186 | <MenuItem onClick={handleCopyPlan}> |
| 187 | {isCopied ? gt("Copied!") : gt("Copy to clipboard")} |
| 188 | </MenuItem> |
| 189 | <MenuItem onClick={handleDownload}> |
| 190 | <T>Download as markdown</T> |
| 191 | </MenuItem> |
| 192 | <MenuItem |
| 193 | onClick={openSaveDialog} |
| 194 | disabled={!canWriteFiles || !workspaceRoot || isSavingToWorkspace} |
| 195 | > |
| 196 | <T>Save to workspace</T> |
| 197 | </MenuItem> |
| 198 | </MenuPopup> |
| 199 | </Menu> |
| 200 | </div> |
| 201 | <div className="mt-4"> |
| 202 | <div |
| 203 | ref={revealRef} |
| 204 | className={cn("relative", isCollapsed && "max-h-104 overflow-hidden")} |
| 205 | data-thread-find-text="true" |
| 206 | data-thread-find-fold={isCollapsed ? "" : undefined} |
| 207 | > |
| 208 | {showPreview ? ( |
| 209 | <ChatMarkdown |
| 210 | text={collapsedPreview ?? ""} |
| 211 | cwd={cwd} |
| 212 | environmentId={environmentId} |
| 213 | threadRef={threadRef} |
| 214 | isStreaming={false} |
| 215 | headingLevelOffset={3} |
| 216 | /> |
| 217 | ) : ( |
| 218 | <ChatMarkdown |
| 219 | text={displayedPlanMarkdown} |
| 220 | cwd={cwd} |
| 221 | environmentId={environmentId} |
| 222 | threadRef={threadRef} |
| 223 | isStreaming={false} |
| 224 | headingLevelOffset={3} |
| 225 | /> |
| 226 | )} |
| 227 | {isCollapsed ? ( |
| 228 | <div className="pointer-events-none absolute inset-x-0 bottom-0 h-24 bg-linear-to-t from-card/95 via-card/80 to-transparent" /> |
| 229 | ) : null} |
| 230 | </div> |
| 231 | {canCollapse ? ( |
| 232 | <div className="mt-4 flex justify-center"> |
| 233 | <Button |
| 234 | size="sm" |
| 235 | variant="outline" |
| 236 | data-scroll-anchor-ignore |
| 237 | onClick={() => setExpanded((value) => !value)} |
| 238 | > |
| 239 | {expanded ? gt("Collapse plan") : gt("Expand plan")} |
| 240 | </Button> |
| 241 | </div> |
| 242 | ) : null} |
| 243 | </div> |
| 244 | |
| 245 | <Dialog |
| 246 | open={isSaveDialogOpen} |
| 247 | onOpenChange={(open) => { |
| 248 | if (!isSavingToWorkspace) { |
| 249 | setIsSaveDialogOpen(open); |
| 250 | } |
| 251 | }} |
| 252 | > |
| 253 | <DialogPopup className="max-w-xl"> |
| 254 | <SavePlanToWorkspaceForm |
| 255 | initialPath={lastSavePath || downloadFilename} |
| 256 | placeholder={downloadFilename} |
| 257 | workspaceRoot={workspaceRoot} |
| 258 | canWriteFiles={canWriteFiles} |
| 259 | saving={isSavingToWorkspace} |
| 260 | onCancel={() => setIsSaveDialogOpen(false)} |
| 261 | onSave={handleSaveToWorkspace} |
| 262 | /> |
| 263 | </DialogPopup> |
| 264 | </Dialog> |
| 265 | </div> |
| 266 | ); |
| 267 | }); |
| 268 | |
| 269 | function SavePlanToWorkspaceForm({ |
| 270 | initialPath, |
| 271 | placeholder, |
| 272 | workspaceRoot, |
| 273 | canWriteFiles, |
| 274 | saving, |
| 275 | onCancel, |
| 276 | onSave, |
| 277 | }: { |
| 278 | initialPath: string; |
| 279 | placeholder: string; |
| 280 | workspaceRoot: string | undefined; |
| 281 | canWriteFiles: boolean; |
| 282 | saving: boolean; |
| 283 | onCancel: () => void; |
| 284 | onSave: (path: string) => void; |
| 285 | }) { |
| 286 | const gt = useGT(); |
| 287 | const [savePath, setSavePath] = useState(initialPath); |
| 288 | const savePathInputId = useId(); |
| 289 | return ( |
| 290 | <> |
| 291 | <DialogHeader> |
| 292 | <DialogTitle> |
| 293 | <T>Save plan to workspace</T> |
| 294 | </DialogTitle> |
| 295 | <DialogDescription> |
| 296 | {workspaceRoot !== undefined ? ( |
| 297 | <T> |
| 298 | Enter a path relative to{" "} |
| 299 | <code> |
| 300 | <Var>{workspaceRoot}</Var> |
| 301 | </code> |
| 302 | . |
| 303 | </T> |
| 304 | ) : ( |
| 305 | <T> |
| 306 | Enter a path relative to <code>the workspace</code>. |
| 307 | </T> |
| 308 | )} |
| 309 | </DialogDescription> |
| 310 | </DialogHeader> |
| 311 | <DialogPanel> |
| 312 | <label htmlFor={savePathInputId} className="grid gap-1.5"> |
| 313 | <span className="text-xs font-medium text-foreground"> |
| 314 | <T>Workspace path</T> |
| 315 | </span> |
| 316 | <Input |
| 317 | id={savePathInputId} |
| 318 | value={savePath} |
| 319 | onChange={(event) => setSavePath(event.target.value)} |
| 320 | placeholder={placeholder} |
| 321 | spellCheck={false} |
| 322 | disabled={saving} |
| 323 | /> |
| 324 | </label> |
| 325 | </DialogPanel> |
| 326 | <DialogFooter> |
| 327 | <Button variant="outline" size="sm" onClick={onCancel} disabled={saving}> |
| 328 | <T>Cancel</T> |
| 329 | </Button> |
| 330 | <Button size="sm" onClick={() => onSave(savePath)} disabled={!canWriteFiles || saving}> |
| 331 | {saving ? gt("Saving...") : gt("Save")} |
| 332 | </Button> |
| 333 | </DialogFooter> |
| 334 | </> |
| 335 | ); |
| 336 | } |