241 lines
9.4 KiB
TypeScript
241 lines
9.4 KiB
TypeScript
import { AdminLayout } from "./AdminLayout.tsx";
|
|
|
|
export const AdminUsersPage = ({
|
|
users,
|
|
}: {
|
|
users: any[];
|
|
}) => {
|
|
return (
|
|
<AdminLayout title="Manage Users" currentPath="/admin/users">
|
|
<div
|
|
id="status-banner"
|
|
style="display: none; margin-bottom: 1rem; padding: 0.75rem 1rem; border-radius: var(--radius-md); font-size: 0.9rem;"
|
|
/>
|
|
|
|
<div style="margin-bottom: 1.5rem;">
|
|
<h1 style="font-size: 1.75rem; font-weight: 700; margin: 0 0 0.5rem 0; color: var(--text-primary);">
|
|
User Management
|
|
</h1>
|
|
<p style="color: var(--text-secondary); margin: 0; font-size: 0.95rem;">
|
|
Manage identities, activate pending registrations, and inspect role
|
|
grants.
|
|
</p>
|
|
</div>
|
|
|
|
{/* Desktop Table View (≥ 768px) */}
|
|
<div class="card desktop-only" style="display: none;">
|
|
<div class="table-container">
|
|
<table>
|
|
<thead>
|
|
<tr>
|
|
<th>Username</th>
|
|
<th>Display Name</th>
|
|
<th>Status</th>
|
|
<th>Actions</th>
|
|
</tr>
|
|
</thead>
|
|
<tbody>
|
|
{users.map((user) => {
|
|
const statusClass = user.account_status === "active"
|
|
? "badge-success"
|
|
: user.account_status === "suspended"
|
|
? "badge-danger"
|
|
: "badge-warning";
|
|
|
|
return (
|
|
<tr key={user.id}>
|
|
<td>
|
|
<strong style="color: var(--text-primary);">
|
|
{user.username}
|
|
</strong>
|
|
</td>
|
|
<td style="color: var(--text-secondary);">
|
|
{user.display_name || "-"}
|
|
</td>
|
|
<td>
|
|
<span class={`badge ${statusClass}`}>
|
|
{user.account_status}
|
|
</span>
|
|
</td>
|
|
<td>
|
|
<div style="display: flex; gap: 0.35rem;">
|
|
<a
|
|
href={`/admin/users/${user.id}`}
|
|
class="btn-outline"
|
|
style="padding: 0.25rem 0.65rem; font-size: 0.8rem; min-height: 32px; text-decoration: none;"
|
|
>
|
|
Manage
|
|
</a>
|
|
{user.account_status === "pending" && (
|
|
<button
|
|
type="button"
|
|
class="btn-primary"
|
|
style="background: var(--success); border-color: var(--success); padding: 0.25rem 0.65rem; font-size: 0.8rem; min-height: 32px;"
|
|
onclick={`updateStatus('${user.id}', 'active')`}
|
|
>
|
|
Activate
|
|
</button>
|
|
)}
|
|
{user.account_status === "active" && (
|
|
<button
|
|
type="button"
|
|
class="btn-danger"
|
|
style="padding: 0.25rem 0.65rem; font-size: 0.8rem; min-height: 32px;"
|
|
onclick={`updateStatus('${user.id}', 'suspended')`}
|
|
>
|
|
Suspend
|
|
</button>
|
|
)}
|
|
{user.account_status === "suspended" && (
|
|
<button
|
|
type="button"
|
|
class="btn-primary"
|
|
style="background: var(--success); border-color: var(--success); padding: 0.25rem 0.65rem; font-size: 0.8rem; min-height: 32px;"
|
|
onclick={`updateStatus('${user.id}', 'active')`}
|
|
>
|
|
Re-Activate
|
|
</button>
|
|
)}
|
|
</div>
|
|
</td>
|
|
</tr>
|
|
);
|
|
})}
|
|
</tbody>
|
|
</table>
|
|
</div>
|
|
</div>
|
|
|
|
{/* Mobile Card Deck (< 768px) */}
|
|
<div
|
|
class="mobile-only"
|
|
style="display: flex; flex-direction: column; gap: 1rem;"
|
|
>
|
|
{users.map((user) => {
|
|
const statusClass = user.account_status === "active"
|
|
? "badge-success"
|
|
: user.account_status === "suspended"
|
|
? "badge-danger"
|
|
: "badge-warning";
|
|
|
|
return (
|
|
<div class="card" key={user.id} style="margin-bottom: 0;">
|
|
<div style="display: flex; justify-content: space-between; align-items: flex-start; margin-bottom: 0.75rem;">
|
|
<div style="display: flex; align-items: center; gap: 0.65rem;">
|
|
<div style="display: flex; align-items: center; justify-content: center; width: 38px; height: 38px; background: var(--primary-light); color: var(--primary); border-radius: var(--radius-md); font-weight: 700; font-size: 1rem;">
|
|
{user.username.charAt(0).toUpperCase()}
|
|
</div>
|
|
<div>
|
|
<h3 style="margin: 0; font-size: 1.05rem; color: var(--text-primary);">
|
|
{user.username}
|
|
</h3>
|
|
<span style="font-size: 0.8rem; color: var(--text-muted);">
|
|
{user.display_name || "No display name"}
|
|
</span>
|
|
</div>
|
|
</div>
|
|
|
|
<span class={`badge ${statusClass}`}>
|
|
{user.account_status}
|
|
</span>
|
|
</div>
|
|
|
|
<div style="display: flex; gap: 0.5rem; margin-top: 1rem;">
|
|
<a
|
|
href={`/admin/users/${user.id}`}
|
|
class="btn-outline"
|
|
style="flex: 1; text-decoration: none; justify-content: center; min-height: 40px; font-size: 0.85rem;"
|
|
>
|
|
Manage User
|
|
</a>
|
|
{user.account_status === "pending" && (
|
|
<button
|
|
type="button"
|
|
class="btn-primary"
|
|
style="flex: 1; background: var(--success); border-color: var(--success); justify-content: center; min-height: 40px; font-size: 0.85rem;"
|
|
onclick={`updateStatus('${user.id}', 'active')`}
|
|
>
|
|
Activate
|
|
</button>
|
|
)}
|
|
{user.account_status === "active" && (
|
|
<button
|
|
type="button"
|
|
class="btn-danger"
|
|
style="flex: 1; justify-content: center; min-height: 40px; font-size: 0.85rem;"
|
|
onclick={`updateStatus('${user.id}', 'suspended')`}
|
|
>
|
|
Suspend
|
|
</button>
|
|
)}
|
|
{user.account_status === "suspended" && (
|
|
<button
|
|
type="button"
|
|
class="btn-primary"
|
|
style="flex: 1; background: var(--success); border-color: var(--success); justify-content: center; min-height: 40px; font-size: 0.85rem;"
|
|
onclick={`updateStatus('${user.id}', 'active')`}
|
|
>
|
|
Re-Activate
|
|
</button>
|
|
)}
|
|
</div>
|
|
</div>
|
|
);
|
|
})}
|
|
</div>
|
|
|
|
<style>
|
|
{`
|
|
@media (min-width: 768px) {
|
|
.desktop-only { display: block !important; }
|
|
.mobile-only { display: none !important; }
|
|
}
|
|
@media (max-width: 767px) {
|
|
.desktop-only { display: none !important; }
|
|
.mobile-only { display: flex !important; }
|
|
}
|
|
`}
|
|
</style>
|
|
|
|
<script
|
|
dangerouslySetInnerHTML={{
|
|
__html: `
|
|
function showNotice(msg, isError) {
|
|
const banner = document.getElementById('status-banner');
|
|
banner.textContent = msg;
|
|
banner.style.display = 'block';
|
|
banner.style.background = isError ? 'var(--danger-bg)' : 'var(--success-bg)';
|
|
banner.style.color = isError ? 'var(--danger-text)' : 'var(--success-text)';
|
|
banner.style.border = isError ? '1px solid var(--danger-border)' : '1px solid var(--success-border)';
|
|
setTimeout(() => { banner.style.display = 'none'; }, 6000);
|
|
}
|
|
|
|
async function updateStatus(userId, status) {
|
|
if (!confirm('Are you sure you want to set this user to ' + status + '?')) {
|
|
return;
|
|
}
|
|
try {
|
|
const res = await fetch('/api/admin/users/' + userId + '/status', {
|
|
method: 'POST',
|
|
headers: { 'Content-Type': 'application/json' },
|
|
body: JSON.stringify({ status })
|
|
});
|
|
if (res.ok) {
|
|
showNotice('User status updated to ' + status, false);
|
|
setTimeout(() => window.location.reload(), 800);
|
|
} else {
|
|
const data = await res.json();
|
|
showNotice(data.error || 'Failed to update status', true);
|
|
}
|
|
} catch (err) {
|
|
showNotice('Network error', true);
|
|
}
|
|
}
|
|
`,
|
|
}}
|
|
>
|
|
</script>
|
|
</AdminLayout>
|
|
);
|
|
};
|