checkpoint2

This commit is contained in:
2026-11-06 10:53:55 +05:30
parent 7f1e77ad6d
commit 69d4195718
20 changed files with 1306 additions and 2963 deletions
+169 -212
View File
@@ -8,15 +8,10 @@ import {
RefreshCw,
Download,
Smartphone,
Sparkles,
Loader2,
Image as ImageIcon,
Zap,
ChevronRight,
SlidersHorizontal,
Image as ImageIcon,
} from 'lucide-react';
import { ToolItem, AppItem, VariantSpec, resolveLocalised } from '@/types';
import { BeforeAfterSlider } from '../ui/BeforeAfterSlider';
import { CardBeforeAfterSlider } from '../ui/CardBeforeAfterSlider';
interface TryItWidgetProps {
@@ -38,7 +33,6 @@ export function TryItWidget({
className = '',
samplePresets,
ctaText,
onSwitchToCompare,
}: TryItWidgetProps) {
const [selectedFile, setSelectedFile] = useState<File | null>(null);
const [previewUrl, setPreviewUrl] = useState<string | null>(null);
@@ -56,53 +50,35 @@ export function TryItWidget({
? samplePresets
: [
{
label: 'Sample Photo',
url: (tool as any).media?.before || '/management/pages/home/media/sample_portrait_1.jpg',
},
{
label: 'Natural Studio Model',
url: '/management/pages/home/media/sample_portrait_2.jpg',
},
{
label: 'Vintage Archival Shot',
url: '/management/pages/home/media/sample_portrait_3.jpg',
label: 'Sample portrait',
url: (tool as any).media?.before || '/management/pages/home/sections/our-tools/blemish-remover/before.jpg',
resultUrl: (tool as any).media?.after || '/management/pages/home/sections/our-tools/blemish-remover/after.jpg',
},
];
const handleFileChange = (file: File) => {
const maxMB = tool.tryIt?.limits?.maxMB || 12;
const acceptedTypes = ['image/jpeg', 'image/png', 'image/webp'];
if (!acceptedTypes.includes(file.type)) {
const validTypes = ['image/jpeg', 'image/png', 'image/webp'];
if (!validTypes.includes(file.type)) {
setStatus('error');
setErrorMessage('Choose a JPG, PNG, or WebP image.');
setErrorMessage('Please upload a valid JPG, PNG, or WebP photo.');
return;
}
if (file.size > maxMB * 1024 * 1024) {
const maxBytes = (tool.tryIt?.limits?.maxMB || 12) * 1024 * 1024;
if (file.size > maxBytes) {
setStatus('error');
setErrorMessage(`File exceeds ${maxMB}MB limit.`);
setErrorMessage(`Image exceeds max size of ${tool.tryIt?.limits?.maxMB || 12}MB.`);
return;
}
const objectUrl = URL.createObjectURL(file);
setSelectedFile(file);
const objectUrl = URL.createObjectURL(file);
setPreviewUrl(objectUrl);
setResultUrl(null);
setStatus('selected');
setErrorMessage('');
};
const loadSamplePreset = async (url: string) => {
try {
setStatus('processing');
const res = await fetch(url);
const blob = await res.blob();
const file = new File([blob], 'sample.jpg', { type: 'image/jpeg' });
handleFileChange(file);
} catch {
setStatus('idle');
}
};
const handleDrop = (e: React.DragEvent) => {
e.preventDefault();
setIsDragOver(false);
@@ -111,98 +87,113 @@ export function TryItWidget({
}
};
const loadSamplePreset = async (presetUrl: string, sampleResult?: string) => {
try {
setStatus('selected');
setPreviewUrl(presetUrl);
if (sampleResult) {
setResultUrl(sampleResult);
}
const res = await fetch(presetUrl);
const blob = await res.blob();
const file = new File([blob], 'sample_photo.jpg', { type: 'image/jpeg' });
setSelectedFile(file);
setErrorMessage('');
} catch {
setStatus('error');
setErrorMessage('Failed to load sample image.');
}
};
const runTryIt = async () => {
if (!selectedFile) return;
if (!selectedFile && !previewUrl) return;
setStatus('processing');
setErrorMessage('');
const formData = new FormData();
formData.append('image', selectedFile);
if (activeVariant) {
formData.append('variant', activeVariant);
}
try {
const formData = new FormData();
if (selectedFile) {
formData.append('image', selectedFile);
} else if (previewUrl) {
const res = await fetch(previewUrl);
const blob = await res.blob();
formData.append('image', blob, 'preset.jpg');
}
if (activeVariant) {
formData.append('variant', activeVariant);
}
const response = await fetch(`/api/try/${tool.id}`, {
method: 'POST',
body: formData,
});
const remainingHeader = response.headers.get('X-Remaining-Tries');
if (remainingHeader) {
setRemainingTries(parseInt(remainingHeader, 10));
}
if (response.status === 429) {
const data = await response.json();
setStatus('limit');
setErrorMessage(data.messageKey === 'dailyBudgetExceeded'
? "Today's free try budget has been reached. Open in mobile app for unlimited instant access."
: "You have reached your daily free try limit on web. Open the app for 50 bonus credits!");
setRemainingTries(0);
return;
}
if (response.status === 503) {
setStatus('unavailable');
setErrorMessage('This AI tool is temporarily undergoing server maintenance. Please try in the mobile app.');
return;
}
if (!response.ok) {
const errorJson = await response.json().catch(() => null);
const errData = await response.json().catch(() => ({}));
setStatus('error');
setErrorMessage(errorJson?.error?.message || 'Processing failed. Please try again.');
setErrorMessage(errData?.error?.message || 'Inference call failed. Please try another image.');
return;
}
const blob = await response.blob();
const resBlobUrl = URL.createObjectURL(blob);
setResultUrl(resBlobUrl);
const url = URL.createObjectURL(blob);
setResultUrl(url);
setStatus('success');
} catch (err: any) {
setRemainingTries((prev) => Math.max(0, prev - 1));
} catch {
setStatus('error');
setErrorMessage(err.message || 'Network connection failed. Please retry.');
setErrorMessage('Network error connecting to inference API.');
}
};
const reset = () => {
setSelectedFile(null);
if (previewUrl) URL.revokeObjectURL(previewUrl);
if (resultUrl) URL.revokeObjectURL(resultUrl);
setPreviewUrl(null);
setResultUrl(null);
setStatus('idle');
setErrorMessage('');
if (fileInputRef.current) fileInputRef.current.value = '';
};
return (
<div className={`relative bg-white rounded-3xl p-5 sm:p-8 lg:p-10 border border-slate-900/[0.08] shadow-cream-md overflow-hidden ${className}`}>
{/* Top Header & Tries Counter */}
<div className="flex items-center justify-between gap-4 pb-4 sm:pb-5 border-b border-slate-100">
<div className="flex items-center gap-2.5">
<div className="w-8 h-8 rounded-xl bg-blue-50 border border-blue-200/80 flex items-center justify-center text-blue-600">
<UploadCloud className="w-4 h-4" />
</div>
<span className="text-xs sm:text-sm font-display font-semibold text-slate-800">
Try Online Free
</span>
<span className="hidden sm:inline-flex items-center gap-1 px-2.5 py-0.5 rounded-full text-[10px] font-mono font-semibold text-blue-700 bg-blue-50 border border-blue-200/80">
Lossless AI
</span>
<div className={`w-full ${className}`}>
{/* Top Header Row */}
<div className="flex items-center justify-between pb-3 border-b border-rule text-xs font-body">
<div className="flex items-center gap-2">
<span className="font-medium text-ink">{resolveLocalised(tool.name, locale)}</span>
<span className="text-muted">· Web trial</span>
</div>
<div className="inline-flex items-center gap-2 bg-emerald-50 border border-emerald-200/90 px-3.5 py-1 rounded-full text-xs font-mono text-emerald-800 font-semibold shadow-2xs">
<span className="w-2 h-2 rounded-full bg-emerald-500 animate-pulse"></span>
<span>{remainingTries} free tries today</span>
<div className="text-muted">
<span>{remainingTries} free tries remaining today</span>
</div>
</div>
{/* Main Body per Status */}
<div className="mt-7">
{/* Error Message */}
{errorMessage && (
<div className="mt-4 p-3 bg-paper-2 border border-rule rounded-card flex items-center gap-2 text-xs text-ink font-body">
<AlertCircle className="w-4 h-4 text-accent shrink-0" aria-hidden="true" />
<span>{errorMessage}</span>
</div>
)}
{/* Main Body */}
<div className="mt-5">
{status === 'idle' && (
<div className="space-y-6">
{/* Drag & Drop Upload Zone - Prominent & Spacious */}
<div className="space-y-4">
{/* Drag & Drop Upload Zone */}
<div
onDrop={handleDrop}
onDragOver={(e) => {
@@ -220,10 +211,10 @@ export function TryItWidget({
role="button"
tabIndex={0}
aria-label="Upload photo to edit"
className={`border-2 border-dashed rounded-3xl p-8 sm:p-12 lg:p-16 text-center cursor-pointer transition-all duration-300 min-h-[380px] sm:min-h-[440px] lg:min-h-[460px] flex flex-col items-center justify-center group ${
className={`border border-dashed rounded-card p-8 sm:p-12 text-center cursor-pointer transition-colors duration-150 min-h-[300px] flex flex-col items-center justify-center ${
isDragOver
? 'border-blue-500 bg-blue-50/70 scale-[1.01] shadow-lg shadow-blue-500/10'
: 'border-slate-300/90 hover:border-blue-500 bg-gradient-to-b from-blue-50/30 via-white to-slate-50/40 hover:bg-blue-50/20'
? 'border-accent bg-paper-2'
: 'border-rule hover:border-ink bg-paper-2/60'
}`}
>
<input
@@ -233,91 +224,89 @@ export function TryItWidget({
className="hidden"
onChange={(e) => e.target.files?.[0] && handleFileChange(e.target.files[0])}
/>
<div className="w-20 h-20 sm:w-24 sm:h-24 mx-auto mb-6 rounded-3xl bg-blue-50 border border-blue-200/80 flex items-center justify-center text-blue-600 group-hover:scale-110 group-hover:bg-blue-600 group-hover:text-white transition-all duration-300 shadow-sm">
<UploadCloud className="w-10 h-10 sm:w-12 sm:h-12" />
<div className="mb-3 text-accent">
<UploadCloud className="w-8 h-8" aria-hidden="true" />
</div>
<div className="inline-flex items-center gap-2.5 px-9 py-4 rounded-full bg-gradient-to-r from-blue-600 via-indigo-600 to-blue-700 hover:from-blue-700 hover:to-indigo-800 text-white font-bold text-sm sm:text-base shadow-lg shadow-blue-500/25 group-hover:shadow-xl group-hover:scale-105 active:scale-95 transition-all mb-4">
<UploadCloud className="w-5 h-5" />
<span>Upload Image</span>
<div className="mb-2">
<span className="btn-primary text-xs">
Choose photo to upload
</span>
</div>
<p className="text-base sm:text-lg font-display font-semibold text-slate-800">
or drag and drop your photo anywhere here
<p className="text-sm font-body text-ink-2 mt-2">
or drag and drop your photo into this workspace
</p>
<p className="mt-2 text-xs font-mono text-slate-500">
Supports JPG, PNG, WEBP up to {tool.tryIt?.limits?.maxMB || 12}MB • Lossless 4K processing
<p className="mt-1 text-xs text-muted font-body">
Supports JPG, PNG, WebP up to {tool.tryIt?.limits?.maxMB || 12}MB
</p>
</div>
{/* Quick Sample Presets */}
<div className="pt-1">
<p className="text-xs font-mono uppercase tracking-wider text-slate-500 font-semibold mb-3">
Or test instantly with a sample photo:
</p>
<div className="flex flex-wrap gap-2.5">
{activePresets.map((preset, idx) => (
<button
key={idx}
onClick={() => loadSamplePreset(preset.url)}
className="inline-flex items-center gap-2 px-4 py-2.5 rounded-xl text-xs font-semibold bg-white border border-slate-200 text-slate-700 hover:bg-blue-50 hover:border-blue-300 hover:text-blue-700 transition-all active:scale-95 shadow-2xs"
>
<ImageIcon className="w-3.5 h-3.5 text-slate-400" />
<span>{preset.label}</span>
</button>
))}
</div>
{/* Quick Sample Preset */}
<div className="pt-1 flex items-center gap-2 text-xs font-body text-muted">
<span>Or test preset:</span>
{activePresets.map((preset, idx) => (
<button
key={idx}
type="button"
onClick={() => loadSamplePreset(preset.url, preset.resultUrl)}
className="inline-flex items-center gap-1.5 px-2.5 py-1 rounded bg-paper-2 hover:bg-paper-3 border border-rule text-ink transition-colors"
>
<ImageIcon className="w-3.5 h-3.5 text-muted" aria-hidden="true" />
<span>{preset.label}</span>
</button>
))}
</div>
</div>
)}
{status === 'selected' && previewUrl && (
<div className="space-y-6">
<div className="grid grid-cols-1 md:grid-cols-2 gap-6 items-center">
{/* Image Preview Card */}
<div className="relative aspect-[4/3] rounded-3xl overflow-hidden border border-slate-200 bg-slate-100 shadow-sm">
{/* eslint-disable-next-line @next/next/no-img-element */}
<div className="space-y-5">
<div className="grid grid-cols-1 md:grid-cols-2 gap-5 items-start">
{/* Preview */}
<div className="relative aspect-[4/3] rounded-card overflow-hidden border border-rule bg-paper-2">
<img src={previewUrl} alt="Selected photo" className="w-full h-full object-cover" />
<button
type="button"
onClick={reset}
className="absolute top-3 right-3 bg-slate-900/80 hover:bg-slate-900 text-white text-xs font-mono px-3 py-1.5 rounded-xl backdrop-blur-md transition-colors"
className="absolute top-2.5 right-2.5 bg-paper-hi text-ink text-xs px-2.5 py-1 rounded border border-rule hover:border-ink transition-colors font-body shadow-sm"
>
Change Photo
Change photo
</button>
</div>
{/* Mode Selection & Run Action */}
<div className="space-y-5">
<div className="space-y-4">
{variants.length > 0 && (
<div>
<label className="block text-xs font-mono uppercase tracking-wider text-slate-600 font-semibold mb-2.5">
{resolveLocalised(tool.tryIt?.variantControl?.label, locale, 'Select Retouch Mode')}
<label className="block text-xs font-body text-muted mb-2">
{resolveLocalised(tool.tryIt?.variantControl?.label, locale, 'Retouch mode')}
</label>
<div className="space-y-2">
<div className="space-y-1.5">
{variants.map((v) => (
<label
key={v.id}
className={`flex items-center justify-between p-3.5 rounded-2xl border cursor-pointer transition-all duration-200 ${
className={`flex items-center justify-between p-2.5 rounded-card border cursor-pointer transition-colors duration-100 ${
activeVariant === v.id
? 'border-blue-600 bg-blue-50/70 text-blue-900 shadow-sm'
: 'border-slate-200 hover:border-slate-300 bg-white'
? 'border-accent bg-paper-hi text-ink'
: 'border-rule hover:border-ink bg-paper-2 text-ink-2'
}`}
>
<div className="flex items-center space-x-3">
<div className="flex items-center space-x-2.5">
<input
type="radio"
name="variant"
value={v.id}
checked={activeVariant === v.id}
onChange={() => setActiveVariant(v.id)}
className="text-blue-600 focus:ring-blue-500 h-4 w-4"
className="accent-[var(--color-accent)] h-3.5 w-3.5"
/>
<div>
<p className="text-xs sm:text-sm font-bold text-slate-900">
<p className="text-xs font-body font-medium text-ink">
{resolveLocalised(v.label, locale, v.description || v.id)}
</p>
{v.description && (
<p className="text-[11px] text-slate-500">{v.description}</p>
<p className="text-[11px] text-muted font-body">{v.description}</p>
)}
</div>
</div>
@@ -327,13 +316,12 @@ export function TryItWidget({
</div>
)}
{/* Run Button */}
<button
type="button"
onClick={runTryIt}
className="w-full py-4 px-6 rounded-2xl font-bold text-xs sm:text-sm text-white bg-gradient-to-r from-blue-600 to-indigo-600 hover:from-blue-700 hover:to-indigo-700 shadow-md shadow-blue-500/25 transition-all flex items-center justify-center gap-2 active:scale-98"
className="btn-primary w-full text-center"
>
<Sparkles className="w-4 h-4 text-white" />
<span>{ctaText || `Run ${resolveLocalised(tool.name, locale)}`}</span>
<span>{ctaText || `Process ${resolveLocalised(tool.name, locale)}`}</span>
</button>
</div>
</div>
@@ -341,120 +329,89 @@ export function TryItWidget({
)}
{status === 'processing' && (
<div className="py-16 sm:py-20 text-center space-y-5">
<div className="relative inline-flex p-5 rounded-3xl bg-blue-50 border border-blue-200 text-blue-600 shadow-sm">
<Loader2 className="w-10 h-10 animate-spin text-blue-600" />
<span className="absolute -top-1 -right-1 flex h-3 w-3">
<span className="animate-ping absolute inline-flex h-full w-full rounded-full bg-blue-400 opacity-75"></span>
<span className="relative inline-flex rounded-full h-3 w-3 bg-blue-500"></span>
</span>
<div className="py-14 text-center space-y-4">
<div className="inline-flex p-4 rounded-card bg-paper-2 border border-rule text-accent">
<Loader2 className="w-8 h-8 animate-spin text-accent" aria-hidden="true" />
</div>
<div>
<h4 className="text-lg font-display font-bold text-slate-900">
CRE Neural Vision Engine Processing...
<h4 className="text-base font-body font-semibold text-ink">
Processing neural layers…
</h4>
<p className="text-xs sm:text-sm text-slate-500 max-w-sm mx-auto mt-1 font-mono">
Synthesizing deep neural layers while preserving natural textures and micro-details.
<p className="text-xs text-muted max-w-sm mx-auto mt-1 font-body">
Synthesizing sub-pixel output in volatile GPU memory while preserving skin pores and edges.
</p>
</div>
</div>
)}
{status === 'success' && previewUrl && resultUrl && (
<div className="space-y-6">
<BeforeAfterSlider
beforeUrl={previewUrl}
afterUrl={resultUrl}
beforeLabel="Original"
afterLabel="CRE AI Result"
caption="Drag slider horizontally to inspect precision AI results and natural textures."
/>
<div className="space-y-5">
<div className="aspect-[4/3] rounded-card overflow-hidden bg-paper-2 border border-rule">
<CardBeforeAfterSlider
beforeUrl={previewUrl}
afterUrl={resultUrl}
beforeLabel="Original"
afterLabel="Enhanced"
/>
</div>
{/* Actions: Download & Reset */}
<div className="flex flex-wrap items-center justify-between gap-3 pt-2">
<button
type="button"
onClick={reset}
className="inline-flex items-center gap-2 px-4 py-2.5 rounded-xl text-xs sm:text-sm font-semibold border border-slate-300 bg-slate-50 hover:bg-slate-100 text-slate-700 transition-colors"
className="btn-secondary"
>
<RefreshCw className="w-4 h-4 text-slate-500" />
<span>Process Another Photo</span>
<RefreshCw className="w-3.5 h-3.5" aria-hidden="true" />
<span>Process another photo</span>
</button>
<a
href={resultUrl}
download={`${tool.slug}-result.png`}
className="inline-flex items-center gap-2 px-6 py-2.5 rounded-xl text-xs sm:text-sm font-bold text-white bg-emerald-600 hover:bg-emerald-700 shadow-md shadow-emerald-500/20 transition-all hover:scale-105 active:scale-95"
className="btn-primary"
>
<Download className="w-4 h-4" />
<span>Download Result (HD)</span>
<Download className="w-3.5 h-3.5" aria-hidden="true" />
<span>Download output</span>
</a>
</div>
{/* Result App Handoff Card */}
{/* Mobile App Handoff */}
{homeApp && (
<div className="mt-6 p-6 rounded-3xl bg-slate-50 border border-slate-200 flex flex-col sm:flex-row items-start sm:items-center justify-between gap-5">
<div className="space-y-1">
<div className="flex items-center gap-2">
<Smartphone className="w-4 h-4 text-indigo-600" />
<span className="text-[11px] font-mono uppercase tracking-widest text-indigo-700 font-bold">
Need 4K Lossless & Batch Processing?
</span>
</div>
<h5 className="font-display font-bold text-slate-900 text-base">
Open in {resolveLocalised(homeApp.name, locale)}
</h5>
<p className="text-xs text-slate-600">
Get 50 free HD export credits on first launch with promo code{' '}
<span className="font-mono font-bold text-blue-600 bg-blue-50 px-1 py-0.5 rounded border border-blue-200">{homeApp.welcomeBonus?.code || 'TRYWEB50'}</span>.
<div className="mt-4 p-4 rounded-card bg-paper-2 border border-rule flex flex-col sm:flex-row items-start sm:items-center justify-between gap-3">
<div className="space-y-0.5">
<span className="text-xs font-body font-semibold text-ink">
Need batch processing or 4K lossless export?
</span>
<p className="text-xs text-muted font-body">
Download {resolveLocalised(homeApp.name, locale)} on your phone for unlimited processing.
</p>
</div>
<Link
href={`/go/${tool.slug}/${homeApp.slug}`}
className="w-full sm:w-auto shrink-0 px-6 py-3 rounded-full text-xs font-bold text-white bg-blue-600 hover:bg-blue-700 shadow-sm transition-all text-center flex items-center justify-center gap-2 hover:scale-105"
href={`/apps/${homeApp.slug}`}
className="btn-secondary shrink-0 text-xs"
>
<span>Open in Mobile App</span>
<ChevronRight className="w-4 h-4" />
<Smartphone className="w-3.5 h-3.5 text-accent" aria-hidden="true" />
<span>Get mobile app</span>
</Link>
</div>
)}
</div>
)}
{(status === 'limit' || status === 'error' || status === 'unavailable') && (
<div className="p-7 sm:p-9 rounded-3xl bg-amber-50 border border-amber-200 text-center space-y-4">
<div className="w-12 h-12 mx-auto rounded-2xl bg-amber-100 border border-amber-300 flex items-center justify-center text-amber-700">
<AlertCircle className="w-6 h-6" />
</div>
<div>
<h4 className="text-base font-display font-bold text-slate-900">
{status === 'limit'
? 'Daily Free Web Limit Reached'
: status === 'unavailable'
? 'Service Temporarily Offline'
: 'Processing Issue'}
</h4>
<p className="text-xs sm:text-sm text-slate-600 max-w-md mx-auto mt-1 font-mono">
{errorMessage}
</p>
</div>
<div className="flex flex-wrap items-center justify-center gap-3 pt-2">
<button
onClick={reset}
className="px-4 py-2 rounded-xl text-xs font-semibold border border-slate-300 bg-white hover:bg-slate-100 text-slate-700"
>
Try Again
</button>
{homeApp && (
<Link
href={`/go/${tool.slug}/${homeApp.slug}`}
className="px-5 py-2 rounded-xl text-xs font-bold text-white bg-blue-600 hover:bg-blue-700 shadow-sm"
>
Open in {resolveLocalised(homeApp.name, locale)}
</Link>
)}
</div>
{status === 'limit' && (
<div className="p-8 text-center space-y-4 bg-paper-2 border border-rule rounded-card">
<h4 className="text-base font-body font-semibold text-ink">
Daily browser limit reached
</h4>
<p className="text-xs text-muted max-w-sm mx-auto font-body">
You have completed all free browser sessions for today. Download our mobile app for unlimited inferences.
</p>
{homeApp && (
<Link href={`/apps/${homeApp.slug}`} className="btn-primary inline-flex">
<span>Open {resolveLocalised(homeApp.name, locale)}</span>
</Link>
)}
</div>
)}
</div>