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 { thrownErrorMessage } from "@t3tools/client-runtime/errors"; |
| 2 | import { useRouter } from "@tanstack/react-router"; |
| 3 | import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime"; |
| 4 | import type { ScopedProjectRef } from "@t3tools/contracts"; |
| 5 | import { msg } from "gt-react"; |
| 6 | |
| 7 | import { useComposerDraftStore } from "../composerDraftStore"; |
| 8 | import { resolveThreadRouteTarget } from "../threadRoutes"; |
| 9 | import { releaseProjectDraftUploads } from "../lib/composerDraftUploads"; |
| 10 | import { waitForProjectRemoval } from "../state/entities"; |
| 11 | import { projectEnvironment } from "../state/projects"; |
| 12 | import { useAtomCommand } from "../state/use-atom-command"; |
| 13 | import { stackedThreadToast, toastManager } from "../components/ui/toast"; |
| 14 | import { m } from "../i18n/messages"; |
| 15 | |
| 16 | export function useRemoveClonedProject() { |
| 17 | const router = useRouter(); |
| 18 | const deleteProject = useAtomCommand(projectEnvironment.delete, { reportFailure: false }); |
| 19 | |
| 20 | return async (projectRef: ScopedProjectRef) => { |
| 21 | const draftStore = useComposerDraftStore.getState(); |
| 22 | const result = await deleteProject({ |
| 23 | environmentId: projectRef.environmentId, |
| 24 | // Leave deletion unforced so a thread created since the failed clone cannot be silently deleted. |
| 25 | input: { projectId: projectRef.projectId }, |
| 26 | }); |
| 27 | if (result._tag === "Failure") { |
| 28 | const error = squashAtomCommandFailure(result); |
| 29 | toastManager.add( |
| 30 | stackedThreadToast({ |
| 31 | type: "error", |
| 32 | title: m(msg("Failed to remove project")), |
| 33 | description: thrownErrorMessage(error, m) ?? m(msg("An error occurred.")), |
| 34 | }), |
| 35 | ); |
| 36 | return false; |
| 37 | } |
| 38 | await waitForProjectRemoval(projectRef); |
| 39 | // Re-read the route after deletion; the user may have left this project. |
| 40 | const routeParams = router.state.matches[router.state.matches.length - 1]?.params ?? {}; |
| 41 | const routeTarget = resolveThreadRouteTarget(routeParams); |
| 42 | const viewingDraft = |
| 43 | routeTarget?.kind === "draft" ? draftStore.getDraftSession(routeTarget.draftId) : null; |
| 44 | const viewingThisProject = |
| 45 | viewingDraft?.environmentId === projectRef.environmentId && |
| 46 | viewingDraft.projectId === projectRef.projectId; |
| 47 | releaseProjectDraftUploads(projectRef); |
| 48 | const projectDraft = draftStore.getDraftThreadByProjectRef(projectRef); |
| 49 | if (projectDraft) draftStore.clearDraftThread(projectDraft.draftId); |
| 50 | draftStore.clearProjectDraftThreadId(projectRef); |
| 51 | if (viewingThisProject) void router.navigate({ to: "/", replace: true }); |
| 52 | return true; |
| 53 | }; |
| 54 | } |