на оценку 5
This commit is contained in:
@@ -0,0 +1,148 @@
|
||||
import { useMutation, useQueryClient } from "@tanstack/react-query";
|
||||
import { Plus, UserRoundPlus, X } from "lucide-react";
|
||||
import { useEffect, useState, type FormEvent } from "react";
|
||||
import { useNavigate } from "react-router-dom";
|
||||
|
||||
import { api, ApiError } from "../api/client";
|
||||
import type { Chat, User } from "../api/types";
|
||||
import { shortID } from "../lib/format";
|
||||
|
||||
type CreateChatDialogProps = {
|
||||
currentUser: User;
|
||||
onClose: () => void;
|
||||
};
|
||||
|
||||
const uuidPattern =
|
||||
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
||||
|
||||
export function CreateChatDialog({
|
||||
currentUser,
|
||||
onClose,
|
||||
}: CreateChatDialogProps) {
|
||||
const navigate = useNavigate();
|
||||
const queryClient = useQueryClient();
|
||||
const [title, setTitle] = useState("");
|
||||
const [memberInput, setMemberInput] = useState("");
|
||||
const [validationError, setValidationError] = useState("");
|
||||
|
||||
useEffect(() => {
|
||||
function closeOnEscape(event: KeyboardEvent) {
|
||||
if (event.key === "Escape") {
|
||||
onClose();
|
||||
}
|
||||
}
|
||||
window.addEventListener("keydown", closeOnEscape);
|
||||
return () => window.removeEventListener("keydown", closeOnEscape);
|
||||
}, [onClose]);
|
||||
|
||||
const mutation = useMutation({
|
||||
mutationFn: api.createChat,
|
||||
onSuccess: (chat) => {
|
||||
queryClient.setQueryData<Chat[]>(["chats"], (current = []) => [
|
||||
...current,
|
||||
chat,
|
||||
]);
|
||||
onClose();
|
||||
navigate(`/app/chats/${chat.id}`);
|
||||
},
|
||||
});
|
||||
|
||||
function submit(event: FormEvent<HTMLFormElement>) {
|
||||
event.preventDefault();
|
||||
setValidationError("");
|
||||
|
||||
const enteredIDs = memberInput
|
||||
.split(/[\s,;]+/)
|
||||
.map((value) => value.trim())
|
||||
.filter(Boolean);
|
||||
const memberIds = Array.from(new Set([currentUser.id, ...enteredIDs]));
|
||||
|
||||
if (!title.trim() || Array.from(title.trim()).length > 100) {
|
||||
setValidationError("Название должно содержать от 1 до 100 символов");
|
||||
return;
|
||||
}
|
||||
const invalidID = enteredIDs.find((id) => !uuidPattern.test(id));
|
||||
if (invalidID) {
|
||||
setValidationError(`Некорректный UUID: ${invalidID}`);
|
||||
return;
|
||||
}
|
||||
mutation.mutate({ title: title.trim(), memberIds });
|
||||
}
|
||||
|
||||
const serverError =
|
||||
mutation.error instanceof ApiError
|
||||
? mutation.error.message
|
||||
: mutation.error
|
||||
? "Не удалось создать чат"
|
||||
: "";
|
||||
|
||||
return (
|
||||
<div className="dialog-backdrop" onMouseDown={onClose}>
|
||||
<section
|
||||
aria-labelledby="create-chat-title"
|
||||
aria-modal="true"
|
||||
className="dialog-card"
|
||||
onMouseDown={(event) => event.stopPropagation()}
|
||||
role="dialog"
|
||||
>
|
||||
<header className="dialog-card__header">
|
||||
<div>
|
||||
<p className="eyebrow">Новый разговор</p>
|
||||
<h2 id="create-chat-title">Создать чат</h2>
|
||||
</div>
|
||||
<button
|
||||
aria-label="Закрыть"
|
||||
className="soft-icon-button"
|
||||
onClick={onClose}
|
||||
type="button"
|
||||
>
|
||||
<X aria-hidden="true" />
|
||||
</button>
|
||||
</header>
|
||||
|
||||
<form className="dialog-form" onSubmit={submit}>
|
||||
<label>
|
||||
<span>Название</span>
|
||||
<div className="input-shell">
|
||||
<Plus aria-hidden="true" />
|
||||
<input
|
||||
autoFocus
|
||||
maxLength={100}
|
||||
onChange={(event) => setTitle(event.target.value)}
|
||||
placeholder="Например, Команда проекта"
|
||||
required
|
||||
value={title}
|
||||
/>
|
||||
</div>
|
||||
</label>
|
||||
|
||||
<label>
|
||||
<span>UUID участников</span>
|
||||
<div className="textarea-shell">
|
||||
<UserRoundPlus aria-hidden="true" />
|
||||
<textarea
|
||||
onChange={(event) => setMemberInput(event.target.value)}
|
||||
placeholder="Вставьте UUID через пробел, запятую или с новой строки"
|
||||
rows={4}
|
||||
value={memberInput}
|
||||
/>
|
||||
</div>
|
||||
<small>
|
||||
Ваш ID добавлен автоматически: <code>{shortID(currentUser.id)}</code>
|
||||
</small>
|
||||
</label>
|
||||
|
||||
{(validationError || serverError) && (
|
||||
<div className="form-error" role="alert">
|
||||
{validationError || serverError}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<button className="primary-button" disabled={mutation.isPending} type="submit">
|
||||
{mutation.isPending ? "Создаём…" : "Создать чат"}
|
||||
</button>
|
||||
</form>
|
||||
</section>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user