(roles)
| 613 | } |
| 614 | |
| 615 | function renderRoles(roles) { |
| 616 | const target = $("roleList"); |
| 617 | if (!target) return; |
| 618 | if (!roles.length) { |
| 619 | target.className = "role-list empty"; |
| 620 | target.textContent = "暂无角色。"; |
| 621 | return; |
| 622 | } |
| 623 | target.className = "role-list"; |
| 624 | target.innerHTML = roles.map((role) => ` |
| 625 | <div class="role-card"> |
| 626 | <div> |
| 627 | <h4>${escapeHtml(role.name || role.user_id)}</h4> |
| 628 | <p>${escapeHtml(role.user_id || "")}</p> |
| 629 | <p>${escapeHtml(role.description || "暂无描述")}</p> |
| 630 | </div> |
| 631 | <div class="actions"> |
| 632 | ${role.user_id === state.userId ? `<span class="badge blue">当前选择</span>` : ""} |
| 633 | ${role.is_current ? `<span class="badge green">当前角色</span>` : `<button data-role-action="switch" data-role="${escapeHtml(role.name)}">切换</button>`} |
| 634 | <button data-role-action="delete" data-role="${escapeHtml(role.name)}">删除</button> |
| 635 | </div> |
| 636 | </div> |
| 637 | `).join(""); |
| 638 | document.querySelectorAll("[data-role-action]").forEach((button) => { |
| 639 | button.addEventListener("click", () => runAction(() => updateRole(button.dataset.roleAction, button.dataset.role))); |
| 640 | }); |
| 641 | } |
| 642 | |
| 643 | async function updateRole(action, roleName) { |
| 644 | if (!roleName) throw new Error("请输入角色名。"); |
no test coverage detected