Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 | 1x 1x 1x 128x 128x 128x 128x 128x 1x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 28x 28x 3x 3x 25x 25x 28x 1x 1x 17x 28x 7x 7x 128x 128x 128x 128x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 7x 6x 7x 1x 1x 1x 1x 7x 7x 7x 7x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x 1x 1x 1x 127x 128x 128x 128x 128x 128x 128x 128x 128x 128x 128x | import { useMemo, useRef, useState } from "react";
import { ApiError } from "../api/client";
import type { CreateTaskInput, Task } from "../api/types";
import { useFocusTrap } from "../hooks/useFocusTrap";
export interface CreateTaskDialogProps {
onClose: () => void;
onSubmit: (input: CreateTaskInput, idempotencyKey: string) => Promise<Task>;
}
interface FormState {
type: string;
priority: string;
payload: string;
maxAttempts: string;
timeoutSeconds: string;
idempotencyKey: string;
}
function newIdempotencyKey(): string {
const cryptoApi = globalThis.crypto as Crypto | undefined;
if (cryptoApi?.randomUUID) {
return cryptoApi.randomUUID();
}
return `ui-${Date.now()}-${Math.random().toString(16).slice(2, 10)}`;
}
export function CreateTaskDialog({ onClose, onSubmit }: CreateTaskDialogProps) {
const [form, setForm] = useState<FormState>({
type: "",
priority: "100",
payload: "{}",
maxAttempts: "3",
timeoutSeconds: "120",
idempotencyKey: newIdempotencyKey(),
});
const [error, setError] = useState<string | null>(null);
const [busy, setBusy] = useState(false);
const dialogRef = useRef<HTMLDivElement>(null);
useFocusTrap(dialogRef, onClose);
const payloadError = useMemo(() => {
const text = form.payload.trim();
if (text === "") {
return null;
}
try {
const parsed: unknown = JSON.parse(text);
if (parsed === null || typeof parsed !== "object" || Array.isArray(parsed)) {
return "Payload должен быть JSON-объектом";
}
return null;
} catch {
return "Payload не является валидным JSON";
}
}, [form.payload]);
const typeError = form.type.trim() === "" ? "Укажите тип задачи" : null;
const canSubmit = !busy && !typeError && !payloadError;
async function submit(event: React.FormEvent) {
event.preventDefault();
if (!canSubmit) {
return;
}
setBusy(true);
setError(null);
try {
const input: CreateTaskInput = {
type: form.type.trim(),
priority: Number(form.priority) || 0,
max_attempts: Number(form.maxAttempts) || undefined,
timeout_seconds: Number(form.timeoutSeconds) || undefined,
payload: form.payload.trim() === "" ? {} : (JSON.parse(form.payload) as Record<string, unknown>),
};
await onSubmit(input, form.idempotencyKey.trim());
onClose();
} catch (caught) {
const message =
caught instanceof ApiError
? `${caught.message}${caught.status ? ` (код ${caught.status})` : ""}`
: caught instanceof Error
? caught.message
: String(caught);
setError(message);
} finally {
setBusy(false);
}
}
return (
<div className="modal" role="dialog" aria-modal="true" aria-label="Новая задача" ref={dialogRef} tabIndex={-1}>
<form className="modal__card" onSubmit={submit}>
<header className="modal__head">
<h2>Новая задача</h2>
<button type="button" className="button button--ghost" onClick={onClose} aria-label="Закрыть">
Закрыть
</button>
</header>
<label className="field">
<span>Тип</span>
<input
data-autofocus
value={form.type}
placeholder="delivery"
onChange={(event) => setForm({ ...form, type: event.target.value })}
/>
</label>
{typeError ? <p className="error-text">{typeError}</p> : null}
<div className="modal__row">
<label className="field">
<span>Приоритет (0–1000)</span>
<input
type="number"
min={0}
max={1000}
value={form.priority}
onChange={(event) => setForm({ ...form, priority: event.target.value })}
/>
</label>
<label className="field">
<span>Попыток</span>
<input
type="number"
min={1}
max={10}
value={form.maxAttempts}
onChange={(event) => setForm({ ...form, maxAttempts: event.target.value })}
/>
</label>
<label className="field">
<span>Лимит, сек</span>
<input
type="number"
min={1}
max={86_400}
value={form.timeoutSeconds}
onChange={(event) => setForm({ ...form, timeoutSeconds: event.target.value })}
/>
</label>
</div>
<label className="field">
<span>Payload (JSON)</span>
<textarea
rows={4}
value={form.payload}
onChange={(event) => setForm({ ...form, payload: event.target.value })}
/>
</label>
{payloadError ? <p className="error-text">{payloadError}</p> : null}
<label className="field">
<span>Idempotency-Key</span>
<input
value={form.idempotencyKey}
onChange={(event) => setForm({ ...form, idempotencyKey: event.target.value })}
/>
</label>
{error ? (
<p className="error-text" role="alert">
{error}
</p>
) : null}
<footer className="modal__foot">
<button type="button" className="button button--ghost" onClick={onClose}>
Отмена
</button>
<button type="submit" className="button button--primary" disabled={!canSubmit}>
{busy ? "Создаём…" : "Создать"}
</button>
</footer>
</form>
</div>
);
}
|