510 lines
15 KiB
TypeScript
510 lines
15 KiB
TypeScript
"use client";
|
|
|
|
import { useState } from "react";
|
|
import { IconGitCommit, IconGitPullRequest, IconLoader2, IconCheck } from "@tabler/icons-react";
|
|
|
|
import { Button } from "@kandev/ui/button";
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
DialogFooter,
|
|
DialogClose,
|
|
} from "@kandev/ui/dialog";
|
|
import {
|
|
AlertDialog,
|
|
AlertDialogAction,
|
|
AlertDialogCancel,
|
|
AlertDialogContent,
|
|
AlertDialogDescription,
|
|
AlertDialogFooter,
|
|
AlertDialogHeader,
|
|
AlertDialogTitle,
|
|
} from "@kandev/ui/alert-dialog";
|
|
import { Checkbox } from "@kandev/ui/checkbox";
|
|
import { Label } from "@kandev/ui/label";
|
|
import { Input } from "@kandev/ui/input";
|
|
import { Textarea } from "@kandev/ui/textarea";
|
|
import { RadioGroup, RadioGroupItem } from "@kandev/ui/radio-group";
|
|
|
|
// --- Discard Confirmation Dialog ---
|
|
|
|
type DiscardDialogProps = {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
fileToDiscard: string | null;
|
|
onConfirm: () => void;
|
|
};
|
|
|
|
export function DiscardDialog({
|
|
open,
|
|
onOpenChange,
|
|
fileToDiscard,
|
|
onConfirm,
|
|
}: DiscardDialogProps) {
|
|
return (
|
|
<AlertDialog open={open} onOpenChange={onOpenChange}>
|
|
<AlertDialogContent>
|
|
<AlertDialogHeader>
|
|
<AlertDialogTitle>Discard changes?</AlertDialogTitle>
|
|
<AlertDialogDescription>
|
|
This will permanently discard all changes to{" "}
|
|
<span className="font-semibold">{fileToDiscard}</span>. This action cannot be undone.
|
|
</AlertDialogDescription>
|
|
</AlertDialogHeader>
|
|
<AlertDialogFooter>
|
|
<AlertDialogCancel>Cancel</AlertDialogCancel>
|
|
<AlertDialogAction
|
|
onClick={onConfirm}
|
|
className="bg-destructive text-destructive-foreground hover:bg-destructive/90"
|
|
>
|
|
Discard
|
|
</AlertDialogAction>
|
|
</AlertDialogFooter>
|
|
</AlertDialogContent>
|
|
</AlertDialog>
|
|
);
|
|
}
|
|
|
|
// --- Commit Dialog ---
|
|
|
|
type CommitDialogProps = {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
commitMessage: string;
|
|
onCommitMessageChange: (value: string) => void;
|
|
onCommit: () => void;
|
|
isLoading: boolean;
|
|
stagedFileCount: number;
|
|
stagedAdditions: number;
|
|
stagedDeletions: number;
|
|
isAmend?: boolean;
|
|
onAmendChange?: (amend: boolean) => void;
|
|
lastCommitMessage?: string | null;
|
|
};
|
|
|
|
export function CommitDialog({
|
|
open,
|
|
onOpenChange,
|
|
commitMessage,
|
|
onCommitMessageChange,
|
|
onCommit,
|
|
isLoading,
|
|
stagedFileCount,
|
|
stagedAdditions,
|
|
stagedDeletions,
|
|
isAmend = false,
|
|
onAmendChange,
|
|
lastCommitMessage,
|
|
}: CommitDialogProps) {
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="sm:max-w-[500px]">
|
|
<DialogHeader>
|
|
<DialogTitle className="flex items-center gap-2">
|
|
<IconGitCommit className="h-5 w-5" />
|
|
{isAmend ? "Amend Commit" : "Commit Changes"}
|
|
</DialogTitle>
|
|
</DialogHeader>
|
|
<div className="space-y-4 py-2">
|
|
<CommitDialogStats
|
|
stagedFileCount={stagedFileCount}
|
|
stagedAdditions={stagedAdditions}
|
|
stagedDeletions={stagedDeletions}
|
|
/>
|
|
{onAmendChange && (
|
|
<div className="flex items-center space-x-2">
|
|
<Checkbox
|
|
id="amend-checkbox"
|
|
checked={isAmend}
|
|
onCheckedChange={(checked) => {
|
|
onAmendChange(checked === true);
|
|
// Pre-fill with last commit message when enabling amend
|
|
if (checked === true && lastCommitMessage && !commitMessage.trim()) {
|
|
onCommitMessageChange(lastCommitMessage);
|
|
}
|
|
}}
|
|
/>
|
|
<Label htmlFor="amend-checkbox" className="text-sm cursor-pointer">
|
|
Amend previous commit
|
|
</Label>
|
|
</div>
|
|
)}
|
|
<Textarea
|
|
placeholder={isAmend ? "Enter new commit message..." : "Enter commit message..."}
|
|
value={commitMessage}
|
|
onChange={(e) => onCommitMessageChange(e.target.value)}
|
|
autoFocus
|
|
/>
|
|
</div>
|
|
<DialogFooter>
|
|
<DialogClose asChild>
|
|
<Button type="button" variant="outline">
|
|
Cancel
|
|
</Button>
|
|
</DialogClose>
|
|
<Button onClick={onCommit} disabled={!commitMessage.trim() || isLoading}>
|
|
{isLoading ? (
|
|
<>
|
|
<IconLoader2 className="h-4 w-4 animate-spin mr-2" />
|
|
{isAmend ? "Amending..." : "Committing..."}
|
|
</>
|
|
) : (
|
|
<>
|
|
<IconCheck className="h-4 w-4 mr-2" />
|
|
{isAmend ? "Amend" : "Commit"}
|
|
</>
|
|
)}
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|
|
|
|
function CommitDialogStats({
|
|
stagedFileCount,
|
|
stagedAdditions,
|
|
stagedDeletions,
|
|
}: {
|
|
stagedFileCount: number;
|
|
stagedAdditions: number;
|
|
stagedDeletions: number;
|
|
}) {
|
|
return (
|
|
<div className="text-sm text-muted-foreground">
|
|
{stagedFileCount > 0 ? (
|
|
<span>
|
|
<span className="font-medium text-foreground">{stagedFileCount}</span> staged file
|
|
{stagedFileCount !== 1 ? "s" : ""}
|
|
{(stagedAdditions > 0 || stagedDeletions > 0) && (
|
|
<span className="ml-2">
|
|
(<span className="text-green-600">+{stagedAdditions}</span>
|
|
{" / "}
|
|
<span className="text-red-600">-{stagedDeletions}</span>)
|
|
</span>
|
|
)}
|
|
</span>
|
|
) : (
|
|
<span>No staged files to commit</span>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// --- Create PR Dialog ---
|
|
|
|
type PRDialogProps = {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
prTitle: string;
|
|
onPrTitleChange: (value: string) => void;
|
|
prBody: string;
|
|
onPrBodyChange: (value: string) => void;
|
|
prDraft: boolean;
|
|
onPrDraftChange: (value: boolean) => void;
|
|
onCreatePR: () => void;
|
|
isLoading: boolean;
|
|
displayBranch: string | null;
|
|
baseBranch: string | undefined;
|
|
};
|
|
|
|
export function PRDialog({
|
|
open,
|
|
onOpenChange,
|
|
prTitle,
|
|
onPrTitleChange,
|
|
prBody,
|
|
onPrBodyChange,
|
|
prDraft,
|
|
onPrDraftChange,
|
|
onCreatePR,
|
|
isLoading,
|
|
displayBranch,
|
|
baseBranch,
|
|
}: PRDialogProps) {
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="sm:max-w-[500px]">
|
|
<DialogHeader>
|
|
<DialogTitle className="flex items-center gap-2">
|
|
<IconGitPullRequest className="h-5 w-5" />
|
|
Create Pull Request
|
|
</DialogTitle>
|
|
</DialogHeader>
|
|
<div className="space-y-4 py-2">
|
|
{displayBranch && <PRBranchInfo displayBranch={displayBranch} baseBranch={baseBranch} />}
|
|
<div className="space-y-2">
|
|
<Label htmlFor="changes-pr-title" className="text-sm">
|
|
Title
|
|
</Label>
|
|
<input
|
|
id="changes-pr-title"
|
|
type="text"
|
|
placeholder="Pull request title..."
|
|
value={prTitle}
|
|
onChange={(e) => onPrTitleChange(e.target.value)}
|
|
className="flex h-9 w-full rounded-md border border-input bg-transparent px-3 py-1 text-sm shadow-sm transition-colors placeholder:text-muted-foreground focus-visible:outline-none focus-visible:ring-1 focus-visible:ring-ring disabled:cursor-not-allowed disabled:opacity-50"
|
|
autoFocus
|
|
/>
|
|
</div>
|
|
<div className="space-y-2">
|
|
<Label htmlFor="changes-pr-body" className="text-sm">
|
|
Description
|
|
</Label>
|
|
<Textarea
|
|
id="changes-pr-body"
|
|
placeholder="Describe your changes..."
|
|
value={prBody}
|
|
onChange={(e) => onPrBodyChange(e.target.value)}
|
|
rows={6}
|
|
className="resize-none max-h-[200px] overflow-y-auto"
|
|
/>
|
|
</div>
|
|
<div className="flex items-center space-x-2">
|
|
<Checkbox
|
|
id="changes-pr-draft"
|
|
checked={prDraft}
|
|
onCheckedChange={(checked) => onPrDraftChange(checked === true)}
|
|
/>
|
|
<Label htmlFor="changes-pr-draft" className="text-sm cursor-pointer">
|
|
Create as draft
|
|
</Label>
|
|
</div>
|
|
</div>
|
|
<DialogFooter>
|
|
<DialogClose asChild>
|
|
<Button type="button" variant="outline">
|
|
Cancel
|
|
</Button>
|
|
</DialogClose>
|
|
<Button onClick={onCreatePR} disabled={!prTitle.trim() || isLoading}>
|
|
{isLoading ? (
|
|
<>
|
|
<IconLoader2 className="h-4 w-4 animate-spin mr-2" />
|
|
Creating...
|
|
</>
|
|
) : (
|
|
<>
|
|
<IconGitPullRequest className="h-4 w-4 mr-2" />
|
|
Create PR
|
|
</>
|
|
)}
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|
|
|
|
function PRBranchInfo({
|
|
displayBranch,
|
|
baseBranch,
|
|
}: {
|
|
displayBranch: string;
|
|
baseBranch: string | undefined;
|
|
}) {
|
|
return (
|
|
<div className="text-sm text-muted-foreground">
|
|
{baseBranch ? (
|
|
<span>
|
|
Creating PR from <span className="font-medium text-foreground">{displayBranch}</span>{" "}
|
|
→ <span className="font-medium text-foreground">{baseBranch}</span>
|
|
</span>
|
|
) : (
|
|
<span>
|
|
Creating PR from <span className="font-medium text-foreground">{displayBranch}</span>
|
|
</span>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
// --- Amend Commit Message Dialog ---
|
|
|
|
type AmendDialogProps = {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
amendMessage: string;
|
|
onAmendMessageChange: (value: string) => void;
|
|
onAmend: () => void;
|
|
isLoading: boolean;
|
|
};
|
|
|
|
export function AmendDialog({
|
|
open,
|
|
onOpenChange,
|
|
amendMessage,
|
|
onAmendMessageChange,
|
|
onAmend,
|
|
isLoading,
|
|
}: AmendDialogProps) {
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="sm:max-w-[500px]">
|
|
<DialogHeader>
|
|
<DialogTitle className="flex items-center gap-2">
|
|
<IconGitCommit className="h-5 w-5" />
|
|
Amend Commit Message
|
|
</DialogTitle>
|
|
</DialogHeader>
|
|
<div className="space-y-4 py-2">
|
|
<p className="text-sm text-muted-foreground">
|
|
Edit the message for the most recent commit.
|
|
</p>
|
|
<Textarea
|
|
placeholder="Enter new commit message..."
|
|
value={amendMessage}
|
|
onChange={(e) => onAmendMessageChange(e.target.value)}
|
|
rows={4}
|
|
className="resize-none"
|
|
autoFocus
|
|
/>
|
|
</div>
|
|
<DialogFooter>
|
|
<DialogClose asChild>
|
|
<Button type="button" variant="outline" className="cursor-pointer">
|
|
Cancel
|
|
</Button>
|
|
</DialogClose>
|
|
<Button
|
|
onClick={onAmend}
|
|
disabled={!amendMessage.trim() || isLoading}
|
|
className="cursor-pointer"
|
|
>
|
|
{isLoading ? (
|
|
<>
|
|
<IconLoader2 className="h-4 w-4 animate-spin mr-2" />
|
|
Amending...
|
|
</>
|
|
) : (
|
|
<>
|
|
<IconCheck className="h-4 w-4 mr-2" />
|
|
Amend
|
|
</>
|
|
)}
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|
|
|
|
// --- Reset to Commit Dialog ---
|
|
|
|
type ResetDialogProps = {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
commitSha: string | null;
|
|
onReset: (mode: "soft" | "hard") => void;
|
|
isLoading: boolean;
|
|
};
|
|
|
|
export function ResetDialog({
|
|
open,
|
|
onOpenChange,
|
|
commitSha,
|
|
onReset,
|
|
isLoading,
|
|
}: ResetDialogProps) {
|
|
const shortSha = commitSha?.slice(0, 7) ?? "";
|
|
const [mode, setMode] = useState<"soft" | "hard">("soft");
|
|
const [confirmation, setConfirmation] = useState("");
|
|
|
|
const isHardResetConfirmed = mode === "hard" && confirmation === shortSha;
|
|
const canReset = !!commitSha && (mode === "soft" || isHardResetConfirmed);
|
|
|
|
const handleOpenChange = (newOpen: boolean) => {
|
|
if (!newOpen) {
|
|
// Reset state when closing
|
|
setMode("soft");
|
|
setConfirmation("");
|
|
}
|
|
onOpenChange(newOpen);
|
|
};
|
|
|
|
const handleReset = () => {
|
|
if (canReset) {
|
|
onReset(mode);
|
|
}
|
|
};
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={handleOpenChange}>
|
|
<DialogContent className="sm:max-w-[450px]">
|
|
<DialogHeader>
|
|
<DialogTitle>Reset to commit {shortSha}</DialogTitle>
|
|
</DialogHeader>
|
|
<div className="space-y-4 py-2">
|
|
<RadioGroup
|
|
value={mode}
|
|
onValueChange={(v) => setMode(v as "soft" | "hard")}
|
|
className="space-y-3"
|
|
>
|
|
<div className="flex items-start space-x-3">
|
|
<RadioGroupItem value="soft" id="reset-soft" className="mt-1" />
|
|
<div className="flex-1">
|
|
<Label htmlFor="reset-soft" className="font-medium cursor-pointer">
|
|
Soft Reset
|
|
</Label>
|
|
<p className="text-sm text-muted-foreground">
|
|
Move HEAD to this commit. Keep all changes staged.
|
|
</p>
|
|
</div>
|
|
</div>
|
|
<div className="flex items-start space-x-3">
|
|
<RadioGroupItem value="hard" id="reset-hard" className="mt-1" />
|
|
<div className="flex-1">
|
|
<Label htmlFor="reset-hard" className="font-medium cursor-pointer text-destructive">
|
|
Hard Reset
|
|
</Label>
|
|
<p className="text-sm text-muted-foreground">
|
|
Discard all changes permanently. This cannot be undone.
|
|
</p>
|
|
</div>
|
|
</div>
|
|
</RadioGroup>
|
|
|
|
{mode === "hard" && (
|
|
<div className="space-y-2 p-3 border border-destructive/50 rounded-md bg-destructive/5">
|
|
<p className="text-xs text-destructive font-medium">
|
|
Type <code className="bg-muted px-1 rounded">{shortSha}</code> to confirm:
|
|
</p>
|
|
<Input
|
|
value={confirmation}
|
|
onChange={(e) => setConfirmation(e.target.value)}
|
|
placeholder={shortSha}
|
|
className="font-mono h-8 text-sm"
|
|
autoComplete="off"
|
|
/>
|
|
</div>
|
|
)}
|
|
</div>
|
|
<DialogFooter>
|
|
<DialogClose asChild>
|
|
<Button type="button" variant="outline" disabled={isLoading}>
|
|
Cancel
|
|
</Button>
|
|
</DialogClose>
|
|
<Button
|
|
onClick={handleReset}
|
|
disabled={!canReset || isLoading}
|
|
variant={mode === "hard" ? "destructive" : "default"}
|
|
className="cursor-pointer"
|
|
>
|
|
{isLoading ? (
|
|
<>
|
|
<IconLoader2 className="h-4 w-4 animate-spin mr-2" />
|
|
Resetting...
|
|
</>
|
|
) : (
|
|
<>Reset</>
|
|
)}
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|