Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

feat(desktop): 更新公告右上角显示版本号并跟随滚动 (#1946)

Shinkua4b757aca508
diff --git a/apps/desktop/src/renderer/__tests__/updateNoticeDialogAutoLayout.test.tsx b/apps/desktop/src/renderer/__tests__/updateNoticeDialogAutoLayout.test.tsx
index b752227647..7e61f909ae 100644
--- a/apps/desktop/src/renderer/__tests__/updateNoticeDialogAutoLayout.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/updateNoticeDialogAutoLayout.test.tsx
@@ -5,11 +5,9 @@
*
* 装完后的自动公告走这条布局,UpdateBanner 的「装前预览」也刻意复用它(见 useUpdateNotice
* 的 onOpenVersion)。所以这个文件既是既有行为的回归护栏,也是新入口渲染形态的说明:
- * - 单版本:右上角是该版本日期,徽标 v<版本>
- * - 跨版本:右上角是版本数,每个版本内容块各自展示 v<版本> 徽标
+ * - 单版本:右上角是 v<版本> 徽标
+ * - 跨版本:右上角是 v<版本> 徽标,跟随滚动更新到当前可见版本
* 两种情况都没有版本跳转器、没有懒加载占位块。
- *
- * 弹窗本身在本次改动里一行未改;这里锁住的是「预览入口依赖的那部分不能被顺手清理掉」。
*/
import { cleanup, render, screen } from '@testing-library/react';
@@ -71,22 +69,26 @@ function renderAuto(releaseNotes: ReleaseNotes[]) {
}
describe('UpdateNoticeDialog auto layout', () => {
- it('single version: date on the right, plain version badge, single-version aria', () => {
+ it('single version: version badge in header and content, single-version aria', () => {
renderAuto([NEWEST]);
expect(screen.queryByText(/update\.notice\.versionsSpan/)).toBeNull();
expect(screen.getAllByText(/2026/).length).toBeGreaterThan(0);
- expect(screen.getByText('v0.1.21')).toBeTruthy();
+ // v0.1.21 appears in header badge AND in the version block subheader.
+ expect(screen.getAllByText('v0.1.21')).toHaveLength(2);
expect(
screen.getByText('update.notice.ariaDescription(version=0.1.21)'),
).toBeTruthy();
});
- it('multiple versions: version count on the right, per-version badges, span aria', () => {
+ it('multiple versions: version badge in header, per-version badges, span aria', () => {
renderAuto([NEWEST, OLDER]);
- expect(screen.getByText('update.notice.versionsSpan(count=2)')).toBeTruthy();
- expect(screen.getByText('v0.1.21')).toBeTruthy();
+ // No version count label anymore — header shows v0.1.21 badge.
+ expect(screen.queryByText(/update\.notice\.versionsSpan/)).toBeNull();
+ // v0.1.21 appears in header badge AND in its content block.
+ expect(screen.getAllByText('v0.1.21')).toHaveLength(2);
+ // v0.1.20 appears only in its content block (header badge shows v0.1.21).
expect(screen.getByText('v0.1.20')).toBeTruthy();
expect(
screen.getByText('update.notice.ariaDescriptionSpan(from=0.1.20,count=2)'),
diff --git a/apps/desktop/src/renderer/__tests__/updateNoticeDialogLayout.test.tsx b/apps/desktop/src/renderer/__tests__/updateNoticeDialogLayout.test.tsx
index 7038b53925..e6b9397bdc 100644
--- a/apps/desktop/src/renderer/__tests__/updateNoticeDialogLayout.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/updateNoticeDialogLayout.test.tsx
@@ -15,7 +15,7 @@
*/
import { act, cleanup, render, screen, waitFor, within } from '@testing-library/react';
-import { afterEach, describe, expect, it, vi } from 'vitest';
+import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import '@/i18n';
import i18n from '@/i18n';
@@ -66,7 +66,19 @@ function renderDialog(notes: ReleaseNotes[]) {
);
}
-afterEach(cleanup);
+afterEach(() => {
+ cleanup();
+ vi.unstubAllGlobals();
+});
+
+beforeEach(() => {
+ vi.stubGlobal('IntersectionObserver', class {
+ observe() {}
+ unobserve() {}
+ disconnect() {}
+ takeRecords() { return []; }
+ });
+});
describe('UpdateNoticeDialog 单栏版式', () => {
it('贡献者名单每个版本只渲染一次,不再 chrome 与子头各一份', () => {
@@ -137,10 +149,11 @@ describe('UpdateNoticeDialog 单栏版式', () => {
expect(screen.getAllByText(i18n.t('update.notice.newFeatures'))).toHaveLength(1);
});
- it('单版本 auto 模式的版本号与日期在版本块里,不在标题栏', () => {
+ it('单版本 auto 模式:版本号出现在标题栏右上角与内容块两处', () => {
renderDialog([topicNotes('0.1.21', ['Kafeifei'])]);
- expect(screen.getByText('v0.1.21')).toBeTruthy();
- // 标题栏右侧在单版本 auto 下不再显示日期或版本计数。
+ // v0.1.21 同时出现在标题栏 VersionBadge 和内容块 VersionBlock 的徽标中。
+ expect(screen.getAllByText('v0.1.21')).toHaveLength(2);
+ // 标题栏右侧不再显示版本计数。
expect(screen.queryByText(i18n.t('update.notice.versionsSpan', { count: 1 }))).toBeNull();
});
diff --git a/apps/desktop/src/renderer/components/UpdateNoticeDialog.tsx b/apps/desktop/src/renderer/components/UpdateNoticeDialog.tsx
index c3b7cbb9bc..834f9cda6d 100644
--- a/apps/desktop/src/renderer/components/UpdateNoticeDialog.tsx
+++ b/apps/desktop/src/renderer/components/UpdateNoticeDialog.tsx
@@ -477,14 +477,92 @@ function VersionDropdown({
function AutoBody({
releaseNotes,
locale,
+ onStickyChange,
}: {
releaseNotes: ReleaseNotes[];
locale: string;
+ onStickyChange: (version: string) => void;
}) {
+ const scrollRef = useRef<HTMLDivElement | null>(null);
+ const blockRefs = useRef<Map<string, HTMLDivElement>>(new Map());
+
+ // Sticky-header observer: track which version block is at the top of the
+ // scroll area so the header badge can follow. Only active for multi-version
+ // auto mode — single-version stays at its initial value.
+ useEffect(() => {
+ const root = scrollRef.current;
+ if (!root || releaseNotes.length <= 1) return;
+ const observer = new IntersectionObserver(
+ (entries) => {
+ let best: { v: string; top: number } | null = null;
+ for (const entry of entries) {
+ if (!entry.isIntersecting) continue;
+ const v = (entry.target as HTMLElement).dataset.version;
+ if (!v) continue;
+ const top = entry.boundingClientRect.top;
+ // Pick the entry whose top is closest to the container top
+ // (largest top = nearest to 0, i.e. the version the user is reading).
+ if (best === null || top > best.top) best = { v, top };
+ }
+ if (best) onStickyChange(best.v);
+ },
+ { root, rootMargin: '0px 0px -99% 0px', threshold: 0 },
+ );
+ for (const el of blockRefs.current.values()) observer.observe(el);
+ return () => observer.disconnect();
+ }, [releaseNotes, onStickyChange]);
+
+ // Fallback: when scrolled to the bottom, pick the last version so the header
+ // badge updates even if the last version is too short to reach the observation
+ // zone at the top. When leaving the bottom, recompute the visible version so
+ // the badge doesn't stay stuck on the last version.
+ const lastVersion = releaseNotes[releaseNotes.length - 1]?.version;
+ const handleAutoScroll = useCallback(() => {
+ const el = scrollRef.current;
+ if (!el || !lastVersion) return;
+ // Guard: only apply the bottom override when the content is actually
+ // scrollable. A +1 px threshold prevents sub-pixel rounding at
+ // non-integer zoom from making scrollHeight − clientHeight = 1 while
+ // the content is visually unscrollable; otherwise scrollTop is always
+ // 0 and the condition would trivially hold at the top, causing the
+ // header to show the last version.
+ if (
+ el.scrollHeight > el.clientHeight + 1 &&
+ el.scrollTop + el.clientHeight >= el.scrollHeight - 1
+ ) {
+ onStickyChange(lastVersion);
+ return;
+ }
+ // Leaving the bottom: find the version block currently closest to the
+ // container top. This mirrors the IntersectionObserver's sticky-header
+ // logic but runs on every scroll event when not at the very bottom,
+ // clearing the bottom override that the IO cannot undo on its own.
+ const containerTop = el.getBoundingClientRect().top;
+ let best: { v: string; top: number } | null = null;
+ for (const [version, blockEl] of blockRefs.current.entries()) {
+ const top = blockEl.getBoundingClientRect().top;
+ if (top > containerTop + 9) continue; // block far below container top (9px = py-2 padding + 1px tolerance)
+ if (best === null || top > best.top) best = { v: version, top };
+ }
+ if (best) onStickyChange(best.v);
+ }, [lastVersion, onStickyChange]);
+
return (
- <div className="flex flex-1 min-h-0 flex-col overflow-y-auto py-2 select-text">
+ <div
+ ref={scrollRef}
+ onScroll={handleAutoScroll}
+ className="flex flex-1 min-h-0 flex-col overflow-y-auto py-2 select-text"
+ >
{releaseNotes.map((notes, i) => (
- <div key={notes.version} className="flex flex-col">
+ <div
+ key={notes.version}
+ ref={(el) => {
+ if (el) blockRefs.current.set(notes.version, el);
+ else blockRefs.current.delete(notes.version);
+ }}
+ data-version={notes.version}
+ className="flex flex-col"
+ >
{i > 0 && <div className="mx-auto h-px w-full max-w-[800px] bg-[var(--cmd-palette-border)]" />}
<VersionBlock notes={notes} locale={locale} />
</div>
@@ -683,16 +761,15 @@ function ManualBody({
// that band is by definition the topmost currently-scrolled-to element.
const observer = new IntersectionObserver(
(entries) => {
- // Pick the most-recently-crossed intersecting entry. Multiple can
- // report in a single callback during fast scrolls; take the one with
- // the smallest positive `top` for stability.
+ // Pick the entry whose top is closest to the container top
+ // (largest top = nearest to 0, i.e. the version the user is reading).
let best: { v: string; top: number } | null = null;
for (const entry of entries) {
if (!entry.isIntersecting) continue;
const v = (entry.target as HTMLElement).dataset.version;
if (!v) continue;
const top = entry.boundingClientRect.top;
- if (best === null || top < best.top) best = { v, top };
+ if (best === null || top > best.top) best = { v, top };
}
if (best) onStickyChange(best.v);
},
@@ -702,6 +779,41 @@ function ManualBody({
return () => observer.disconnect();
}, [allVersions, onStickyChange]);
+ // Fallback: when scrolled to the bottom, pick the last version so the header
+ // badge updates even if the last version is too short to reach the top
+ // observation zone. When leaving the bottom, recompute the visible version so
+ // the badge doesn't stay stuck on the last version.
+ const lastVersion = allVersions[allVersions.length - 1];
+ const handleManualScroll = useCallback(() => {
+ const el = scrollRef.current;
+ if (!el || !lastVersion) return;
+ // Guard: only apply the bottom override when the content is actually
+ // scrollable. A +1 px threshold prevents sub-pixel rounding at
+ // non-integer zoom from making scrollHeight − clientHeight = 1 while
+ // the content is visually unscrollable; otherwise scrollTop is always
+ // 0 and the condition would trivially hold at the top, causing the
+ // header to show the last version.
+ if (
+ el.scrollHeight > el.clientHeight + 1 &&
+ el.scrollTop + el.clientHeight >= el.scrollHeight - 1
+ ) {
+ onStickyChange(lastVersion);
+ return;
+ }
+ // Leaving the bottom: find the version block currently closest to the
+ // container top. This mirrors the IntersectionObserver's sticky-header
+ // logic but runs on every scroll event when not at the very bottom,
+ // clearing the bottom override that the IO cannot undo on its own.
+ const containerTop = el.getBoundingClientRect().top;
+ let best: { v: string; top: number } | null = null;
+ for (const [version, blockEl] of blockRefs.current.entries()) {
+ const top = blockEl.getBoundingClientRect().top;
+ if (top > containerTop + 9) continue; // block far below container top (9px = py-2 padding + 1px tolerance)
+ if (best === null || top > best.top) best = { v: version, top };
+ }
+ if (best) onStickyChange(best.v);
+ }, [lastVersion, onStickyChange]);
+
// Programmatic jump handler exposed to parent (header dropdown).
useEffect(() => {
registerJump((v: string) => {
@@ -715,7 +827,11 @@ function ManualBody({
}, [registerJump, startLoad]);
return (
- <div ref={scrollRef} className="flex flex-1 min-h-0 flex-col overflow-y-auto py-2 select-text">
+ <div
+ ref={scrollRef}
+ onScroll={handleManualScroll}
+ className="flex flex-1 min-h-0 flex-col overflow-y-auto py-2 select-text"
+ >
{allVersions.map((v, i) => {
const notes = notesMap.get(v);
const state = stateMap.get(v) ?? 'idle';
@@ -808,8 +924,18 @@ export function UpdateNoticeDialog({
}, [onDismiss]);
// Reset sticky when dialog re-opens (avoids showing last-session's badge).
+ // Preserve the current sticky version across locale refreshes — when the
+ // dialog is already open and releaseNotes changes only because of a locale
+ // switch, the scroll position in ManualBody remains in place so the badge
+ // should not jump back to the newest version.
+ const wasOpenRef = useRef(false);
useEffect(() => {
- if (open) setStickyVersion(releaseNotes?.[0]?.version ?? '');
+ if (open) {
+ if (!wasOpenRef.current) {
+ setStickyVersion(releaseNotes?.[0]?.version ?? '');
+ }
+ }
+ wasOpenRef.current = open;
}, [open, releaseNotes]);
if (!releaseNotes || !mode || releaseNotes.length === 0) {
@@ -838,17 +964,10 @@ export function UpdateNoticeDialog({
})
: t('update.notice.ariaDescription', { version: newest.version });
- // Header's right cell — a version count, and in manual mode the entry point
- // for jumping across history. Version number, date and contributors all moved
- // into each version's own block, so there is nothing else left up here.
- // - Manual: "N versions" + dropdown
- // - Auto multi: "N versions", static
- // - Auto single: nothing (a single version's identity is in its block)
- const versionCountLabel = isManual
- ? t('update.notice.versionsSpan', { count: allVersions?.length ?? 1 })
- : isAutoMulti
- ? t('update.notice.versionsSpan', { count: releaseNotes.length })
- : null;
+ // Header's right cell: version badge that follows scroll.
+ // - Manual multi: v<version> badge + dropdown for history navigation.
+ // - Auto multi: v<version> badge, updates as user scrolls.
+ // - Auto / manual single: static v<version> badge.
return (
<AlertDialog.Root
@@ -916,11 +1035,11 @@ export function UpdateNoticeDialog({
{t('update.notice.title')}
</AlertDialog.Title>
<div className="min-w-0 justify-self-end">
- {isManual && allVersions && allVersions.length > 1 && versionCountLabel ? (
+ {isManual && allVersions && allVersions.length > 1 ? (
<VersionDropdown
versions={allVersions}
currentVersion={stickyVersion || newest.version}
- triggerLabel={versionCountLabel}
+ triggerLabel={`v${stickyVersion || newest.version}`}
triggerAriaLabel={t('update.notice.versionJumpAria', {
count: allVersions.length,
version: stickyVersion || newest.version,
@@ -939,11 +1058,9 @@ export function UpdateNoticeDialog({
if (!dropOpen) dropdownClosedAtRef.current = Date.now();
}}
/>
- ) : versionCountLabel ? (
- <span className="whitespace-nowrap text-13 text-[var(--cmd-palette-item-meta)]">
- {versionCountLabel}
- </span>
- ) : null}
+ ) : (
+ <VersionBadge label={`v${stickyVersion || newest.version}`} />
+ )}
</div>
</div>
@@ -963,7 +1080,11 @@ export function UpdateNoticeDialog({
registerJump={registerJump}
/>
) : (
- <AutoBody releaseNotes={releaseNotes} locale={i18n.language} />
+ <AutoBody
+ releaseNotes={releaseNotes}
+ locale={i18n.language}
+ onStickyChange={setStickyVersion}
+ />
)}
<div className="h-px bg-[var(--cmd-palette-border)]" />