sd-text-fix / ui /src /components /Batch.tsx
Kazzze's picture
Update to current version
2a261b0 verified
Raw History Blame Contribute Delete
5.53 kB
import { useEffect, useState } from 'react'
import { FolderOpen, LoaderCircle, Play, Square, X } from 'lucide-react'
import { api, type BatchState } from '@/api'
import { useStore } from '@/store'
import { Button } from '@/components/ui/button'
import { Field, Input, SwitchField, Textarea } from '@/components/ui/controls'
/** A whole folder at once: every image in it gets the list of replacements, with the look and
* the model settings of the editor, and goes into a new folder next to the host's outputs. */
export function Batch({ onClose }: { onClose: () => void }) {
const options = useStore((state) => state.options)
const look = useStore((state) => state.look)
const model = useStore((state) => state.model)
const [input, setInput] = useState(() => localStorage.getItem('textfix.batch.input') ?? '')
const [lines, setLines] = useState(() => localStorage.getItem('textfix.batch.lines') ?? '')
const [blend, setBlend] = useState(!!options?.canBlend)
const [state, setState] = useState<BatchState | null>(null)
const [error, setError] = useState('')
const running = !!state?.running
// While a folder is being worked on, ask how far it got once a second.
useEffect(() => {
let stop = false
const tick = async () => {
try {
const now = await api.batch({ action: 'status' })
if (!stop) setState(now)
} catch {
// the next tick asks again
}
}
void tick()
const timer = window.setInterval(() => void tick(), 1000)
return () => {
stop = true
window.clearInterval(timer)
}
}, [])
const start = async () => {
setError('')
localStorage.setItem('textfix.batch.input', input)
localStorage.setItem('textfix.batch.lines', lines)
try {
setState(await api.batch({ action: 'start', input, lines, blend, look, model }))
} catch (failure) {
setError(failure instanceof Error ? failure.message : String(failure))
}
}
const progress = state && state.total ? Math.round((state.done / state.total) * 100) : 0
return (
<div className="fixed inset-0 z-40 flex items-center justify-center bg-black/50 p-6" onPointerDown={onClose}>
<div
className="w-[520px] max-w-full space-y-3 rounded-lg border bg-card p-4 shadow-2xl"
onPointerDown={(event) => event.stopPropagation()}
>
<div className="flex items-center gap-2">
<FolderOpen className="size-4 text-primary" />
<h2 className="flex-1 font-semibold">A whole folder at once</h2>
<Button variant="ghost" size="icon-sm" onClick={onClose} aria-label="Close">
<X />
</Button>
</div>
<Field label="Folder with the images" hint="PNG, JPG and WebP files in it; subfolders are left out">
<Input
value={input}
disabled={running}
placeholder="D:\\images\\to-fix"
onChange={(event) => setInput(event.target.value)}
/>
</Field>
<Field
label="New text"
hint="One line for each text found on every image, the most prominent first. An empty line leaves that text as it is; a dash (-) erases it."
>
<Textarea value={lines} disabled={running} rows={4} onChange={(event) => setLines(event.target.value)} />
</Field>
{options?.canBlend && (
<SwitchField
label="Blend in with the model"
hint="The model pass with the settings of the side panel, and the prompt each image carries"
checked={blend}
onChange={setBlend}
disabled={running}
/>
)}
<p className="text-xs text-muted-foreground">
The look is the one set in the side panel now. The images go into a new folder: the originals stay as they are.
</p>
{state && (state.running || state.total > 0) && (
<div className="space-y-1.5 rounded-md border p-2 text-xs">
<div className="h-1.5 overflow-hidden rounded bg-muted">
<div className="h-full bg-primary transition-[width]" style={{ width: `${progress}%` }} />
</div>
<div className="flex justify-between gap-2 text-muted-foreground">
<span>
{state.done} of {state.total} · changed {state.changed}
{state.failed ? ` · failed ${state.failed}` : ''}
</span>
<span className="truncate">{state.running ? state.current : 'Done'}</span>
</div>
{state.output && <div className="break-all font-mono">{state.output}</div>}
{state.warnings.map((line, index) => (
<div key={index} className="text-amber-500">
{line}
</div>
))}
</div>
)}
{error && <p className="text-xs text-destructive">{error}</p>}
<div className="flex justify-end gap-2">
{running ? (
<Button variant="secondary" onClick={() => void api.batch({ action: 'stop' }).then(setState)}>
<Square /> Stop after this image
</Button>
) : (
<Button disabled={!input.trim() || !lines.trim()} onClick={() => void start()}>
{running ? <LoaderCircle className="animate-spin" /> : <Play />} Start
</Button>
)}
</div>
</div>
</div>
)
}