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。

fix(desktop): restore plugin catalog scroll on detail back

qsoyq3cabf0161698
diff --git a/apps/desktop/src/renderer/features/plugin/GhostPluginPage.tsx b/apps/desktop/src/renderer/features/plugin/GhostPluginPage.tsx
index 7f5244b102..62253d04f5 100644
--- a/apps/desktop/src/renderer/features/plugin/GhostPluginPage.tsx
+++ b/apps/desktop/src/renderer/features/plugin/GhostPluginPage.tsx
@@ -110,6 +110,7 @@ import {
PluginManagementLayout,
PluginManagementPage,
} from './PluginManagementLayout';
+import { usePluginListScrollRestoration } from './lib/usePluginListScrollRestoration';
import { GhostPagePanelHost } from './GhostPagePanelHost';
import { GhostPluginDetailView } from './GhostPluginDetailView';
import {
@@ -426,6 +427,15 @@ export function GhostPluginPage({
}, [ignoredRoundKey]);
const [originFilter, setOriginFilter] = useState<PluginPresentationFilter>('all');
const [marketDetail, setMarketDetail] = useState<PluginMarketDetail | null>(null);
+ // 列表与详情是互斥分支;列表节点会卸载,返回时需显式恢复进入前的滚动位置。
+ const pluginCatalogVisible = marketDetail === null && selectedId === null;
+ const {
+ listRef: pluginCatalogListRef,
+ onListScroll: onPluginCatalogScroll,
+ capture: capturePluginCatalogScroll,
+ requestRestore: requestPluginCatalogScrollRestore,
+ clearPendingRestore: clearPluginCatalogScrollRestore,
+ } = usePluginListScrollRestoration(pluginCatalogVisible);
const [marketBusyId, setMarketBusyId] = useState<string | null>(null);
// 市场操作的同步互斥锁。React state 在提交前有窗口期,快速连点会让多个回调
// 都读到 null;ref 先到先得,state 只驱动按钮禁用等 UI 展示。每次占锁都返回
@@ -487,13 +497,14 @@ export function GhostPluginPage({
}
}, []);
useEffect(() => {
+ clearPluginCatalogScrollRestore();
setMarketSnapshot(null);
setMarketDetail(null);
marketBusyLockRef.current = null;
setMarketBusyId(null);
marketDetailRequestRef.current += 1;
void refreshMarket();
- }, [refreshMarket, mode, dataOwnerId]);
+ }, [clearPluginCatalogScrollRestore, refreshMarket, mode, dataOwnerId]);
const refreshMarketOnForeground = useCallback(() => refreshMarket(true), [refreshMarket]);
usePluginMarketForegroundRefresh(refreshMarketOnForeground, lastMarketRefreshAtRef);
useEffect(() => {
@@ -1233,6 +1244,7 @@ export function GhostPluginPage({
// 与 handleMarketUpdate 共用同一互斥锁:更新进行中不叠加其它市场操作。
const marketBusyLease = acquireMarketBusy(pluginId);
if (!marketBusyLease) return;
+ capturePluginCatalogScroll();
const requestId = ++marketDetailRequestRef.current;
try {
const detail = await window.electronAPI.pluginMarket.detail(pluginId);
@@ -1253,8 +1265,13 @@ export function GhostPluginPage({
releaseMarketBusy(marketBusyLease);
}
},
- [acquireMarketBusy, isMarketBusyLeaseActive, releaseMarketBusy, t],
+ [acquireMarketBusy, capturePluginCatalogScroll, isMarketBusyLeaseActive, releaseMarketBusy, t],
);
+ const handleMarketBack = useCallback(() => {
+ requestPluginCatalogScrollRestore();
+ marketDetailRequestRef.current += 1;
+ setMarketDetail(null);
+ }, [requestPluginCatalogScrollRestore]);
const refreshVisibleMarketDetail = useCallback(async (pluginId: string) => {
// A background icon renewal may observe navigation, but must never invalidate a
@@ -1427,10 +1444,7 @@ export function GhostPluginPage({
<MarketPluginDetailView
detail={marketDetail}
busy={marketBusyId === marketDetail.pluginId}
- onBack={() => {
- marketDetailRequestRef.current += 1;
- setMarketDetail(null);
- }}
+ onBack={handleMarketBack}
onInstall={
canOfferMarketInstall(mode, marketDetail.ghostId)
? () => void handleInstallFromMarket()
@@ -1505,10 +1519,12 @@ export function GhostPluginPage({
>
<div className="flex min-h-0 flex-1">
<main
+ ref={pluginCatalogListRef}
className={cn(
'min-h-0 w-full min-w-0 flex-1 overflow-y-auto [scrollbar-gutter:stable_both-edges]',
embedded ? 'bg-transparent' : 'bg-[var(--surface)]',
)}
+ onScroll={onPluginCatalogScroll}
>
<PluginManagementPage>
<header className="plugin-motion-page-header pb-2">
diff --git a/apps/desktop/src/renderer/features/plugin/__tests__/usePluginListScrollRestoration.test.tsx b/apps/desktop/src/renderer/features/plugin/__tests__/usePluginListScrollRestoration.test.tsx
new file mode 100644
index 0000000000..5b12fe5167
--- /dev/null
+++ b/apps/desktop/src/renderer/features/plugin/__tests__/usePluginListScrollRestoration.test.tsx
@@ -0,0 +1,77 @@
+/**
+ * Regression coverage for the Plugin catalog's conditional list/detail mount.
+ * [PROTOCOL]: 变更时更新此头部,然后检查 CLAUDE.md
+ * @vitest-environment jsdom
+ */
+
+import { act, fireEvent, render, screen } from '@testing-library/react';
+import { describe, expect, it } from 'vitest';
+
+import {
+ usePluginListScrollRestoration,
+ type PluginListScrollRestoration,
+} from '../lib/usePluginListScrollRestoration';
+
+let restoration: PluginListScrollRestoration;
+
+function Harness({ listVisible }: { listVisible: boolean }) {
+ restoration = usePluginListScrollRestoration(listVisible);
+ return listVisible ? (
+ <main data-testid="plugin-list" ref={restoration.listRef} onScroll={restoration.onListScroll} />
+ ) : (
+ <div data-testid="plugin-detail" />
+ );
+}
+
+function setScrollTop(element: HTMLElement, value: number) {
+ Object.defineProperty(element, 'scrollTop', {
+ configurable: true,
+ writable: true,
+ value,
+ });
+}
+
+describe('usePluginListScrollRestoration', () => {
+ it('restores the captured catalog position when the list is mounted again', () => {
+ const view = render(<Harness listVisible />);
+ const list = screen.getByTestId('plugin-list');
+ setScrollTop(list, 640);
+
+ act(() => {
+ restoration.capture();
+ restoration.requestRestore();
+ });
+ view.rerender(<Harness listVisible={false} />);
+ view.rerender(<Harness listVisible />);
+
+ expect(screen.getByTestId('plugin-list').scrollTop).toBe(640);
+ });
+
+ it('does not restore a position unless the return path requests it', () => {
+ const view = render(<Harness listVisible />);
+ const list = screen.getByTestId('plugin-list');
+ setScrollTop(list, 320);
+ fireEvent.scroll(list);
+
+ view.rerender(<Harness listVisible={false} />);
+ view.rerender(<Harness listVisible />);
+
+ expect(screen.getByTestId('plugin-list').scrollTop).toBe(0);
+ });
+
+ it('can cancel a pending restore when the owning catalog changes', () => {
+ const view = render(<Harness listVisible />);
+ const list = screen.getByTestId('plugin-list');
+ setScrollTop(list, 280);
+ fireEvent.scroll(list);
+
+ act(() => {
+ restoration.requestRestore();
+ restoration.clearPendingRestore();
+ });
+ view.rerender(<Harness listVisible={false} />);
+ view.rerender(<Harness listVisible />);
+
+ expect(screen.getByTestId('plugin-list').scrollTop).toBe(0);
+ });
+});
diff --git a/apps/desktop/src/renderer/features/plugin/lib/usePluginListScrollRestoration.ts b/apps/desktop/src/renderer/features/plugin/lib/usePluginListScrollRestoration.ts
new file mode 100644
index 0000000000..248abbe723
--- /dev/null
+++ b/apps/desktop/src/renderer/features/plugin/lib/usePluginListScrollRestoration.ts
@@ -0,0 +1,57 @@
+import { useCallback, useLayoutEffect, useRef, type RefObject, type UIEvent } from 'react';
+
+export interface PluginListScrollRestoration {
+ listRef: RefObject<HTMLElement | null>;
+ onListScroll: (event: UIEvent<HTMLElement>) => void;
+ capture: () => void;
+ requestRestore: () => void;
+ clearPendingRestore: () => void;
+}
+
+/**
+ * Keeps a catalog's scroll offset across a conditional list/detail mount.
+ * The caller decides when a return should restore the saved position.
+ */
+export function usePluginListScrollRestoration(listVisible: boolean): PluginListScrollRestoration {
+ const listRef = useRef<HTMLElement | null>(null);
+ const scrollTopRef = useRef(0);
+ const pendingRestoreRef = useRef(false);
+ const wasListVisibleRef = useRef(listVisible);
+
+ const onListScroll = useCallback((event: UIEvent<HTMLElement>) => {
+ scrollTopRef.current = event.currentTarget.scrollTop;
+ }, []);
+
+ const capture = useCallback(() => {
+ const list = listRef.current;
+ if (list) scrollTopRef.current = list.scrollTop;
+ }, []);
+
+ const requestRestore = useCallback(() => {
+ pendingRestoreRef.current = true;
+ }, []);
+
+ const clearPendingRestore = useCallback(() => {
+ pendingRestoreRef.current = false;
+ }, []);
+
+ useLayoutEffect(() => {
+ const wasListVisible = wasListVisibleRef.current;
+ wasListVisibleRef.current = listVisible;
+ if (!listVisible || wasListVisible || !pendingRestoreRef.current) return;
+
+ const list = listRef.current;
+ if (!list) return;
+ // Restore before paint so returning from detail never flashes the top.
+ list.scrollTop = scrollTopRef.current;
+ pendingRestoreRef.current = false;
+ }, [listVisible]);
+
+ return {
+ listRef,
+ onListScroll,
+ capture,
+ requestRestore,
+ clearPendingRestore,
+ };
+}