forked from op7418/CodePilot
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImageGenCard.tsx
More file actions
190 lines (173 loc) · 5.89 KB
/
ImageGenCard.tsx
File metadata and controls
190 lines (173 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
'use client';
import { useState, useCallback } from 'react';
import { DownloadSimple, ArrowsCounterClockwise, PaintBrush } from '@/components/ui/icon';
import { cn } from '@/lib/utils';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';
import { ImageLightbox } from './ImageLightbox';
import { useTranslation } from '@/hooks/useTranslation';
import type { TranslationKey } from '@/i18n';
interface ImageGenImage {
data: string;
mimeType: string;
localPath?: string;
}
interface ImageGenCardProps {
images: ImageGenImage[];
prompt: string;
aspectRatio?: string;
imageSize?: string;
model?: string;
onRegenerate?: () => void;
referenceImages?: Array<{ mimeType: string; data: string; localPath?: string }>;
}
function imageUrl(img: ImageGenImage): string {
if (img.localPath) {
return `/api/media/serve?path=${encodeURIComponent(img.localPath)}`;
}
return `data:${img.mimeType};base64,${img.data}`;
}
export function ImageGenCard({
images,
prompt,
aspectRatio,
imageSize,
model,
onRegenerate,
referenceImages,
}: ImageGenCardProps) {
const { t } = useTranslation();
const [lightboxOpen, setLightboxOpen] = useState(false);
const [lightboxIndex, setLightboxIndex] = useState(0);
const lightboxImages = images.map((img, i) => ({
src: imageUrl(img),
alt: `${t('imageGen.generated' as TranslationKey)} ${i + 1}`,
}));
const handlePreview = useCallback((index: number) => {
setLightboxIndex(index);
setLightboxOpen(true);
}, []);
const handleDownload = useCallback(async (img: ImageGenImage, index: number) => {
const url = imageUrl(img);
const ext = img.mimeType.split('/')[1] || 'png';
const filename = `generated-${Date.now()}-${index + 1}.${ext}`;
try {
const response = await fetch(url);
const blob = await response.blob();
const blobUrl = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = blobUrl;
link.download = filename;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
URL.revokeObjectURL(blobUrl);
} catch {
// Fallback: open in new tab
window.open(url, '_blank');
}
}, []);
if (images.length === 0) return null;
const gridCols =
images.length === 1
? 'grid-cols-1 max-w-sm'
: images.length === 2
? 'grid-cols-2 max-w-md'
: 'grid-cols-3 max-w-lg';
return (
<div className="rounded-lg border border-border/50 bg-card p-3 space-y-2.5">
{/* Image grid */}
<div className={cn('grid gap-2', gridCols)}>
{images.map((img, i) => (
<Button
key={i}
variant="ghost"
onClick={() => handlePreview(i)}
className="relative group overflow-hidden rounded-md border border-border/30 bg-muted/30 p-0 h-auto"
>
{/* eslint-disable-next-line @next/next/no-img-element */}
<img
src={imageUrl(img)}
alt={`${t('imageGen.generated' as TranslationKey)} ${i + 1}`}
className="w-full h-auto object-cover transition-transform group-hover:scale-[1.02]"
/>
<div className="absolute inset-0 bg-black/0 group-hover:bg-black/10 transition-colors" />
</Button>
))}
</div>
{/* Prompt text */}
<p className="text-sm text-foreground/80 leading-relaxed">{prompt}</p>
{/* Reference images (垫图) */}
{referenceImages && referenceImages.length > 0 && (
<div>
<span className="text-[10px] text-muted-foreground/60 mb-1 block">
{t('imageGen.referenceImages' as TranslationKey)}
</span>
<div className="flex gap-1.5 flex-wrap">
{referenceImages.map((ref, i) => {
const src = ref.localPath
? `/api/media/serve?path=${encodeURIComponent(ref.localPath)}`
: ref.data
? `data:${ref.mimeType};base64,${ref.data}`
: '';
if (!src) return null;
return (
<div key={i} className="w-12 h-12 rounded border border-border/30 overflow-hidden bg-muted/30">
{/* eslint-disable-next-line @next/next/no-img-element */}
<img src={src} alt={`Reference ${i + 1}`} className="w-full h-full object-cover" />
</div>
);
})}
</div>
</div>
)}
{/* Badges + Actions */}
<div className="flex items-center justify-between gap-2">
<div className="flex items-center gap-1.5 flex-wrap">
{model && (
<Badge variant="secondary" className="text-[10px] gap-1">
<PaintBrush size={12} />
{model}
</Badge>
)}
{aspectRatio && (
<Badge variant="outline" className="text-[10px]">
{aspectRatio}
</Badge>
)}
{imageSize && (
<Badge variant="outline" className="text-[10px]">
{imageSize}
</Badge>
)}
</div>
<div className="flex items-center gap-1">
<Button
variant="ghost"
size="icon-xs"
onClick={() => handleDownload(images[0], 0)}
title={t('imageGen.download' as TranslationKey)}
>
<DownloadSimple size={14} />
</Button>
{onRegenerate && (
<Button
variant="ghost"
size="icon-xs"
onClick={onRegenerate}
title={t('imageGen.regenerate' as TranslationKey)}
>
<ArrowsCounterClockwise size={14} />
</Button>
)}
</div>
</div>
<ImageLightbox
images={lightboxImages}
initialIndex={lightboxIndex}
open={lightboxOpen}
onOpenChange={setLightboxOpen}
/>
</div>
);
}