CI / validate-monitoring (push) Successful in 8s
CI / lint (push) Successful in 13s
CI / federation-interop (push) Successful in 1m41s
CI / test (push) Successful in 4m13s
CI / images (push) Skipped
CI / assets (push) Skipped
CI / python-package (push) Skipped
CI / trigger-deploy (push) Skipped
CI / notify (push) Successful in 5s
The header's name link and Sign out become a button opening a menu of Profile, Sharing, Apps, Sessions, Import and Sign out; the main nav keeps Overview, History and Album art. Keyboard and screen-reader friendly. The Profile page now refetches the user after a failed change instead of showing stale state. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
98 lines
3.2 KiB
JavaScript
98 lines
3.2 KiB
JavaScript
import { api, describeError, upload } from "./api.js";
|
|
import { avatarImage } from "./avatar.js";
|
|
import { errorBox, h, replace } from "./dom.js";
|
|
import { signedInPage } from "./layout.js";
|
|
|
|
const root = document.getElementById("avatar-panel");
|
|
const messages = document.getElementById("messages");
|
|
|
|
const SOURCES = {
|
|
upload: "An image you uploaded.",
|
|
gravatar: "Your Gravatar, copied to this server.",
|
|
none: "You haven't set a profile image yet.",
|
|
};
|
|
|
|
let user = await signedInPage("account.html");
|
|
render();
|
|
|
|
function say(notice) {
|
|
replace(messages, notice);
|
|
}
|
|
|
|
// Runs a change, adopts the user it returns (or refetches), and redraws everything that
|
|
// shows the image - this panel and the one in the page header.
|
|
async function change(action, done) {
|
|
const buttons = root.querySelectorAll("button");
|
|
buttons.forEach((button) => (button.disabled = true));
|
|
try {
|
|
user = (await action()) ?? (await api("/auth/me"));
|
|
say(h("p", { class: "notice notice-ok" }, done));
|
|
} catch (error) {
|
|
say(errorBox(describeError(error)));
|
|
// The change may have half-happened, or the page was out of date: show what's true now.
|
|
user = await api("/auth/me").catch(() => user);
|
|
}
|
|
render();
|
|
const slot = document.querySelector(".account-avatar");
|
|
if (slot) replace(slot, avatarImage(user, "avatar-sm"));
|
|
}
|
|
|
|
function render() {
|
|
const file = h("input", { type: "file", accept: "image/jpeg,image/png,image/webp", hidden: true });
|
|
file.addEventListener("change", () => {
|
|
if (!file.files[0]) return;
|
|
const data = new FormData();
|
|
data.append("file", file.files[0]);
|
|
change(() => upload("/me/avatar", data, undefined, "PUT"), "Profile image updated.");
|
|
});
|
|
|
|
const buttons = [
|
|
h("button", { type: "button", class: "button", onclick: () => file.click() }, "Upload image"),
|
|
user.gravatar_enabled
|
|
? h(
|
|
"button",
|
|
{
|
|
type: "button",
|
|
class: "button button-quiet",
|
|
onclick: () => change(() => api("/me/avatar/gravatar", { method: "POST" }), "Now using your Gravatar."),
|
|
},
|
|
user.avatar_source === "gravatar" ? "Refresh Gravatar" : "Use my Gravatar",
|
|
)
|
|
: null,
|
|
user.has_avatar
|
|
? h(
|
|
"button",
|
|
{
|
|
type: "button",
|
|
class: "button button-quiet",
|
|
onclick: () => change(() => api("/me/avatar", { method: "DELETE" }), "Profile image removed."),
|
|
},
|
|
"Remove",
|
|
)
|
|
: null,
|
|
];
|
|
|
|
replace(
|
|
root,
|
|
h(
|
|
"div",
|
|
{ class: "avatar-row" },
|
|
avatarImage(user, "avatar-lg"),
|
|
h(
|
|
"div",
|
|
{ class: "avatar-body" },
|
|
h("p", {}, SOURCES[user.avatar_source] ?? SOURCES.none),
|
|
h("p", { class: "muted" }, "JPEG, PNG or WebP. Only you see it, unless you choose to share it on your fediverse profile."),
|
|
h("div", { class: "button-row" }, buttons, file),
|
|
user.gravatar_enabled
|
|
? h(
|
|
"p",
|
|
{ class: "muted" },
|
|
"Using Gravatar makes this server ask Gravatar for the picture linked to your email address, sending it a hash of that address (not the address itself). Your browser never contacts Gravatar.",
|
|
)
|
|
: null,
|
|
),
|
|
),
|
|
);
|
|
}
|