mirror of
https://github.com/snapotter-hq/SnapOtter.git
synced 2026-08-03 07:46:42 +02:00
Add complete i18n infrastructure with 21 supported languages: English, Simplified Chinese, Traditional Chinese, Japanese, Korean, Spanish, French, Italian, Brazilian Portuguese, German, Dutch, Swedish, Russian, Polish, Ukrainian, Arabic (RTL), Turkish, Hindi, Vietnamese, Indonesian, and Thai. - I18nProvider context with three-tier locale detection (user preference > navigator.languages > instance default > English) - ~1500 translation keys per locale with TypeScript-enforced completeness - Dynamic code-splitting: only the active locale is loaded at runtime - Language selectors in footer, login page, settings, and mobile sidebar - Arabic RTL support with CSS logical properties across all components - Tool names, descriptions, and categories translated via i18n helpers - Public API endpoint GET /api/v1/config/locale for instance default - Multi-script font stack (CJK, Arabic, Devanagari, Thai, Cyrillic) - format() and plural() helpers for interpolation and pluralization - API error translation mapping (translateApiError) - 36 Playwright e2e tests verifying all 21 locales load correctly - 25 unit tests for format, plural, locale detection, and completeness - Updated translations.md docs and CLAUDE.md conventions
223 lines
7.4 KiB
TypeScript
223 lines
7.4 KiB
TypeScript
import { Download } from "lucide-react";
|
|
import { useEffect, useRef, useState } from "react";
|
|
import { ProgressCard } from "@/components/common/progress-card";
|
|
import { useTranslation } from "@/contexts/i18n-context";
|
|
import { useToolProcessor } from "@/hooks/use-tool-processor";
|
|
import { format } from "@/lib/format";
|
|
import { useFileStore } from "@/stores/file-store";
|
|
|
|
const LOSSY_FORMATS = new Set(["jpeg", "webp", "avif", "jxl"]);
|
|
|
|
export interface RedEyeRemovalControlsProps {
|
|
settings?: Record<string, unknown>;
|
|
onChange?: (settings: Record<string, unknown>) => void;
|
|
}
|
|
|
|
export function RedEyeRemovalControls({
|
|
settings: initialSettings,
|
|
onChange,
|
|
}: RedEyeRemovalControlsProps) {
|
|
const [sensitivity, setSensitivity] = useState(50);
|
|
const [strength, setStrength] = useState(70);
|
|
const [outputFormat, setOutputFormat] = useState<
|
|
"original" | "png" | "jpeg" | "webp" | "avif" | "jxl"
|
|
>("original");
|
|
const [quality, setQuality] = useState(90);
|
|
|
|
// One-time init from pipeline settings
|
|
const initializedRef = useRef(false);
|
|
useEffect(() => {
|
|
if (!initialSettings || initializedRef.current) return;
|
|
initializedRef.current = true;
|
|
if (initialSettings.sensitivity != null) setSensitivity(Number(initialSettings.sensitivity));
|
|
if (initialSettings.strength != null) setStrength(Number(initialSettings.strength));
|
|
if (initialSettings.format != null)
|
|
setOutputFormat(
|
|
initialSettings.format as "original" | "png" | "jpeg" | "webp" | "avif" | "jxl",
|
|
);
|
|
if (initialSettings.quality != null) setQuality(Number(initialSettings.quality));
|
|
}, [initialSettings]);
|
|
|
|
// Emit settings on change
|
|
const onChangeRef = useRef(onChange);
|
|
useEffect(() => {
|
|
onChangeRef.current = onChange;
|
|
});
|
|
|
|
useEffect(() => {
|
|
onChangeRef.current?.({
|
|
sensitivity,
|
|
strength,
|
|
format: outputFormat,
|
|
quality,
|
|
});
|
|
}, [sensitivity, strength, outputFormat, quality]);
|
|
|
|
const tabClass = (active: boolean) =>
|
|
`flex-1 text-xs py-1.5 rounded ${active ? "bg-primary text-primary-foreground" : "bg-muted text-muted-foreground hover:bg-muted/80"}`;
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
{/* Sensitivity slider */}
|
|
<div>
|
|
<div className="flex justify-between items-center">
|
|
<p className="text-sm font-medium text-muted-foreground">Sensitivity</p>
|
|
<span className="text-sm font-mono tabular-nums font-medium">{sensitivity}</span>
|
|
</div>
|
|
<input
|
|
type="range"
|
|
min={0}
|
|
max={100}
|
|
step={1}
|
|
value={sensitivity}
|
|
onChange={(e) => setSensitivity(Number(e.target.value))}
|
|
className="w-full h-1.5 rounded-full appearance-none bg-muted accent-primary"
|
|
/>
|
|
<div className="flex justify-between text-[10px] text-muted-foreground mt-0.5">
|
|
<span>Strict</span>
|
|
<span>Aggressive</span>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Correction Strength slider */}
|
|
<div>
|
|
<div className="flex justify-between items-center">
|
|
<p className="text-sm font-medium text-muted-foreground">Correction Strength</p>
|
|
<span className="text-sm font-mono tabular-nums font-medium">{strength}</span>
|
|
</div>
|
|
<input
|
|
type="range"
|
|
min={0}
|
|
max={100}
|
|
step={1}
|
|
value={strength}
|
|
onChange={(e) => setStrength(Number(e.target.value))}
|
|
className="w-full h-1.5 rounded-full appearance-none bg-muted accent-primary"
|
|
/>
|
|
<div className="flex justify-between text-[10px] text-muted-foreground mt-0.5">
|
|
<span>Subtle</span>
|
|
<span>Dark</span>
|
|
</div>
|
|
</div>
|
|
|
|
<div className="border-t border-border pt-3" />
|
|
|
|
{/* Output format */}
|
|
<div>
|
|
<p className="text-xs text-muted-foreground mb-1">Output Format</p>
|
|
<div className="grid grid-cols-3 gap-1">
|
|
{(["original", "png", "jpeg", "webp", "avif", "jxl"] as const).map((f) => (
|
|
<button
|
|
key={f}
|
|
type="button"
|
|
onClick={() => setOutputFormat(f)}
|
|
className={tabClass(outputFormat === f)}
|
|
>
|
|
{f === "original" ? "Original" : f.toUpperCase()}
|
|
</button>
|
|
))}
|
|
</div>
|
|
</div>
|
|
|
|
{/* Quality slider (lossy formats only) */}
|
|
{LOSSY_FORMATS.has(outputFormat) && (
|
|
<div>
|
|
<div className="flex justify-between items-center">
|
|
<p className="text-sm font-medium text-muted-foreground">Quality</p>
|
|
<span className="text-sm font-mono tabular-nums font-medium">{quality}</span>
|
|
</div>
|
|
<input
|
|
type="range"
|
|
min={1}
|
|
max={100}
|
|
step={1}
|
|
value={quality}
|
|
onChange={(e) => setQuality(Number(e.target.value))}
|
|
className="w-full h-1.5 rounded-full appearance-none bg-muted accent-primary"
|
|
/>
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function RedEyeRemovalSettings() {
|
|
const { t } = useTranslation();
|
|
const { files } = useFileStore();
|
|
const {
|
|
processFiles,
|
|
processAllFiles,
|
|
processing,
|
|
error,
|
|
downloadUrl,
|
|
originalSize,
|
|
processedSize,
|
|
progress,
|
|
} = useToolProcessor("red-eye-removal");
|
|
const [settings, setSettings] = useState<Record<string, unknown>>({});
|
|
|
|
const handleProcess = () => {
|
|
if (files.length > 1) {
|
|
processAllFiles(files, settings);
|
|
} else {
|
|
processFiles(files, settings);
|
|
}
|
|
};
|
|
|
|
const hasFile = files.length > 0;
|
|
const hasMultiple = files.length > 1;
|
|
|
|
return (
|
|
<div className="space-y-4">
|
|
<RedEyeRemovalControls onChange={setSettings} />
|
|
|
|
{/* Error */}
|
|
{error && <p className="text-xs text-red-500">{error}</p>}
|
|
|
|
{/* Size info */}
|
|
{originalSize != null && processedSize != null && (
|
|
<div className="text-xs text-muted-foreground space-y-0.5">
|
|
<p>Original: {(originalSize / 1024).toFixed(1)} KB</p>
|
|
<p>Fixed: {(processedSize / 1024).toFixed(1)} KB</p>
|
|
</div>
|
|
)}
|
|
|
|
{/* Process button / progress */}
|
|
{processing ? (
|
|
<ProgressCard
|
|
active={processing}
|
|
phase={progress.phase === "idle" ? "uploading" : progress.phase}
|
|
label={hasMultiple ? `Fixing red eye in ${files.length} images` : "Fixing red eye"}
|
|
percent={progress.percent}
|
|
elapsed={progress.elapsed}
|
|
/>
|
|
) : (
|
|
<button
|
|
type="button"
|
|
data-testid="red-eye-removal-submit"
|
|
onClick={handleProcess}
|
|
disabled={!hasFile || processing}
|
|
className="w-full py-2.5 rounded-lg bg-primary text-primary-foreground font-medium disabled:opacity-50 disabled:cursor-not-allowed flex items-center justify-center gap-2"
|
|
>
|
|
{hasMultiple
|
|
? format(t.toolSettings["red-eye-removal"].submitBatch, { count: files.length })
|
|
: t.toolSettings["red-eye-removal"].submit}
|
|
</button>
|
|
)}
|
|
|
|
{/* Download (single file - batch uses Download All ZIP in tool-page) */}
|
|
{!hasMultiple && downloadUrl && (
|
|
<a
|
|
href={downloadUrl}
|
|
download
|
|
data-testid="red-eye-removal-download"
|
|
className="w-full py-2.5 rounded-lg border border-primary text-primary font-medium flex items-center justify-center gap-2 hover:bg-primary/5"
|
|
>
|
|
<Download className="h-4 w-4" />
|
|
Download
|
|
</a>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|