Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。
TypeScript agentai-agentai-assistantandroidclaude-codecodexelectroniosllmmacosreact-nativetypescriptwindows
import { getMobileAuthOwner, isMobileAuthOwnerCurrent } from '@/auth/authOwnerGeneration';
import { readTeammateTasks } from '@/session/readTeammateTasks';
import { deviceDisplayName } from '@/device-link/deviceName';
import { useIsFocused, useLocalSearchParams, useRouter } from 'expo-router';
import { withNewSessionCreationRemoval } from '@/session/newSessionCreation';
import { useHeaderHeight } from 'expo-router/react-navigation';
import { useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from 'react';
import { useSharedValue } from 'react-native-reanimated';
import {
ActivityIndicator,
AppState,
RefreshControl,
ScrollView,
SectionList,
StyleSheet,
View,
useWindowDimensions,
} from 'react-native';
import { Text } from '@/components/AppText';
import { SafeAreaView, useSafeAreaInsets } from 'react-native-safe-area-context';
import { useTranslation } from 'react-i18next';
import type { TFunction } from 'i18next';
import { ConnectionBanner, useShowConnectionBanner } from '@/components/ConnectionBanner';
import { resolveEffectiveConnectionError } from '@/components/connectionBannerVisibility';
import { unresponsiveDevicesStore, useUnresponsiveDevices } from '@/device-link/unresponsiveDevicesStore';
import { goBackGuarded } from '@/utils/backGuard';
import { useGuardedPush } from '@/utils/useGuardedPush';
import { mapContentEqual } from '@/utils/valueEquality';
import { useStableValue } from '@/utils/useStableValue';
import {
MainWindowActionButton,
MainWindowActionGroup,
MainWindowEmptyState,
MainWindowMetric,
MainWindowOptionButton,
RemoteListSyncingPlaceholder,
SummaryStrip,
} from '@/components/MobilePrimitives';
import {
SimpleStackHeader,
simpleScrollInsetProps,
simpleScrollScreenSafeAreaEdges,
usesNativeStackHeader,
} from '@/platform/chrome';
import { buildMainWindowLayout } from '@/components/mainWindowLayout';
import { useDeviceLink } from '@/device-link/DeviceLinkContext';
import { formatRemoteError } from '@/device-link/remoteStatus';
import { isExplicitRemoteNotFoundError } from '@/session/newSessionWorktree';
import { withTransientRemoteRetry } from '@/device-link/remoteRetry';
import { useRemoteSyncTask } from '@/device-link/remoteSyncTask';
import { useRemoteProjectAliases } from '@/session/remoteProjectAliases';
import { resolveMobileProjectRouteTitle } from '@/session/projectAliasPresentation';
import {
automationGroupKey,
buildRemoteSessionListContext,
buildRemoteSessionSections,
deviceSessionEmptyState,
remoteSessionControlsSummary,
remoteSessionFilterLabel,
summarizeRemoteSessionOverview,
type RemoteAutomationSessionGroup,
type RemoteSessionListItem,
type RemoteSessionScheduleInfo,
type RemoteSessionStatusFilter,
} from '@/session/sessionList';
import {
mobileSessionBulkActionButtonLabel,
mobileSessionBulkPatch,
pruneSessionSelection,
sessionIdsForListItem,
summarizeMobileSessionBulkAction,
visibleMobileSessionBulkActions,
visibleSessionIdsFromSections,
type MobileSessionBulkAction,
} from '@/session/sessionSelection';
import { serializeNewSessionDeviceOptions } from '@/session/newSession';
import { ConversationSearchFilterSheet } from '@/session/ConversationSearchFilterSheet';
import { useConversationSearchFilterMenu } from '@/session/useConversationSearchFilterMenu';
import { HomeSearchBar } from '@/session/HomeSearchBar';
import {
conversationSearchAllowsLocalWrites,
listConversationSearchProjects,
shouldReplaceListWithSearchResults,
} from '@/session/conversationSearch';
import { useConversationSearch } from '@/session/useConversationSearch';
import { selectVisibleDeviceSessions, sessionMatchesProjectDir } from '@/session/mobileHome';
import { HomeListViewportContext, HomeSessionRow } from '@/session/HomeSurface';
import { useTeammateRoster } from '@/session/useTeammateRoster';
import { createDisclosureListCell, ListDisclosureScope, useListDisclosureTransition } from '@/session/listDisclosureTransition';
import { RenameSessionModal } from '@/session/RenameSessionModal';
import { SessionOptionsPresenter } from '@/session/SessionOptionsExpoSheet';
import { SwipeableSessionRow, type SessionSwipeControls } from '@/session/SwipeableSessionRow';
import type { SessionSwipeAction } from '@/session/swipeRowRegistry';
import { useSessionListActions } from '@/session/useSessionListActions';
import { useMobileMakerTransport } from '@/device-link/useMobileMakerTransport';
import {
RemoteSessionStoreSubscriptionGate,
remoteSessionStore,
useRemoteMessageVersion,
useRemoteSessions,
useRemoteSessionStoreVersion,
} from '@/session/remoteSessionStore';
import {
remoteScheduleEventStore,
useRemoteScheduleEventSnapshot,
useRemoteScheduleMirrorInvalidations,
} from '@/scheduler/remoteScheduleEvents';
import {
getScheduleIndexInvalidationVersion,
invalidateOfflineScheduleIndexFailureFor,
invalidateRunningSessionScheduleEntries,
loadSharedSessionScheduleIndex,
} from '@/session/scheduleIndex';
import { shouldSuppressRemoteListEmptyState } from '@/session/sessionEmptyState';
import { anyMayHaveBoundSchedules, countSessionsWithActiveBoundSchedules } from '@/session/archiveScheduleConfirm';
import type { RemoteSession } from '@/session/types';
import { useTheme, useThemedStyles, type ThemeColors } from '@/theme';
import { fontWeight, lineHeight, radius, spacing, typeScale } from '@/theme/tokens';
const LIST_LIMIT = 200;
// label 走 i18n key(在使用点 t() 求值),不在模块顶层冻结语言。
const STATUS_FILTERS: Array<{ value: RemoteSessionStatusFilter; labelKey: string }> = [
{ value: 'active', labelKey: 'devices.detail.filter.active' },
{ value: 'waiting', labelKey: 'devices.detail.filter.waiting' },
{ value: 'automation', labelKey: 'devices.detail.filter.automation' },
{ value: 'archived', labelKey: 'devices.detail.filter.archived' },
{ value: 'all', labelKey: 'devices.detail.filter.all' },
];
type RemoteListStatusFilter = Extract<RemoteSessionStatusFilter, 'active' | 'archived' | 'all'>;
export default function DeviceDetailScreen() {
const screenFocused = useIsFocused();
const scrollY = useSharedValue(0);
const { height: viewportHeight } = useWindowDimensions();
const viewport = useMemo(() => ({ scrollY, viewportHeight }), [scrollY, viewportHeight]);
return (
<RemoteSessionStoreSubscriptionGate enabled={screenFocused}>
<HomeListViewportContext.Provider value={viewport}>
<DeviceDetailScreenContent />
</HomeListViewportContext.Provider>
</RemoteSessionStoreSubscriptionGate>
);
}
// 设备页列表的 cell 与首页同一套展开 / 收起过渡;组内子行由 DisclosureItem 负责收起。
const DeviceListCell = createDisclosureListCell();
function DeviceDetailScreenContent() {
const viewport = useContext(HomeListViewportContext);
const screenFocused = useIsFocused();
// Task rows mark the teammate looking after them from the roster cache; keep it current here too.
useTeammateRoster(screenFocused);
const screenFocusedRef = useRef(screenFocused);
screenFocusedRef.current = screenFocused;
const [appStateActive, setAppStateActive] = useState(AppState.currentState === 'active');
useEffect(() => {
const subscription = AppState.addEventListener('change', (state) => setAppStateActive(state === 'active'));
return () => subscription.remove();
}, []);
const canLoadScheduleIndex = useCallback(() => screenFocusedRef.current && AppState.currentState === 'active', []);
const styles = useThemedStyles(makeStyles);
const { colors } = useTheme();
const { t, i18n: i18nInstance } = useTranslation();
const params = useLocalSearchParams<{
deviceId: string;
deviceName?: string;
name?: string;
workingDir?: string;
projectName?: string;
automationGroupKey?: string;
automationName?: string;
automationWorkingDir?: string;
automationSessionIds?: string;
statusFilter?: string;
search?: string;
}>();
const deviceId = readRouteString(params.deviceId) ?? '';
const deviceName = deviceDisplayName(deviceId, readRouteString(params.name) ?? readRouteString(params.deviceName));
// 从首页「查看全部 N 条对话」进来时带 workingDir + 项目名 → 仅显示该项目的会话(项目作用域)。
const projectWorkingDir = readRouteString(params.workingDir);
const routeProjectName = readRouteString(params.projectName);
// 从自动化组「查看全部 N 次运行」进来时带组键 + 任务名 → 仅显示该任务的运行(自动化任务作用域)。
const automationScopeKey = readRouteString(params.automationGroupKey);
const automationScopeName = readRouteString(params.automationName);
// 入口组行所在的项目目录(项目分组入口才带):组键不含项目 scope,跨项目任务按它把
// 组键匹配限定在本项目内,页面显示与组行标称的 N 一致。
const automationScopeDir = readRouteString(params.automationWorkingDir);
// 入口带的 sessionId 快照:scheduleIndex 尚未加载完成时先按它立即显示,index 就绪后与组键匹配取并集。
const automationScopeSessionIds = useMemo(
() => parseSessionIdsParam(readRouteString(params.automationSessionIds)),
[params.automationSessionIds],
);
const router = useRouter();
// 前进导航统一走守卫 push,防止列表卡顿时连点把同一页压进栈 N 层(锁语义见 navigationLock.ts)。
const guardedPush = useGuardedPush();
const { width: screenWidth } = useWindowDimensions();
const { connectionIssue, invoke, status, subscribe, unsubscribe } = useDeviceLink();
const maker = useMobileMakerTransport(deviceId);
const scheduleEventSnapshot = useRemoteScheduleEventSnapshot(deviceId);
const scheduleMirrorInvalidations = useRemoteScheduleMirrorInvalidations();
const allSessions = useRemoteSessions();
const projectAliases = useRemoteProjectAliases();
// filter 必须 memo:裸 filter 每次渲染都产新数组,会让下游全部 [sessions, ...] 依赖的
// useMemo 逐 emit 失效,派生链(索引 → sections → 全列表行)整体重建(2026-07-18
// 重渲染风暴)。store 层已保证 allSessions 引用在内容未变时稳定,这里不能亲手打破。
// 列表隐藏 Orca worker 子会话(本期不支持进 worker 聊天);Lead + 普通会话保留。仅 mobile 侧过滤。
// 与首页卡片口径对齐:首页已 exclude worker,「查看全部 N 条」不能再把它们露出来。
// 用展示用 canonicalDeviceId(设备归并结果)匹配 —— 被认领的 stale 会话也能显示,数量与卡片相符。
const sessions = useMemo(
() => selectVisibleDeviceSessions(allSessions, deviceId, projectWorkingDir),
[allSessions, deviceId, projectWorkingDir],
);
const projectName = useMemo(() => resolveMobileProjectRouteTitle({
deviceId,
workingDir: projectWorkingDir,
routeTitle: routeProjectName,
sessions,
projectAliases,
}), [deviceId, projectAliases, projectWorkingDir, routeProjectName, sessions]);
const messageVersion = useRemoteMessageVersion();
const storeVersion = useRemoteSessionStoreVersion();
const [statusFilter, setStatusFilter] = useState<RemoteSessionStatusFilter>(
() => parseRouteStatusFilter(readRouteString(params.statusFilter)),
);
const requestedListFilter = automationScopeKey ? 'all' : remoteListStatusFilter(statusFilter);
const listQueryScope = useMemo(() => ({ deviceId, filter: requestedListFilter }), [deviceId, requestedListFilter]);
const listQueryScopeRef = useRef(listQueryScope);
listQueryScopeRef.current = listQueryScope;
const [syncedListQueryScope, setSyncedListQueryScope] = useState<typeof listQueryScope | null>(null);
const [filtersOpen, setFiltersOpen] = useState(false);
const [searchOpen, setSearchOpen] = useState(() => readRouteString(params.search) === '1');
const [searchFilterOpen, setSearchFilterOpen] = useState(false);
const insets = useSafeAreaInsets();
const headerHeight = useHeaderHeight();
const [loading, setLoading] = useState(false);
const [error, setError] = useState<string | null>(null);
useLayoutEffect(() => { setError(null); }, [listQueryScope]);
// 熔断 open(电脑端未响应):relay 可能仍 online,可见性与 banner 文案单独入参。
const unresponsiveDevices = useUnresponsiveDevices();
const deviceUnresponsive = !!deviceId && unresponsiveDevices.has(deviceId);
const effectiveError = resolveEffectiveConnectionError(error, deviceUnresponsive);
const searchOrigins = useMemo(() => (deviceId ? [{
deviceId,
deviceName,
reachable: !deviceUnresponsive,
}] : []), [deviceId, deviceName, deviceUnresponsive]);
const searchProjects = useMemo(
() => listConversationSearchProjects(sessions, deviceId ? new Set([deviceId]) : undefined, projectAliases),
[deviceId, sessions, projectAliases],
);
const indexedSearch = useConversationSearch({
enabled: !automationScopeKey && !!deviceId,
lockedWorkingDirs: projectWorkingDir ? [projectWorkingDir] : null,
origins: searchOrigins,
projects: searchProjects,
projectAliases,
});
const searchQuery = indexedSearch.query;
const setSearchQuery = indexedSearch.setQuery;
const searchFilterA11y = t('devices.list.search.filterAria', {
agent: t(`devices.list.search.filter.agent.${indexedSearch.agentFilter}`),
lastActivity: t(`devices.list.search.filter.lastActivity.${indexedSearch.lastActivityFilter}`),
projects: projectWorkingDir
? (projectName ?? projectWorkingDir)
: indexedSearch.projectSelection === 'all'
? t('devices.list.search.filter.allProjects')
: t('devices.list.search.filter.selectedProjects', { count: indexedSearch.projectSelection.length }),
sort: t(`devices.list.search.filter.sort.${indexedSearch.sortBy}`),
status: t(`devices.list.search.filter.status.${indexedSearch.statusFilter}`),
});
const searchFilterMenu = useConversationSearchFilterMenu({
activeCount: indexedSearch.activeFilterCount,
agentKind: indexedSearch.agentFilter,
lastActivity: indexedSearch.lastActivityFilter,
lockedProjects: !!projectWorkingDir,
onAgentKindChange: indexedSearch.setAgentFilter,
onLastActivityChange: indexedSearch.setLastActivityFilter,
onProjectsChange: indexedSearch.setProjectSelection,
onReset: indexedSearch.resetFilters,
onSortChange: indexedSearch.setSortBy,
onStatusChange: indexedSearch.setStatusFilter,
projectSelection: indexedSearch.projectSelection,
projects: searchProjects,
sortBy: indexedSearch.sortBy,
status: indexedSearch.statusFilter,
});
// 自动化 / 项目分支视图的条件挂载 banner:普通弱网断线也要有可见信号(防闪延迟后)
const showConnectionBanner = useShowConnectionBanner(status, error, connectionIssue, deviceUnresponsive);
const [lastSyncedAt, setLastSyncedAt] = useState<number | null>(null);
const [selectedSessionIds, setSelectedSessionIds] = useState<string[]>([]);
const [selectionRequested, setSelectionRequested] = useState(false);
const [expandedAutomationGroups, setExpandedAutomationGroups] = useState<string[]>([]);
const [bulkActionPending, setBulkActionPending] = useState<MobileSessionBulkAction | null>(null);
const [bulkConfirmAction, setBulkConfirmAction] = useState<MobileSessionBulkAction | null>(null);
// 批量归档确认里「绑定了自动化的任务数」;按请求序号丢弃过期结果。
const [bulkScheduleCount, setBulkScheduleCount] = useState(0);
// 查询返回(成功或失败)前不允许确认归档,否则提醒还没出现任务就已归档。
const [bulkScheduleLoading, setBulkScheduleLoading] = useState(false);
const bulkScheduleRequestRef = useRef(0);
const [bulkNotice, setBulkNotice] = useState<string | null>(null);
const [scheduleIndex, setScheduleIndex] = useState<Map<string, RemoteSessionScheduleInfo>>(
() => new Map(),
);
// 与首页共用同一套展开 / 收起过渡(listDisclosureTransition);行的归档、置顶移位也走它。
const disclosure = useListDisclosureTransition();
const runDisclosure = disclosure.run;
const animateListChange = useCallback((apply: () => void) => runDisclosure(apply, { nested: true }), [runDisclosure]);
const {
actionSheetSession,
closeRenameSession,
confirmRenameSession,
handleSessionSheetAction,
handleSessionSheetClosed,
renameSessionDraft,
renameSessionTarget,
sessionSwipeControls,
setActionSheetSession,
setRenameSessionDraft,
swipeRegistry,
} = useSessionListActions({ animateListChange });
useEffect(() => {
if (!deviceId || !scheduleMirrorInvalidations.has(deviceId)) return;
setScheduleIndex((current) => invalidateRunningSessionScheduleEntries(
current,
sessions.map((session) => session.id),
));
}, [deviceId, scheduleMirrorInvalidations, sessions]);
const syncSessions = useCallback(async () => {
if (!deviceId) return;
const owner = getMobileAuthOwner();
const isCurrentQuery = () => listQueryScopeRef.current === listQueryScope && isMobileAuthOwnerCurrent(owner);
if (!isCurrentQuery()) return;
setLoading(true);
setError(null);
try {
const mutationEpoch = remoteSessionStore.captureDeviceSessionListMutationEpoch(deviceId);
const list = await withTransientRemoteRetry(async () => {
await subscribe(`device:${deviceId}`, deviceId, ['sessions']);
const sessions = await invoke<RemoteSession[]>(deviceId, 'local-db:sessions:list', [
LIST_LIMIT,
// 自动化任务作用域页承诺展示"该任务的全部 N 次运行",归档的 run 也算,
// 必须拉全量;其余模式仍按当前筛选拉取。
requestedListFilter,
// 首拉 / reseed / 筛选重拉都是权威快照,不并入被控端写前查询。
{ includePinned: true, fresh: true },
]);
return readTeammateTasks({ invoke, deviceId, deviceName, scope: JSON.stringify([owner, deviceId, listQueryScope]),
sessions: Array.isArray(sessions) ? sessions : [], previous: remoteSessionStore.getHomeSessions(),
status: requestedListFilter, limit: LIST_LIMIT, current: isCurrentQuery,
publishInitial: rows => {
if (isCurrentQuery() && remoteSessionStore.isDeviceSessionListMutationEpochCurrent(deviceId, mutationEpoch)) {
remoteSessionStore.setDeviceSessions(deviceId, deviceName, rows);
}
},
});
});
if (!isCurrentQuery()) return;
if (!remoteSessionStore.isDeviceSessionListMutationEpochCurrent(deviceId, mutationEpoch)) {
// The existing sync runner queues one follow-up after this stale read.
remoteSessionStore.requestReseed(deviceId);
return;
}
remoteSessionStore.setDeviceSessions(deviceId, deviceName, Array.isArray(list) ? list : []);
// A successful sessions:list is authoritative reachability evidence even when relay
// presence was not replayed. Retire both offline caches before the schedule reload.
remoteScheduleEventStore.clearDeviceMirrorInvalidation(deviceId);
invalidateOfflineScheduleIndexFailureFor(deviceId);
// 节流缓存与首页共用同一 key(deviceId):两页交替浏览时不重复全量拉取(单飞 + TTL,
// 拥塞背景见 scheduleIndex 注释)。
const invalidationVersion = getScheduleIndexInvalidationVersion(deviceId);
void loadSharedSessionScheduleIndex(deviceId, maker, canLoadScheduleIndex)
.then((nextIndex) => {
if (getScheduleIndexInvalidationVersion(deviceId) !== invalidationVersion) return;
setScheduleIndex(nextIndex);
})
.catch(() => {
if (!canLoadScheduleIndex()) return;
if (getScheduleIndexInvalidationVersion(deviceId) !== invalidationVersion) return;
setScheduleIndex(new Map());
});
setLastSyncedAt(Date.now());
setSyncedListQueryScope(listQueryScope);
} catch (err) {
if (isCurrentQuery()) setError(formatRemoteError(err));
} finally {
if (isCurrentQuery()) setLoading(false);
}
}, [canLoadScheduleIndex, deviceId, deviceName, invoke, listQueryScope, maker, requestedListFilter, subscribe]);
const loadSessions = useRemoteSyncTask(syncSessions);
useEffect(() => {
const unregisterReseed = remoteSessionStore.registerReseedHandler(deviceId, () => void loadSessions());
return () => {
unregisterReseed();
void unsubscribe(`device:${deviceId}`, deviceId, ['sessions']).catch(() => undefined);
};
}, [deviceId, loadSessions, unsubscribe]);
useEffect(() => {
void loadSessions();
}, [loadSessions, listQueryScope]);
useEffect(() => {
if (screenFocused && appStateActive && scheduleEventSnapshot.sessionIndexVersion > 0) void loadSessions();
}, [appStateActive, loadSessions, scheduleEventSnapshot.sessionIndexVersion, screenFocused]);
// Once sessions have loaded, visibility alone must resume a cancelled index
// even before the first schedule event. Keep the initial list-first ordering.
// The event store invalidates once; all visible consumers share its next scan.
useEffect(() => {
if (!screenFocused || !appStateActive) return;
if (
lastSyncedAt === null
&& scheduleEventSnapshot.scheduleListVersion === 0
&& scheduleEventSnapshot.unreadClearVersion === 0
) return;
const invalidationVersion = getScheduleIndexInvalidationVersion(deviceId);
void loadSharedSessionScheduleIndex(deviceId, maker, canLoadScheduleIndex)
.then((nextIndex) => {
if (getScheduleIndexInvalidationVersion(deviceId) !== invalidationVersion) return;
setScheduleIndex(nextIndex);
})
.catch(() => {
// 失败保留旧徽标,与整页 load 的容错口径一致。
});
}, [
appStateActive,
canLoadScheduleIndex,
deviceId,
maker,
lastSyncedAt,
scheduleEventSnapshot.scheduleListVersion,
scheduleEventSnapshot.unreadClearVersion,
screenFocused,
]);
// 派生索引依赖全局 messageVersion / storeVersion,逐 emit 重建出内容相同的新 Map;
// useStableValue 在内容未变时保留旧引用,阻断 sections 派生链的无谓全量重建
// (与首页同款处理,风暴背景见 devices/index.tsx 对应注释)。
const messagePreviewIndexRaw = useMemo(
() => remoteSessionStore.getSessionListMessagePreviewIndex(sessions),
[messageVersion, sessions],
);
const messagePreviewIndex = useStableValue(messagePreviewIndexRaw, mapContentEqual);
const pendingInteractionIndexRaw = useMemo(() => new Map(
sessions
.map((session) => [session.id, remoteSessionStore.getPendingInteractions(session.id).length] as const)
.filter(([, count]) => count > 0),
), [sessions, storeVersion]);
const pendingInteractionIndex = useStableValue(pendingInteractionIndexRaw, mapContentEqual);
const filterCounts = useMemo(
() => summarizeRemoteSessionOverview(sessions, pendingInteractionIndex, scheduleIndex),
[pendingInteractionIndex, scheduleIndex, sessions],
);
const sections = useMemo(
() => buildRemoteSessionSections(sessions, Date.now(), {
projectAliases,
projectAliasDeviceId: deviceId,
messagePreviewIndex,
pendingInteractionIndex,
scheduleIndex,
searchQuery,
// 自动化任务作用域页无筛选 UI 且承诺"全部 N 次运行",statusFilter 固定 'all'
// (本页 statusFilter state 停留在初值 'active',若沿用会把归档 run 滤掉)。
statusFilter: automationScopeKey ? 'all' : statusFilter,
// 未起名会话的显示文案:共享层不兜中文串,由这里给已解析的 i18n 值。
unnamedLabel: t('session.menu.unnamedTitle'),
// 项目作用域精简页与完整设备详情页都折叠自动化组(HomeSessionRow 支持组行
// 展开,与首页交互一致);自动化任务作用域页本身就是"某任务的全部运行",必须平铺不折叠。
groupAutomations: !automationScopeKey,
}),
[
automationScopeKey,
deviceId,
projectAliases,
i18nInstance.language,
messagePreviewIndex,
pendingInteractionIndex,
scheduleIndex,
searchQuery,
sessions,
statusFilter,
t,
],
);
const displaySections = useMemo(() => {
if (automationScopeKey || !shouldReplaceListWithSearchResults(searchQuery, indexedSearch.status)) {
return sections;
}
return [{ key: 'search', title: '', data: indexedSearch.results }];
}, [automationScopeKey, indexedSearch.results, indexedSearch.status, searchQuery, sections]);
const listContext = useMemo(
() => buildRemoteSessionListContext({
overview: filterCounts,
searchQuery,
sections: displaySections,
statusFilter,
}),
[displaySections, filterCounts, searchQuery, statusFilter],
);
const visibleSessionIds = useMemo(() => visibleSessionIdsFromSections(displaySections), [displaySections]);
const selectedSessionIdSet = useMemo(() => new Set(selectedSessionIds), [selectedSessionIds]);
const selectedSessions = useMemo(() => {
const selected = new Set(selectedSessionIds);
return sessions.filter((session) => selected.has(session.id));
}, [selectedSessionIds, sessions]);
const bulkActionSummaries = useMemo(() => ({
archive: summarizeMobileSessionBulkAction(selectedSessions, 'archive'),
delete: summarizeMobileSessionBulkAction(selectedSessions, 'delete'),
pin: summarizeMobileSessionBulkAction(selectedSessions, 'pin'),
restore: summarizeMobileSessionBulkAction(selectedSessions, 'restore'),
unpin: summarizeMobileSessionBulkAction(selectedSessions, 'unpin'),
}), [i18nInstance.language, selectedSessions]);
const bulkActionLayout = useMemo(
() => visibleMobileSessionBulkActions(bulkActionSummaries),
[bulkActionSummaries],
);
const bulkConfirmSummary = bulkConfirmAction ? bulkActionSummaries[bulkConfirmAction] : null;
const selectionMode = selectionRequested || selectedSessionIds.length > 0;
const controlsSummary = useMemo(
() => remoteSessionControlsSummary(statusFilter, filterCounts),
[filterCounts, statusFilter, t],
);
const windowLayout = buildMainWindowLayout({
actionCount: 3,
kind: 'detail',
metricCount: 4,
screenWidth,
});
const emptyState = useMemo(
() => deviceSessionEmptyState(statusFilter, searchQuery),
[searchQuery, statusFilter, t],
);
// 当前设备和筛选同步完成前抑制空状态,旧筛选的成功不能证明新筛选为空。
// 同步失败时 ConnectionBanner 已有错误提示,不再同时显示 loading 或空结果。
// 抑制期渲染 RemoteListSyncingPlaceholder(800ms 内空白,超时浮现「正在同步」),
// 慢链路 / 连接翻覆下首同步拖长时不再是无限期纯白。
const suppressListEmptyState = shouldSuppressRemoteListEmptyState({
// Used only by ListEmptyComponent. An automation group may be empty even
// when the host's unfiltered sections contain other tasks.
itemCount: 0,
hasSyncedThisOpen: syncedListQueryScope === listQueryScope,
loading: loading || indexedSearch.status === 'searching',
failed: !!effectiveError,
});
useEffect(() => {
setSelectedSessionIds((prev) => {
const next = pruneSessionSelection(prev, visibleSessionIds);
return next.length === prev.length && next.every((id, index) => id === prev[index]) ? prev : next;
});
}, [visibleSessionIds]);
const clearSelection = useCallback(() => {
setSelectionRequested(false);
setSelectedSessionIds([]);
setBulkConfirmAction(null);
setBulkNotice(null);
}, []);
const openSession = useCallback((targetSessionId: string, focusClientId?: string) => {
if (swipeRegistry.closeOpenRow()) return;
// 打开会话是远端交互,用当前(可达)设备 endpoint = 页面级 deviceId(对被认领会话即 canonical 当前设备,
// 其物理机就是当前设备、可达)。不用会话物理旧 shard id:re-link 后旧设备不可达会导致会话根本打不开。
// 本地 store 乐观更新(bulk applySessionPatch)另按会话物理 shard 路由;纯 stale 会话(current shard 无
// 副本)的本地乐观回显不完美是已知限制,与 Home 页固有一致(见 PR 描述「已知限制」)。
guardedPush({
pathname: '/sessions/[sessionId]',
params: {
sessionId: targetSessionId,
deviceId,
deviceName,
...(focusClientId ? { focusClientId } : {}),
},
});
}, [deviceId, deviceName, guardedPush, swipeRegistry]);
const toggleAutomationGroup = useCallback((key: string) => {
runDisclosure(() => setExpandedAutomationGroups((prev) =>
prev.includes(key) ? prev.filter((item) => item !== key) : [...prev, key],
));
}, [runDisclosure]);
// 自动化组「查看全部 N 次运行」:与项目「查看全部」一致,进入该任务的专属列表页
// (本路由的自动化任务作用域模式)。
const openAutomationGroup = useCallback((group: RemoteAutomationSessionGroup) => {
// 组内同项目时带上项目维度(baseKey 不含项目 scope,跨项目任务只显示
// 本项目的 run,与组行标称的 N 一致)。
const primaryWorkingDir = (group.items.find((item) => item.session.id === group.primarySessionId) ?? group.items[0])
?.session.workingDir?.trim();
guardedPush({
pathname: '/devices/[deviceId]',
params: {
deviceId,
name: deviceName,
automationGroupKey: group.baseKey,
automationName: group.title,
...(primaryWorkingDir ? { automationWorkingDir: primaryWorkingDir } : {}),
automationSessionIds: JSON.stringify(group.sessionIds),
},
});
}, [deviceId, deviceName, guardedPush]);
const toggleSelection = useCallback((sessionIds: readonly string[]) => {
setBulkConfirmAction(null);
setBulkNotice(null);
setSelectedSessionIds((prev) => {
const next = new Set(prev);
const allSelected = sessionIds.every((id) => next.has(id));
for (const id of sessionIds) {
if (allSelected) next.delete(id);
else next.add(id);
}
return [...next];
});
}, []);
const beginSelection = useCallback((sessionIds: readonly string[]) => {
setBulkConfirmAction(null);
setBulkNotice(null);
setSelectedSessionIds((prev) => Array.from(new Set([...prev, ...sessionIds])));
}, []);
const executeBulkAction = useCallback(async (
action: MobileSessionBulkAction,
targets: readonly RemoteSession[],
) => {
if (bulkActionPending !== null || targets.length === 0) return;
setBulkActionPending(action);
setBulkNotice(null);
const patch = mobileSessionBulkPatch(action);
// 乐观批量:先把全部目标当帧 applySessionPatch(行立即消失 / 重排 / 变化)、
// 退出确认态并清空选择,RPC 转后台并发;失败的条目用点击时的会话快照原样
// 还原(归档 / 删除行已移出列表,反向 patch 复活不了)并提示。页面内并发由
// bulkActionPending 互斥,不需要写序守卫。
// 本地乐观更新按会话物理 shard 路由(被认领的 stale 会话物理仍在旧 shard),
// 与 Home 一致;否则 applySessionPatch 在错误 shard 找不到该会话、乐观更新
// 丢失。远端 patch 走当前设备 transport(认领会话的物理机就是当前设备)。
const rows = targets.map((session) => ({
session,
rowDeviceId: session.deviceLinkDeviceId ?? remoteSessionStore.getSessionDeviceId(session.id) ?? deviceId,
}));
for (const { session, rowDeviceId } of rows) {
remoteSessionStore.applySessionPatch(rowDeviceId, session.id, patch);
}
setBulkConfirmAction(null);
setSelectedSessionIds([]);
setSelectionRequested(false);
try {
const failed: typeof rows = [];
await Promise.all(rows.map(async (row) => {
try {
const updated = await withNewSessionCreationRemoval(row.session.id, deviceId, patch.status,
() => maker.patchSessionMeta(row.session.id, patch));
if (updated) remoteSessionStore.applySessionPatch(row.rowDeviceId, row.session.id, updated);
} catch (err) {
if (isExplicitRemoteNotFoundError(err) && patch.status !== undefined) {
remoteSessionStore.applySessionPatch(row.rowDeviceId, row.session.id, { status: 'deleted' });
remoteSessionStore.requestReseed(row.rowDeviceId);
return;
}
failed.push(row);
}
}));
if (failed.length > 0) {
for (const { session, rowDeviceId } of failed) {
const shardName = remoteSessionStore.getSessions()
.find((s) => s.deviceLinkDeviceId === rowDeviceId)?.deviceLinkDeviceName
?? session.deviceLinkDeviceName
?? rowDeviceId;
remoteSessionStore.upsertDeviceSession(rowDeviceId, shardName, session);
}
setSelectedSessionIds(failed.map(({ session }) => session.id));
setBulkNotice(t('devices.detail.bulk.partialFailure', { done: rows.length - failed.length, failed: failed.length }));
}
await loadSessions();
} finally {
setBulkActionPending(null);
}
}, [bulkActionPending, deviceId, loadSessions, maker, t]);
// 确认卡打开后所选任务变了:提醒是按打开时的候选算的,关掉卡片让用户重新发起,
// 避免确认时归档了从未被查询过的任务。
useEffect(() => {
bulkScheduleRequestRef.current += 1;
setBulkScheduleLoading(false);
setBulkConfirmAction(null);
}, [selectedSessionIds]);
const requestBulkAction = useCallback((action: MobileSessionBulkAction) => {
const summary = bulkActionSummaries[action];
if (summary.candidates.length === 0) {
setBulkNotice(
action === 'archive'
? t('devices.detail.bulk.noArchivable')
: action === 'pin'
? t('devices.detail.bulk.noPinnable')
: action === 'restore'
? t('devices.detail.bulk.noRestorable')
: action === 'unpin'
? t('devices.detail.bulk.noUnpinnable')
: t('devices.detail.bulk.noDeletable'),
);
return;
}
const scheduleRequest = ++bulkScheduleRequestRef.current;
setBulkScheduleCount(0);
// 全部已知无绑定时不查询、不禁用确认(与单条路径同一个快路径)。
const needsLookup = action === 'archive' && anyMayHaveBoundSchedules(deviceId, summary.candidates);
setBulkScheduleLoading(needsLookup);
if (needsLookup) {
void countSessionsWithActiveBoundSchedules(deviceId, invoke, summary.candidates)
.then((count) => {
if (bulkScheduleRequestRef.current !== scheduleRequest) return;
setBulkScheduleCount(count);
setBulkScheduleLoading(false);
});
}
setBulkConfirmAction(action);
}, [bulkActionSummaries, deviceId, invoke, t]);
const actionOverlays = (
<>
<ConversationSearchFilterSheet
activeCount={indexedSearch.activeFilterCount}
agentKind={indexedSearch.agentFilter}
lastActivity={indexedSearch.lastActivityFilter}
lockedProjects={!!projectWorkingDir}
onAgentKindChange={indexedSearch.setAgentFilter}
onClose={() => setSearchFilterOpen(false)}
onLastActivityChange={indexedSearch.setLastActivityFilter}
onProjectsChange={indexedSearch.setProjectSelection}
onReset={indexedSearch.resetFilters}
onSortChange={indexedSearch.setSortBy}
onStatusChange={indexedSearch.setStatusFilter}
projectSelection={indexedSearch.projectSelection}
projects={searchProjects}
sortBy={indexedSearch.sortBy}
status={indexedSearch.statusFilter}
topOffset={insets.top + 56}
visible={searchFilterOpen}
/>
<SessionListActionOverlays
actionSheetSession={actionSheetSession}
closeRenameSession={closeRenameSession}
confirmRenameSession={confirmRenameSession}
handleSessionSheetAction={handleSessionSheetAction}
handleSessionSheetClosed={handleSessionSheetClosed}
renameSessionDraft={renameSessionDraft}
renameSessionTarget={renameSessionTarget}
setActionSheetSession={setActionSheetSession}
setRenameSessionDraft={setRenameSessionDraft}
/>
</>
);
// 自动化任务作用域(从组行「查看全部 N 次运行」进入):干净布局 —— 头部 + 该任务全部运行的
// 平铺列表,交互与项目作用域页同构。运行归属 = 入口 sessionId 快照 ∪ 组键匹配(scheduleIndex
// 就绪后能捕获快照之外新产生的 run)。
if (automationScopeKey) {
const scopeIdSet = new Set(automationScopeSessionIds);
const allItems = sections.flatMap((section) => section.data);
// 组键升级:入口可能带 fallback 键(源页 scheduleIndex 未就绪时进入),本页 index 就绪后
// 同批 run 的组键会变成 schedule:<id> —— 只按入口键匹配会把快照之外的 run(归档 / 新产生)
// 漏掉。把快照内 run 的实际组键并入匹配集,让 fallback 键随 index 就绪自动升级到 schedule 键。
const scopeKeys = new Set<string>([automationScopeKey]);
for (const item of allItems) {
if (!scopeIdSet.has(item.session.id)) continue;
const key = automationGroupKey(item);
if (key) scopeKeys.add(key);
}
const runItems = allItems.filter((item) => {
if (scopeIdSet.has(item.session.id)) return true;
const key = automationGroupKey(item);
if (!key || !scopeKeys.has(key)) return false;
// 入口带项目维度时,组键匹配的行还要落在同一项目(与首页项目分桶同一归一化语义);
// 快照内的 id 是入口组的精确成员,不受此过滤影响。
return !automationScopeDir || sessionMatchesProjectDir(item.session.workingDir, automationScopeDir);
});
return (
<SafeAreaView edges={simpleScrollScreenSafeAreaEdges()} style={styles.safeArea} testID="deviceDetail.screen">
<SimpleStackHeader
syncing={!showConnectionBanner && (loading || status === 'connecting')}
backTestID="deviceDetail.backButton"
onBack={() => goBackGuarded(router)}
title={automationScopeName ?? t('devices.detail.automationScope.title')}
titleTestID="deviceDetail.title"
/>
{showConnectionBanner ? (
<ConnectionBanner
deviceUnresponsive={deviceUnresponsive}
error={error}
issue={connectionIssue}
lastSyncedAt={lastSyncedAt}
loading={loading}
onSync={() => void loadSessions()}
status={status}
/>
) : null}
<SectionList
{...simpleScrollInsetProps}
sections={runItems.length > 0 ? [{ key: 'automation-runs', title: '', data: runItems }] : []}
keyExtractor={(item) => item.session.id}
refreshControl={<RefreshControl refreshing={loading} onRefresh={loadSessions} tintColor={colors.textSecondary} />}
stickySectionHeadersEnabled={false}
renderSectionHeader={() => null}
contentContainerStyle={[styles.listContent, { paddingBottom: spacing.xxl }]}
testID="deviceDetail.automationRunList"
renderItem={({ item }) => (
<DeviceDetailSessionRow
item={item}
onOpenSession={(it) => openSession(
it.session.id,
'searchFocusClientId' in it ? (it as { searchFocusClientId?: string }).searchFocusClientId : undefined,
)}
swipe={sessionSwipeControls}
testID={`deviceDetail.automationRunRow.${item.session.id}`}
/>
)}
ListEmptyComponent={suppressListEmptyState ? (
effectiveError ? null : <RemoteListSyncingPlaceholder testID="deviceDetail.automationSyncing" />
) : (
<MainWindowEmptyState
centered
copy={t('devices.detail.automationScope.emptyCopy')}
style={{
marginTop: spacing.xxl,
minHeight: windowLayout.emptyMinHeight,
padding: windowLayout.emptyPadding,
}}
testID="deviceDetail.automationEmpty"
title={t('devices.detail.automationScope.emptyTitle')}
/>
)}
/>
{actionOverlays}
</SafeAreaView>
);
}
// 项目作用域(从首页「查看全部 N 条对话」进入):干净布局 —— 头部 + 直接的会话列表(复用首页
// 干净会话行),不带整台电脑的控制台(metric pill / 分组 / 筛选 / 批量 / listContext)。
// 整台电脑模式(无 workingDir)继续走下面完整的 console 布局。
if (projectWorkingDir) {
const projectItems = sections.flatMap((section) => section.data);
const connectionBanner = showConnectionBanner ? (
<ConnectionBanner
deviceUnresponsive={deviceUnresponsive}
error={error}
issue={connectionIssue}
lastSyncedAt={lastSyncedAt}
loading={loading}
onSync={() => void loadSessions()}
status={status}
/>
) : null;
// iOS 顶栏透明、内容铺到顶栏下:搜索入口放进列表头,由列表让出顶栏高度;不透明顶栏在
// iOS 26+ 会被 react-native-screens 再垫一层顶部安全区,标题栏下方出现大块空白。
return (
<SafeAreaView edges={simpleScrollScreenSafeAreaEdges()} style={styles.safeArea} testID="deviceDetail.screen">
<SimpleStackHeader
scrollEdge
syncing={!showConnectionBanner && (loading || status === 'connecting')}
action={{
label: t('devices.common.create'),
// 在这个项目里建新对话:预填 workingDir。
onPress: () => guardedPush({
pathname: '/sessions/new',
params: {
deviceId,
deviceName,
workingDir: projectWorkingDir,
deviceOptions: serializeNewSessionDeviceOptions([{ deviceId, name: deviceName }]),
},
}),
testID: 'deviceDetail.newSessionButton',
}}
backTestID="deviceDetail.backButton"
onBack={() => goBackGuarded(router)}
title={projectName ?? deviceName}
titleTestID="deviceDetail.title"
/>
{/* 连接提示只是零高度锚点;iOS 内容区从屏幕顶开始,锚点挪到顶栏下沿。 */}
{usesNativeStackHeader()
? <View pointerEvents="box-none" style={[styles.projectConnectionAnchor, { top: headerHeight }]}>{connectionBanner}</View>
: connectionBanner}
<ListDisclosureScope controller={disclosure.controller}>
<SectionList
CellRendererComponent={DeviceListCell}
onScroll={(event) => { if (viewport) viewport.scrollY.value = event.nativeEvent.contentOffset.y; }}
scrollEventThrottle={16}
{...simpleScrollInsetProps}
sections={displaySections}
keyExtractor={(item) => item.automationGroup?.key ?? item.session.id}
// Fabric can reattach a clipped Swipeable child before its old native parent removes it.
// Keep JS virtualization, but avoid the Android native detach/reattach race for this list.
removeClippedSubviews={false}
refreshControl={<RefreshControl refreshing={loading} onRefresh={loadSessions} tintColor={colors.textSecondary} />}
stickySectionHeadersEnabled={false}
renderSectionHeader={() => null}
contentContainerStyle={[styles.listContent, { paddingBottom: spacing.xxl }]}
testID="deviceDetail.projectSessionList"
ListHeaderComponent={<View style={styles.projectSearchChrome}>
{searchOpen || !!searchQuery.trim() ? (
<HomeSearchBar
autoFocus={searchOpen && !searchQuery}
filterA11y={searchFilterA11y}
filterActions={searchFilterMenu.filterActions}
filterActive={indexedSearch.activeFilterCount > 0}
onChangeQuery={setSearchQuery}
onDismiss={() => setSearchOpen(false)}
onFilterAction={searchFilterMenu.onFilterAction}
onOpenFilter={() => setSearchFilterOpen(true)}
padded={false}
query={searchQuery}
testIDs={{
clear: 'deviceDetail.projectSearchCloseButton',
filter: 'deviceDetail.projectSearchFilterButton',
input: 'deviceDetail.projectSearchInput',
row: 'deviceDetail.projectSearchRow',
}}
/>
) : (
<MainWindowActionGroup
density="compact"
secondaryActions={[
{
accessibilityLabel: t('devices.detail.search.openA11y'),
active: false,
label: t('devices.detail.search.label'),
onPress: () => setSearchOpen(true),
testID: 'deviceDetail.projectSearchToggleButton',
},
]}
testID="deviceDetail.projectSearchActions"
/>
)}
</View>}
renderItem={({ item, index, section }) => (
<DeviceDetailSessionRow
asBlock
expandedAutomationGroups={expandedAutomationGroups}
// 分割线唯一化(与首页同规则):紧邻自动化块上边界的行不画自己的缩进线,
// 相邻两个块之间只保留一根全宽线(后块不画顶线)。
hideDivider={!!section.data[index + 1]?.automationGroup}
item={item}
onOpenAutomationGroup={openAutomationGroup}
onOpenSession={(it) => openSession(
it.session.id,
'searchFocusClientId' in it ? (it as { searchFocusClientId?: string }).searchFocusClientId : undefined,
)}
onToggleAutomationGroup={toggleAutomationGroup}
suppressBlockTopBorder={!!section.data[index - 1]?.automationGroup}
swipe={sessionSwipeControls}
testID={`deviceDetail.projectSessionRow.${item.session.id}`}
/>
)}
ListEmptyComponent={suppressListEmptyState ? (
effectiveError ? null : <RemoteListSyncingPlaceholder testID="deviceDetail.projectSyncing" />
) : (
<MainWindowEmptyState
centered
copy={searchQuery.trim() ? deviceSessionEmptyState(statusFilter, searchQuery).copy : t('devices.detail.projectScope.emptyCopy')}
style={{
marginTop: spacing.xxl,
minHeight: windowLayout.emptyMinHeight,
padding: windowLayout.emptyPadding,
}}
testID="deviceDetail.projectEmpty"
title={searchQuery.trim() ? deviceSessionEmptyState(statusFilter, searchQuery).title : t('devices.detail.projectScope.emptyTitle')}
/>
)}
/>
</ListDisclosureScope>
{actionOverlays}
</SafeAreaView>
);
}
return (
<SafeAreaView edges={simpleScrollScreenSafeAreaEdges()} style={styles.safeArea} testID="deviceDetail.screen">
<SimpleStackHeader
syncing={!showConnectionBanner && (loading || status === 'connecting')}
action={{
label: t('devices.common.create'),
onPress: () => guardedPush({
pathname: '/sessions/new',
params: {
deviceId,
deviceName,
deviceOptions: serializeNewSessionDeviceOptions([{ deviceId, name: deviceName }]),
},
}),
testID: 'deviceDetail.newSessionButton',
}}
backTestID="deviceDetail.backButton"
onBack={() => goBackGuarded(router)}
title={projectName ?? deviceName}
titleTestID="deviceDetail.title"
/>
<ConnectionBanner
deviceUnresponsive={deviceUnresponsive}
error={error}
issue={connectionIssue}
lastSyncedAt={lastSyncedAt}
loading={loading}
onSync={() => void loadSessions()}
status={status}
/>
<SummaryStrip
style={{
gap: windowLayout.summaryGap,
paddingHorizontal: windowLayout.contentPaddingHorizontal,
paddingVertical: windowLayout.contentPaddingVertical,
}}
testID="deviceDetail.summary"
>
<MainWindowActionButton action={{
label: t('remoteDesktop.title'),
onPress: () => guardedPush({ pathname: '/devices/desktop/[deviceId]', params: { deviceId, deviceName } }),
testID: 'deviceDetail.remoteDesktop',
}} />
<View style={[styles.summaryTopRow, { gap: windowLayout.metricGap }]}>
<MainWindowMetric
accessibilityLabel={t('devices.detail.metric.activeA11y')}
label={t('devices.detail.metric.active')}
onPress={() => setStatusFilter('active')}
selected={statusFilter === 'active'}
style={{ minHeight: windowLayout.metricMinHeight, minWidth: windowLayout.metricMinWidth }}
testID="deviceDetail.metric.活动"
variant="pill"
value={filterCounts.active}
/>
<MainWindowMetric
accessibilityLabel={t('devices.detail.metric.waitingA11y')}
label={t('devices.detail.metric.waiting')}
onPress={() => setStatusFilter('waiting')}
selected={statusFilter === 'waiting'}
style={{ minHeight: windowLayout.metricMinHeight, minWidth: windowLayout.metricMinWidth }}
testID="deviceDetail.metric.待处理"
variant="pill"
urgent={filterCounts.waiting > 0}
value={filterCounts.waiting}
/>
<MainWindowMetric
accessibilityLabel={t('devices.detail.metric.automationA11y')}
label={t('devices.detail.metric.automation')}
onPress={() => setStatusFilter('automation')}
selected={statusFilter === 'automation'}
style={{ minHeight: windowLayout.metricMinHeight, minWidth: windowLayout.metricMinWidth }}
testID="deviceDetail.metric.自动化"
variant="pill"
value={filterCounts.automation}
/>
{loading ? <ActivityIndicator color={colors.textSecondary} /> : null}
</View>
</SummaryStrip>
<View
style={[
styles.controls,
{
gap: windowLayout.toolbarGap,
paddingHorizontal: windowLayout.toolbarPaddingHorizontal,
paddingVertical: windowLayout.toolbarPaddingVertical,
},
]}
testID="deviceDetail.sessionControls"
>
<View style={[styles.compactControlsRow, { gap: windowLayout.toolbarGap, minHeight: windowLayout.toolbarMinHeight }]}>
<Text style={styles.controlsSummary} numberOfLines={1} testID="deviceDetail.controlsSummary">
{controlsSummary}
</Text>
<MainWindowActionGroup
density="compact"
secondaryActions={[
{
accessibilityLabel: searchOpen ? t('devices.detail.search.closeA11y') : t('devices.detail.search.openA11y'),
active: searchOpen || !!searchQuery.trim(),
label: t('devices.detail.search.label'),
onPress: () => {
if (searchOpen && !searchQuery.trim()) {
setSearchOpen(false);
return;
}
setSearchOpen(true);
},
testID: 'deviceDetail.searchToggleButton',
},
{
accessibilityLabel: filtersOpen ? t('devices.detail.filters.collapseA11y') : t('devices.detail.filters.expandA11y'),
active: filtersOpen,
label: t('devices.detail.filters.label'),
onPress: () => setFiltersOpen((value) => !value),
testID: 'deviceDetail.filtersToggleButton',
},
{
label: t('session.new.select'),
accessibilityLabel: t('session.new.select'),
active: selectionMode,
onPress: () => {
swipeRegistry.closeOpenRow();
if (selectionMode) clearSelection();
else setSelectionRequested(true);
},
testID: 'deviceDetail.selectionToggleButton',
},
]}
testID="deviceDetail.toolbarActions"
/>
</View>
{searchOpen || !!searchQuery.trim() ? (
<HomeSearchBar
autoFocus={searchOpen && !searchQuery}
filterA11y={searchFilterA11y}
filterActions={searchFilterMenu.filterActions}
filterActive={indexedSearch.activeFilterCount > 0}
onChangeQuery={setSearchQuery}
onFilterAction={searchFilterMenu.onFilterAction}
onOpenFilter={() => setSearchFilterOpen(true)}
padded={false}
query={searchQuery}
testIDs={{
clear: 'deviceDetail.searchCloseButton',
filter: 'deviceDetail.searchFilterButton',
input: 'deviceDetail.searchInput',
row: 'deviceDetail.searchRow',
}}
/>
) : null}
{filtersOpen ? (
<>
<ScrollView
horizontal
showsHorizontalScrollIndicator={false}
style={styles.segmentScroll}
contentContainerStyle={styles.segmentScrollContent}
>
{STATUS_FILTERS.map((item) => {
const label = t(item.labelKey);
return (
<MainWindowOptionButton
accessibilityLabel={t('devices.detail.filter.a11y', { label })}
key={item.value}
label={remoteSessionFilterLabel(item.value, filterCounts, label)}
onPress={() => setStatusFilter(item.value)}
selected={statusFilter === item.value}
testID={`deviceDetail.statusFilter.${item.value}`}
/>
);
})}
</ScrollView>
</>
) : null}
{selectionMode ? (
<View style={styles.selectionBar} testID="deviceDetail.selectionBar">
<View style={styles.selectionHeaderRow}>
<View style={styles.selectionTitleBlock}>
<Text style={styles.selectionText} testID="deviceDetail.selectionCount">
{t('devices.detail.selection.count', { count: selectedSessionIds.length })}
</Text>
<Text style={styles.selectionMeta}>
{bulkActionLayout.primary.length + bulkActionLayout.destructive.length > 0
? t('devices.detail.selection.canAct')
: t('devices.detail.selection.cannotAct')}
</Text>
</View>
<MainWindowActionGroup
density="compact"
secondaryActions={[
{
accessibilityLabel: t('devices.detail.selection.clearA11y'),
disabled: bulkActionPending !== null,
label: t('devices.common.cancel'),
onPress: clearSelection,
testID: 'deviceDetail.clearSelectionButton',
},
]}
testID="deviceDetail.selectionHeaderActions"
/>
</View>
{bulkActionLayout.primary.length > 0 ? (
<MainWindowActionGroup
secondaryActions={bulkActionLayout.primary.map((action) => ({
accessibilityLabel: bulkActionAccessibilityLabel(action, t),
disabled: bulkActionPending !== null,
label: bulkActionPending === action
? bulkActionPendingLabel(action, t)
: mobileSessionBulkActionButtonLabel(bulkActionSummaries[action]),
onPress: () => requestBulkAction(action),
testID: bulkActionTestID(action),
}))}
testID="deviceDetail.bulkPrimaryActions"
/>
) : null}
{bulkActionLayout.destructive.length > 0 ? (
<MainWindowActionGroup
dangerActions={bulkActionLayout.destructive.map((action) => ({
accessibilityLabel: bulkActionAccessibilityLabel(action, t),
disabled: bulkActionPending !== null,
label: bulkActionPending === action
? bulkActionPendingLabel(action, t)
: mobileSessionBulkActionButtonLabel(bulkActionSummaries[action]),
onPress: () => requestBulkAction(action),
testID: bulkActionTestID(action),
tone: 'danger',
}))}
testID="deviceDetail.bulkDangerActions"
/>
) : null}
{bulkConfirmSummary ? (
<View style={styles.bulkConfirmCard} testID="deviceDetail.bulkConfirmCard">
<Text style={styles.bulkConfirmTitle}>{bulkConfirmSummary.title}</Text>
<Text style={styles.bulkConfirmText}>{bulkConfirmSummary.description}</Text>
{bulkConfirmSummary.action === 'archive' && bulkScheduleCount > 0 ? (
<Text style={styles.bulkConfirmText}>
{t('session.menu.archiveScheduleBulkNotice', { count: bulkScheduleCount })}
</Text>
) : null}
<MainWindowActionGroup
primaryActions={[
{
accessibilityLabel: bulkActionAccessibilityLabel(bulkConfirmSummary.action, t),
disabled: bulkActionPending !== null || bulkConfirmSummary.candidates.length === 0
|| (bulkConfirmSummary.action === 'archive' && bulkScheduleLoading),
label: bulkActionPending === bulkConfirmSummary.action
? bulkActionPendingLabel(bulkConfirmSummary.action, t)
: bulkConfirmSummary.confirmText,
onPress: () => void executeBulkAction(bulkConfirmSummary.action, bulkConfirmSummary.candidates),
testID: 'deviceDetail.bulkConfirmButton',
tone: 'primary',
},
]}
cancelAction={{
accessibilityLabel: t('devices.detail.bulk.cancelA11y'),
disabled: bulkActionPending !== null,
label: t('devices.common.cancel'),
onPress: () => setBulkConfirmAction(null),
testID: 'deviceDetail.bulkConfirmCancelButton',
}}
testID="deviceDetail.bulkConfirmActions"
/>
</View>
) : null}
{bulkActionLayout.primary.length === 0 && bulkActionLayout.destructive.length === 0 ? (
<Text style={styles.bulkNotice}>{t('devices.detail.bulk.noneModifiable')}</Text>
) : null}
</View>
) : null}
{bulkNotice ? (
<Text style={styles.bulkNotice} testID="deviceDetail.bulkNotice">{bulkNotice}</Text>
) : null}
<View style={styles.listContextCard} testID="deviceDetail.listContext">
<View style={styles.listContextHeader}>
<Text style={styles.listContextTitle} numberOfLines={1}>
{listContext.title}
</Text>
<Text style={styles.listContextCount} testID="deviceDetail.listContextCount">
{listContext.resultCount}
</Text>
</View>
<Text style={styles.listContextDetail} numberOfLines={1}>
{listContext.detail}
</Text>
<Text style={styles.listContextHint}>
{listContext.hint}
</Text>
</View>
</View>
<ListDisclosureScope controller={disclosure.controller}>
<SectionList
CellRendererComponent={DeviceListCell}
onScroll={(event) => { if (viewport) viewport.scrollY.value = event.nativeEvent.contentOffset.y; }}
scrollEventThrottle={16}
{...simpleScrollInsetProps}
sections={displaySections}
keyExtractor={(item) => item.automationGroup?.key ?? item.session.id}
refreshControl={<RefreshControl refreshing={loading} onRefresh={loadSessions} tintColor={colors.textSecondary} />}
stickySectionHeadersEnabled={false}
contentContainerStyle={[
styles.listContent,
{
paddingHorizontal: windowLayout.listPaddingHorizontal,
paddingVertical: windowLayout.listPaddingVertical,
},
]}
testID="deviceDetail.sessionList"
renderSectionHeader={({ section }) => (
section.title ? <Text style={styles.sectionTitle}>{section.title}</Text> : null
)}
ListEmptyComponent={suppressListEmptyState ? (
effectiveError ? null : <RemoteListSyncingPlaceholder testID="deviceDetail.syncing" />
) : (
<MainWindowEmptyState
centered
copy={emptyState.copy}
style={{
marginTop: spacing.xxl,
minHeight: windowLayout.emptyMinHeight,
padding: windowLayout.emptyPadding,
}}
testID="deviceDetail.empty"
title={emptyState.title}
/>
)}
renderItem={({ item }) => (
<DeviceDetailSessionRow
expandedAutomationGroups={expandedAutomationGroups}
item={item}
onLongPress={conversationSearchAllowsLocalWrites(item)
? () => beginSelection(sessionIdsForListItem(item))
: undefined}
onOpenAutomationGroup={item.automationGroup ? openAutomationGroup : undefined}
onOpenSession={(it) => openSession(
it.session.id,
'searchFocusClientId' in it ? (it as { searchFocusClientId?: string }).searchFocusClientId : undefined,
)}
onPressSelection={conversationSearchAllowsLocalWrites(item)
? () => toggleSelection(sessionIdsForListItem(item))
: undefined}
onToggleAutomationGroup={toggleAutomationGroup}
selected={sessionIdsForListItem(item).every((id) => selectedSessionIdSet.has(id))}
selectionMode={selectionMode}
swipe={conversationSearchAllowsLocalWrites(item) ? sessionSwipeControls : undefined}
testID="deviceDetail.sessionRow"
/>
)}
/>
</ListDisclosureScope>
{actionOverlays}
</SafeAreaView>
);
}
function DeviceDetailSessionRow({
asBlock = false,
expandedAutomationGroups,
hideDivider = false,
item,
onLongPress,
onOpenAutomationGroup,
onOpenSession,
onPressSelection,
onToggleAutomationGroup,
selected = false,
selectionMode = false,
suppressBlockTopBorder = false,
swipe,
testID,
}: {
asBlock?: boolean;
expandedAutomationGroups?: readonly string[];
hideDivider?: boolean;
item: RemoteSessionListItem;
onLongPress?: () => void;
onOpenAutomationGroup?: (group: RemoteAutomationSessionGroup) => void;
onOpenSession(item: RemoteSessionListItem): void;
onPressSelection?: () => void;
onToggleAutomationGroup?: (key: string) => void;
selected?: boolean;
selectionMode?: boolean;
suppressBlockTopBorder?: boolean;
swipe?: SessionSwipeControls;
testID: string;
}) {
const consoleList = testID === 'deviceDetail.sessionRow';
const row = (
<HomeSessionRow
asBlock={asBlock}
automationChildTestID={consoleList ? 'deviceDetail.automationGroupChild' : undefined}
automationChildrenTestID={consoleList ? 'deviceDetail.automationGroupChildren' : undefined}
expandedAutomationGroups={expandedAutomationGroups}
groupRowTestID={consoleList ? 'deviceDetail.automationGroupRow' : undefined}
hideDivider={hideDivider}
item={item}
onLongPress={onLongPress}
onOpenAutomationGroup={onOpenAutomationGroup}
onOpenSession={onOpenSession}
onPressSelection={onPressSelection}
onToggleAutomationGroup={onToggleAutomationGroup}
selected={selected}
selectionMarkTestID={consoleList ? 'deviceDetail.sessionSelectionMark' : undefined}
selectionMode={selectionMode}
suppressBlockTopBorder={suppressBlockTopBorder}
swipe={selectionMode ? undefined : swipe}
testID={testID}
titleTestIDPrefix="deviceDetail.sessionRowTitle"
/>
);
if (!swipe || selectionMode || item.automationGroup) return row;
return (
<SwipeableSessionRow
onArchive={swipe.onArchive}
onShowOptions={swipe.onShowOptions}
onTogglePin={swipe.onTogglePin}
onMarkRead={swipe.onMarkRead}
registry={swipe.registry}
session={item.session as RemoteSession}
testID={`${testID}.swipe`}
>
{row}
</SwipeableSessionRow>
);
}
function SessionListActionOverlays({
actionSheetSession,
closeRenameSession,
confirmRenameSession,
handleSessionSheetAction,
handleSessionSheetClosed,
renameSessionDraft,
renameSessionTarget,
setActionSheetSession,
setRenameSessionDraft,
}: {
actionSheetSession: RemoteSession | null;
closeRenameSession(): void;
confirmRenameSession(): void;
handleSessionSheetAction(action: SessionSwipeAction): void;
handleSessionSheetClosed(): void;
renameSessionDraft: string;
renameSessionTarget: RemoteSession | null;
setActionSheetSession(session: RemoteSession | null): void;
setRenameSessionDraft(value: string): void;
}) {
return (
<>
<SessionOptionsPresenter
session={actionSheetSession}
onAction={handleSessionSheetAction}
onClose={() => setActionSheetSession(null)}
onClosed={handleSessionSheetClosed}
pinnedAt={actionSheetSession?.pinnedAt}
status={actionSheetSession?.status}
visible={actionSheetSession !== null}
/>
<RenameSessionModal
draft={renameSessionDraft}
onCancel={closeRenameSession}
onChangeDraft={setRenameSessionDraft}
onConfirm={confirmRenameSession}
saving={false}
visible={renameSessionTarget !== null}
/>
</>
);
}
function remoteListStatusFilter(filter: RemoteSessionStatusFilter): RemoteListStatusFilter {
if (filter === 'archived') return 'archived';
if (filter === 'all' || filter === 'waiting' || filter === 'automation') return 'all';
return 'active';
}
function bulkActionPendingLabel(action: MobileSessionBulkAction, t: TFunction): string {
if (action === 'pin') return t('devices.detail.bulk.pinning');
if (action === 'unpin') return t('devices.detail.bulk.unpinning');
if (action === 'archive') return t('devices.detail.bulk.archiving');
if (action === 'restore') return t('devices.detail.bulk.restoring');
return t('devices.detail.bulk.deleting');
}
function bulkActionAccessibilityLabel(action: MobileSessionBulkAction, t: TFunction): string {
if (action === 'pin') return t('devices.detail.bulk.pinA11y');
if (action === 'unpin') return t('devices.detail.bulk.unpinA11y');
if (action === 'archive') return t('devices.detail.bulk.archiveA11y');
if (action === 'restore') return t('devices.detail.bulk.restoreA11y');
return t('devices.detail.bulk.deleteA11y');
}
function bulkActionTestID(action: MobileSessionBulkAction): string {
if (action === 'pin') return 'deviceDetail.bulkPinButton';
if (action === 'unpin') return 'deviceDetail.bulkUnpinButton';
if (action === 'archive') return 'deviceDetail.bulkArchiveButton';
if (action === 'restore') return 'deviceDetail.bulkRestoreButton';
return 'deviceDetail.bulkDeleteButton';
}
/** 解析自动化作用域入口带的 sessionId 快照(JSON 数组);解析失败按空快照处理,仅靠组键匹配。 */
function parseSessionIdsParam(value: string | null): string[] {
if (!value) return [];
try {
const parsed: unknown = JSON.parse(value);
return Array.isArray(parsed) ? parsed.filter((item): item is string => typeof item === 'string') : [];
} catch {
return [];
}
}
function parseRouteStatusFilter(value: string | null): RemoteSessionStatusFilter {
if (
value === 'active'
|| value === 'waiting'
|| value === 'automation'
|| value === 'archived'
|| value === 'all'
) {
return value;
}
return 'active';
}
function readRouteString(value: unknown): string | null {
if (Array.isArray(value)) {
const first = value.find((item) => typeof item === 'string' && item.trim().length > 0);
return first ?? null;
}
return typeof value === 'string' && value.trim().length > 0 ? value : null;
}
const makeStyles = (colors: ThemeColors) => StyleSheet.create({
safeArea: { flex: 1, backgroundColor: colors.surface },
summaryTopRow: {
alignItems: 'stretch',
flexDirection: 'row',
flexWrap: 'wrap',
gap: spacing.sm,
},
listContent: { paddingHorizontal: spacing.lg, paddingVertical: spacing.sm },
controls: {
borderBottomColor: colors.border,
borderBottomWidth: StyleSheet.hairlineWidth,
gap: spacing.sm,
paddingHorizontal: spacing.lg,
paddingVertical: spacing.sm,
},
compactControlsRow: {
alignItems: 'center',
flexDirection: 'row',
gap: spacing.sm,
minHeight: 38,
},
controlsSummary: {
color: colors.textSecondary,
flex: 1,
fontSize: typeScale.footnote,
lineHeight: lineHeight.caption,
fontWeight: fontWeight.regular,
minWidth: 0,
},
projectConnectionAnchor: { left: 0, position: 'absolute', right: 0 },
projectSearchChrome: { paddingBottom: spacing.sm },
segmentScroll: {
marginHorizontal: -spacing.lg,
},
segmentScrollContent: {
gap: spacing.sm,
paddingHorizontal: spacing.lg,
},
selectionBar: {
backgroundColor: colors.surfaceElevated,
borderColor: colors.border,
borderRadius: radius.container,
borderWidth: StyleSheet.hairlineWidth,
gap: spacing.sm,
padding: spacing.md,
},
selectionHeaderRow: {
alignItems: 'center',
flexDirection: 'row',
gap: spacing.sm,
justifyContent: 'space-between',
},
selectionTitleBlock: {
flex: 1,
gap: 2,
minWidth: 0,
},
selectionText: { color: colors.textPrimary, fontSize: typeScale.body, lineHeight: lineHeight.body, fontWeight: fontWeight.medium },
selectionMeta: { color: colors.textTertiary, fontSize: typeScale.caption, lineHeight: lineHeight.caption, fontWeight: fontWeight.regular },
bulkConfirmCard: {
backgroundColor: colors.surfaceElevated,
borderColor: colors.border,
borderRadius: radius.container,
borderWidth: StyleSheet.hairlineWidth,
gap: spacing.sm,
padding: spacing.md,
},
bulkConfirmTitle: {
color: colors.textPrimary,
fontSize: typeScale.body,
fontWeight: fontWeight.medium,
lineHeight: lineHeight.body,
},
bulkConfirmText: {
color: colors.textSecondary,
fontSize: typeScale.footnote,
lineHeight: lineHeight.caption,
},
bulkNotice: { color: colors.textSecondary, fontSize: typeScale.footnote, lineHeight: lineHeight.caption },
listContextCard: {
backgroundColor: colors.surfaceElevated,
borderColor: colors.border,
borderRadius: radius.container,
borderWidth: StyleSheet.hairlineWidth,
gap: spacing.xs,
padding: spacing.md,
},
listContextHeader: {
alignItems: 'center',
flexDirection: 'row',
gap: spacing.sm,
justifyContent: 'space-between',
},
listContextTitle: {
color: colors.textPrimary,
flex: 1,
fontSize: typeScale.body,
lineHeight: lineHeight.body,
fontWeight: fontWeight.medium,
minWidth: 0,
},
listContextCount: {
color: colors.textPrimary,
fontSize: typeScale.body,
lineHeight: lineHeight.body,
fontWeight: fontWeight.medium,
},
listContextDetail: {
color: colors.textSecondary,
fontSize: typeScale.footnote,
lineHeight: lineHeight.caption,
fontWeight: fontWeight.regular,
},
listContextHint: {
color: colors.textTertiary,
fontSize: typeScale.footnote,
lineHeight: lineHeight.caption,
},
sectionTitle: {
color: colors.textTertiary,
fontSize: typeScale.footnote,
lineHeight: lineHeight.caption,
fontWeight: fontWeight.semibold,
paddingBottom: spacing.xs,
paddingTop: spacing.md,
},
});