|
| 1 | +import { useState } from "react"; |
| 2 | +import { AlertDialog, toast } from "@heroui/react"; |
| 3 | +import { buildWorktreeLocation } from "@/shared/worktree"; |
| 4 | +import { friendlyError, msg } from "@/shared/messages"; |
| 5 | +import { readBridge } from "@/renderer/bridge"; |
| 6 | +import { openGitReviewForWorktree } from "@/renderer/actions/gitActions"; |
| 7 | +import { Button } from "@/renderer/components/common/Button"; |
| 8 | +import { useAppStore } from "@/renderer/state/appStore"; |
| 9 | +import { usePullFromSourceDialogStore } from "@/renderer/state/pullFromSourceDialogStore"; |
| 10 | + |
| 11 | +export function PullFromSourceDialog() { |
| 12 | + const dialog = usePullFromSourceDialogStore((s) => s.dialog); |
| 13 | + const closeDialog = usePullFromSourceDialogStore((s) => s.closeDialog); |
| 14 | + const project = useAppStore((s) => s.projects.find((p) => p.id === dialog?.projectId)); |
| 15 | + const [isPulling, setIsPulling] = useState(false); |
| 16 | + |
| 17 | + if (!dialog || !project) return null; |
| 18 | + |
| 19 | + const activeDialog = dialog; |
| 20 | + const activeProject = project; |
| 21 | + |
| 22 | + function handleClose() { |
| 23 | + closeDialog(); |
| 24 | + } |
| 25 | + |
| 26 | + async function handleStashPullReapply() { |
| 27 | + setIsPulling(true); |
| 28 | + try { |
| 29 | + const stashPreservedMessage = msg("git.pull.stashPreserved"); |
| 30 | + const result = await readBridge().gitPullFromSource({ |
| 31 | + worktreeLocation: buildWorktreeLocation(activeProject.location, activeDialog.worktreePath), |
| 32 | + sourceBranch: activeDialog.sourceBranch, |
| 33 | + preserveLocalChanges: true, |
| 34 | + }); |
| 35 | + closeDialog(); |
| 36 | + if (result.conflicting) { |
| 37 | + const detail = result.conflictFiles?.length |
| 38 | + ? `\nConflicts:\n${result.conflictFiles.join("\n")}` |
| 39 | + : ""; |
| 40 | + const stashNote = result.stashPreserved ? `\n${stashPreservedMessage}` : ""; |
| 41 | + toast.danger((result.error ?? msg("git.merge.conflicts")) + detail + stashNote); |
| 42 | + openGitReviewForWorktree(activeDialog.projectId, activeDialog.worktreePath); |
| 43 | + activeDialog.onComplete?.(); |
| 44 | + return; |
| 45 | + } |
| 46 | + if (!result.merged) { |
| 47 | + const fallback = msg("git.pull.failed", { detail: msg("git.merge.failed") }); |
| 48 | + const message = result.error ?? fallback; |
| 49 | + const stashNote = |
| 50 | + result.stashPreserved && !message.includes(stashPreservedMessage) |
| 51 | + ? `\n${stashPreservedMessage}` |
| 52 | + : ""; |
| 53 | + toast.danger(message + stashNote); |
| 54 | + return; |
| 55 | + } |
| 56 | + activeDialog.onComplete?.(); |
| 57 | + } catch (error) { |
| 58 | + console.error("[git] stash pull from source failed", error); |
| 59 | + toast.danger(friendlyError(error)); |
| 60 | + } finally { |
| 61 | + setIsPulling(false); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + const dialogContent = ( |
| 66 | + <> |
| 67 | + <AlertDialog.Header className="gap-1"> |
| 68 | + <AlertDialog.Heading>Pull from {activeDialog.sourceBranch}?</AlertDialog.Heading> |
| 69 | + <p className="text-sm leading-5 text-muted"> |
| 70 | + This worktree has local changes. Lightcode can temporarily stash them, pull from{" "} |
| 71 | + {activeDialog.sourceBranch}, then re-apply your changes. |
| 72 | + </p> |
| 73 | + </AlertDialog.Header> |
| 74 | + <AlertDialog.Footer> |
| 75 | + <Button slot="close" variant="ghost" className="text-muted" isDisabled={isPulling}> |
| 76 | + Cancel |
| 77 | + </Button> |
| 78 | + <Button variant="tertiary" onPress={handleStashPullReapply} isPending={isPulling}> |
| 79 | + Stash & Pull |
| 80 | + </Button> |
| 81 | + </AlertDialog.Footer> |
| 82 | + </> |
| 83 | + ); |
| 84 | + |
| 85 | + return ( |
| 86 | + <AlertDialog.Backdrop isOpen onOpenChange={(open) => !open && handleClose()}> |
| 87 | + <AlertDialog.Container size="sm"> |
| 88 | + <AlertDialog.Dialog className="sm:max-w-[420px] !p-4">{dialogContent}</AlertDialog.Dialog> |
| 89 | + </AlertDialog.Container> |
| 90 | + </AlertDialog.Backdrop> |
| 91 | + ); |
| 92 | +} |
0 commit comments