Files
t3code/apps/mobile/src/features/threads/ThreadNavigationSidebar.tsx
T

1110 lines
42 KiB
TypeScript

import { useAndroidControlSizing } from "../../components/useAndroidControlSizing";
import { useAppearancePreferences } from "../settings/appearance/AppearancePreferencesProvider";
import { computeThreadMoveAvailability } from "./threadOrder";
import type {
EnvironmentProject,
EnvironmentThreadShell,
} from "@t3tools/client-runtime/state/shell";
import {
threadSearchMatchKey,
type EnvironmentThreadSearchMatch,
} from "@t3tools/client-runtime/state/thread-search";
import { LegendList } from "@legendapp/list/react-native";
import type { MenuAction } from "@react-native-menu/menu";
import { useAtomValue } from "@effect/atom-react";
import { useCallback, useEffect, useMemo, useRef, useState } from "react";
import type { LayoutChangeEvent, TextInputInstance } from "react-native";
import { Platform, StyleSheet, TextInput, View } from "react-native";
import { GestureDetector, useNativeGesture } from "react-native-gesture-handler";
import type { SwipeableMethods } from "react-native-gesture-handler/ReanimatedSwipeable";
import { useSafeAreaInsets } from "react-native-safe-area-context";
import type { SearchBarCommands } from "react-native-screens";
import { AppText as Text } from "../../components/AppText";
import { CompactBrandTitle } from "../../components/CompactBrandTitle";
import { ControlPillMenu } from "../../components/ControlPill";
import { SymbolView } from "../../components/AppSymbol";
import { NATIVE_LIQUID_GLASS_SUPPORTED } from "../../native/native-glass";
import { NativeStackScreenOptions } from "../../native/StackHeader";
import { scopedProjectKey, scopedThreadKey } from "../../lib/scopedEntities";
import { useProjects, useNavigationThreadShells } from "../../state/entities";
import { useThreadSearch } from "../../state/queries";
import { useThreadListV2ShelfPreferences } from "./use-thread-list-v2-shelf-preferences";
import { usePendingThreadOrder } from "../../state/thread-order";
import { threadListEnvironmentsAtom } from "../../state/server";
import { usePendingNewTasks } from "../../state/use-pending-new-tasks";
import { useQueuedThreadKeys } from "../../state/use-thread-outbox";
import { useWorkspaceState } from "../../state/workspace";
import { useSavedRemoteConnections } from "../../state/use-remote-environment-registry";
import { useHardwareKeyboardCommand } from "../keyboard/hardwareKeyboardCommands";
import { useThreadJumpShortcuts } from "../keyboard/threadKeyboardShortcuts";
import { useHomeListOptions } from "../home/home-list-options";
import { buildHomeListFilterMenu } from "../home/home-list-filter-menu";
import { buildHomeProjectScopes } from "../home/homeThreadList";
import { SwipeableScrollGateProvider, useSwipeableScrollGate } from "../home/thread-swipe-actions";
import { usePendingTaskListActions } from "../home/usePendingTaskListActions";
import { useThreadListActions } from "../home/useThreadListActions";
import {
getConnectionAwareBrandHeaderOptions,
WorkspaceConnectionTitle,
} from "../home/WorkspaceConnectionTitle";
import { SidebarHeaderActions } from "./sidebar-header-actions";
import { MaterialThreadListToolbar } from "../home/MaterialThreadListToolbar";
import { useMaterialToolbarLayout } from "../../components/useMaterialToolbarLayout";
import { useMaterialFabScroll } from "../home/MaterialFabScrollContext";
import { SidebarFilterButton } from "./sidebar-filter-button";
import { createSidebarHeaderItems } from "./sidebar-native-header-items";
import { SidebarNavigationShell } from "./sidebar-navigation-shell";
import {
ThreadListV2PendingRow,
ThreadListV2Row,
ThreadListV2SettledShelfHeader,
ThreadListV2ShowMoreRow,
ThreadListV2SnoozedShelfHeader,
ThreadListV2WorkingShelfHeader,
} from "./thread-list-v2-items";
import { useThreadRowProviderInstanceResolver } from "./thread-provider-instance";
import {
buildThreadListV2Items,
getThreadListV2OrderedSection,
buildThreadListV2ListItems,
isThreadListV2ListItem,
threadListV2ListItemsAreEqual,
threadListInboxReturns,
THREAD_LIST_V2_SETTLED_INITIAL_COUNT,
THREAD_LIST_V2_SETTLED_PAGE_COUNT,
type ThreadListV2ListItem,
} from "./threadListV2";
/** The sidebar list: flat v2 rows with queued tasks spliced in, plus a
settled "Show more" pager row. */
type SidebarListItem =
| ThreadListV2ListItem
| { readonly type: "v2-show-more"; readonly key: string; readonly hiddenCount: number };
const SIDEBAR_STICKY_HEADER_HEIGHT = 106;
interface ThreadNavigationSidebarProps {
readonly width: number;
readonly visible: boolean;
readonly selectedThreadKey: string | null;
readonly onOpenSettings: () => void;
readonly onOpenEnvironmentSettings: () => void;
readonly onNewThreadOnBranch: (thread: EnvironmentThreadShell) => void;
readonly onNewThreadInProject: (project: EnvironmentProject) => void;
readonly onSearchQueryChange: (query: string) => void;
readonly onSelectThread: (thread: EnvironmentThreadShell) => void;
readonly onRequestVisibility: () => void;
readonly searchQuery: string;
}
/**
* iPad/large-width sidebar column.
*
* On iOS the pane is hosted inside its own navigation-inert single-screen
* native stack (SidebarNavigationShell) so the header is a real
* UINavigationBar: large title, native bar-button items, and a
* UISearchController search field — the same chrome a UISplitViewController
* column gets. Other platforms keep the custom header chrome.
*/
export function ThreadNavigationSidebar(props: ThreadNavigationSidebarProps) {
if (Platform.OS !== "ios") {
return <ThreadNavigationSidebarPane {...props} nativeChrome={false} />;
}
return <NativeSidebarContainer {...props} />;
}
function NativeSidebarContainer(props: ThreadNavigationSidebarProps) {
return (
<View
testID="thread-navigation-sidebar"
className="flex-1 border-border bg-drawer"
style={{ borderRightWidth: StyleSheet.hairlineWidth, width: props.width }}
>
<SidebarNavigationShell>
<ThreadNavigationSidebarPane {...props} nativeChrome />
</SidebarNavigationShell>
</View>
);
}
function ThreadNavigationSidebarPane(
props: ThreadNavigationSidebarProps & { readonly nativeChrome: boolean },
) {
const { themeVariables: materialTheme } = useAppearancePreferences();
const drawerColor = materialTheme["--color-drawer"];
const insets = useSafeAreaInsets();
const { fabClearance } = useAndroidControlSizing();
const projects = useProjects();
const threads = useNavigationThreadShells();
const { environments: workspaceEnvironments, state: catalogState } = useWorkspaceState();
const { savedConnectionsById } = useSavedRemoteConnections();
const searchInputRef = useRef<TextInputInstance>(null);
const searchBarRef = useRef<SearchBarCommands>(null);
const openSwipeableRef = useRef<SwipeableMethods | null>(null);
const sidebarScrollGesture = useNativeGesture();
const {
archiveThread,
confirmDeleteThread,
settleThread,
snoozeThread,
unsnoozeThread,
unsettleThread,
pinThread,
unpinThread,
setThreadAutoSettle,
moveThread,
renameThread,
regenerateThreadTitle,
} = useThreadListActions();
const pendingTasks = usePendingNewTasks();
const queuedThreadKeys = useQueuedThreadKeys();
const { openPendingTask, confirmDeletePendingTask } = usePendingTaskListActions();
const environments = useMemo(
() =>
Object.values(savedConnectionsById)
.map((connection) => ({
environmentId: connection.environmentId,
label: connection.environmentLabel,
}))
.sort((left, right) => left.label.localeCompare(right.label)),
[savedConnectionsById],
);
const availableEnvironmentIds = useMemo(
() => new Set(environments.map((environment) => environment.environmentId)),
[environments],
);
const { options, setSelectedEnvironmentId } = useHomeListOptions(availableEnvironmentIds);
const searchEnvironmentIds = useMemo(
() =>
options.selectedEnvironmentId === null
? workspaceEnvironments
.filter((environment) => environment.connectionState === "connected")
.map((environment) => environment.environmentId)
: workspaceEnvironments.some(
(environment) =>
environment.environmentId === options.selectedEnvironmentId &&
environment.connectionState === "connected",
)
? [options.selectedEnvironmentId]
: [],
[options.selectedEnvironmentId, workspaceEnvironments],
);
const threadSearch = useThreadSearch(searchEnvironmentIds, props.searchQuery);
const threadSearchMatchByKey = useMemo(() => {
const matches = new Map<string, EnvironmentThreadSearchMatch>();
for (const match of threadSearch.matches) {
if (match.source === "user" || match.source === "assistant") {
matches.set(threadSearchMatchKey(match), match);
}
}
return matches;
}, [threadSearch.matches]);
const matchedThreadKeys = useMemo(
() => new Set(threadSearch.matches.map(threadSearchMatchKey)),
[threadSearch.matches],
);
const [selectedProjectKey, setSelectedProjectKey] = useState<string | null>(null);
const projectScopes = useMemo(
() =>
buildHomeProjectScopes({
projects,
environmentId: options.selectedEnvironmentId,
projectGroupingMode: options.projectGroupingMode,
}),
[options.projectGroupingMode, options.selectedEnvironmentId, projects],
);
const projectFilterOptions = useMemo(
() =>
projectScopes.map((scope) => ({
key: scope.key,
label: scope.title,
})),
[projectScopes],
);
const projectTitleByProjectKey = useMemo(
() =>
new Map(
projectScopes.flatMap((scope) =>
scope.projectRefs.map(
(projectRef) =>
[
scopedProjectKey(projectRef.environmentId, projectRef.projectId),
scope.title,
] as const,
),
),
),
[projectScopes],
);
const selectedProjectScope = useMemo(
() =>
selectedProjectKey === null
? null
: (projectScopes.find((scope) => scope.key === selectedProjectKey) ?? null),
[projectScopes, selectedProjectKey],
);
useEffect(() => {
if (
selectedProjectKey !== null &&
!projectFilterOptions.some((project) => project.key === selectedProjectKey)
) {
setSelectedProjectKey(null);
}
}, [projectFilterOptions, selectedProjectKey]);
const selectedProjectRefs = useMemo(
() =>
selectedProjectScope === null
? null
: new Set(
selectedProjectScope.projectRefs.map((projectRef) =>
scopedProjectKey(projectRef.environmentId, projectRef.projectId),
),
),
[selectedProjectScope],
);
const projectByKey = useMemo(() => {
const map = new Map<string, EnvironmentProject>();
for (const project of projects) {
map.set(scopedProjectKey(project.environmentId, project.id), project);
}
return map;
}, [projects]);
// Thread List v2 (beta) support — same model as the compact Home list
// (HomeScreen.tsx): flat creation-order card block + settled recency tail.
// The settled tail renders in pages; expansion resets when the filter
// context changes so environment/search flips never inherit a deep page.
const [settledVisibleCount, setSettledVisibleCount] = useState(
THREAD_LIST_V2_SETTLED_INITIAL_COUNT,
);
const settledResetKey = `${options.selectedEnvironmentId ?? "all"}:${selectedProjectKey ?? "all"}:${props.searchQuery.trim()}`;
const lastSettledResetKeyRef = useRef(settledResetKey);
if (lastSettledResetKeyRef.current !== settledResetKey) {
lastSettledResetKeyRef.current = settledResetKey;
setSettledVisibleCount(THREAD_LIST_V2_SETTLED_INITIAL_COUNT);
}
const showMoreSettled = useCallback(
() => setSettledVisibleCount((count) => count + THREAD_LIST_V2_SETTLED_PAGE_COUNT),
[],
);
const {
loaded: shelfPreferencesLoaded,
settledShelfExpanded,
snoozedShelfExpanded,
workingShelfEnabled,
workingShelfExpanded,
toggleSettledShelf,
toggleSnoozedShelf,
toggleWorkingShelf,
} = useThreadListV2ShelfPreferences();
// The queued-start and snooze helpers need a clock while the pane stays open.
const [nowMinute, setNowMinute] = useState(() => new Date().toISOString().slice(0, 16));
// Snooze wake times are second-precise; a counter bumped exactly at the
// next wake boundary re-runs the partition with a fresh clock so a woken
// thread reappears immediately instead of on the next minute tick.
const [snoozeWakeTick, bumpSnoozeWakeTick] = useState(0);
useEffect(() => {
// Refresh immediately because the mount-time value can be hours old.
setNowMinute(new Date().toISOString().slice(0, 16));
const id = setInterval(() => setNowMinute(new Date().toISOString().slice(0, 16)), 60_000);
return () => clearInterval(id);
}, []);
// Threads on servers without the settlement capability never classify as
// settled (the user could neither un-settle nor pin them).
const listEnvironments = useAtomValue(threadListEnvironmentsAtom);
const {
providersByEnvironmentId,
machineByEnvironmentId,
settlementEnvironmentIds,
snoozeEnvironmentIds,
pinningEnvironmentIds,
autoSettleOptOutEnvironmentIds,
pinReorderEnvironmentIds,
activeReorderEnvironmentIds,
titleRegenerationEnvironmentIds,
} = listEnvironments;
const resolveProviderInstance = useThreadRowProviderInstanceResolver(providersByEnvironmentId);
const pendingOrder = usePendingThreadOrder(nowMinute, snoozeWakeTick);
// Up/down menu availability for every card, computed once per section per
// rebuild (see computeThreadMoveAvailability): per-thread planner calls made
// list construction quadratic, and this list rebuilds on every minute tick.
const threadMoveAvailability = useMemo(() => {
const sectionAvailability = (section: "pinned" | "active") =>
computeThreadMoveAvailability({
allThreads: threads,
section,
pendingOrder,
reorderableEnvironmentIds:
section === "pinned" ? pinReorderEnvironmentIds : activeReorderEnvironmentIds,
ordered: getThreadListV2OrderedSection({
threads,
section,
pendingOrder,
now: new Date().toISOString(),
settlementEnvironmentIds,
snoozeEnvironmentIds,
queuedThreadKeys,
}),
});
// The Working beta orders the inbox by time, so only pins can move.
return new Map([
...sectionAvailability("pinned"),
...(workingShelfEnabled ? [] : sectionAvailability("active")),
]);
}, [
workingShelfEnabled,
pinReorderEnvironmentIds,
activeReorderEnvironmentIds,
threads,
pendingOrder,
queuedThreadKeys,
settlementEnvironmentIds,
snoozeEnvironmentIds,
nowMinute,
snoozeWakeTick,
]);
const threadListV2Layout = useMemo(() => {
threadListInboxReturns.observe(workingShelfEnabled ? threads : null);
return buildThreadListV2Items({
pendingOrder,
threads: threads.filter((thread) => thread.archivedAt === null),
environmentId: options.selectedEnvironmentId,
projectRefs: selectedProjectScope === null ? null : selectedProjectScope.projectRefs,
searchQuery: props.searchQuery,
matchedThreadKeys,
settlementEnvironmentIds,
snoozeEnvironmentIds,
queuedThreadKeys,
settledLimit: settledVisibleCount,
now: new Date().toISOString(),
workingShelfEnabled,
workingShelfExpanded,
inboxReturnAt: threadListInboxReturns.returnedAt,
snoozedShelfExpanded,
settledShelfExpanded,
selectedThreadKey: props.selectedThreadKey ?? null,
});
}, [
workingShelfEnabled,
workingShelfExpanded,
pendingOrder,
queuedThreadKeys,
nowMinute,
snoozeWakeTick,
snoozedShelfExpanded,
settledShelfExpanded,
props.selectedThreadKey,
options.selectedEnvironmentId,
props.searchQuery,
matchedThreadKeys,
settledVisibleCount,
settlementEnvironmentIds,
snoozeEnvironmentIds,
threads,
selectedProjectScope,
]);
// Re-partition the moment the earliest snooze expires (clamped to the
// signed-32-bit setTimeout range; far-future wakes re-arm at the clamp).
const nextSnoozeWakeAt = threadListV2Layout.nextSnoozeWakeAt;
useEffect(() => {
if (nextSnoozeWakeAt === null) return;
const wakeAtMs = Date.parse(nextSnoozeWakeAt);
if (Number.isNaN(wakeAtMs)) return;
const delayMs = Math.min(Math.max(0, wakeAtMs - Date.now()) + 50, 2_147_483_647);
const id = setTimeout(() => bumpSnoozeWakeTick((tick) => tick + 1), delayMs);
return () => clearTimeout(id);
// snoozeWakeTick must re-arm the timer even when nextSnoozeWakeAt is
// unchanged: after a clamped fire (wake beyond the 32-bit setTimeout
// range) the boundary string is identical and the chain would die.
}, [nextSnoozeWakeAt, snoozeWakeTick]);
const listItems = useMemo<readonly SidebarListItem[]>(() => {
// Queued offline tasks are not thread shells, so the v2 item builder
// never sees them; the shared splice puts them below the active block
// (mirrors the compact Home v2 list) where they stay visible and
// deletable while their environment is offline. Same environment scope
// and search filter as the list.
const v2SearchQuery = props.searchQuery.trim().toLocaleLowerCase();
const v2PendingTasks = pendingTasks.filter(
(pendingTask) =>
(options.selectedEnvironmentId === null ||
pendingTask.environmentId === options.selectedEnvironmentId) &&
(selectedProjectRefs === null ||
selectedProjectRefs.has(
scopedProjectKey(pendingTask.environmentId, pendingTask.projectId),
)) &&
(v2SearchQuery.length === 0 ||
pendingTask.title.toLocaleLowerCase().includes(v2SearchQuery)),
);
const items: SidebarListItem[] = buildThreadListV2ListItems({
items: threadListV2Layout.items,
pendingTasks: v2PendingTasks,
workingCount: threadListV2Layout.workingCount,
workingShelfExpanded,
workingShelfHeaderIndex: threadListV2Layout.workingShelfHeaderIndex,
snoozedCount: threadListV2Layout.snoozedCount,
snoozedShelfExpanded,
snoozedShelfHeaderIndex: threadListV2Layout.snoozedShelfHeaderIndex,
settledCount: threadListV2Layout.settledCount,
settledShelfExpanded,
settledShelfHeaderIndex: threadListV2Layout.settledShelfHeaderIndex,
snoozeLabelNow: `${nowMinute}:00.000Z`,
snoozeEnvironmentIds,
queuedThreadKeys,
moveAvailability: threadMoveAvailability,
shelfPreferencesLoading: !shelfPreferencesLoaded,
});
if (settledShelfExpanded && threadListV2Layout.hiddenSettledCount > 0) {
items.push({
type: "v2-show-more",
key: "v2-show-more",
hiddenCount: threadListV2Layout.hiddenSettledCount,
});
}
return items;
}, [
nowMinute,
options.selectedEnvironmentId,
pendingTasks,
props.searchQuery,
queuedThreadKeys,
threadMoveAvailability,
selectedProjectRefs,
settledShelfExpanded,
shelfPreferencesLoaded,
snoozedShelfExpanded,
snoozeEnvironmentIds,
threadListV2Layout,
workingShelfExpanded,
]);
const listMenuActions = useMemo<MenuAction[]>(
() => [
{
id: "environment",
title: "Environment",
subactions: [
{
id: "environment:all",
title: "All environments",
subtitle: "Show threads from every environment",
state: options.selectedEnvironmentId === null ? "on" : "off",
},
...environments.map((environment) => ({
id: `environment:${environment.environmentId}`,
title: environment.label,
state:
options.selectedEnvironmentId === environment.environmentId
? ("on" as const)
: ("off" as const),
})),
],
},
...(projectFilterOptions.length === 0
? []
: ([
{
id: "project",
title: "Project",
subactions: [
{
id: "project:all",
title: "All projects",
subtitle: "Show threads from every project",
state: selectedProjectKey === null ? "on" : "off",
},
...projectFilterOptions.map((project) => ({
id: `project:${project.key}`,
title: project.label,
state: selectedProjectKey === project.key ? ("on" as const) : ("off" as const),
})),
],
},
] satisfies MenuAction[])),
],
[environments, options, projectFilterOptions, selectedProjectKey],
);
const handleListMenuAction = useCallback(
({ nativeEvent }: { readonly nativeEvent: { readonly event: string } }) => {
const event = nativeEvent.event;
if (event === "environment:all") {
setSelectedEnvironmentId(null);
return;
}
if (event.startsWith("environment:")) {
const environment = environments.find(
(candidate) => String(candidate.environmentId) === event.slice("environment:".length),
);
if (environment) setSelectedEnvironmentId(environment.environmentId);
return;
}
if (event === "project:all") {
setSelectedProjectKey(null);
return;
}
if (event.startsWith("project:")) {
const projectKey = event.slice("project:".length);
if (projectFilterOptions.some((project) => project.key === projectKey)) {
setSelectedProjectKey(projectKey);
}
return;
}
},
[environments, projectFilterOptions, setSelectedEnvironmentId],
);
const [measuredHeaderHeight, setMeasuredHeaderHeight] = useState<number | null>(null);
const { height, paddingTop, paddingBottom } = useMaterialToolbarLayout();
// The sticky header (title row, search field, optional connection status)
// is measured so the list inset always matches its real height — no
// hardcoded per-variant constants.
const stickyHeaderHeight =
measuredHeaderHeight ??
(Platform.OS === "android"
? paddingTop + height + paddingBottom
: insets.top + SIDEBAR_STICKY_HEADER_HEIGHT);
const topListInset = stickyHeaderHeight + 6;
const handleStickyHeaderLayout = useCallback((event: LayoutChangeEvent) => {
const nextHeight = event.nativeEvent.layout.height;
setMeasuredHeaderHeight((current) => (current === nextHeight ? current : nextHeight));
}, []);
const handleSwipeableWillOpen = useCallback((methods: SwipeableMethods) => {
if (openSwipeableRef.current !== methods) {
openSwipeableRef.current?.close();
openSwipeableRef.current = methods;
}
}, []);
const handleSwipeableClose = useCallback((methods: SwipeableMethods) => {
if (openSwipeableRef.current === methods) {
openSwipeableRef.current = null;
}
}, []);
const handleSelectThread = useCallback(
(thread: EnvironmentThreadShell) => {
props.onSelectThread(thread);
openSwipeableRef.current?.close();
},
[props.onSelectThread],
);
const handleScrollBeginDrag = useCallback(() => {
openSwipeableRef.current?.close();
}, []);
const onMaterialFabScroll = useMaterialFabScroll();
const { swipeEnabled, scrollGateHandlers } = useSwipeableScrollGate({
onScroll: onMaterialFabScroll,
onScrollBeginDrag: handleScrollBeginDrag,
});
// The sticky header's project shells and search maps feed row props, so
// they have to bust the recycler's memoization — otherwise a row keeps the
// blank favicon and fallback title it was first rendered with. The minute
// clock deliberately stays out: its per-row text lives on the items, so a
// tick only re-renders rows whose displayed text actually moved.
const listExtraData = useMemo(
() => ({
selectedThreadKey: props.selectedThreadKey ?? "",
projectByKey,
projectTitleByProjectKey,
savedConnectionsById,
listEnvironments,
threadSearchMatchByKey,
// Rows read it for their reorder menu items.
workingShelfEnabled,
}),
[
props.selectedThreadKey,
projectByKey,
projectTitleByProjectKey,
savedConnectionsById,
listEnvironments,
threadSearchMatchByKey,
workingShelfEnabled,
],
);
useThreadJumpShortcuts(listItems, handleSelectThread);
const sidebarItemsAreEqual = useCallback(
(previous: SidebarListItem, item: SidebarListItem): boolean => {
if (isThreadListV2ListItem(previous) && isThreadListV2ListItem(item)) {
return threadListV2ListItemsAreEqual(previous, item);
}
if (previous.type === "v2-show-more" && item.type === "v2-show-more") {
return previous.hiddenCount === item.hiddenCount;
}
return false;
},
[],
);
const focusSearch = useCallback(() => {
if (Platform.OS === "android") return false;
const focus = () => {
if (props.nativeChrome) {
searchBarRef.current?.focus();
return;
}
searchInputRef.current?.focus();
};
if (!props.visible) {
props.onRequestVisibility();
setTimeout(focus, 240);
} else {
focus();
}
return true;
}, [props.nativeChrome, props.onRequestVisibility, props.visible]);
useHardwareKeyboardCommand("focusSearch", focusSearch);
const renderListItem = useCallback(
({ item }: { readonly item: SidebarListItem }) => {
switch (item.type) {
case "v2-pending": {
const pendingScopeKey = scopedProjectKey(
item.pendingTask.environmentId,
item.pendingTask.projectId,
);
return (
<ThreadListV2PendingRow
pendingTask={item.pendingTask}
project={projectByKey.get(pendingScopeKey) ?? null}
projectTitle={projectTitleByProjectKey.get(pendingScopeKey)}
environmentLabel={
Object.keys(savedConnectionsById).length > 1
? (savedConnectionsById[item.pendingTask.environmentId]?.environmentLabel ?? null)
: null
}
environmentMachine={machineByEnvironmentId.get(item.pendingTask.environmentId)}
pane="sidebar"
showPendingDivider={item.showPendingDivider}
onSelectPendingTask={openPendingTask}
onDeletePendingTask={confirmDeletePendingTask}
/>
);
}
case "v2-thread": {
const thread = item.item.thread;
const scopeKey = scopedProjectKey(thread.environmentId, thread.projectId);
// Intentional difference from Home: the sidebar never passes
// `showTrailingDivider` because its rows render no Home-style row
// hairline at all — card rows carry tonal containers in this pane
// (the hairline branch is !sidebarPane-only) and slim rows have no
// hairline branch. The stamp still rides the shared list items
// because Home's boundary suppression consumes it; the sidebar's
// only cost is the occasional divider-only equality invalidation,
// which re-renders identically.
return (
<ThreadListV2Row
onNewThreadOnBranch={props.onNewThreadOnBranch}
thread={thread}
variant={item.item.variant}
hasQueuedMessages={item.hasQueuedMessages}
snoozed={item.item.snoozed}
pinned={item.item.pinned}
snoozePresetMinute={item.snoozePresetMinute ?? ""}
snoozeWakeLabelText={item.snoozeWakeLabelText}
timeLabel={item.timeLabel}
project={projectByKey.get(scopeKey) ?? null}
projectTitle={projectTitleByProjectKey.get(scopeKey)}
providerInstance={resolveProviderInstance(thread)}
providers={providersByEnvironmentId.get(thread.environmentId)}
environmentLabel={
Object.keys(savedConnectionsById).length > 1
? (savedConnectionsById[thread.environmentId]?.environmentLabel ?? null)
: null
}
environmentMachine={machineByEnvironmentId.get(thread.environmentId)}
searchMatch={threadSearchMatchByKey.get(
threadSearchMatchKey({
environmentId: thread.environmentId,
threadId: thread.id,
}),
)}
searchQuery={props.searchQuery}
pane="sidebar"
selected={
scopedThreadKey(thread.environmentId, thread.id) === props.selectedThreadKey
}
fullSwipeWidth={props.width - 20}
onSelectThread={handleSelectThread}
onDeleteThread={confirmDeleteThread}
onArchiveThread={archiveThread}
onRenameThread={renameThread}
onRegenerateThreadTitle={regenerateThreadTitle}
titleRegenerationSupported={titleRegenerationEnvironmentIds.has(thread.environmentId)}
settlementSupported={settlementEnvironmentIds.has(thread.environmentId)}
onSettleThread={settleThread}
snoozeSupported={snoozeEnvironmentIds.has(thread.environmentId)}
pinningSupported={pinningEnvironmentIds.has(thread.environmentId)}
autoSettleOptOutSupported={autoSettleOptOutEnvironmentIds.has(thread.environmentId)}
reorderSupported={
item.item.pinned
? pinReorderEnvironmentIds.has(thread.environmentId)
: !workingShelfEnabled && activeReorderEnvironmentIds.has(thread.environmentId)
}
canMoveUp={item.canMoveUp}
canMoveDown={item.canMoveDown}
onSnoozeThread={snoozeThread}
onUnsnoozeThread={unsnoozeThread}
onUnsettleThread={unsettleThread}
onPinThread={pinThread}
onUnpinThread={unpinThread}
onSetThreadAutoSettle={setThreadAutoSettle}
onMoveThread={moveThread}
onSwipeableClose={handleSwipeableClose}
onSwipeableWillOpen={handleSwipeableWillOpen}
simultaneousSwipeGesture={sidebarScrollGesture}
/>
);
}
case "v2-working-shelf":
return (
<ThreadListV2WorkingShelfHeader
count={item.count}
disabled={item.disabled}
expanded={item.expanded}
onToggle={toggleWorkingShelf}
pane="sidebar"
/>
);
case "v2-snoozed-shelf":
return (
<ThreadListV2SnoozedShelfHeader
count={item.count}
disabled={item.disabled}
expanded={item.expanded}
onToggle={toggleSnoozedShelf}
pane="sidebar"
/>
);
case "v2-settled-shelf":
return (
<ThreadListV2SettledShelfHeader
count={item.count}
disabled={item.disabled}
expanded={item.expanded}
onToggle={toggleSettledShelf}
pane="sidebar"
/>
);
case "v2-show-more":
return (
<ThreadListV2ShowMoreRow
pane="sidebar"
hiddenCount={item.hiddenCount}
onPress={showMoreSettled}
/>
);
}
},
[
archiveThread,
activeReorderEnvironmentIds,
confirmDeletePendingTask,
confirmDeleteThread,
handleSelectThread,
handleSwipeableClose,
handleSwipeableWillOpen,
machineByEnvironmentId,
moveThread,
openPendingTask,
pinReorderEnvironmentIds,
pinThread,
pinningEnvironmentIds,
autoSettleOptOutEnvironmentIds,
autoSettleOptOutEnvironmentIds,
setThreadAutoSettle,
projectByKey,
projectTitleByProjectKey,
regenerateThreadTitle,
renameThread,
props.onNewThreadOnBranch,
props.searchQuery,
props.selectedThreadKey,
props.width,
savedConnectionsById,
resolveProviderInstance,
providersByEnvironmentId,
threadSearchMatchByKey,
titleRegenerationEnvironmentIds,
settleThread,
settlementEnvironmentIds,
showMoreSettled,
sidebarScrollGesture,
snoozeEnvironmentIds,
snoozeThread,
toggleSettledShelf,
toggleSnoozedShelf,
toggleWorkingShelf,
unpinThread,
unsettleThread,
unsnoozeThread,
workingShelfEnabled,
],
);
// The list ignores sort/group options, so only the environment and project
// filters can light the "customized" state.
const filterCustomized = options.selectedEnvironmentId !== null || selectedProjectKey !== null;
const filterIcon = filterCustomized
? "line.3.horizontal.decrease.circle.fill"
: "line.3.horizontal.decrease.circle";
const filterMenu = useMemo(
() =>
buildHomeListFilterMenu({
environments,
projects: projectFilterOptions,
selectedEnvironmentId: options.selectedEnvironmentId,
selectedProjectKey,
onEnvironmentChange: setSelectedEnvironmentId,
onProjectChange: setSelectedProjectKey,
}),
[environments, options, projectFilterOptions, selectedProjectKey, setSelectedEnvironmentId],
);
const nativeHeaderItems = useMemo(
() =>
createSidebarHeaderItems({
filterIcon,
filterMenu,
onOpenSettings: props.onOpenSettings,
}),
[filterIcon, filterMenu, props.onOpenSettings],
);
// Snoozed threads need no special case: the shelf header is a list row
// even while collapsed.
const listEmpty = (
<Text
className={
Platform.OS === "android"
? "px-4 py-4 text-center text-sm text-drawer-foreground-muted"
: "px-2 py-4 text-sm text-drawer-foreground-muted"
}
>
{catalogState.isLoadingConnections
? "Loading threads…"
: Platform.OS === "android" && !catalogState.hasConnections
? "No environments connected"
: props.searchQuery.trim().length > 0
? threadSearch.isPending
? "Searching thread messages…"
: "No matching threads"
: selectedProjectScope !== null
? `No threads in ${selectedProjectScope.title}`
: "No threads yet"}
</Text>
);
if (props.nativeChrome) {
return (
<>
<NativeStackScreenOptions
optionsVersion={[nativeHeaderItems, props.width]}
options={{
// Re-applies the shell's static brand slot with the
// connection-status swap so reconnects surface in the header
// instead of shifting the list.
...getConnectionAwareBrandHeaderOptions({
headerWidth: props.width,
trailingItemCount: nativeHeaderItems.length,
onOpenEnvironments: props.onOpenEnvironmentSettings,
fallbackTitleStyle: { fontSize: 18, fontWeight: "800" },
}),
headerSearchBarOptions: {
ref: searchBarRef,
autoCapitalize: "none",
hideNavigationBar: false,
// Keep the search bar pinned under the title — UIKit's default
// hidesSearchBarWhenScrolling collapses it on scroll.
hideWhenScrolling: false,
obscureBackground: false,
placeholder: "Search",
placement: "stacked",
onCancelButtonPress: () => {
props.onSearchQueryChange("");
},
onChangeText: (event) => {
props.onSearchQueryChange(event.nativeEvent.text);
},
},
unstable_headerRightItems: () => nativeHeaderItems,
}}
/>
<View className="flex-1">
<SwipeableScrollGateProvider enabled={swipeEnabled}>
<GestureDetector gesture={sidebarScrollGesture}>
<LegendList
data={listItems}
drawDistance={500}
estimatedItemSize={64}
extraData={listExtraData}
getItemType={(item) => item.type}
itemsAreEqual={sidebarItemsAreEqual}
keyExtractor={(item) => item.key}
renderItem={renderListItem}
automaticallyAdjustsScrollIndicatorInsets={NATIVE_LIQUID_GLASS_SUPPORTED}
contentInsetAdjustmentBehavior={
NATIVE_LIQUID_GLASS_SUPPORTED ? "automatic" : "never"
}
contentContainerStyle={[
styles.threadListContent,
Platform.OS === "android" ? { paddingHorizontal: 0 } : null,
{
paddingBottom: Math.max(insets.bottom, 16) + 16,
paddingTop: 6,
},
]}
keyboardDismissMode="on-drag"
keyboardShouldPersistTaps="handled"
{...scrollGateHandlers}
recycleItems
scrollEventThrottle={16}
showsVerticalScrollIndicator={false}
style={styles.threadList}
ListEmptyComponent={listEmpty}
/>
</GestureDetector>
</SwipeableScrollGateProvider>
</View>
</>
);
}
return (
<View
testID="thread-navigation-sidebar"
className={
Platform.OS === "android" ? "flex-1 bg-header" : "flex-1 border-r border-border bg-drawer"
}
style={{ width: props.width }}
>
<View
className="flex-1"
style={
Platform.OS === "android"
? {
marginTop: stickyHeaderHeight,
marginHorizontal: 4,
paddingBottom: insets.bottom,
backgroundColor: drawerColor,
borderTopLeftRadius: 28,
borderTopRightRadius: 28,
overflow: "hidden",
}
: { paddingBottom: insets.bottom }
}
>
{Platform.OS === "android" && listItems.length === 0 ? (
<View className="flex-1 items-center justify-center">{listEmpty}</View>
) : (
<SwipeableScrollGateProvider enabled={swipeEnabled}>
<GestureDetector gesture={sidebarScrollGesture}>
<LegendList
data={listItems}
drawDistance={500}
estimatedItemSize={64}
extraData={listExtraData}
getItemType={(item) => item.type}
itemsAreEqual={sidebarItemsAreEqual}
keyExtractor={(item) => item.key}
renderItem={renderListItem}
contentContainerStyle={[
styles.threadListContent,
Platform.OS === "android" ? { paddingHorizontal: 0 } : null,
{
paddingBottom:
Platform.OS === "android"
? Math.max(insets.bottom, 16) + fabClearance - insets.bottom
: 16 + insets.bottom,
paddingTop: Platform.OS === "android" ? 6 : topListInset,
},
]}
keyboardDismissMode="on-drag"
keyboardShouldPersistTaps="handled"
{...scrollGateHandlers}
recycleItems
scrollEventThrottle={16}
showsVerticalScrollIndicator={false}
style={styles.threadList}
ListEmptyComponent={listEmpty}
/>
</GestureDetector>
</SwipeableScrollGateProvider>
)}
</View>
{Platform.OS === "android" ? (
<MaterialThreadListToolbar
sidebar
onLayout={handleStickyHeaderLayout}
searchQuery={props.searchQuery}
onSearchQueryChange={props.onSearchQueryChange}
filterActions={listMenuActions}
filterCustomized={filterCustomized}
onFilterAction={handleListMenuAction}
onOpenSettings={props.onOpenSettings}
onOpenEnvironments={props.onOpenEnvironmentSettings}
onRequestVisibility={props.onRequestVisibility}
/>
) : (
<View
className="absolute inset-x-0 top-0 z-[4] bg-drawer"
collapsable={false}
onLayout={handleStickyHeaderLayout}
pointerEvents="auto"
style={{ paddingTop: insets.top }}
>
<View className="h-[50px] flex-row items-end gap-0.5 pr-2 pl-5">
{/* Title slot doubles as the connection status surface: while an
environment reconnects, the brand fades to a status label in
place (no layout shift in the list below). */}
<WorkspaceConnectionTitle
grow
onPress={props.onOpenEnvironmentSettings}
size="pageTitle"
brand={
<View className="h-11 flex-1 justify-center">
<CompactBrandTitle allowFontScaling={false} />
</View>
}
/>
<View className="flex-row items-center gap-2.5">
<ControlPillMenu actions={listMenuActions} onPressAction={handleListMenuAction}>
<SidebarFilterButton accessibilityLabel="Filter threads" icon={filterIcon} />
</ControlPillMenu>
<SidebarHeaderActions onOpenSettings={props.onOpenSettings} />
</View>
</View>
<View className="mx-4 mt-[9px] h-[38px] flex-row items-center gap-1.5 rounded-xl bg-sidebar-search pr-2.5 pl-[11px]">
<SymbolView
name="magnifyingglass"
size={15}
tintColorClassName="accent-drawer-foreground-muted"
type="monochrome"
/>
<TextInput
ref={searchInputRef}
accessibilityLabel="Search threads"
autoCapitalize="none"
autoCorrect={false}
clearButtonMode="while-editing"
onChangeText={props.onSearchQueryChange}
placeholder="Search"
placeholderTextColorClassName="accent-placeholder"
selectionColorClassName={undefined}
cursorColorClassName={undefined}
selectionHandleColorClassName={undefined}
returnKeyType="search"
className="h-[34px] flex-1 px-0 py-0 font-sans text-base text-drawer-foreground"
value={props.searchQuery}
/>
</View>
</View>
)}
</View>
);
}
const styles = StyleSheet.create({
threadList: {
flex: 1,
},
threadListContent: {
paddingHorizontal: 8,
},
});