mirror of
https://github.com/AutoMaker-Org/automaker.git
synced 2026-01-30 06:12:03 +00:00
Integrate Claude Haiku to automatically generate commit messages when committing worktree changes. Shows a sparkle animation while generating and auto-populates the commit message field. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
207 lines
6.1 KiB
TypeScript
207 lines
6.1 KiB
TypeScript
import { useState, useEffect } from 'react';
|
|
import {
|
|
Dialog,
|
|
DialogContent,
|
|
DialogDescription,
|
|
DialogFooter,
|
|
DialogHeader,
|
|
DialogTitle,
|
|
} from '@/components/ui/dialog';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Textarea } from '@/components/ui/textarea';
|
|
import { Label } from '@/components/ui/label';
|
|
import { GitCommit, Loader2, Sparkles } from 'lucide-react';
|
|
import { getElectronAPI } from '@/lib/electron';
|
|
import { toast } from 'sonner';
|
|
|
|
interface WorktreeInfo {
|
|
path: string;
|
|
branch: string;
|
|
isMain: boolean;
|
|
hasChanges?: boolean;
|
|
changedFilesCount?: number;
|
|
}
|
|
|
|
interface CommitWorktreeDialogProps {
|
|
open: boolean;
|
|
onOpenChange: (open: boolean) => void;
|
|
worktree: WorktreeInfo | null;
|
|
onCommitted: () => void;
|
|
}
|
|
|
|
export function CommitWorktreeDialog({
|
|
open,
|
|
onOpenChange,
|
|
worktree,
|
|
onCommitted,
|
|
}: CommitWorktreeDialogProps) {
|
|
const [message, setMessage] = useState('');
|
|
const [isLoading, setIsLoading] = useState(false);
|
|
const [isGenerating, setIsGenerating] = useState(false);
|
|
const [error, setError] = useState<string | null>(null);
|
|
|
|
const handleCommit = async () => {
|
|
if (!worktree || !message.trim()) return;
|
|
|
|
setIsLoading(true);
|
|
setError(null);
|
|
|
|
try {
|
|
const api = getElectronAPI();
|
|
if (!api?.worktree?.commit) {
|
|
setError('Worktree API not available');
|
|
return;
|
|
}
|
|
const result = await api.worktree.commit(worktree.path, message);
|
|
|
|
if (result.success && result.result) {
|
|
if (result.result.committed) {
|
|
toast.success('Changes committed', {
|
|
description: `Commit ${result.result.commitHash} on ${result.result.branch}`,
|
|
});
|
|
onCommitted();
|
|
onOpenChange(false);
|
|
setMessage('');
|
|
} else {
|
|
toast.info('No changes to commit', {
|
|
description: result.result.message,
|
|
});
|
|
}
|
|
} else {
|
|
setError(result.error || 'Failed to commit changes');
|
|
}
|
|
} catch (err) {
|
|
setError(err instanceof Error ? err.message : 'Failed to commit');
|
|
} finally {
|
|
setIsLoading(false);
|
|
}
|
|
};
|
|
|
|
const handleKeyDown = (e: React.KeyboardEvent) => {
|
|
if (e.key === 'Enter' && e.metaKey && !isLoading && message.trim()) {
|
|
handleCommit();
|
|
}
|
|
};
|
|
|
|
// Generate AI commit message when dialog opens
|
|
useEffect(() => {
|
|
if (open && worktree) {
|
|
// Reset state
|
|
setMessage('');
|
|
setError(null);
|
|
setIsGenerating(true);
|
|
|
|
const generateMessage = async () => {
|
|
try {
|
|
const api = getElectronAPI();
|
|
if (!api?.worktree?.generateCommitMessage) {
|
|
setError('AI commit message generation not available');
|
|
setIsGenerating(false);
|
|
return;
|
|
}
|
|
|
|
const result = await api.worktree.generateCommitMessage(worktree.path);
|
|
|
|
if (result.success && result.message) {
|
|
setMessage(result.message);
|
|
} else {
|
|
// Don't show error toast, just log it and leave message empty
|
|
console.warn('Failed to generate commit message:', result.error);
|
|
setMessage('');
|
|
}
|
|
} catch (err) {
|
|
// Don't show error toast for generation failures
|
|
console.warn('Error generating commit message:', err);
|
|
setMessage('');
|
|
} finally {
|
|
setIsGenerating(false);
|
|
}
|
|
};
|
|
|
|
generateMessage();
|
|
}
|
|
}, [open, worktree]);
|
|
|
|
if (!worktree) return null;
|
|
|
|
return (
|
|
<Dialog open={open} onOpenChange={onOpenChange}>
|
|
<DialogContent className="sm:max-w-[500px]">
|
|
<DialogHeader>
|
|
<DialogTitle className="flex items-center gap-2">
|
|
<GitCommit className="w-5 h-5" />
|
|
Commit Changes
|
|
</DialogTitle>
|
|
<DialogDescription>
|
|
Commit changes in the{' '}
|
|
<code className="font-mono bg-muted px-1 rounded">{worktree.branch}</code> worktree.
|
|
{worktree.changedFilesCount && (
|
|
<span className="ml-1">
|
|
({worktree.changedFilesCount} file
|
|
{worktree.changedFilesCount > 1 ? 's' : ''} changed)
|
|
</span>
|
|
)}
|
|
</DialogDescription>
|
|
</DialogHeader>
|
|
|
|
<div className="grid gap-4 py-4">
|
|
<div className="grid gap-2">
|
|
<Label htmlFor="commit-message" className="flex items-center gap-2">
|
|
Commit Message
|
|
{isGenerating && (
|
|
<span className="flex items-center gap-1 text-xs text-muted-foreground">
|
|
<Sparkles className="w-3 h-3 animate-pulse" />
|
|
Generating...
|
|
</span>
|
|
)}
|
|
</Label>
|
|
<Textarea
|
|
id="commit-message"
|
|
placeholder={
|
|
isGenerating ? 'Generating commit message...' : 'Describe your changes...'
|
|
}
|
|
value={message}
|
|
onChange={(e) => {
|
|
setMessage(e.target.value);
|
|
setError(null);
|
|
}}
|
|
onKeyDown={handleKeyDown}
|
|
className="min-h-[100px] font-mono text-sm"
|
|
autoFocus
|
|
disabled={isGenerating}
|
|
/>
|
|
{error && <p className="text-sm text-destructive">{error}</p>}
|
|
</div>
|
|
|
|
<p className="text-xs text-muted-foreground">
|
|
Press <kbd className="px-1 py-0.5 bg-muted rounded text-xs">Cmd+Enter</kbd> to commit
|
|
</p>
|
|
</div>
|
|
|
|
<DialogFooter>
|
|
<Button
|
|
variant="ghost"
|
|
onClick={() => onOpenChange(false)}
|
|
disabled={isLoading || isGenerating}
|
|
>
|
|
Cancel
|
|
</Button>
|
|
<Button onClick={handleCommit} disabled={isLoading || isGenerating || !message.trim()}>
|
|
{isLoading ? (
|
|
<>
|
|
<Loader2 className="w-4 h-4 mr-2 animate-spin" />
|
|
Committing...
|
|
</>
|
|
) : (
|
|
<>
|
|
<GitCommit className="w-4 h-4 mr-2" />
|
|
Commit
|
|
</>
|
|
)}
|
|
</Button>
|
|
</DialogFooter>
|
|
</DialogContent>
|
|
</Dialog>
|
|
);
|
|
}
|