feat: folder mapping UI on account add (probe + map modal)

ADD now probes both connections, lists folders on each side, and opens a
mapping modal to route source->destination folders (e.g. Спам -> Spam) so we
append into the existing folder instead of creating a duplicate.

- store: SetTaskFolderMapping (+ round-trip test)
- httpapi: POST /tasks/{id}/probe (test both, return folder lists),
  PUT /tasks/{id}/folder-mapping
- web: FolderMappingModal (reuses Modal, size=lg), submitAccount probes then
  opens the modal; confirm creates the account and saves the task mapping

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MMHQTtnQtQqL8muAXHr9kd
This commit is contained in:
2026-07-02 14:39:31 +07:00
parent 331497aff4
commit 0bb584fe10
9 changed files with 329 additions and 7 deletions
+39 -4
View File
@@ -1,8 +1,9 @@
import { useEffect, useRef, useState, type ChangeEvent, type FormEvent } from 'react'
import { cancelAccount, createAccount, deleteAccount, getTask, importCSV, runTask, testAccounts, type TaskDetail as TaskDetailData } from '../api'
import { cancelAccount, createAccount, deleteAccount, getTask, importCSV, probeFolders, runTask, setFolderMapping, testAccounts, type TaskDetail as TaskDetailData } from '../api'
import { connectTaskWS, type TaskEvent } from '../ws'
import { StatusBadge } from '../components/StatusBadge'
import { useConfirm } from '../components/ConfirmProvider'
import { FolderMappingModal } from '../components/FolderMappingModal'
const emptyAccount = { src_login: '', src_pass: '', dst_login: '', dst_pass: '' }
@@ -72,7 +73,8 @@ export function TaskDetail({ id }: { id: number }) {
const [notFound, setNotFound] = useState(false)
const [log, setLog] = useState<{ type: string; text: string }[]>([])
const [form, setForm] = useState(emptyAccount)
const [busy, setBusy] = useState<'test' | 'run' | 'add' | 'import' | 'delete' | null>(null)
const [busy, setBusy] = useState<'test' | 'run' | 'add' | 'import' | 'delete' | 'probe' | null>(null)
const [mapState, setMapState] = useState<{ src: string[]; dst: string[]; creds: typeof emptyAccount } | null>(null)
const confirm = useConfirm()
const [error, setError] = useState<string | null>(null)
const [live, setLive] = useState<Record<number, LiveProgress>>({})
@@ -164,11 +166,35 @@ export function TaskDetail({ id }: { id: number }) {
async function submitAccount(e: FormEvent) {
e.preventDefault()
setBusy('probe')
setError(null)
try {
const creds = { ...form }
const res = await probeFolders(id, creds)
if (!res.src.ok || !res.dst.ok) {
const parts: string[] = []
if (!res.src.ok) parts.push(`source: ${res.src.error ?? 'login failed'}`)
if (!res.dst.ok) parts.push(`destination: ${res.dst.error ?? 'login failed'}`)
setError(`Connection test failed — ${parts.join('; ')}`)
return
}
setMapState({ src: res.src.folders ?? [], dst: res.dst.folders ?? [], creds })
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to test connections')
} finally {
setBusy(null)
}
}
async function confirmMapping(mapping: Record<string, string>) {
if (!mapState) return
setBusy('add')
setError(null)
try {
await createAccount(id, form)
await createAccount(id, mapState.creds)
await setFolderMapping(id, mapping)
setForm(emptyAccount)
setMapState(null)
reload()
} catch (err) {
setError(err instanceof Error ? err.message : 'Failed to add account')
@@ -382,7 +408,7 @@ export function TaskDetail({ id }: { id: number }) {
</div>
<div className="btn-row">
<button className="btn btn-primary" disabled={busy !== null}>
{busy === 'add' ? 'Adding…' : 'Add account'}
{busy === 'probe' ? 'Testing…' : busy === 'add' ? 'Adding…' : 'Add account'}
</button>
</div>
</form>
@@ -510,6 +536,15 @@ export function TaskDetail({ id }: { id: number }) {
</table>
</div>
</div>
<FolderMappingModal
open={mapState !== null}
srcFolders={mapState?.src ?? []}
dstFolders={mapState?.dst ?? []}
initialMapping={task.folder_mapping ?? {}}
onCancel={() => setMapState(null)}
onConfirm={confirmMapping}
/>
</>
)
}