From 04fdaa934e691f3ea85cd0db30ecc270c1ffacdb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 4 Sep 2026 09:23:06 +0300 Subject: [PATCH] Upload progress while the photo crosses MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Choosing a photo now posts over XHR with upload.onprogress: a hairline bar in the accent under the profile head climbs as the bytes cross, a quiet cancel aborts (the input clears, nothing is posted), and completion follows the server's own redirect so the saved page and its banner render as always. FormData is captured before the input is disabled — a disabled control is unlisted and the post would carry no file. No upload box, no change: the plain hard post remains the fallback. JS comments in the inline script are block comments — the string is one collapsed line, and a // would swallow the rest of the handler. --- crates/im-web/assets/main.css | 26 ++++++++++++++++++++ crates/im-web/src/layout.rs | 45 ++++++++++++++++++++++++++++++++++- style/main.scss | 28 ++++++++++++++++++++++ 3 files changed, 98 insertions(+), 1 deletion(-) diff --git a/crates/im-web/assets/main.css b/crates/im-web/assets/main.css index d8360dd..bd029d5 100644 --- a/crates/im-web/assets/main.css +++ b/crates/im-web/assets/main.css @@ -477,6 +477,32 @@ body:has(.auth-stage) > .auth-stage::-webkit-scrollbar { border-radius: var(--radius); } +.profile-upload-row { + display: flex; + gap: 8px; + align-items: center; +} + +.upload-progress { + flex: 1; + height: 2px; + background: var(--panel-sunken); + border-radius: 1px; + overflow: hidden; +} + +.upload-progress-fill { + height: 100%; + width: 0; + background: var(--accent); + transition: width var(--dur-fast) var(--ease); +} + +.upload-cancel { + height: 24px; + padding: 0 8px; +} + .profile-stats { margin: 0; display: flex; diff --git a/crates/im-web/src/layout.rs b/crates/im-web/src/layout.rs index 731203e..d95e4be 100644 --- a/crates/im-web/src/layout.rs +++ b/crates/im-web/src/layout.rs @@ -95,7 +95,50 @@ pub async fn avatar_script(cx: &Cx) -> Result { }); \ document.addEventListener('change', function (e) { \ var input = e.target.closest ? e.target.closest('[data-autosubmit]') : null; \ - if (input && input.form) { input.form.submit(); } \ + if (!input || !input.form || input.form.__imUploading) { return; } \ + var form = input.form; \ + var head = input.closest('.profile-head'); \ + if (!head) { form.submit(); return; } \ + /* Captured before the input is disabled — a disabled control \ + is no longer listed, and the post would carry no file. */ \ + var data = new FormData(form); \ + form.__imUploading = true; \ + input.disabled = true; \ + var row = document.createElement('div'); \ + row.className = 'profile-upload-row'; \ + var bar = document.createElement('div'); \ + bar.className = 'upload-progress'; \ + var fill = document.createElement('div'); \ + fill.className = 'upload-progress-fill'; \ + bar.appendChild(fill); \ + row.appendChild(bar); \ + var cancel = document.createElement('button'); \ + cancel.type = 'button'; \ + cancel.className = 'admin-action upload-cancel'; \ + cancel.textContent = '\\u00d7'; \ + cancel.setAttribute('aria-label', 'Cancel upload'); \ + row.appendChild(cancel); \ + head.insertAdjacentElement('afterend', row); \ + var settle = function () { \ + form.__imUploading = false; \ + input.disabled = false; \ + if (row.parentNode) { row.parentNode.removeChild(row); } \ + }; \ + var x = new XMLHttpRequest(); \ + x.open('POST', form.getAttribute('action')); \ + x.upload.onprogress = function (ev) { \ + if (ev.lengthComputable && ev.total > 0) { \ + fill.style.width = Math.min(100, Math.round((ev.loaded / ev.total) * 100)) + '%'; \ + } \ + }; \ + x.onload = function () { \ + settle(); \ + window.location.href = x.responseURL || form.getAttribute('action'); \ + }; \ + x.onerror = function () { settle(); form.submit(); }; \ + x.onabort = function () { input.value = ''; settle(); }; \ + cancel.addEventListener('click', function () { x.abort(); }); \ + x.send(data); \ }); \ })();"; view! { cx => } diff --git a/style/main.scss b/style/main.scss index 688370f..f16a9d8 100644 --- a/style/main.scss +++ b/style/main.scss @@ -538,6 +538,34 @@ body:has(.auth-stage) { border-radius: var(--radius); } +// Upload progress: a client-made hairline under the profile head while the +// photo crosses, a quiet cancel beside it — no native chrome. +.profile-upload-row { + display: flex; + gap: 8px; + align-items: center; +} + +.upload-progress { + flex: 1; + height: 2px; + background: var(--panel-sunken); + border-radius: 1px; + overflow: hidden; +} + +.upload-progress-fill { + height: 100%; + width: 0; + background: var(--accent); + transition: width var(--dur-fast) var(--ease); +} + +.upload-cancel { + height: 24px; + padding: 0 8px; +} + .profile-stats { margin: 0; display: flex;