Skip to content
TypeScript

refactor(web): ChatView's project clone banner lives in useProjectCloneBanner (#23068)

Julius Marminge7be791c67534
diff --git a/apps/web/src/components/ChatView.tsx b/apps/web/src/components/ChatView.tsx
index 95fdfef8371..7bb4c62b850 100644
--- a/apps/web/src/components/ChatView.tsx
+++ b/apps/web/src/components/ChatView.tsx
@@ -1,3 +1,4 @@
+import { useProjectCloneBanner } from "./chat/useProjectCloneBanner";
import { useAttachmentPreviewHandoff } from "./chat/useAttachmentPreviewHandoff";
import { useTimelineRewind } from "./chat/useTimelineRewind";
import { useThreadLifecycle } from "./chat/useThreadLifecycle";
@@ -176,7 +177,7 @@ import { makeWorkspaceFileDropHandlers } from "./chat/workspaceFileDrop";
import { isEditableFocused } from "../lib/editableFocus";
import { DEFAULT_RESOLVED_KEYBINDINGS } from "@t3tools/shared/keybindings";
import { shortcutLabelForCommand } from "../keybindings";
-import { PaperclipIcon, ChevronDownIcon, DownloadIcon } from "lucide-react";
+import { PaperclipIcon, ChevronDownIcon } from "lucide-react";
import { cn } from "~/lib/utils";
import { COLLAPSED_SIDEBAR_TITLEBAR_INSET_CLASS } from "~/workspaceTitlebar";
import { stackedThreadToast, toastManager } from "./ui/toast";
@@ -198,7 +199,6 @@ import {
import { useNowMinute } from "../hooks/useNowMinute";
import { usePanelAnimationSettings, usePanelPresence } from "../panelAnimations";
import { useNewThreadHandler } from "../hooks/useHandleNewThread";
-import { useRemoveClonedProject } from "../hooks/useRemoveClonedProject";
import { useOpenPanelPullRequestUrl } from "../hooks/useOpenPanelPullRequestUrl";
import { resolveAppModelSelectionForInstance } from "../modelSelection";
import { isPreviewFocused } from "../lib/previewFocus";
@@ -235,10 +235,6 @@ import {
import { threadEnvironment } from "../state/threads";
import { resolveProviderSkillsForCwd } from "@t3tools/client-runtime/providerSkills";
import { vcsEnvironment } from "../state/vcs";
-import { sourceControlEnvironment } from "../state/sourceControl";
-import { useProjectClone } from "../state/projectClones";
-import { projectCloneDisplayName, projectCloneProgressSummary } from "@t3tools/contracts";
-import { dismissProjectFileCopy, useProjectFileCopy } from "../lib/addProjectToMachine";
import {
type EnvironmentPresentation,
useEnvironmentProviders,
@@ -1117,110 +1113,8 @@ export default function ChatView(props: ChatViewProps) {
activeProject ?? undefined,
);
const activeProjectScripts = activeProject ? resolveProjectScripts(settings, activeProject) : [];
- const activeProjectClone = useProjectClone(activeProjectRef);
- const cancelProjectClone = useAtomCommand(sourceControlEnvironment.cancelProjectClone, {
- reportFailure: false,
- });
- const retryProjectClone = useAtomCommand(sourceControlEnvironment.retryProjectClone, {
- reportFailure: false,
- });
- const removeClonedProject = useRemoveClonedProject();
- const runProjectCloneAction = async (
- title: string,
- action: () => Promise<AtomCommandResult<unknown, unknown>>,
- ): Promise<void> => {
- const result = await action();
- if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
- const error = squashAtomCommandFailure(result);
- toastManager.add(
- stackedThreadToast({
- type: "error",
- title,
- description: error instanceof Error ? error.message : "An error occurred.",
- }),
- );
- }
- };
- const runProjectCloneActionStable = useStableCallback(runProjectCloneAction);
- const activeProjectFileCopy = useProjectFileCopy(activeProjectRef);
- const projectCloneSendBlockReason =
- activeProjectClone?.phase === "running"
- ? "Cloning repository"
- : activeProjectClone !== null && activeProjectClone.phase !== "done"
- ? "Repository not cloned"
- : activeProjectFileCopy?.phase === "copying"
- ? "Copying files"
- : null;
- const projectCloneBannerItem = (() => {
- if (activeProjectFileCopy?.phase === "failed" && activeProjectRef) {
- return {
- id: `project-file-copy:${activeProjectRef.projectId}`,
- variant: "error",
- icon: <DownloadIcon />,
- title: activeProjectFileCopy.title,
- description: activeProjectFileCopy.failures.join("\n"),
- onDismiss: () => dismissProjectFileCopy(activeProjectRef),
- };
- }
- if (!activeProjectClone || !activeProjectRef || activeProjectClone.phase === "done") {
- return null;
- }
- const name = projectCloneDisplayName(activeProjectClone);
- const { environmentId, projectId } = activeProjectRef;
- if (activeProjectClone.phase === "running") {
- return {
- id: `project-clone:${projectId}`,
- variant: "info",
- priority: "activity",
- icon: <DownloadIcon />,
- title: `Cloning ${name}`,
- description: projectCloneProgressSummary(activeProjectClone),
- actions: (
- <Button
- size="xs"
- variant="ghost"
- onClick={() =>
- void runProjectCloneActionStable("Failed to cancel clone", () =>
- cancelProjectClone({ environmentId, input: { projectId } }),
- )
- }
- >
- Cancel
- </Button>
- ),
- };
- }
- const cancelled = activeProjectClone.phase === "cancelled";
- return {
- id: `project-clone:${projectId}`,
- variant: cancelled ? "warning" : "error",
- icon: <DownloadIcon />,
- title: cancelled ? `Cancelled cloning ${name}` : `Failed to clone ${name}`,
- description: cancelled ? "Retry to bring in the repository." : activeProjectClone.error,
- actions: (
- <>
- <Button
- size="xs"
- variant="ghost"
- onClick={() => void removeClonedProject({ environmentId, projectId })}
- >
- Remove project
- </Button>
- <Button
- size="xs"
- variant="ghost"
- onClick={() =>
- void runProjectCloneActionStable("Failed to retry clone", () =>
- retryProjectClone({ environmentId, input: { projectId } }),
- )
- }
- >
- Retry
- </Button>
- </>
- ),
- };
- })() as ComposerBannerStackItem | null;
+ const { projectCloneBannerItem, projectCloneSendBlockReason } =
+ useProjectCloneBanner(activeProjectRef);
const activeProjectDefaultModelSelection = activeProjectSettings.settings.defaultModelSelection;
const handleNewThreadInActiveProject = () => {
startNewThreadForProject(activeProjectRef, handleNewThread);
diff --git a/apps/web/src/components/chat/useProjectCloneBanner.tsx b/apps/web/src/components/chat/useProjectCloneBanner.tsx
new file mode 100644
index 00000000000..ab9bcfa09bf
--- /dev/null
+++ b/apps/web/src/components/chat/useProjectCloneBanner.tsx
@@ -0,0 +1,134 @@
+import { useStableCallback } from "../../hooks/useStableCallback";
+import {
+ isAtomCommandInterrupted,
+ squashAtomCommandFailure,
+ type AtomCommandResult,
+} from "@t3tools/client-runtime/state/runtime";
+import { DownloadIcon } from "lucide-react";
+import { stackedThreadToast, toastManager } from "../ui/toast";
+import { useRemoveClonedProject } from "../../hooks/useRemoveClonedProject";
+import { sourceControlEnvironment } from "../../state/sourceControl";
+import { useProjectClone } from "../../state/projectClones";
+import {
+ projectCloneDisplayName,
+ projectCloneProgressSummary,
+ type ScopedProjectRef,
+} from "@t3tools/contracts";
+import { dismissProjectFileCopy, useProjectFileCopy } from "../../lib/addProjectToMachine";
+import { type ComposerBannerStackItem } from "./ComposerBannerStack";
+import { useAtomCommand } from "../../state/use-atom-command";
+import { Button } from "../ui/button";
+/**
+ * A project still being cloned or copied into place: the composer banner with its cancel,
+ * retry, and remove actions, and the reason sending waits until it is done.
+ */
+export function useProjectCloneBanner(activeProjectRef: ScopedProjectRef | null) {
+ const activeProjectClone = useProjectClone(activeProjectRef);
+ const cancelProjectClone = useAtomCommand(sourceControlEnvironment.cancelProjectClone, {
+ reportFailure: false,
+ });
+ const retryProjectClone = useAtomCommand(sourceControlEnvironment.retryProjectClone, {
+ reportFailure: false,
+ });
+ const removeClonedProject = useRemoveClonedProject();
+ const runProjectCloneAction = async (
+ title: string,
+ action: () => Promise<AtomCommandResult<unknown, unknown>>,
+ ): Promise<void> => {
+ const result = await action();
+ if (result._tag === "Failure" && !isAtomCommandInterrupted(result)) {
+ const error = squashAtomCommandFailure(result);
+ toastManager.add(
+ stackedThreadToast({
+ type: "error",
+ title,
+ description: error instanceof Error ? error.message : "An error occurred.",
+ }),
+ );
+ }
+ };
+ const runProjectCloneActionStable = useStableCallback(runProjectCloneAction);
+ const activeProjectFileCopy = useProjectFileCopy(activeProjectRef);
+ const projectCloneSendBlockReason =
+ activeProjectClone?.phase === "running"
+ ? "Cloning repository"
+ : activeProjectClone !== null && activeProjectClone.phase !== "done"
+ ? "Repository not cloned"
+ : activeProjectFileCopy?.phase === "copying"
+ ? "Copying files"
+ : null;
+ const projectCloneBannerItem = (() => {
+ if (activeProjectFileCopy?.phase === "failed" && activeProjectRef) {
+ return {
+ id: `project-file-copy:${activeProjectRef.projectId}`,
+ variant: "error",
+ icon: <DownloadIcon />,
+ title: activeProjectFileCopy.title,
+ description: activeProjectFileCopy.failures.join("\n"),
+ onDismiss: () => dismissProjectFileCopy(activeProjectRef),
+ };
+ }
+ if (!activeProjectClone || !activeProjectRef || activeProjectClone.phase === "done") {
+ return null;
+ }
+ const name = projectCloneDisplayName(activeProjectClone);
+ const { environmentId, projectId } = activeProjectRef;
+ if (activeProjectClone.phase === "running") {
+ return {
+ id: `project-clone:${projectId}`,
+ variant: "info",
+ priority: "activity",
+ icon: <DownloadIcon />,
+ title: `Cloning ${name}`,
+ description: projectCloneProgressSummary(activeProjectClone),
+ actions: (
+ <Button
+ size="xs"
+ variant="ghost"
+ onClick={() =>
+ void runProjectCloneActionStable("Failed to cancel clone", () =>
+ cancelProjectClone({ environmentId, input: { projectId } }),
+ )
+ }
+ >
+ Cancel
+ </Button>
+ ),
+ };
+ }
+ const cancelled = activeProjectClone.phase === "cancelled";
+ return {
+ id: `project-clone:${projectId}`,
+ variant: cancelled ? "warning" : "error",
+ icon: <DownloadIcon />,
+ title: cancelled ? `Cancelled cloning ${name}` : `Failed to clone ${name}`,
+ description: cancelled ? "Retry to bring in the repository." : activeProjectClone.error,
+ actions: (
+ <>
+ <Button
+ size="xs"
+ variant="ghost"
+ onClick={() => void removeClonedProject({ environmentId, projectId })}
+ >
+ Remove project
+ </Button>
+ <Button
+ size="xs"
+ variant="ghost"
+ onClick={() =>
+ void runProjectCloneActionStable("Failed to retry clone", () =>
+ retryProjectClone({ environmentId, input: { projectId } }),
+ )
+ }
+ >
+ Retry
+ </Button>
+ </>
+ ),
+ };
+ })() as ComposerBannerStackItem | null;
+ return {
+ projectCloneBannerItem,
+ projectCloneSendBlockReason,
+ };
+}