Spaces:
Running
Running
File size: 35,865 Bytes
91939c2 | 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 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 | import { useState, useRef } from "react";
import { useSelector, useDispatch } from "react-redux";
import {
PiRobot,
PiBrain,
PiGraduationCap,
PiCode,
PiFlask,
PiBook,
PiStar,
PiRocket,
PiLightning,
PiSparkle,
PiGlobe,
PiHeart,
PiShield,
PiWrench,
PiMagnifyingGlass,
PiChartLine,
PiPenNib,
PiMusicNotes,
PiCamera,
PiGameController,
PiPencil,
} from "react-icons/pi";
import {
FiSearch,
FiX,
FiUpload,
FiFileText,
FiTrash2,
FiGlobe,
FiLock,
FiCheck,
FiArrowLeft,
} from "react-icons/fi";
import { cancelCreateSpace } from "../../store/slices/appSlice";
import { dmService } from "../../services/dm.service";
import { usernameColors } from "../../constants/usernameColors";
const agentIcons = [
{ id: "robot", icon: PiRobot, label: "Robot" },
{ id: "brain", icon: PiBrain, label: "Trí tuệ" },
{ id: "graduation", icon: PiGraduationCap, label: "Học tập" },
{ id: "code", icon: PiCode, label: "Lập trình" },
{ id: "flask", icon: PiFlask, label: "Thí nghiệm" },
{ id: "book", icon: PiBook, label: "Sách" },
{ id: "star", icon: PiStar, label: "Ngôi sao" },
{ id: "rocket", icon: PiRocket, label: "Tên lửa" },
{ id: "lightning", icon: PiLightning, label: "Tia sét" },
{ id: "sparkle", icon: PiSparkle, label: "Lấp lánh" },
{ id: "globe", icon: PiGlobe, label: "Thế giới" },
{ id: "heart", icon: PiHeart, label: "Yêu thích" },
{ id: "shield", icon: PiShield, label: "Bảo vệ" },
{ id: "wrench", icon: PiWrench, label: "Công cụ" },
{ id: "search", icon: PiMagnifyingGlass, label: "Tìm kiếm" },
{ id: "chart", icon: PiChartLine, label: "Phân tích" },
{ id: "pen", icon: PiPenNib, label: "Sáng tạo" },
{ id: "music", icon: PiMusicNotes, label: "Âm nhạc" },
{ id: "camera", icon: PiCamera, label: "Hình ảnh" },
{ id: "game", icon: PiGameController, label: "Game" },
{ id: "pencil", icon: PiPencil, label: "Viết lách" },
];
const availableTools = [
{ id: "web_search", label: "Tìm kiếm web", desc: "Tìm kiếm thông tin trên internet" },
{ id: "calculator", label: "Máy tính", desc: "Thực hiện phép tính toán học" },
{ id: "code_interpreter", label: "Chạy code", desc: "Thực thi và kiểm tra code" },
{ id: "image_gen", label: "Tạo ảnh", desc: "Tạo hình ảnh từ mô tả" },
{ id: "pdf_reader", label: "Đọc PDF", desc: "Trích xuất nội dung từ file PDF" },
{ id: "translator", label: "Dịch thuật", desc: "Dịch ngôn ngữ đa ngôn ngữ" },
];
function CreateAgent({ editMode = false, initialData = null, onCancel }) {
const dispatch = useDispatch();
const { isDark } = useSelector((state) => state.theme);
const fileInputRef = useRef(null);
const [agentName, setAgentName] = useState(initialData?.name || "");
const [agentDescription, setAgentDescription] = useState(initialData?.description || "");
const [agentIcon, setAgentIcon] = useState(initialData?.icon || agentIcons[0].id);
const [agentColor, setAgentColor] = useState(initialData?.color || usernameColors[0].value);
const [systemPrompt, setSystemPrompt] = useState(initialData?.systemPrompt || "");
const [selectedTools, setSelectedTools] = useState(initialData?.tools || []);
const [toolSearchQuery, setToolSearchQuery] = useState("");
const [uploadedFiles, setUploadedFiles] = useState([]);
const [visibility, setVisibility] = useState(initialData?.visibility || "public");
// Members state (giống tạo space)
const [members, setMembers] = useState([]);
const [memberSearchQuery, setMemberSearchQuery] = useState("");
const [searchResults, setSearchResults] = useState([]);
const [isSearching, setIsSearching] = useState(false);
const handleMemberSearch = async (query) => {
setMemberSearchQuery(query);
if (!query.trim()) {
setSearchResults([]);
setIsSearching(false);
return;
}
};
const handleMemberSearchKeyDown = async (e) => {
if (e.key === "Enter") {
e.preventDefault();
const query = memberSearchQuery.trim();
if (!query) {
setSearchResults([]);
return;
}
setIsSearching(true);
try {
const { data } = await dmService.searchUsers(query);
const users = (data.users || []).map((user) => ({
id: user.id,
name: user.display_name || user.email || "Unknown",
avatar: user.avatar_url || null,
email: user.email || "",
}));
setSearchResults(users);
} catch {
setSearchResults([]);
} finally {
setIsSearching(false);
}
}
};
const addMember = (user) => {
if (!members.some((m) => m.id === user.id)) {
setMembers([...members, user]);
}
};
const removeMember = (userId) => {
setMembers((prev) => prev.filter((m) => m.id !== userId));
};
const toggleTool = (toolId) => {
setSelectedTools((prev) =>
prev.includes(toolId)
? prev.filter((id) => id !== toolId)
: [...prev, toolId]
);
};
const filteredTools = availableTools.filter(
(tool) =>
tool.label.toLowerCase().includes(toolSearchQuery.toLowerCase()) ||
tool.desc.toLowerCase().includes(toolSearchQuery.toLowerCase())
);
const handleFileUpload = (e) => {
const files = Array.from(e.target.files);
const newFiles = files.map((file) => ({
id: `${file.name}-${Date.now()}`,
name: file.name,
size: file.size,
type: file.type,
}));
setUploadedFiles((prev) => [...prev, ...newFiles]);
e.target.value = "";
};
const removeFile = (fileId) => {
setUploadedFiles((prev) => prev.filter((f) => f.id !== fileId));
};
const formatFileSize = (bytes) => {
if (bytes < 1024) return `${bytes} B`;
if (bytes < 1024 * 1024) return `${(bytes / 1024).toFixed(1)} KB`;
return `${(bytes / (1024 * 1024)).toFixed(1)} MB`;
};
const handleSubmit = () => {
if (agentName.trim()) {
const agentData = {
id: editMode && initialData ? initialData.id : agentName
.toLowerCase()
.replace(/\s+/g, "-")
.replace(/[^a-z0-9-]/g, ""),
name: agentName.trim(),
description: agentDescription.trim(),
icon: agentIcon,
color: agentColor.trim(),
systemPrompt: systemPrompt.trim(),
tools: selectedTools,
files: uploadedFiles.map((f) => f.name),
visibility,
members: members.map((m) => m.id),
};
console.log(editMode ? "Updating agent:" : "Creating agent:", agentData);
if (editMode && onCancel) {
onCancel();
} else {
dispatch(cancelCreateSpace());
}
}
};
const selectedIconData = agentIcons.find((s) => s.id === agentIcon);
const SelectedIcon = selectedIconData?.icon || PiRobot;
const selectedColorHex = usernameColors.find((c) => c.value === agentColor)?.hex || usernameColors[0].hex;
return (
<div
className="flex-1 flex flex-col min-w-0"
style={{ background: "var(--bg-surface)" }}
>
<div
className="px-4 py-3 border-b flex-shrink-0 flex items-center gap-3"
style={{
borderColor: "var(--border-primary)",
background: "var(--bg-surface-secondary)",
}}
>
{editMode && (
<button
onClick={() => {
if (onCancel) onCancel();
}}
className="p-1.5 rounded-md hover:opacity-70 transition-opacity"
style={{ color: "var(--text-secondary)" }}
title="Quay lại"
>
<FiArrowLeft size={18} />
</button>
)}
<div>
<div
className="text-[15px] font-semibold"
style={{ color: "var(--text-primary)" }}
>
{editMode ? "Chỉnh sửa Agent" : "Tạo Agent mới"}
</div>
<div
className="text-xs mt-0.5"
style={{ color: "var(--text-secondary)" }}
>
{editMode ? "Cập nhật thông tin agent" : "Tạo trợ lý AI tùy chỉnh"}
</div>
</div>
</div>
<div className="flex-1 overflow-y-auto p-6">
<div className="max-w-lg mx-auto space-y-6">
{/* Icon Selection */}
<div>
<h3
className="text-sm font-semibold mb-3"
style={{ color: "var(--text-primary)" }}
>
Chọn icon
</h3>
<div className="flex flex-wrap gap-2">
{agentIcons.map(({ id, icon: Icon }) => (
<button
key={id}
onClick={() => setAgentIcon(id)}
className="w-12 h-12 rounded-lg flex items-center justify-center transition-colors"
style={{
background:
agentIcon === id
? "var(--primary)"
: "var(--card-bg-secondary)",
color:
agentIcon === id
? isDark
? "var(--bg-surface)"
: "#fff"
: "var(--text-secondary)",
}}
onMouseEnter={(e) => {
if (agentIcon !== id)
e.currentTarget.style.background = "var(--hover-primary)";
}}
onMouseLeave={(e) => {
if (agentIcon !== id)
e.currentTarget.style.background =
"var(--card-bg-secondary)";
}}
>
<Icon size={24} />
</button>
))}
</div>
</div>
{/* Color Selection */}
<div>
<h3
className="text-sm font-semibold mb-3"
style={{ color: "var(--text-primary)" }}
>
Chọn màu
</h3>
<div className="flex flex-wrap gap-2">
{usernameColors.map((color) => (
<button
key={`${color.value}-${agentColor}`}
onClick={() => setAgentColor(color.value)}
className="w-8 h-8 rounded-lg flex items-center justify-center border-2 transition-all"
style={{
background: color.hex,
borderColor:
agentColor === color.value
? "#fff"
: "transparent",
boxShadow:
agentColor === color.value
? "0 0 0 2px var(--primary)"
: "none",
transform:
agentColor === color.value ? "scale(1.15)" : "scale(1)",
}}
title={color.name}
>
{agentColor === color.value && (
<span
style={{
color: "#fff",
fontSize: "14px",
textShadow: "0 1px 2px rgba(0,0,0,0.4)",
}}
>
✓
</span>
)}
</button>
))}
</div>
{agentColor && (
<div className="mt-2 text-xs" style={{ color: "var(--text-muted)" }}>
Màu hiện tại:{" "}
<span
style={{
color: usernameColors.find((c) => c.value === agentColor)?.hex || agentColor,
fontWeight: "600",
}}
>
{usernameColors.find((c) => c.value === agentColor)?.name || agentColor}
</span>
</div>
)}
</div>
{/* Agent Name */}
<div>
<h3
className="text-sm font-semibold mb-3"
style={{ color: "var(--text-primary)" }}
>
Tên Agent
</h3>
<input
type="text"
value={agentName}
onChange={(e) => setAgentName(e.target.value)}
placeholder="VD: Trợ lý Toán học, Code Reviewer..."
className="w-full px-3 py-2 rounded-md text-sm border outline-none"
style={{
background: "var(--input-bg)",
borderColor: "var(--input-border)",
color: "var(--input-text)",
}}
onFocus={(e) =>
(e.currentTarget.style.borderColor = "var(--primary)")
}
onBlur={(e) =>
(e.currentTarget.style.borderColor = "var(--input-border)")
}
/>
</div>
{/* Description */}
<div>
<h3
className="text-sm font-semibold mb-3"
style={{ color: "var(--text-primary)" }}
>
Mô tả
</h3>
<textarea
value={agentDescription}
onChange={(e) => setAgentDescription(e.target.value)}
placeholder="Mô tả ngắn gọn về agent này..."
rows={2}
className="w-full px-3 py-2 rounded-md text-sm border outline-none resize-none"
style={{
background: "var(--input-bg)",
borderColor: "var(--input-border)",
color: "var(--input-text)",
}}
onFocus={(e) =>
(e.currentTarget.style.borderColor = "var(--primary)")
}
onBlur={(e) =>
(e.currentTarget.style.borderColor = "var(--input-border)")
}
/>
</div>
{/* System Prompt */}
<div>
<h3
className="text-sm font-semibold mb-3"
style={{ color: "var(--text-primary)" }}
>
System Prompt (tùy chọn)
</h3>
<textarea
value={systemPrompt}
onChange={(e) => setSystemPrompt(e.target.value)}
placeholder="Mô tả hành vi, vai trò và kiến thức chuyên môn của agent..."
rows={4}
className="w-full px-3 py-2 rounded-md text-sm border outline-none resize-none"
style={{
background: "var(--input-bg)",
borderColor: "var(--input-border)",
color: "var(--input-text)",
}}
onFocus={(e) =>
(e.currentTarget.style.borderColor = "var(--primary)")
}
onBlur={(e) =>
(e.currentTarget.style.borderColor = "var(--input-border)")
}
/>
</div>
{/* Tools Selection */}
<div>
<h3
className="text-sm font-semibold mb-3"
style={{ color: "var(--text-primary)" }}
>
Công cụ (Tools)
</h3>
{/* Tool search */}
<div className="relative mb-3">
<FiSearch
size={16}
className="absolute left-3 top-1/2 -translate-y-1/2"
style={{ color: "var(--text-muted)" }}
/>
<input
type="text"
value={toolSearchQuery}
onChange={(e) => setToolSearchQuery(e.target.value)}
placeholder="Tìm công cụ..."
className="w-full pl-9 pr-3 py-2 rounded-md text-sm border outline-none"
style={{
background: "var(--input-bg)",
borderColor: "var(--input-border)",
color: "var(--input-text)",
}}
onFocus={(e) =>
(e.currentTarget.style.borderColor = "var(--primary)")
}
onBlur={(e) =>
(e.currentTarget.style.borderColor = "var(--input-border)")
}
/>
</div>
<div className="space-y-2">
{filteredTools.map((tool) => {
const isSelected = selectedTools.includes(tool.id);
return (
<label
key={tool.id}
className="flex items-center gap-3 px-3 py-2.5 rounded-lg cursor-pointer transition-colors"
style={{
background: isSelected
? "var(--primary-active)"
: "var(--card-bg-secondary)",
}}
onMouseEnter={(e) => {
if (!isSelected)
e.currentTarget.style.background = "var(--hover-primary)";
}}
onMouseLeave={(e) => {
if (!isSelected)
e.currentTarget.style.background =
"var(--card-bg-secondary)";
}}
>
<input
type="checkbox"
checked={isSelected}
onChange={() => toggleTool(tool.id)}
className="w-4 h-4 cursor-pointer flex-shrink-0"
style={{ accentColor: "var(--primary)" }}
/>
<div className="flex-1 min-w-0">
<div
className="text-sm font-medium"
style={{ color: "var(--text-primary)" }}
>
{tool.label}
</div>
<div
className="text-xs"
style={{ color: "var(--text-secondary)" }}
>
{tool.desc}
</div>
</div>
</label>
);
})}
{filteredTools.length === 0 && (
<div
className="text-sm text-center py-4"
style={{ color: "var(--text-secondary)" }}
>
Không tìm thấy công cụ nào
</div>
)}
</div>
</div>
{/* PDF Upload */}
<div>
<h3
className="text-sm font-semibold mb-3"
style={{ color: "var(--text-primary)" }}
>
Tài liệu kiến thức (PDF)
</h3>
<div
className="border-2 border-dashed rounded-lg p-6 text-center cursor-pointer transition-colors"
style={{
borderColor: "var(--input-border)",
background: "var(--card-bg-secondary)",
}}
onClick={() => fileInputRef.current?.click()}
onMouseEnter={(e) => {
e.currentTarget.style.borderColor = "var(--primary)";
}}
onMouseLeave={(e) => {
e.currentTarget.style.borderColor = "var(--input-border)";
}}
>
<FiUpload
size={28}
className="mx-auto mb-2"
style={{ color: "var(--text-secondary)" }}
/>
<div
className="text-sm font-medium"
style={{ color: "var(--text-primary)" }}
>
Nhấn để tải lên PDF
</div>
<div
className="text-xs mt-1"
style={{ color: "var(--text-secondary)" }}
>
Hoặc kéo thả file vào đây
</div>
<input
ref={fileInputRef}
type="file"
accept=".pdf"
multiple
className="hidden"
onChange={handleFileUpload}
/>
</div>
{uploadedFiles.length > 0 && (
<div className="mt-3 space-y-2">
{uploadedFiles.map((file) => (
<div
key={file.id}
className="flex items-center gap-3 px-3 py-2 rounded-lg"
style={{ background: "var(--card-bg-secondary)" }}
>
<FiFileText
size={18}
style={{ color: "var(--primary)" }}
/>
<div className="flex-1 min-w-0">
<div
className="text-sm font-medium truncate"
style={{ color: "var(--text-primary)" }}
>
{file.name}
</div>
<div
className="text-xs"
style={{ color: "var(--text-secondary)" }}
>
{formatFileSize(file.size)}
</div>
</div>
<button
onClick={() => removeFile(file.id)}
className="p-1 rounded hover:opacity-70 transition-opacity"
style={{ color: "var(--text-secondary)" }}
>
<FiTrash2 size={14} />
</button>
</div>
))}
</div>
)}
</div>
{/* Visibility */}
<div>
<h3
className="text-sm font-semibold mb-3"
style={{ color: "var(--text-primary)" }}
>
Quyền truy cập
</h3>
<div className="space-y-2">
<label
className="flex items-center gap-3 px-3 py-3 rounded-lg cursor-pointer transition-colors"
style={{
background:
visibility === "public"
? "var(--primary-active)"
: "var(--card-bg-secondary)",
}}
onMouseEnter={(e) => {
if (visibility !== "public")
e.currentTarget.style.background = "var(--hover-primary)";
}}
onMouseLeave={(e) => {
if (visibility !== "public")
e.currentTarget.style.background =
"var(--card-bg-secondary)";
}}
>
<div
className="w-10 h-10 rounded-lg flex items-center justify-center flex-shrink-0"
style={{
background:
visibility === "public"
? "var(--primary)"
: "var(--bg-surface)",
color:
visibility === "public"
? "#fff"
: "var(--text-secondary)",
}}
>
<FiGlobe size={18} />
</div>
<div className="flex-1 min-w-0">
<div
className="text-sm font-medium"
style={{ color: "var(--text-primary)" }}
>
Công khai
</div>
<div
className="text-xs"
style={{ color: "var(--text-secondary)" }}
>
Mọi người trong workspace đều có thể sử dụng agent này
</div>
</div>
<input
type="radio"
name="visibility"
value="public"
checked={visibility === "public"}
onChange={() => setVisibility("public")}
className="w-4 h-4 cursor-pointer"
style={{ accentColor: "var(--primary)" }}
/>
</label>
<label
className="flex items-center gap-3 px-3 py-3 rounded-lg cursor-pointer transition-colors"
style={{
background:
visibility === "private"
? "var(--primary-active)"
: "var(--card-bg-secondary)",
}}
onMouseEnter={(e) => {
if (visibility !== "private")
e.currentTarget.style.background = "var(--hover-primary)";
}}
onMouseLeave={(e) => {
if (visibility !== "private")
e.currentTarget.style.background =
"var(--card-bg-secondary)";
}}
>
<div
className="w-10 h-10 rounded-lg flex items-center justify-center flex-shrink-0"
style={{
background:
visibility === "private"
? "var(--primary)"
: "var(--bg-surface)",
color:
visibility === "private"
? "#fff"
: "var(--text-secondary)",
}}
>
<FiLock size={18} />
</div>
<div className="flex-1 min-w-0">
<div
className="text-sm font-medium"
style={{ color: "var(--text-primary)" }}
>
Riêng tư
</div>
<div
className="text-xs"
style={{ color: "var(--text-secondary)" }}
>
Chỉ bạn và người được thêm mới có thể sử dụng agent này
</div>
</div>
<input
type="radio"
name="visibility"
value="private"
checked={visibility === "private"}
onChange={() => setVisibility("private")}
className="w-4 h-4 cursor-pointer"
style={{ accentColor: "var(--primary)" }}
/>
</label>
</div>
</div>
{/* Members - chỉ hiện khi chọn Private */}
{visibility === "private" && (
<div>
<h3
className="text-sm font-semibold mb-3"
style={{ color: "var(--text-primary)" }}
>
Thêm người dùng
</h3>
<div className="relative mb-3">
{isSearching ? (
<div className="absolute left-3 top-1/2 -translate-y-1/2">
<div
className="w-4 h-4 border-2 border-t-transparent rounded-full animate-spin"
style={{
borderColor: "var(--text-muted)",
borderTopColor: "transparent",
}}
/>
</div>
) : (
<button
onClick={() =>
handleMemberSearchKeyDown({
key: "Enter",
preventDefault: () => {},
})
}
className="absolute left-3 top-1/2 -translate-y-1/2 hover:opacity-70 transition-opacity"
style={{ color: "var(--text-muted)" }}
>
<FiSearch size={16} />
</button>
)}
<input
type="text"
value={memberSearchQuery}
onChange={(e) => handleMemberSearch(e.target.value)}
onKeyDown={handleMemberSearchKeyDown}
placeholder="Nhập tên và nhấn Enter để tìm..."
className="w-full pl-9 pr-3 py-2 rounded-md text-sm border outline-none"
style={{
background: "var(--input-bg)",
borderColor: "var(--input-border)",
color: "var(--input-text)",
}}
onFocus={(e) =>
(e.currentTarget.style.borderColor = "var(--primary)")
}
onBlur={(e) =>
(e.currentTarget.style.borderColor = "var(--input-border)")
}
/>
</div>
{members.length > 0 && (
<div className="mb-3">
<div
className="text-xs font-medium mb-2"
style={{ color: "var(--text-secondary)" }}
>
Đã chọn ({members.length})
</div>
<div className="flex flex-wrap gap-2">
{members.map((member) => (
<div
key={member.id}
className="flex items-center gap-2 px-3 py-1.5 rounded-full text-sm"
style={{
background: "var(--primary-active)",
color: "var(--primary)",
}}
>
<div
className="w-5 h-5 rounded-full flex items-center justify-center text-[10px] font-semibold"
style={{
background: "var(--primary)",
color: "#fff",
}}
>
{member.name?.charAt(0)?.toUpperCase() || "?"}
</div>
<span className="font-medium">{member.name}</span>
<button
onClick={() => removeMember(member.id)}
className="ml-1 hover:opacity-70"
>
<FiX size={14} />
</button>
</div>
))}
</div>
</div>
)}
{searchResults.length > 0 && (
<div>
<div
className="text-xs font-medium mb-2"
style={{ color: "var(--text-secondary)" }}
>
Kết quả tìm kiếm
</div>
<div className="space-y-1">
{searchResults.map((user) => {
const isSelected = members.some((m) => m.id === user.id);
return (
<label
key={user.id}
className="flex items-center gap-3 px-3 py-2 rounded-md cursor-pointer transition-colors hover:opacity-80"
>
<div
className="w-8 h-8 rounded-full flex items-center justify-center text-xs font-semibold"
style={{
background: "var(--primary-active)",
color: "var(--primary)",
}}
>
{user.name?.charAt(0)?.toUpperCase() || "?"}
</div>
<div className="flex-1 min-w-0">
<div
className="text-sm font-medium truncate"
style={{ color: "var(--text-primary)" }}
>
{user.name}
</div>
</div>
<input
type="checkbox"
checked={isSelected}
onChange={() => {
if (isSelected) {
removeMember(user.id);
} else {
addMember(user);
}
}}
className="w-4 h-4 cursor-pointer"
style={{ accentColor: "var(--primary)" }}
/>
</label>
);
})}
</div>
</div>
)}
</div>
)}
{/* Preview */}
{agentName && (
<div>
<h3
className="text-sm font-semibold mb-3"
style={{ color: "var(--text-primary)" }}
>
Xem trước
</h3>
<div
className="p-4 rounded-lg flex items-center gap-3"
style={{ background: "var(--card-bg-secondary)" }}
>
<div
className="w-10 h-10 rounded-lg flex items-center justify-center"
style={{ background: "var(--primary)" }}
>
<SelectedIcon
size={24}
color="#fff"
/>
</div>
<div>
<div
className="text-sm font-medium"
style={{ color: "var(--text-primary)" }}
>
{agentName}
</div>
{agentDescription && (
<div
className="text-xs"
style={{ color: "var(--text-secondary)" }}
>
{agentDescription.length > 60
? agentDescription.slice(0, 60) + "..."
: agentDescription}
</div>
)}
</div>
</div>
</div>
)}
</div>
</div>
<div
className="px-6 py-4 border-t flex justify-end gap-3"
style={{
borderColor: "var(--border-primary)",
background: "var(--bg-surface-secondary)",
}}
>
<button
onClick={() => {
if (editMode && onCancel) {
onCancel();
} else {
dispatch(cancelCreateSpace());
}
}}
className="px-4 py-2 rounded-md text-sm font-medium"
style={{
color: "var(--text-secondary)",
}}
onMouseEnter={(e) =>
(e.currentTarget.style.background = "var(--hover-primary)")
}
onMouseLeave={(e) =>
(e.currentTarget.style.background = "transparent")
}
>
Hủy
</button>
<button
onClick={handleSubmit}
disabled={!agentName.trim()}
className="px-4 py-2 rounded-md text-sm font-medium disabled:opacity-50 disabled:cursor-not-allowed"
style={{
background: "var(--primary)",
color: isDark ? "var(--bg-surface)" : "#fff",
}}
onMouseEnter={(e) => {
if (agentName.trim())
e.currentTarget.style.background = "var(--primary-hover)";
}}
onMouseLeave={(e) => {
if (agentName.trim())
e.currentTarget.style.background = "var(--primary)";
}}
>
{editMode ? "Lưu thay đổi" : "Tạo Agent"}
</button>
</div>
</div>
);
}
export default CreateAgent;
|