import { useEffect, useState } from "preact/hooks"; import { Alert, Badge, Button, Checkbox, Input, } from "@silverbulletmd/silverbullet/ui"; import { createToken, createUser, deleteToken, deleteUser, formatApiError, getUser, listUsers, setUserAdmin, setUserPassword, } from "../api.ts"; import { useNavigate } from "../navigation.ts"; import { spacesUrl } from "../routes.ts"; import type { UserInfo } from "../types.ts"; function useUserList(onUnauthorized: () => void) { const [users, setUsers] = useState>({}); const [loaded, setLoaded] = useState(false); const [error, setError] = useState(""); useEffect(() => { listUsers() .then((users) => { setUsers(users); setLoaded(true); }) .catch((error: any) => { if (error.unauthorized) onUnauthorized(); else setError(formatApiError(error)); setLoaded(true); }); }, []); return { users, loaded, error }; } export function UserList({ currentUsername, onUnauthorized, }: { currentUsername: string; onUnauthorized: () => void; }) { const { users, loaded, error } = useUserList(onUnauthorized); return (
{/* No heading: this screen is only ever reached from the tab bar, which already names it. See SpaceList for the non-admin case. */} {error && {error}} {!loaded &&

Loading…

} {loaded && Object.keys(users).length === 0 &&

No users yet.

} {loaded && Object.keys(users).length > 0 && ( {/* Actions column; the header stays empty. */} {Object.entries(users) .sort((a, b) => a[0].localeCompare(b[0])) .map(([name, user]) => { const href = spacesUrl(`/users/${encodeURIComponent(name)}`); return ( ); })}
Name Role
{name} {" "} {name === currentUsername && you} {user.admin ? "admin" : "user"} {/* Same destination as the name — an explicit control for anyone who doesn't read the name as clickable, mirroring the spaces list. */} Edit
)} {loaded && ( )}
); } export function NewUser({ onUnauthorized }: { onUnauthorized: () => void }) { const navigate = useNavigate(); const [username, setUsername] = useState(""); const [password, setPassword] = useState(""); const [admin, setAdmin] = useState(false); const [error, setError] = useState(""); return (
{ event.preventDefault(); createUser(username, password, admin) .then(() => navigate( spacesUrl(`/users/${encodeURIComponent(username.trim())}`), ), ) .catch((error: any) => { if (error.unauthorized) onUnauthorized(); else setError(formatApiError(error)); }); }} >

Create user

{error && {error}} setUsername(event.currentTarget.value)} /> setPassword(event.currentTarget.value)} />
Cancel
); } export function UserDetail({ username, currentUsername, onUnauthorized, }: { username: string; currentUsername: string; onUnauthorized: () => void; }) { const navigate = useNavigate(); const [user, setUser] = useState(); const [loaded, setLoaded] = useState(false); const [notFound, setNotFound] = useState(false); const [error, setError] = useState(""); const [password, setPassword] = useState(""); const [tokenName, setTokenName] = useState(""); const [shownToken, setShownToken] = useState(); const isSelf = username === currentUsername; async function reload() { try { setUser(await getUser(username)); setError(""); } catch (error: any) { if (error.unauthorized) onUnauthorized(); else if (error.notFound) setNotFound(true); else setError(formatApiError(error)); } finally { setLoaded(true); } } useEffect(() => { void reload(); }, [username]); async function run(action: () => Promise) { try { await action(); setError(""); } catch (error: any) { if (error.unauthorized) onUnauthorized(); else setError(formatApiError(error)); } } if (!loaded) return

Loading…

; if (notFound) { return (

User not found

Return to users

); } if (!user) return {error || "User not found"}; const tokenNames = Object.keys(user.tokens); return (

{username} {isSelf && you}

{error && {error}}

Role

Password

setPassword(event.currentTarget.value)} />

API tokens

{tokenNames.length === 0 &&

No tokens.

} {tokenNames.length > 0 && (
    {tokenNames.map((name) => (
  • {name} created{" "} {new Date(user.tokens[name].createdAt).toLocaleString()}
  • ))}
)}
setTokenName(event.currentTarget.value)} />
{shownToken && (
This token is shown only once — copy it now. event.currentTarget.select()} />
)}
); } function loginUrlForUser(username: string): string { const next = spacesUrl(`/users/${encodeURIComponent(username)}`); return `${spacesUrl("/login")}?next=${encodeURIComponent(next)}`; }