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(sidebar): 保留离线设备分组名称 (#5436)

Dash25388846915a
diff --git a/apps/desktop/src/renderer/__tests__/onlineDeviceHeaders.test.tsx b/apps/desktop/src/renderer/__tests__/onlineDeviceHeaders.test.tsx
index b96f83d0fd..6f049e67d0 100644
--- a/apps/desktop/src/renderer/__tests__/onlineDeviceHeaders.test.tsx
+++ b/apps/desktop/src/renderer/__tests__/onlineDeviceHeaders.test.tsx
@@ -1,10 +1,11 @@
// @vitest-environment jsdom
import type { ReactNode } from 'react';
-import { cleanup, fireEvent, render, screen, within } from '@testing-library/react';
+import { act, cleanup, fireEvent, render, screen, within } from '@testing-library/react';
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
import type { Session } from '@/lib/ccAgent.types';
import type { ProjectNode } from '../features/cc-agent/lib/projectGrouping';
+import { remoteProjectsStore } from '../features/device-link/remoteProjectsStore';
import {
ProjectsSection,
type ProjectsSectionProps,
@@ -131,6 +132,7 @@ function props(groupDevice: boolean): ProjectsSectionProps {
}
beforeEach(() => {
+ remoteProjectsStore.clear();
localStorage.clear();
localStorage.setItem('sidebar.collapse.projectSessionLimit', '1');
// ProjectsSection 渲染时读 window.electronAPI.platform 做项目键比较(main 897de9031);
@@ -142,6 +144,7 @@ beforeEach(() => {
});
afterEach(() => {
cleanup();
+ remoteProjectsStore.clear();
localStorage.clear();
});
@@ -172,6 +175,91 @@ describe('Online device headers without tasks', () => {
});
});
+describe('Offline device header names', () => {
+ function cachedProjectProps(): ProjectsSectionProps {
+ const p = props(true);
+ p.projects = [
+ {
+ projectKey: 'device:offline:/project',
+ displayName: 'Cached project',
+ workingDir: '/project',
+ scope: 'remote',
+ remoteHostId: null,
+ deviceLinkDeviceId: 'offline',
+ deviceLinkDeviceName: 'Saved computer',
+ deviceLinkConnectionStatus: 'disconnected',
+ segments: 1,
+ sessions: [
+ {
+ id: 'cached-task',
+ title: 'Cached task',
+ status: 'active',
+ workingDir: '/project',
+ deviceLinkDeviceId: 'offline',
+ deviceLinkDeviceName: 'Saved computer',
+ createdAt: '2026-10-04T00:00:00Z',
+ updatedAt: '2026-10-04T00:00:00Z',
+ } as Session,
+ ],
+ latestActivityAt: '2026-10-04T00:00:00Z',
+ },
+ ];
+ p.allProjectKeysForOrder = p.projects.map((project) => project.projectKey);
+ return p;
+ }
+
+ it('keeps the cached name after disconnecting and uses the current name on reconnect', () => {
+ remoteProjectsStore.setDeviceSessions('offline', 'Saved computer', []);
+ const p = cachedProjectProps();
+ const onlineIndex = new Map(p.remoteDeviceIndex);
+ onlineIndex.set('offline', { name: 'Current computer', online: true });
+ const view = render(<ProjectsSection {...p} remoteDeviceIndex={onlineIndex} />);
+ expect(screen.getByText('Current computer')).toBeTruthy();
+ expect(screen.queryByText('Saved computer')).toBeNull();
+
+ act(() => remoteProjectsStore.markDeviceDisconnected('offline'));
+ view.rerender(<ProjectsSection {...p} />);
+ expect(screen.getByText('Saved computer')).toBeTruthy();
+ expect(screen.queryByText('offline')).toBeNull();
+ expect(screen.getByText('ccAgent.sidebar.deviceGroup.offline')).toBeTruthy();
+
+ act(() => remoteProjectsStore.renameDevice('offline', 'Renamed computer'));
+ expect(screen.getByText('Renamed computer')).toBeTruthy();
+ expect(screen.queryByText('Saved computer')).toBeNull();
+
+ view.rerender(<ProjectsSection {...p} remoteDeviceIndex={onlineIndex} />);
+ expect(screen.getByText('Current computer')).toBeTruthy();
+ expect(screen.queryByText('ccAgent.sidebar.deviceGroup.offline')).toBeNull();
+ });
+
+ it('reads the name from a restored offline snapshot on first render', () => {
+ remoteProjectsStore.hydrateFromCache([
+ {
+ deviceId: 'offline',
+ deviceName: 'Restored computer',
+ sessions: cachedProjectProps().projects[0].sessions,
+ },
+ ]);
+ render(<ProjectsSection {...cachedProjectProps()} />);
+ expect(screen.getByText('Restored computer')).toBeTruthy();
+ expect(screen.queryByText('offline')).toBeNull();
+ });
+
+ it('still falls back to the device ID when no name is known', () => {
+ render(<ProjectsSection {...cachedProjectProps()} />);
+ expect(screen.getByText('offline')).toBeTruthy();
+ });
+
+ it('does not enable device grouping when only disconnected devices remain', () => {
+ remoteProjectsStore.setDeviceSessions('offline', 'Saved computer', []);
+ remoteProjectsStore.markDeviceDisconnected('offline');
+ render(<ProjectsSection {...cachedProjectProps()} remoteDeviceIndex={new Map()} />);
+ expect(screen.getByText('Cached project')).toBeTruthy();
+ expect(screen.queryByText('Saved computer')).toBeNull();
+ expect(screen.queryByText('ccAgent.sidebar.deviceGroup.local')).toBeNull();
+ });
+});
+
describe('Cindy Make sidebar entry', () => {
function makeProps(groupDevice: boolean, custom: boolean) {
const p = props(groupDevice);
diff --git a/apps/desktop/src/renderer/features/cc-agent/sidebar/sections/ProjectsSection.tsx b/apps/desktop/src/renderer/features/cc-agent/sidebar/sections/ProjectsSection.tsx
index 60e08dda58..3cf6db0719 100644
--- a/apps/desktop/src/renderer/features/cc-agent/sidebar/sections/ProjectsSection.tsx
+++ b/apps/desktop/src/renderer/features/cc-agent/sidebar/sections/ProjectsSection.tsx
@@ -49,6 +49,7 @@ import { useTranslation } from 'react-i18next';
import { cn } from '@/lib/utils';
import { Tip } from '@/components/ui/tooltip';
import { useEffectiveSelectedMachineId } from '@/features/device-link/useMachineSwitcher';
+import { useRemoteDevices } from '@/features/device-link/remoteProjectsStore';
import { MACHINE_ALL } from '@/features/device-link/selectedMachineStore';
import {
projectOrderWriteLedger,
@@ -315,6 +316,12 @@ export function ProjectsSection({
isCreateDialogueDisabled = false,
}: ProjectsSectionProps) {
const { t } = useTranslation();
+ const remoteDevices = useRemoteDevices();
+ // 分组索引会排除断线设备;已有缓存条目的段头仍需读取设备名,并跟随改名更新。
+ const cachedDeviceNames = useMemo(
+ () => new Map(remoteDevices.map((device) => [device.deviceId, device.deviceName])),
+ [remoteDevices],
+ );
const localPlatform = window.electronAPI.platform;
const projectComparisonKey = useCallback(
(projectKey: string) => projectKeyComparisonKey(projectKey, localPlatform) ?? projectKey,
@@ -1148,7 +1155,7 @@ export function ProjectsSection({
? remoteDeviceIndex?.get(section.deviceId)
: undefined;
const name = section.deviceId
- ? (device?.name ?? section.deviceId)
+ ? (device?.name ?? cachedDeviceNames.get(section.deviceId) ?? section.deviceId)
: t('ccAgent.sidebar.deviceGroup.local');
const online = section.deviceId ? (device?.online ?? false) : true;
const sectionCollapsed = collapsedDevices.has(key);