From 5fbac6627c519c07fb37ade7733cdcd407fdfcde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Fri, 30 Oct 2026 00:31:01 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=92=84=20style:=20the=20profile=20corner?= =?UTF-8?q?=20shows=20the=20whole=20display=20name,=20wrapping=20onto=20a?= =?UTF-8?q?=20second=20line=20at=20a=20space=20instead=20of=20cutting=20it?= =?UTF-8?q?=20with=20an=20ellipsis;=20the=20bell=20opens=20a=20small=20pop?= =?UTF-8?q?over=20of=20the=20eight=20newest=20notifications=20(unread=20fi?= =?UTF-8?q?rst,=20loaded=20from=20/notifications/peek=20when=20it=20opens)?= =?UTF-8?q?=20with=20an=20Open=20notifications=20button=20to=20the=20inbox?= =?UTF-8?q?,=20instead=20of=20going=20straight=20there;=20a=20modifier=20o?= =?UTF-8?q?r=20middle=20click,=20or=20no=20JavaScript,=20still=20follows?= =?UTF-8?q?=20the=20link?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- crates/forge-web/assets/nav.js | 92 ++++++++++ crates/forge-web/src/i18n/misc.rs | 17 ++ crates/forge-web/src/pages/notifications.rs | 187 ++++++++++++++++++-- crates/forge-web/src/views/mod.rs | 15 +- style/_shell.scss | 146 ++++++++++++++- 5 files changed, 433 insertions(+), 24 deletions(-) diff --git a/crates/forge-web/assets/nav.js b/crates/forge-web/assets/nav.js index 0125511..4e03596 100644 --- a/crates/forge-web/assets/nav.js +++ b/crates/forge-web/assets/nav.js @@ -706,6 +706,98 @@ closeDropdowns(e.target && e.target.nodeType === 1 ? e.target : null); }, false); + // The bell beside the profile corner opens a popover of the newest notifications instead of going + // to the inbox. A plain click toggles it; with a modifier or a middle click the link's own + // behaviour stays. The Popover API light-dismisses it and Escape closes it. Its list is fetched + // each time it opens (see the toggle handler). The bell and the popover are siblings. + var peekPress = null; // at a press: whether the popover was open (null before any press) + + function isPeekOpen(pop) { + try { return pop.matches(':popover-open'); } catch (err) { return false; } + } + + // Under the bell, opening upward: the popover's bottom sits 6px above the bell's top, and its left + // edge follows the bell but stays inside the viewport. + function placePeek(bell, pop) { + if (!bell) { return; } + var r = bell.getBoundingClientRect(); + var left = Math.max(8, Math.min(r.left, window.innerWidth - pop.offsetWidth - 8)); + pop.style.left = left + 'px'; + pop.style.bottom = (window.innerHeight - r.top + 6) + 'px'; + } + + // Fills the popover's list with the server's fragment. A reader who closed it meanwhile, or a newer + // open, leaves the answer unused. + function loadPeek(pop) { + var list = pop.querySelector('.bell-list'); + if (!list) { return; } + var seq = ++peekSeq; + var note = document.createElement('p'); + note.className = 'bell-empty muted'; + note.textContent = pop.getAttribute('data-loading') || ''; + list.replaceChildren(note); + fetch('/notifications/peek', { headers: { accept: 'text/html' }, credentials: 'same-origin' }).then(function (r) { + if (!r.ok) { throw new Error('status ' + r.status); } + return r.text(); + }).then(function (html) { + if (seq !== peekSeq || !isPeekOpen(pop)) { return; } + var tpl = document.createElement('template'); + tpl.innerHTML = html; + list.replaceChildren(tpl.content); + }).catch(function () { + if (seq !== peekSeq || !isPeekOpen(pop)) { return; } + var err = document.createElement('p'); + err.className = 'bell-empty muted'; + err.textContent = pop.getAttribute('data-error') || ''; + list.replaceChildren(err); + }); + } + var peekSeq = 0; // a fetch's number: only the newest answer is drawn + + // Records whether the popover was open when the press began. Light dismiss closes it on the press, + // so the click that follows must not reopen it. + document.addEventListener('pointerdown', function (e) { + var bell = e.target && e.target.closest ? e.target.closest('.corner-bell') : null; + if (!bell) { return; } + var pop = bell.nextElementSibling; + peekPress = !!(pop && pop.classList.contains('bell-pop') && isPeekOpen(pop)); + }, true); + + document.addEventListener('click', function (e) { + if (e.button !== 0 || e.metaKey || e.ctrlKey || e.shiftKey || e.altKey) { return; } + var bell = e.target && e.target.closest ? e.target.closest('.corner-bell') : null; + if (!bell) { return; } + var pop = bell.nextElementSibling; + if (!pop || !pop.classList.contains('bell-pop')) { return; } + e.preventDefault(); + // A keyboard click has no press: the popover's own state decides. + var was = peekPress === null ? isPeekOpen(pop) : peekPress; + peekPress = null; + try { + if (was) { pop.hidePopover(); } else { pop.showPopover(); } + } catch (err) { } + }, false); + + // The popover's open state drives its bell's aria-expanded; opening places it and fetches its list. + document.addEventListener('toggle', function (e) { + var pop = e.target; + if (!pop || !pop.classList || !pop.classList.contains('bell-pop')) { return; } + var bell = pop.previousElementSibling; + var open = e.newState === 'open'; + if (bell && bell.classList.contains('corner-bell')) { bell.setAttribute('aria-expanded', open ? 'true' : 'false'); } + if (open) { + placePeek(bell, pop); + loadPeek(pop); + } + }, true); + + // A page swap replaces the sidebar; an open popover goes with the page it was opened on. + document.addEventListener('forge:swapped', function () { + document.querySelectorAll('.bell-pop').forEach(function (pop) { + try { pop.hidePopover(); } catch (err) { } + }); + }); + // Escape closes the open dropdowns and returns focus to the summary of the one that held it. document.addEventListener('keydown', function (e) { if (e.key !== 'Escape') { return; } diff --git a/crates/forge-web/src/i18n/misc.rs b/crates/forge-web/src/i18n/misc.rs index 1dad1ae..83e6bba 100644 --- a/crates/forge-web/src/i18n/misc.rs +++ b/crates/forge-web/src/i18n/misc.rs @@ -178,6 +178,11 @@ pub enum K { Cancelled, YourCiRuns, SearchPublicReposFor, + // The bell's popover: the newest notifications, opened from the profile corner. + OpenNotifications, + PeekLoading, + PeekError, + AllCaughtUp, } /// Every variant, for the coverage test below. @@ -333,6 +338,10 @@ const ALL: &[K] = &[ K::Cancelled, K::YourCiRuns, K::SearchPublicReposFor, + K::OpenNotifications, + K::PeekLoading, + K::PeekError, + K::AllCaughtUp, ]; pub fn t(lang: Lang, key: K) -> &'static str { @@ -663,6 +672,14 @@ pub fn t(lang: Lang, key: K) -> &'static str { (Cancelled, Tr) => "iptal edildi", (YourCiRuns, En) => "Your CI runs", (YourCiRuns, Tr) => "CI çalıştırmaların", + (OpenNotifications, En) => "Open notifications", + (OpenNotifications, Tr) => "Bildirimleri aç", + (PeekLoading, En) => "Loading…", + (PeekLoading, Tr) => "Yükleniyor…", + (PeekError, En) => "Notifications could not be loaded.", + (PeekError, Tr) => "Bildirimler yüklenemedi.", + (AllCaughtUp, En) => "You're all caught up", + (AllCaughtUp, Tr) => "Hepsini gördünüz", (SearchPublicReposFor, En) => "Search public repositories for", (SearchPublicReposFor, Tr) => "Herkese açık depolarda ara:", } diff --git a/crates/forge-web/src/pages/notifications.rs b/crates/forge-web/src/pages/notifications.rs index 8bba69e..3ced06b 100644 --- a/crates/forge-web/src/pages/notifications.rs +++ b/crates/forge-web/src/pages/notifications.rs @@ -19,9 +19,9 @@ use topcoat::context::{Cx, app_context}; use topcoat::router::content::Form; use topcoat::router::content::sse::Event; use topcoat::router::error::{SeeOther, bad_request, not_found, see_other}; -use topcoat::router::response::Response; +use topcoat::router::response::{IntoResponse, Response}; use topcoat::router::{Body, StatusCode, page, path_param, query_params, route}; -use topcoat::view::{View, component, view}; +use topcoat::view::{View, ViewExt, component, view}; use crate::auth::{current_user, percent_encode, require_csrf, require_user}; use crate::i18n::alerts as alert_text; @@ -51,6 +51,9 @@ path_param!(admin_note); /// The pseudo repo id of the admin alerts group in the inbox. const ADMIN_GROUP: &str = "admin"; +/// Rows in the bell's popover. +const PEEK_SIZE: u32 = 8; + #[query_params(error = bad_request)] struct InboxQuery { all: Option, @@ -146,6 +149,15 @@ impl Entry { } } + fn unread(&self) -> bool { + match self { + Entry::Issue(note) => note.unread, + Entry::Discussion(note) => note.unread, + Entry::Advisory(note) => note.unread, + Entry::Admin(note) => note.unread, + } + } + fn updated_at(&self) -> &str { match self { Entry::Issue(note) => ¬e.updated_at, @@ -291,6 +303,115 @@ fn admin_row(lang: Lang, note: &AdminNotice, tz: &str, now: i64) -> RowView { } } +/// Whether the viewer may see an inbox entry: its repo must be readable, and security alerts need +/// the repo's managers, the same rule as the security page. `readable` caches the repo checks. +/// Admin alerts need no repo. +async fn entry_visible( + state: &AppState, + user: &User, + readable: &mut HashMap, + entry: &Entry, +) -> Result { + if let Entry::Admin(_) = entry { + return Ok(true); + } + let repo_id = entry.repo_id(); + let visible = match readable.get(repo_id) { + Some(visible) => *visible, + None => { + let visible = can_read(state, user, repo_id).await?; + readable.insert(repo_id.to_owned(), visible); + visible + } + }; + Ok(visible + && (!matches!(entry, Entry::Advisory(_)) + || can_manage_alerts(state, user, repo_id).await?)) +} + +/// The newest entries of each list the inbox reads, unread ones only or all of them, at most +/// `limit` from each list. +async fn list_entries( + state: &AppState, + user: &User, + unread_only: bool, + limit: u32, +) -> Result> { + let mut entries: Vec = state + .store + .list_notifications(&user.id, unread_only, None, limit) + .await? + .into_iter() + .map(Entry::Issue) + .collect(); + entries.extend( + state + .store + .list_discussion_notifications(&user.id, unread_only, None, limit) + .await? + .into_iter() + .map(Entry::Discussion), + ); + entries.extend( + state + .store + .list_advisory_notifications(&user.id, unread_only, None, limit) + .await? + .into_iter() + .map(Entry::Advisory), + ); + if user.is_admin { + entries.extend( + state + .store + .list_admin_notices(&user.id, unread_only, None, limit) + .await? + .into_iter() + .map(Entry::Admin), + ); + } + Ok(entries) +} + +/// The bell's popover rows: the viewer's newest entries, unread ones first, then read ones, each +/// newest first. Entries the viewer can no longer read are left out and stay as they are. +async fn peek_rows(state: &AppState, user: &User, lang: Lang, now: i64) -> Result> { + let tz = user.timezone.clone(); + let mut entries = list_entries(state, user, true, PEEK_SIZE).await?; + // Under PEEK_SIZE unread entries in all, every list was read to its end, so none is missing; + // the read ones come from the newest of each list. + if entries.len() < PEEK_SIZE as usize { + entries.extend( + list_entries(state, user, false, PEEK_SIZE) + .await? + .into_iter() + .filter(|entry| !entry.unread()), + ); + } + entries.sort_by(|a, b| { + b.unread() + .cmp(&a.unread()) + .then_with(|| b.updated_at().cmp(a.updated_at())) + }); + let mut readable = HashMap::new(); + let mut rows = Vec::new(); + for entry in &entries { + if rows.len() == PEEK_SIZE as usize { + break; + } + if !entry_visible(state, user, &mut readable, entry).await? { + continue; + } + rows.push(match entry { + Entry::Issue(note) => issue_row(lang, note, &tz, now), + Entry::Discussion(note) => discussion_row(lang, note, &tz, now), + Entry::Advisory(note) => advisory_row(lang, note, &tz, now), + Entry::Admin(note) => admin_row(lang, note, &tz, now), + }); + } + Ok(rows) +} + #[page("/notifications")] async fn inbox(cx: &Cx) -> Result { let viewer = require_user(cx).await?; @@ -380,19 +501,7 @@ async fn inbox(cx: &Cx) -> Result { } continue; } - let visible = match readable.get(&repo_id) { - Some(visible) => *visible, - None => { - let visible = can_read(state, user, &repo_id).await?; - readable.insert(repo_id.clone(), visible); - visible - } - }; - // Security alerts are for the repo's managers only, the same rule as the security page. - let visible = visible - && (!matches!(entry, Entry::Advisory(_)) - || can_manage_alerts(state, user, &repo_id).await?); - if !visible { + if !entry_visible(state, user, &mut readable, &entry).await? { hidden.push(entry); continue; } @@ -460,6 +569,54 @@ async fn inbox(cx: &Cx) -> Result { }) } +/// `GET /notifications/peek`: the bell's popover, the viewer's newest notifications as an HTML +/// fragment. Anonymous viewers get a bare 401. +#[route(GET "/notifications/peek")] +async fn peek(cx: &Cx) -> Result { + let Some((user, _)) = current_user(cx).await? else { + let mut res = Response::new(Body::empty()); + *res.status_mut() = StatusCode::UNAUTHORIZED; + return Ok(res); + }; + let state = app_context::(cx); + let lang = i18n::lang(cx, Some(&user)); + let rows = peek_rows(state, &user, lang, now_unix()).await?; + let fragment = view! { peek_list(lang: lang, rows: rows) }; + fragment.first().await?.into_response(cx) +} + +/// The bell's popover list: each row opens through its open route, which marks it read. An empty +/// list says the viewer is all caught up. +#[component] +async fn peek_list(lang: Lang, rows: Vec) -> Result { + let empty = rows.is_empty(); + Ok(view! { + if empty { +

(g::t(lang, K::AllCaughtUp))

+ } + + }) +} + /// The URL of the inbox's live stream for the tab the viewer is on. fn inbox_live_url(all: bool) -> String { if all { diff --git a/crates/forge-web/src/views/mod.rs b/crates/forge-web/src/views/mod.rs index 47f306e..ba5a52a 100644 --- a/crates/forge-web/src/views/mod.rs +++ b/crates/forge-web/src/views/mod.rs @@ -357,7 +357,12 @@ async fn profile_corner(lang: Lang, corner: ProfileCorner) -> Result let settings_label = o::t(lang, OK::Settings); let admin_label = ad::t(lang, AK::AdminArea); let logout_label = t(lang, Key::LogOut); + let bell_title = i18n::t(lang, Key::Notifications); + let open_label = mi::t(lang, MK::OpenNotifications); + let loading_label = mi::t(lang, MK::PeekLoading); + let error_label = mi::t(lang, MK::PeekError); Ok(view! { +
@@ -373,7 +378,6 @@ async fn profile_corner(lang: Lang, corner: ProfileCorner) -> Result (count.as_str()) } -
(theme_label) @@ -402,6 +406,15 @@ async fn profile_corner(lang: Lang, corner: ProfileCorner) -> Result
+ + +
}) } diff --git a/style/_shell.scss b/style/_shell.scss index 9aacab4..be4c877 100644 --- a/style/_shell.scss +++ b/style/_shell.scss @@ -653,17 +653,23 @@ // Profile corner: the signed-in user at the bottom of the sidebar. Its menu opens upward. -.corner { +// The corner's outer box: the rule above it and its place at the sidebar's foot. The bell sits in it +// beside the details, so the box holds both. +.corner-wrap { position: relative; margin-top: auto; padding-top: 8px; border-top: 1px solid var(--border); } -.nav-panel .corner { +.nav-panel .corner-wrap { margin-top: 12px; } +.corner { + position: relative; +} + // The phone panel scrolls, so a menu opening upward would be cut at its top: it opens in place. .nav-panel .corner-menu { position: static; @@ -690,13 +696,24 @@ } } +// The bell overlays the summary's right end, so the chips and the name keep clear of it. +.corner-wrap .corner-btn { + padding-right: 48px; +} + +// The name wraps onto at most two lines, breaking at spaces. A single word too long for the line +// breaks inside it. .corner-name { min-width: 0; overflow: hidden; - font-size: 14px; + font-size: 13.5px; font-weight: 600; - text-overflow: ellipsis; - white-space: nowrap; + line-height: 1.2; + white-space: normal; + overflow-wrap: break-word; + display: -webkit-box; + -webkit-line-clamp: 2; + -webkit-box-orient: vertical; } // The avatar on its own when nothing runs; while a run of the viewer's is queued or running (.hot) @@ -768,8 +785,12 @@ a.chip { } } -// The bell at the corner's right end: the inbox. +// The bell at the corner's right end, over the summary row (its 50px: the 8px top padding and the +// 34px avatar with the summary's padding). It opens the popover of the newest notifications. .corner-bell { + position: absolute; + top: 19px; + right: 14px; display: grid; flex: none; place-items: center; @@ -789,6 +810,115 @@ a.chip { } } +// The bell's popover: the newest notifications, opened upward from the bell (nav.js places it). The +// UA's centring is reset so the fixed placement holds. It has no display of its own: the UA hides it +// while closed. +.bell-pop { + position: fixed; + inset: auto; + margin: 0; + box-sizing: border-box; + width: 320px; + max-width: calc(100vw - 16px); + max-height: min(420px, 70vh); + padding: 0; + overflow: hidden; + background: var(--surface); + color: var(--text); + border: 1px solid var(--border); + border-radius: var(--radius-md); + box-shadow: var(--shadow); +} + +.bell-box { + display: flex; + flex-direction: column; + max-height: min(420px, 70vh); +} + +.bell-head { + padding: 10px 14px 8px; + border-bottom: 1px solid var(--border); + font-size: 14px; + font-weight: 600; +} + +.bell-list { + flex: 1 1 auto; + min-height: 0; + overflow-y: auto; + padding: 4px; +} + +.bell-rows { + margin: 0; + padding: 0; + list-style: none; +} + +.bell-item { + display: grid; + grid-template-columns: 8px minmax(0, 1fr); + gap: 10px; + align-items: start; + padding: 8px 10px; + border-radius: var(--radius-md); + color: var(--text); + text-decoration: none; + + &:hover { + background: var(--surface-2); + text-decoration: none; + } +} + +.bell-dot { + width: 8px; + height: 8px; + margin-top: 6px; + border-radius: 50%; + + &.unread { + background: var(--accent); + } +} + +.bell-text { + display: grid; + gap: 2px; + min-width: 0; +} + +.bell-line { + font-size: 13.5px; + line-height: 1.3; + overflow-wrap: break-word; + display: -webkit-box; + -webkit-line-clamp: 2; + -webkit-box-orient: vertical; + overflow: hidden; +} + +.bell-meta { + display: flex; + flex-wrap: wrap; + gap: 6px; + font-size: 12px; +} + +.bell-empty { + margin: 0; + padding: 10px; + font-size: 13px; +} + +.bell-open { + display: block; + margin: 4px; + text-align: center; + text-decoration: none; +} + .chip-review { color: var(--important); background: color-mix(in srgb, var(--important) 12%, var(--surface)); @@ -1016,12 +1146,12 @@ button.corner-link.link-button { // The profile corner stays above a repo page's fixed tabs, which end where it starts: 50px of // corner, its rule and the sidebar's 24px bottom padding. - .shell-sidebar .corner { + .shell-sidebar .corner-wrap { position: relative; z-index: 6; } - .shell:has(> .shell-sidebar .corner) .repo-tabs { + .shell:has(> .shell-sidebar .corner-wrap) .repo-tabs { bottom: 76px; }