use shared action card component

This commit is contained in:
Chad Curtis committed 2025-08-20 16:21:57 +00:00
1 parent 6ec3c165b3
commit 01a380520c
7 files changed
+240 -229

No files matched your search

+15 -61
View File
@@ -1,8 +1,5 @@
import { memo } from 'react';
import { Card, CardContent, CardHeader } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { Play, Loader2, XCircle, CheckCircle } from 'lucide-react';
import { cn } from '@/lib/utils';
import { ActionCard } from './shared/ActionCard';
import type { BuildActionComponentProps } from '@/types/build';
interface BuildActionProps extends BuildActionComponentProps {
@@ -10,25 +7,18 @@ interface BuildActionProps extends BuildActionComponentProps {
}
export const BuildAction = memo(({ action, result, status, isError, isLoading }: BuildActionProps) => {
// Handle both new status-based and old boolean-based props for compatibility
const effectiveStatus = status || (
isLoading ? 'RUNNING' :
isError ? 'FAILED' : 'SUCCESS'
);
const actionConfig = {
build: { title: 'Build Project', verb: 'build' },
deploy: { title: 'Deploy Project', verb: 'deploy' },
'auto-build': { title: 'Auto-Build Project', verb: 'auto-build' },
}[action] || { title: 'Build Action', verb: 'process' };
const getActionIcon = () => {
if (effectiveStatus === 'RUNNING') return <Loader2 className="h-4 w-4 animate-spin" />;
if (effectiveStatus === 'FAILED') return <XCircle className="h-4 w-4 text-destructive" />;
return <Play className="h-4 w-4" />;
};
const getActionDescription = () => {
const effectiveStatus = status || (
isLoading ? 'RUNNING' :
isError ? 'FAILED' : 'SUCCESS'
);
if (effectiveStatus === 'RUNNING') return `${actionConfig.verb.charAt(0).toUpperCase() + actionConfig.verb.slice(1)}ing project...`;
if (effectiveStatus === 'FAILED') return `${actionConfig.verb.charAt(0).toUpperCase() + actionConfig.verb.slice(1)} failed`;
if (effectiveStatus === 'SUCCESS') return `${actionConfig.verb.charAt(0).toUpperCase() + actionConfig.verb.slice(1)} completed`;
@@ -36,51 +26,15 @@ export const BuildAction = memo(({ action, result, status, isError, isLoading }:
};
return (
<Card className={cn("mt-2", effectiveStatus === 'FAILED' ? "border-destructive/50" : "border-muted")}>
<CardHeader className="pb-3">
<div className="flex items-center justify-between">
<div className="flex items-center gap-2">
{getActionIcon()}
<span className="font-mono text-sm font-medium">{actionConfig.title}</span>
</div>
<div className="flex items-center gap-2">
{effectiveStatus === 'RUNNING' ? (
<Badge variant="secondary" className="text-xs bg-blue-100 text-blue-800 dark:bg-blue-900/20 dark:text-blue-400">
<Loader2 className="h-3 w-3 mr-1 animate-spin" />
Running
</Badge>
) : effectiveStatus === 'FAILED' ? (
<Badge variant="destructive" className="text-xs">
<XCircle className="h-3 w-3 mr-1" />
Failed
</Badge>
) : (
<Badge variant="secondary" className="text-xs bg-green-100 text-green-800 dark:bg-green-900/20 dark:text-green-400">
<CheckCircle className="h-3 w-3 mr-1" />
Success
</Badge>
)}
</div>
</div>
</CardHeader>
<CardContent className="pt-0">
<div className="text-sm text-muted-foreground mb-3">
{getActionDescription()}
</div>
{result && (
<div className={cn(
"p-3 rounded-md text-sm",
effectiveStatus === 'FAILED'
? "bg-destructive/10 text-destructive border border-destructive/20"
: "bg-green-50 text-green-800 dark:bg-green-900/20 dark:text-green-400 border border-green-200 dark:border-green-800/30"
)}>
<pre className="whitespace-pre-wrap break-words">{result}</pre>
</div>
)}
</CardContent>
</Card>
<ActionCard
title={actionConfig.title}
description={getActionDescription()}
result={result}
status={status}
isError={isError}
isLoading={isLoading}
successLabel="Completed"
/>
);
});
+2 -4
View File
@@ -4,7 +4,7 @@ import { TestApp } from '@/test/TestApp';
import { GitCommit } from './GitCommit';
describe('GitCommit', () => {
it('renders commit message and shows committing state', () => {
it('renders commit message', () => {
render(
<TestApp>
<GitCommit message="feat: add new feature" />
@@ -12,8 +12,6 @@ describe('GitCommit', () => {
);
expect(screen.getByText('feat: add new feature')).toBeInTheDocument();
expect(screen.getByText('Committing')).toBeInTheDocument();
expect(screen.getByText('Git commit')).toBeInTheDocument();
});
it('shows success state with commit details', () => {
@@ -38,7 +36,7 @@ describe('GitCommit', () => {
expect(screen.getByText('Committed')).toBeInTheDocument();
expect(screen.getByText('Successfully committed 3 files')).toBeInTheDocument();
expect(screen.getByText('abc1234')).toBeInTheDocument();
expect(screen.getByText('main branch')).toBeInTheDocument();
expect(screen.getByText('2 added')).toBeInTheDocument();
expect(screen.getByText('1 modified')).toBeInTheDocument();
});
+31 -66
View File
@@ -1,7 +1,5 @@
import { Card, CardContent, CardHeader } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { GitCommit as GitCommitIcon, CheckCircle, XCircle, GitBranch, Plus, Minus, Edit } from 'lucide-react';
import { cn } from '@/lib/utils';
import { ActionCard } from './shared/ActionCard';
import { GitCommit as GitCommitIcon, GitBranch, Plus, Minus, Edit } from 'lucide-react';
interface GitCommitProps {
message: string;
@@ -80,61 +78,33 @@ export function GitCommit({
const firstLine = getFirstLine(message);
const commitEmoji = getCommitEmoji(message);
return (
<Card className={cn("mt-2", isError ? "border-destructive/50" : "border-muted", className)}>
<CardHeader className="pb-3">
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<div className="flex items-center gap-2">
<GitCommitIcon className="h-4 w-4 text-muted-foreground" />
<span className="text-lg">{commitEmoji}</span>
</div>
<div className="flex-1 min-w-0">
<div className="font-mono text-sm font-medium truncate">
{firstLine}
</div>
<div className="text-xs text-muted-foreground">
Git commit
{stats?.branch && (
<span className="ml-2 inline-flex items-center gap-1">
<GitBranch className="h-3 w-3" />
{stats.branch}
</span>
)}
{stats?.hash && (
<span className="ml-2 font-mono">
{stats.hash}
</span>
)}
</div>
</div>
</div>
<div className="flex items-center gap-2">
{result !== undefined ? (
isError ? (
<Badge variant="destructive" className="text-xs">
<XCircle className="h-3 w-3 mr-1" />
Failed
</Badge>
) : (
<Badge variant="secondary" className="text-xs bg-green-100 text-green-800 dark:bg-green-900/20 dark:text-green-400">
<CheckCircle className="h-3 w-3 mr-1" />
Committed
</Badge>
)
) : (
<Badge variant="secondary" className="text-xs bg-blue-100 text-blue-800 dark:bg-blue-900/20 dark:text-blue-400">
<GitCommitIcon className="h-3 w-3 mr-1" />
Committing
</Badge>
)}
</div>
</div>
</CardHeader>
const getCommitDescription = () => {
let desc = 'Git commit';
if (stats?.branch) {
desc += ` • ${stats.branch} branch`;
}
if (stats?.hash) {
desc += ` • ${stats.hash}`;
}
return desc;
};
return (
<ActionCard
title={firstLine}
description={getCommitDescription()}
icon={<span className="text-lg">{commitEmoji}</span>}
result={result}
isError={isError}
className={className}
showResult={false}
runningIcon={<GitCommitIcon className="h-3 w-3" />}
runningLabel="Committing"
successLabel="Committed"
>
{/* Full commit message - only show if multi-line */}
{message.includes('\n') && (
<CardContent className="pt-0">
<div className="mb-3">
<details className="group">
<summary className="cursor-pointer text-xs text-muted-foreground hover:text-foreground transition-colors mb-2 flex items-center gap-1">
<span className="group-open:rotate-90 transition-transform">▶</span>
@@ -148,19 +118,14 @@ export function GitCommit({
</div>
</div>
</details>
</CardContent>
</div>
)}
{result && (
<CardContent className={cn("pt-0", message.includes('\n') ? "pt-3" : "")}>
<>
{/* Summary */}
{summary && (
<div className={cn(
"mb-3 p-3 rounded-md text-sm font-medium",
isError
? "bg-destructive/10 text-destructive border border-destructive/20"
: "bg-green-50 text-green-800 dark:bg-green-900/20 dark:text-green-400 border border-green-200 dark:border-green-800/30"
)}>
<div className="mb-3 p-3 rounded-md text-sm font-muted-foreground">
{summary}
</div>
)}
@@ -207,8 +172,8 @@ export function GitCommit({
</span>
)}
</div>
</CardContent>
</>
)}
</Card>
</ActionCard>
);
}
+16 -49
View File
@@ -1,7 +1,5 @@
import { Card, CardContent, CardHeader } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { Play, CheckCircle, XCircle } from 'lucide-react';
import { cn } from '@/lib/utils';
import { ActionCard } from './shared/ActionCard';
import { Play } from 'lucide-react';
interface ScriptRunnerProps {
script: string;
@@ -104,52 +102,21 @@ export function ScriptRunner({ script, result, isError = false, className }: Scr
const { summary, details } = parseOutput(result || '');
return (
<Card className={cn("mt-2", isError ? "border-destructive/50" : "border-muted", className)}>
<CardHeader className="pb-3">
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<div className="flex items-center gap-2">
<Play className="h-4 w-4 text-muted-foreground" />
<span className="text-lg">{scriptInfo.icon}</span>
</div>
<div>
<div className="font-mono text-sm font-medium">npm run {script}</div>
<div className="text-xs text-muted-foreground">{scriptInfo.description}</div>
</div>
</div>
<div className="flex items-center gap-2">
{result !== undefined ? (
isError ? (
<Badge variant="destructive" className="text-xs">
<XCircle className="h-3 w-3 mr-1" />
Failed
</Badge>
) : (
<Badge variant="secondary" className="text-xs bg-green-100 text-green-800 dark:bg-green-900/20 dark:text-green-400">
<CheckCircle className="h-3 w-3 mr-1" />
Success
</Badge>
)
) : (
<Badge variant="secondary" className="text-xs bg-blue-100 text-blue-800 dark:bg-blue-900/20 dark:text-blue-400">
<Play className="h-3 w-3 mr-1" />
Running
</Badge>
)}
</div>
</div>
</CardHeader>
<ActionCard
title={`npm run ${script}`}
description={scriptInfo.description}
icon={<span className="text-lg">{scriptInfo.icon}</span>}
result={result}
isError={isError}
className={className}
runningIcon={<Play className="h-3 w-3" />}
runningLabel="Running"
>
{result && (
<CardContent className="pt-0">
<>
{/* Summary */}
{summary && (
<div className={cn(
"mb-3 p-3 rounded-md text-sm font-medium",
isError
? "bg-destructive/10 text-destructive border border-destructive/20"
: "bg-green-50 text-green-800 dark:bg-green-900/20 dark:text-green-400 border border-green-200 dark:border-green-800/30"
)}>
<div className="mb-3 p-3 rounded-md text-sm font-muted-foreground">
{summary}
</div>
)}
@@ -201,8 +168,8 @@ export function ScriptRunner({ script, result, isError = false, className }: Scr
)}
</div>
)}
</CardContent>
</>
)}
</Card>
</ActionCard>
);
}
+16 -49
View File
@@ -1,7 +1,5 @@
import { Card, CardContent, CardHeader } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { Terminal, CheckCircle, XCircle, Play } from 'lucide-react';
import { cn } from '@/lib/utils';
import { ActionCard } from './shared/ActionCard';
import { Terminal } from 'lucide-react';
interface ShellCommandProps {
command: string;
@@ -122,52 +120,21 @@ export function ShellCommand({ command, result, isError = false, className }: Sh
const { summary, details } = parseOutput(result || '');
return (
<Card className={cn("mt-2", isError ? "border-destructive/50" : "border-muted", className)}>
<CardHeader className="pb-3">
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
<div className="flex items-center gap-2">
<Terminal className="h-4 w-4 text-muted-foreground" />
<span className="text-lg">{commandInfo.icon}</span>
</div>
<div>
<div className="font-mono text-sm font-medium">$ {commandInfo.fullCommand}</div>
<div className="text-xs text-muted-foreground">{commandInfo.description}</div>
</div>
</div>
<div className="flex items-center gap-2">
{result !== undefined ? (
isError ? (
<Badge variant="destructive" className="text-xs">
<XCircle className="h-3 w-3 mr-1" />
Failed
</Badge>
) : (
<Badge variant="secondary" className="text-xs bg-green-100 text-green-800 dark:bg-green-900/20 dark:text-green-400">
<CheckCircle className="h-3 w-3 mr-1" />
Success
</Badge>
)
) : (
<Badge variant="secondary" className="text-xs bg-blue-100 text-blue-800 dark:bg-blue-900/20 dark:text-blue-400">
<Play className="h-3 w-3 mr-1" />
Running
</Badge>
)}
</div>
</div>
</CardHeader>
<ActionCard
title={`$ ${commandInfo.fullCommand}`}
description={commandInfo.description}
icon={<span className="text-lg">{commandInfo.icon}</span>}
result={result}
isError={isError}
className={className}
runningIcon={<Terminal className="h-3 w-3" />}
runningLabel="Executing"
>
{result && (
<CardContent className="pt-0">
<>
{/* Summary */}
{summary && (
<div className={cn(
"mb-3 p-3 rounded-md text-sm font-medium",
isError
? "bg-destructive/10 text-destructive border border-destructive/20"
: "bg-green-50 text-green-800 dark:bg-green-900/20 dark:text-green-400 border border-green-200 dark:border-green-800/30"
)}>
<div className="mb-3 p-3 rounded-md text-sm font-muted-foreground">
{summary}
</div>
)}
@@ -210,8 +177,8 @@ export function ShellCommand({ command, result, isError = false, className }: Sh
)}
</div>
)}
</CardContent>
</>
)}
</Card>
</ActionCard>
);
}
+69
View File
@@ -0,0 +1,69 @@
import { Card, CardContent, CardHeader } from '@/components/ui/card';
import { cn } from '@/lib/utils';
import { ActionStatus, type ActionStatusProps } from './ActionStatus';
export interface ActionCardProps extends Omit<ActionStatusProps, 'status'> {
title: string;
description?: string;
icon?: React.ReactNode;
result?: string;
children?: React.ReactNode;
className?: string;
showResult?: boolean;
status?: ActionStatusProps['status'];
}
export function ActionCard({
title,
description,
icon,
result,
children,
className,
showResult = true,
...statusProps
}: ActionCardProps) {
const { Badge, Icon, ResultClass, BorderClass } = ActionStatus({
_result: result,
...statusProps,
status: statusProps.status
});
return (
<Card className={cn("mt-2", BorderClass(), className)}>
<CardHeader className="pb-3">
<div className="flex items-center justify-between">
<div className="flex items-center gap-3">
{icon && (
<div className="flex items-center gap-2">
{Icon('md')}
{icon}
</div>
)}
<div>
<div className="font-mono text-sm font-medium">{title}</div>
{description && (
<div className="text-xs text-muted-foreground">{description}</div>
)}
</div>
</div>
<div className="flex items-center gap-2">
<Badge />
</div>
</div>
</CardHeader>
{(children || (showResult && result)) && (
<CardContent className="pt-0">
{children}
{showResult && result && (
<div className={ResultClass()}>
<pre className="whitespace-pre-wrap break-words">{result}</pre>
</div>
)}
</CardContent>
)}
</Card>
);
}
+91
View File
@@ -0,0 +1,91 @@
import { Badge } from '@/components/ui/badge';
import { Loader2, XCircle, CheckCircle, Play } from 'lucide-react';
import { cn } from '@/lib/utils';
export interface ActionStatusProps {
status?: 'RUNNING' | 'SUCCESS' | 'FAILED' | 'PENDING';
isLoading?: boolean;
isError?: boolean;
_result?: string;
runningIcon?: React.ReactNode;
successIcon?: React.ReactNode;
failedIcon?: React.ReactNode;
successLabel?: string;
failedLabel?: string;
runningLabel?: string;
}
export function ActionStatus({
status,
isLoading,
isError,
_result,
runningIcon = <Loader2 className="h-3 w-3 animate-spin" />,
successIcon = <CheckCircle className="h-3 w-3" />,
failedIcon = <XCircle className="h-3 w-3" />,
successLabel = 'Success',
failedLabel = 'Failed',
runningLabel = 'Running'
}: ActionStatusProps) {
// Handle both new status-based and old boolean-based props for compatibility
const effectiveStatus = status || (
isLoading ? 'RUNNING' :
isError ? 'FAILED' : 'SUCCESS'
) as 'RUNNING' | 'SUCCESS' | 'FAILED' | 'PENDING';
const getBadge = () => {
if (effectiveStatus === 'RUNNING') {
return (
<Badge variant="secondary" className="text-xs bg-blue-100 text-blue-800 dark:bg-blue-900/20 dark:text-blue-400">
{runningIcon}
{runningLabel}
</Badge>
);
}
if (effectiveStatus === 'FAILED') {
return (
<Badge variant="destructive" className="text-xs">
{failedIcon}
{failedLabel}
</Badge>
);
}
return (
<Badge variant="secondary" className="text-xs bg-green-100 text-green-800 dark:bg-green-900/20 dark:text-green-400">
{successIcon}
{successLabel}
</Badge>
);
};
const getIcon = (size: 'sm' | 'md' = 'md') => {
const sizeClass = size === 'sm' ? 'h-3 w-3' : 'h-4 w-4';
if (effectiveStatus === 'RUNNING') return <Loader2 className={`${sizeClass} animate-spin`} />;
if (effectiveStatus === 'FAILED') return <XCircle className={`${sizeClass} text-destructive`} />;
return <Play className={sizeClass} />;
};
const getResultClass = () => {
return cn(
"p-3 rounded-md text-sm",
effectiveStatus === 'FAILED'
? "bg-destructive/10 text-destructive border border-destructive/20"
: "bg-green-50 text-green-800 dark:bg-green-900/20 dark:text-green-400 border border-green-200 dark:border-green-800/30"
);
};
const getBorderClass = () => {
return effectiveStatus === 'FAILED' ? "border-destructive/50" : "border-muted";
};
return {
Badge: getBadge,
Icon: getIcon,
ResultClass: getResultClass,
BorderClass: getBorderClass,
status: effectiveStatus
};
}