From 01a380520c595dd8894fa0604a9b6cd7048e8fd2 Mon Sep 17 00:00:00 2001 From: Chad Curtis Date: Wed, 20 Aug 2025 16:21:57 +0000 Subject: [PATCH] use shared action card component --- src/components/ai/BuildAction.tsx | 76 ++++-------------- src/components/ai/GitCommit.test.tsx | 6 +- src/components/ai/GitCommit.tsx | 97 ++++++++--------------- src/components/ai/ScriptRunner.tsx | 65 ++++----------- src/components/ai/ShellCommand.tsx | 65 ++++----------- src/components/ai/shared/ActionCard.tsx | 69 ++++++++++++++++ src/components/ai/shared/ActionStatus.tsx | 91 +++++++++++++++++++++ 7 files changed, 240 insertions(+), 229 deletions(-) create mode 100644 src/components/ai/shared/ActionCard.tsx create mode 100644 src/components/ai/shared/ActionStatus.tsx diff --git a/src/components/ai/BuildAction.tsx b/src/components/ai/BuildAction.tsx index 10a2d57d..c08d5853 100644 --- a/src/components/ai/BuildAction.tsx +++ b/src/components/ai/BuildAction.tsx @@ -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 ; - if (effectiveStatus === 'FAILED') return ; - return ; - }; - 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 ( - - -
-
- {getActionIcon()} - {actionConfig.title} -
-
- {effectiveStatus === 'RUNNING' ? ( - - - Running - - ) : effectiveStatus === 'FAILED' ? ( - - - Failed - - ) : ( - - - Success - - )} -
-
-
- - -
- {getActionDescription()} -
- - {result && ( -
-
{result}
-
- )} -
-
+ ); }); diff --git a/src/components/ai/GitCommit.test.tsx b/src/components/ai/GitCommit.test.tsx index c0905bc4..2d5b0f26 100644 --- a/src/components/ai/GitCommit.test.tsx +++ b/src/components/ai/GitCommit.test.tsx @@ -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( @@ -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(); }); diff --git a/src/components/ai/GitCommit.tsx b/src/components/ai/GitCommit.tsx index 31a2759b..5fdde51e 100644 --- a/src/components/ai/GitCommit.tsx +++ b/src/components/ai/GitCommit.tsx @@ -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 ( - - -
-
-
- - {commitEmoji} -
-
-
- {firstLine} -
-
- Git commit - {stats?.branch && ( - - - {stats.branch} - - )} - {stats?.hash && ( - - {stats.hash} - - )} -
-
-
-
- {result !== undefined ? ( - isError ? ( - - - Failed - - ) : ( - - - Committed - - ) - ) : ( - - - Committing - - )} -
-
-
+ const getCommitDescription = () => { + let desc = 'Git commit'; + if (stats?.branch) { + desc += ` • ${stats.branch} branch`; + } + if (stats?.hash) { + desc += ` • ${stats.hash}`; + } + return desc; + }; + return ( + {commitEmoji}} + result={result} + isError={isError} + className={className} + showResult={false} + runningIcon={} + runningLabel="Committing" + successLabel="Committed" + > {/* Full commit message - only show if multi-line */} {message.includes('\n') && ( - +
▶ @@ -148,19 +118,14 @@ export function GitCommit({
-
+ )} {result && ( - + <> {/* Summary */} {summary && ( -
+
{summary}
)} @@ -207,8 +172,8 @@ export function GitCommit({ )}
-
+ )} -
+
); } \ No newline at end of file diff --git a/src/components/ai/ScriptRunner.tsx b/src/components/ai/ScriptRunner.tsx index 77f08059..2de60a8c 100644 --- a/src/components/ai/ScriptRunner.tsx +++ b/src/components/ai/ScriptRunner.tsx @@ -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 ( - - -
-
-
- - {scriptInfo.icon} -
-
-
npm run {script}
-
{scriptInfo.description}
-
-
-
- {result !== undefined ? ( - isError ? ( - - - Failed - - ) : ( - - - Success - - ) - ) : ( - - - Running - - )} -
-
-
- + {scriptInfo.icon}} + result={result} + isError={isError} + className={className} + runningIcon={} + runningLabel="Running" + > {result && ( - + <> {/* Summary */} {summary && ( -
+
{summary}
)} @@ -201,8 +168,8 @@ export function ScriptRunner({ script, result, isError = false, className }: Scr )}
)} -
+ )} -
+ ); } \ No newline at end of file diff --git a/src/components/ai/ShellCommand.tsx b/src/components/ai/ShellCommand.tsx index e1902015..1f7e7d62 100644 --- a/src/components/ai/ShellCommand.tsx +++ b/src/components/ai/ShellCommand.tsx @@ -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 ( - - -
-
-
- - {commandInfo.icon} -
-
-
$ {commandInfo.fullCommand}
-
{commandInfo.description}
-
-
-
- {result !== undefined ? ( - isError ? ( - - - Failed - - ) : ( - - - Success - - ) - ) : ( - - - Running - - )} -
-
-
- + {commandInfo.icon}} + result={result} + isError={isError} + className={className} + runningIcon={} + runningLabel="Executing" + > {result && ( - + <> {/* Summary */} {summary && ( -
+
{summary}
)} @@ -210,8 +177,8 @@ export function ShellCommand({ command, result, isError = false, className }: Sh )}
)} -
+ )} -
+ ); } \ No newline at end of file diff --git a/src/components/ai/shared/ActionCard.tsx b/src/components/ai/shared/ActionCard.tsx new file mode 100644 index 00000000..034cd429 --- /dev/null +++ b/src/components/ai/shared/ActionCard.tsx @@ -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 { + 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 ( + + +
+
+ {icon && ( +
+ {Icon('md')} + {icon} +
+ )} +
+
{title}
+ {description && ( +
{description}
+ )} +
+
+
+ +
+
+
+ + {(children || (showResult && result)) && ( + + {children} + + {showResult && result && ( +
+
{result}
+
+ )} +
+ )} +
+ ); +} \ No newline at end of file diff --git a/src/components/ai/shared/ActionStatus.tsx b/src/components/ai/shared/ActionStatus.tsx new file mode 100644 index 00000000..ac845cd1 --- /dev/null +++ b/src/components/ai/shared/ActionStatus.tsx @@ -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 = , + successIcon = , + failedIcon = , + 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 ( + + {runningIcon} + {runningLabel} + + ); + } + + if (effectiveStatus === 'FAILED') { + return ( + + {failedIcon} + {failedLabel} + + ); + } + + return ( + + {successIcon} + {successLabel} + + ); + }; + + const getIcon = (size: 'sm' | 'md' = 'md') => { + const sizeClass = size === 'sm' ? 'h-3 w-3' : 'h-4 w-4'; + + if (effectiveStatus === 'RUNNING') return ; + if (effectiveStatus === 'FAILED') return ; + return ; + }; + + 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 + }; +} \ No newline at end of file