The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { T, msg, useGT, useMessages } from "gt-react"; |
| 2 | import { thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 3 | import { m } from "~/i18n/messages"; |
| 4 | import { useStableCallback } from "../../hooks/useStableCallback"; |
| 5 | import { |
| 6 | isAtomCommandInterrupted, |
| 7 | squashAtomCommandFailure, |
| 8 | type AtomCommandResult, |
| 9 | } from "@t3tools/client-runtime/state/runtime"; |
| 10 | import { DownloadIcon } from "lucide-react"; |
| 11 | import { stackedThreadToast, toastManager } from "../ui/toast"; |
| 12 | import { useRemoveClonedProject } from "../../hooks/useRemoveClonedProject"; |
| 13 | import { sourceControlEnvironment } from "../../state/sourceControl"; |
| 14 | import { useProjectClone } from "../../state/projectClones"; |
| 15 | import { projectCloneDisplayName, type ScopedProjectRef } from "@t3tools/contracts"; |
| 16 | import { projectCloneProgressSummary } from "@t3tools/client-runtime/contract-labels"; |
| 17 | import { dismissProjectFileCopy, useProjectFileCopy } from "../../lib/addProjectToMachine"; |
| 18 | import { type ComposerBannerStackItem } from "./ComposerBannerStack"; |
| 19 | import { useAtomCommand } from "../../state/use-atom-command"; |
| 20 | import { Button } from "../ui/button"; |
| 21 | /** |
| 22 | * A project still being cloned or copied into place: the composer banner with its cancel, |
| 23 | * retry, and remove actions, and the reason sending waits until it is done. |
| 24 | */ |
| 25 | export function useProjectCloneBanner(activeProjectRef: ScopedProjectRef | null) { |
| 26 | const gt = useGT(); |
| 27 | const messages = useMessages(); |
| 28 | const activeProjectClone = useProjectClone(activeProjectRef); |
| 29 | const cancelProjectClone = useAtomCommand(sourceControlEnvironment.cancelProjectClone, { |
| 30 | reportFailure: false, |
| 31 | }); |
| 32 | const retryProjectClone = useAtomCommand(sourceControlEnvironment.retryProjectClone, { |
| 33 | reportFailure: false, |
| 34 | }); |
| 35 | const removeClonedProject = useRemoveClonedProject(); |
| 36 | const runProjectCloneAction = async ( |
| 37 | title: string, |
| 38 | action: () => Promise<AtomCommandResult<unknown, unknown>>, |
| 39 | ): Promise<void> => { |
| 40 | const result = await action(); |
| 41 | if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) { |
| 42 | const error = squashAtomCommandFailure(result); |
| 43 | toastManager.add( |
| 44 | stackedThreadToast({ |
| 45 | type: "error", |
| 46 | title, |
| 47 | description: thrownErrorMessage(error, m) ?? m(msg("An error occurred.")), |
| 48 | }), |
| 49 | ); |
| 50 | } |
| 51 | }; |
| 52 | const runProjectCloneActionStable = useStableCallback(runProjectCloneAction); |
| 53 | const activeProjectFileCopy = useProjectFileCopy(activeProjectRef); |
| 54 | const projectCloneSendBlockReason = |
| 55 | activeProjectClone?.phase === "running" |
| 56 | ? gt("Cloning repository") |
| 57 | : activeProjectClone !== null && activeProjectClone.phase !== "done" |
| 58 | ? gt("Repository not cloned") |
| 59 | : activeProjectFileCopy?.phase === "copying" |
| 60 | ? gt("Copying files") |
| 61 | : null; |
| 62 | const projectCloneBannerItem = (() => { |
| 63 | if (activeProjectFileCopy?.phase === "failed" && activeProjectRef) { |
| 64 | return { |
| 65 | id: `project-file-copy:${activeProjectRef.projectId}`, |
| 66 | variant: "error", |
| 67 | icon: <DownloadIcon />, |
| 68 | title: messages(activeProjectFileCopy.title), |
| 69 | description: activeProjectFileCopy.failures.join("\n"), |
| 70 | onDismiss: () => dismissProjectFileCopy(activeProjectRef), |
| 71 | }; |
| 72 | } |
| 73 | if (!activeProjectClone || !activeProjectRef || activeProjectClone.phase === "done") { |
| 74 | return null; |
| 75 | } |
| 76 | const name = projectCloneDisplayName(activeProjectClone); |
| 77 | const { environmentId, projectId } = activeProjectRef; |
| 78 | if (activeProjectClone.phase === "running") { |
| 79 | return { |
| 80 | id: `project-clone:${projectId}`, |
| 81 | variant: "info", |
| 82 | icon: <DownloadIcon />, |
| 83 | title: gt("Cloning {name}", { name }), |
| 84 | description: projectCloneProgressSummary(activeProjectClone, messages), |
| 85 | actions: ( |
| 86 | <Button |
| 87 | size="xs" |
| 88 | variant="ghost" |
| 89 | onClick={() => |
| 90 | void runProjectCloneActionStable(m(msg("Failed to cancel clone")), () => |
| 91 | cancelProjectClone({ environmentId, input: { projectId } }), |
| 92 | ) |
| 93 | } |
| 94 | > |
| 95 | <T>Cancel</T> |
| 96 | </Button> |
| 97 | ), |
| 98 | }; |
| 99 | } |
| 100 | const cancelled = activeProjectClone.phase === "cancelled"; |
| 101 | return { |
| 102 | id: `project-clone:${projectId}`, |
| 103 | variant: cancelled ? "warning" : "error", |
| 104 | icon: <DownloadIcon />, |
| 105 | title: cancelled |
| 106 | ? gt("Cancelled cloning {name}", { name }) |
| 107 | : gt("Failed to clone {name}", { name }), |
| 108 | description: cancelled ? gt("Retry to bring in the repository.") : activeProjectClone.error, |
| 109 | actions: ( |
| 110 | <> |
| 111 | <Button |
| 112 | size="xs" |
| 113 | variant="ghost" |
| 114 | onClick={() => void removeClonedProject({ environmentId, projectId })} |
| 115 | > |
| 116 | <T>Remove project</T> |
| 117 | </Button> |
| 118 | <Button |
| 119 | size="xs" |
| 120 | variant="ghost" |
| 121 | onClick={() => |
| 122 | void runProjectCloneActionStable(m(msg("Failed to retry clone")), () => |
| 123 | retryProjectClone({ environmentId, input: { projectId } }), |
| 124 | ) |
| 125 | } |
| 126 | > |
| 127 | <T context="verb, retries the clone">Retry</T> |
| 128 | </Button> |
| 129 | </> |
| 130 | ), |
| 131 | }; |
| 132 | })() as ComposerBannerStackItem | null; |
| 133 | return { |
| 134 | projectCloneBannerItem, |
| 135 | projectCloneSendBlockReason, |
| 136 | }; |
| 137 | } |