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