The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { useGT, useMessages } from "gt-react-native"; |
| 2 | |
| 3 | import { useContentTopInset } from "../../components/ui/useBarInset"; |
| 4 | import { usePaneRole } from "../../components/ui/Pane"; |
| 5 | import { ScreenContent } from "../../components/ui/ScreenContent"; |
| 6 | import { useActiveNewTask } from "../../state/active-new-task"; |
| 7 | import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator"; |
| 8 | import { useAndroidControlSizing } from "../../components/ui/useAndroidControlSizing"; |
| 9 | import type { ThreadMoveDestination } from "../threads/threadOrder"; |
| 10 | import type { LegendListRef } from "@legendapp/list/react-native"; |
| 11 | import { AnimatedLegendList } from "@legendapp/list/reanimated"; |
| 12 | import { THREAD_LIST_ROW_LAYOUT } from "../threads/threadListAnimations"; |
| 13 | import { |
| 14 | type EnvironmentProject, |
| 15 | type EnvironmentThreadShell, |
| 16 | } from "@t3tools/client-runtime/state/shell"; |
| 17 | import { pickableProjectGroups } from "@t3tools/client-runtime/state/project-grouping"; |
| 18 | import { threadSearchMatchKey } from "@t3tools/client-runtime/state/thread-search"; |
| 19 | import { type EnvironmentId, type SidebarProjectGroupingMode } from "@t3tools/contracts"; |
| 20 | import { useAtomValue } from "@effect/atom-react"; |
| 21 | import { |
| 22 | use, |
| 23 | useEffect, |
| 24 | useLayoutEffect, |
| 25 | useMemo, |
| 26 | useRef, |
| 27 | useState, |
| 28 | type ComponentProps, |
| 29 | } from "react"; |
| 30 | import { |
| 31 | Platform, |
| 32 | ScrollView, |
| 33 | useWindowDimensions, |
| 34 | View, |
| 35 | type ScrollViewProps, |
| 36 | type GestureResponderEvent, |
| 37 | type NativeScrollEvent, |
| 38 | type NativeSyntheticEvent, |
| 39 | } from "react-native"; |
| 40 | import { NativeScrollViewMarker as ScrollViewMarker } from "../../native/NativeScrollViewMarker"; |
| 41 | import { GestureDetector, useNativeGesture } from "react-native-gesture-handler"; |
| 42 | import type { SwipeableMethods } from "react-native-gesture-handler/ReanimatedSwipeable"; |
| 43 | import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 44 | |
| 45 | import { cn } from "../../lib/cn"; |
| 46 | import { |
| 47 | ANDROID_READING_MAX_WIDTH, |
| 48 | deriveCenteredContentHorizontalPadding, |
| 49 | } from "../../lib/layout"; |
| 50 | import { EmptyState } from "../../components/ui/EmptyState"; |
| 51 | import { FloatingActionButton } from "../../components/ui/FloatingActionButton"; |
| 52 | import type { WorkspaceEnvironment, WorkspaceState } from "../../state/workspaceModel"; |
| 53 | import type { SavedRemoteConnection } from "../../lib/connection"; |
| 54 | import { scopedProjectKey, scopedThreadKey } from "../../lib/scopedEntities"; |
| 55 | import { NativePrimaryColumnContext } from "../../native/v5-workspace-context"; |
| 56 | import { nativeHeaderScrollEdgeEffects } from "../../native/scrollEdgeEffects"; |
| 57 | import { |
| 58 | useNativeColumnLayoutMetrics, |
| 59 | useNativeLayoutMetrics, |
| 60 | } from "../../native/native-layout-metrics"; |
| 61 | import { useWorkspaceListEmptyTopInset } from "../layout/workspace/workspace-list-insets"; |
| 62 | import { useNativeWorkspaceColumnsSupported } from "../../native/NativeWorkspaceColumns"; |
| 63 | import { useThreadSearch } from "../../state/queries"; |
| 64 | import { requestThreadMessageJump } from "../threads/threadMessageJump"; |
| 65 | import { useThreadListSearchMatches } from "../threads/useThreadListSearchMatches"; |
| 66 | import { useThreadListV2 } from "../threads/useThreadListV2"; |
| 67 | import { useSidebarRowThreadKey } from "../threads/useSidebarRowThreadKey"; |
| 68 | import { useThreadListNow } from "../threads/use-thread-list-now"; |
| 69 | import { useSearchScrollRestoration } from "../layout/workspace/useSearchScrollRestoration"; |
| 70 | import { useThreadJumpShortcuts } from "../keyboard/threadKeyboardShortcuts"; |
| 71 | import { usePendingThreadOrder } from "../../state/thread-order"; |
| 72 | import { threadListEnvironmentsAtom } from "../../state/presentation"; |
| 73 | import type { PendingNewTask } from "../../state/use-pending-new-tasks"; |
| 74 | import { useQueuedThreadKeys } from "../../state/use-thread-outbox"; |
| 75 | import { |
| 76 | ThreadListV2PendingRow, |
| 77 | ThreadListV2Row, |
| 78 | ThreadListV2SettledShelfHeader, |
| 79 | ThreadListV2ShowMoreRow, |
| 80 | ThreadListV2SnoozedShelfHeader, |
| 81 | ThreadListV2WorkingShelfHeader, |
| 82 | } from "../threads/thread-list-v2-items"; |
| 83 | import { useThreadRowProviderInstanceResolver } from "../threads/thread-provider-instance"; |
| 84 | import { useOpenThreadPullRequests } from "../threads/use-open-thread-pull-requests"; |
| 85 | import type { ThreadPrPresentation } from "../../state/thread-pr-presentation"; |
| 86 | import { |
| 87 | threadListV2ListItemsAreEqual, |
| 88 | THREAD_LIST_V2_SETTLED_INITIAL_COUNT, |
| 89 | THREAD_LIST_V2_SETTLED_PAGE_COUNT, |
| 90 | type ThreadListV2ListItem, |
| 91 | } from "../threads/threadListV2"; |
| 92 | import { useThreadListV2ShelfPreferences } from "../threads/use-thread-list-v2-shelf-preferences"; |
| 93 | import type { HomeListFilterMenuEnvironment } from "./home-list-filter-menu"; |
| 94 | import { |
| 95 | buildHomeProjectScopes, |
| 96 | findHomeProjectScope, |
| 97 | type HomeProjectSortOrder, |
| 98 | sortHomeProjectScopes, |
| 99 | } from "./homeThreadList"; |
| 100 | import { createSwipeRowActivation } from "./swipe-row-activation"; |
| 101 | import { SwipeableScrollGateProvider, useSwipeableScrollGate } from "./thread-swipe-actions"; |
| 102 | import { useFabScroll } from "./FabScrollContext"; |
| 103 | import { holdLaunchSplashForHomeRows, settleLaunchSplashHomeRows } from "./home-launch-splash"; |
| 104 | import { useWorkspaceTabs } from "../layout/workspace/WorkspaceTabsContext"; |
| 105 | import { ThreadListFilters } from "./ThreadListFilters"; |
| 106 | import { deriveHomeEmptyState } from "./home-empty-state"; |
| 107 | |
| 108 | interface HomeScreenProps { |
| 109 | /** Whether this list is the one on screen; see `useHomeRouteVisible`. */ |
| 110 | readonly visible: boolean; |
| 111 | readonly projects: ReadonlyArray<EnvironmentProject>; |
| 112 | readonly threads: ReadonlyArray<EnvironmentThreadShell>; |
| 113 | readonly pendingTasks: ReadonlyArray<PendingNewTask>; |
| 114 | readonly catalogState: WorkspaceState; |
| 115 | readonly savedConnectionsById: Readonly<Record<string, SavedRemoteConnection>>; |
| 116 | readonly environments: ReadonlyArray< |
| 117 | HomeListFilterMenuEnvironment & Pick<WorkspaceEnvironment, "connectionState"> |
| 118 | >; |
| 119 | readonly searchQuery: string; |
| 120 | readonly selectedEnvironmentId: EnvironmentId | null; |
| 121 | readonly selectedProjectKey: string | null; |
| 122 | readonly projectSortOrder: HomeProjectSortOrder; |
| 123 | readonly projectGroupingMode: SidebarProjectGroupingMode; |
| 124 | readonly onSearchQueryChange: (query: string) => void; |
| 125 | readonly onEnvironmentChange: (environmentId: EnvironmentId | null) => void; |
| 126 | readonly onProjectChange: (projectKey: string | null) => void; |
| 127 | readonly onAddConnection: () => void; |
| 128 | readonly onOpenSettings: () => void; |
| 129 | readonly onStartNewTask: () => void; |
| 130 | readonly onSelectThread: (thread: EnvironmentThreadShell) => void; |
| 131 | readonly onArchiveThread: (thread: EnvironmentThreadShell) => void; |
| 132 | readonly onDeleteThread: (thread: EnvironmentThreadShell) => void; |
| 133 | readonly onSettleThread: (thread: EnvironmentThreadShell) => Promise<boolean>; |
| 134 | readonly onSnoozeThread: ( |
| 135 | thread: EnvironmentThreadShell, |
| 136 | snoozedUntil: string, |
| 137 | ) => Promise<boolean>; |
| 138 | readonly onUnsnoozeThread: (thread: EnvironmentThreadShell) => Promise<boolean>; |
| 139 | readonly onUnsettleThread: (thread: EnvironmentThreadShell) => void; |
| 140 | readonly onPinThread: (thread: EnvironmentThreadShell) => Promise<boolean>; |
| 141 | readonly onUnpinThread: (thread: EnvironmentThreadShell) => Promise<boolean>; |
| 142 | readonly onSetThreadAutoSettle: ( |
| 143 | thread: EnvironmentThreadShell, |
| 144 | enabled: boolean, |
| 145 | ) => Promise<boolean>; |
| 146 | readonly onMoveThread: ( |
| 147 | thread: EnvironmentThreadShell, |
| 148 | direction: ThreadMoveDestination, |
| 149 | ) => Promise<boolean>; |
| 150 | readonly onRenameThread: (thread: EnvironmentThreadShell) => void; |
| 151 | readonly onRegenerateThreadTitle: (thread: EnvironmentThreadShell) => Promise<boolean>; |
| 152 | readonly onSelectPendingTask: (pendingTask: PendingNewTask) => void; |
| 153 | readonly onDeletePendingTask: (pendingTask: PendingNewTask) => void; |
| 154 | readonly onNewThreadOnBranch: (thread: EnvironmentThreadShell) => void; |
| 155 | readonly onNewThreadInProject: (project: EnvironmentProject) => void; |
| 156 | } |
| 157 | |
| 158 | const ESTIMATED_THREAD_LIST_V2_ROW_HEIGHT = 72; |
| 159 | // Rows away from the viewport skip their swipe actions (see |
| 160 | // swipe-row-activation), so render further ahead: a fast fling then reaches |
| 161 | // rows that are already built instead of rows still being rebuilt. |
| 162 | const THREAD_LIST_V2_DRAW_DISTANCE = 1_000; |
| 163 | |
| 164 | function HomeTopContentSpacer() { |
| 165 | return <View className="h-4" />; |
| 166 | } |
| 167 | |
| 168 | function HomeScrollView(props: ComponentProps<typeof ScrollView>) { |
| 169 | const primaryColumn = use(NativePrimaryColumnContext); |
| 170 | const sidebarSurface = usePaneRole() === "sidebar" && primaryColumn !== null; |
| 171 | if (Platform.OS !== "ios") return <ScrollView {...props} />; |
| 172 | |
| 173 | // Register the scroll view explicitly; layout and header siblings hide it from UIKit. |
| 174 | return ( |
| 175 | <ScrollViewMarker |
| 176 | style={{ flex: 1 }} |
| 177 | scrollEdgeEffects={{ |
| 178 | ...nativeHeaderScrollEdgeEffects(Platform.OS, Platform.Version), |
| 179 | top: Number.parseInt(String(Platform.Version), 10) >= 27 ? "automatic" : "soft", |
| 180 | }} |
| 181 | > |
| 182 | <ScrollView |
| 183 | {...props} |
| 184 | // UIKit's scroll-edge material needs the scroll view's own surface color. |
| 185 | className={cn(props.className, sidebarSurface ? "bg-drawer" : "bg-screen")} |
| 186 | /> |
| 187 | </ScrollViewMarker> |
| 188 | ); |
| 189 | } |
| 190 | |
| 191 | function renderHomeScrollView(props: ScrollViewProps) { |
| 192 | // The list passes its content and ref in these props; keep the marker inside the real ScrollView. |
| 193 | return <HomeScrollView {...props} />; |
| 194 | } |
| 195 | |
| 196 | export function HomeScreen(props: HomeScreenProps) { |
| 197 | const gt = useGT(); |
| 198 | const m = useMessages(); |
| 199 | const usesNativeWorkspaceColumns = useNativeWorkspaceColumnsSupported(); |
| 200 | const workspace = useWorkspaceTabs(); |
| 201 | const [initialScrollOffset] = useState(() => workspace?.getScrollOffset("threads") ?? 0); |
| 202 | const primaryColumn = use(NativePrimaryColumnContext); |
| 203 | const sidebarSurface = usePaneRole() === "sidebar" && primaryColumn !== null; |
| 204 | const contentBackground = sidebarSurface ? "bg-drawer" : "bg-screen"; |
| 205 | const containerClassName = cn( |
| 206 | "flex-1", |
| 207 | Platform.OS === "android" ? "bg-header" : contentBackground, |
| 208 | ); |
| 209 | const columnMetrics = useNativeColumnLayoutMetrics(); |
| 210 | const activeNewTask = useActiveNewTask(); |
| 211 | const sidebarRowThreadKey = useSidebarRowThreadKey(primaryColumn?.selectedThreadKey ?? null); |
| 212 | const selectedThreadKey = activeNewTask ? null : sidebarRowThreadKey; |
| 213 | const fullSwipeWidth = primaryColumn && columnMetrics ? columnMetrics.width - 20 : undefined; |
| 214 | const queuedThreadKeys = useQueuedThreadKeys(); |
| 215 | const openSwipeableRef = useRef<SwipeableMethods | null>(null); |
| 216 | const homeScrollGesture = useNativeGesture(); |
| 217 | const insets = useSafeAreaInsets(); |
| 218 | const contentTopInset = useContentTopInset(); |
| 219 | const emptyTopInset = useWorkspaceListEmptyTopInset(); |
| 220 | const { fabClearance } = useAndroidControlSizing(); |
| 221 | const screenMetrics = useNativeLayoutMetrics(); |
| 222 | const contentSideInsets = usesNativeWorkspaceColumns |
| 223 | ? (columnMetrics ?? screenMetrics)?.safeArea |
| 224 | : undefined; |
| 225 | // Wide Android lists need reading-width gutters to clear edges and camera cutouts. |
| 226 | const { width: windowWidth } = useWindowDimensions(); |
| 227 | const androidListGutter = |
| 228 | Platform.OS === "android" |
| 229 | ? deriveCenteredContentHorizontalPadding({ |
| 230 | viewportWidth: windowWidth, |
| 231 | maxContentWidth: ANDROID_READING_MAX_WIDTH, |
| 232 | minimumPadding: 0, |
| 233 | }) |
| 234 | : 0; |
| 235 | const iosBottomClearance = Math.max( |
| 236 | columnMetrics?.safeArea.bottom ?? insets.bottom, |
| 237 | 24, |
| 238 | workspace?.bottomClearance ?? 0, |
| 239 | ); |
| 240 | const searchEnvironmentIds = |
| 241 | props.selectedEnvironmentId === null |
| 242 | ? props.environments |
| 243 | .filter((environment) => environment.connectionState === "connected") |
| 244 | .map((environment) => environment.environmentId) |
| 245 | : props.environments.some( |
| 246 | (environment) => |
| 247 | environment.environmentId === props.selectedEnvironmentId && |
| 248 | environment.connectionState === "connected", |
| 249 | ) |
| 250 | ? [props.selectedEnvironmentId] |
| 251 | : []; |
| 252 | const handleSwipeableWillOpen = (methods: SwipeableMethods) => { |
| 253 | if (openSwipeableRef.current !== methods) { |
| 254 | openSwipeableRef.current?.close(); |
| 255 | openSwipeableRef.current = methods; |
| 256 | } |
| 257 | }; |
| 258 | |
| 259 | const handleSwipeableClose = (methods: SwipeableMethods) => { |
| 260 | if (openSwipeableRef.current === methods) { |
| 261 | openSwipeableRef.current = null; |
| 262 | } |
| 263 | }; |
| 264 | |
| 265 | const handleScrollBeginDrag = () => { |
| 266 | openSwipeableRef.current?.close(); |
| 267 | }; |
| 268 | const openThreadPullRequests = useOpenThreadPullRequests(); |
| 269 | const handleOpenThreadPullRequests = ( |
| 270 | thread: EnvironmentThreadShell, |
| 271 | pr: ThreadPrPresentation, |
| 272 | ) => { |
| 273 | openSwipeableRef.current?.close(); |
| 274 | openThreadPullRequests(thread, pr); |
| 275 | }; |
| 276 | const onFabScroll = useFabScroll(); |
| 277 | const listRef = useRef<LegendListRef>(null); |
| 278 | const searchMode = workspace?.state.tabs.threads.searchMode; |
| 279 | const restoredOffset = workspace?.state.tabs.threads.scrollOffset; |
| 280 | useSearchScrollRestoration(searchMode, () => { |
| 281 | if (restoredOffset === undefined) return; |
| 282 | void listRef.current?.scrollToOffset({ |
| 283 | offset: restoredOffset - contentTopInset, |
| 284 | animated: false, |
| 285 | }); |
| 286 | }); |
| 287 | const swipeRowActivation = createSwipeRowActivation(); |
| 288 | const activateVisibleRows = (rows: ReadonlyArray<ThreadListV2ListItem>) => { |
| 289 | const state = listRef.current?.getState(); |
| 290 | if (state === undefined || !(state.end >= 0)) return; |
| 291 | swipeRowActivation.activate( |
| 292 | rows.slice(Math.max(0, state.start - 2), state.end + 3).map((row) => row.key), |
| 293 | ); |
| 294 | }; |
| 295 | // Status-bar, accessibility and programmatic scrolls bypass the gate; activate visible rows after settling. |
| 296 | const activationTimerRef = useRef<ReturnType<typeof setTimeout> | undefined>(undefined); |
| 297 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- clears the row activation timer on unmount |
| 298 | useEffect(() => () => clearTimeout(activationTimerRef.current), []); |
| 299 | const handleListScroll = (event: NativeSyntheticEvent<NativeScrollEvent>) => { |
| 300 | onFabScroll?.(event); |
| 301 | if (workspace) |
| 302 | workspace.recordScrollOffset( |
| 303 | "threads", |
| 304 | Math.max(0, event.nativeEvent.contentOffset.y + contentTopInset), |
| 305 | ); |
| 306 | clearTimeout(activationTimerRef.current); |
| 307 | activationTimerRef.current = setTimeout( |
| 308 | () => activateVisibleRows(listRef.current?.getState().data ?? []), |
| 309 | 200, |
| 310 | ); |
| 311 | }; |
| 312 | const trackListTouches = (event: GestureResponderEvent, started: boolean) => { |
| 313 | const { changedTouches, touches } = event.nativeEvent; |
| 314 | swipeRowActivation.trackTouches( |
| 315 | started ? changedTouches.map((touch) => String(touch.identifier)) : [], |
| 316 | touches.map((touch) => String(touch.identifier)), |
| 317 | ); |
| 318 | }; |
| 319 | const { swipeEnabled, scrollGateHandlers } = useSwipeableScrollGate({ |
| 320 | onScroll: handleListScroll, |
| 321 | onScrollBeginDrag: handleScrollBeginDrag, |
| 322 | }); |
| 323 | |
| 324 | const { projects, selectedEnvironmentId, projectGroupingMode } = props; |
| 325 | const projectScopes = useMemo( |
| 326 | () => |
| 327 | buildHomeProjectScopes( |
| 328 | { |
| 329 | projects, |
| 330 | environmentId: selectedEnvironmentId, |
| 331 | projectGroupingMode, |
| 332 | }, |
| 333 | m, |
| 334 | ), |
| 335 | [projects, selectedEnvironmentId, projectGroupingMode, m], |
| 336 | ); |
| 337 | const hasSearchQuery = props.searchQuery.trim().length > 0; |
| 338 | const projectByKey = (() => { |
| 339 | const map = new Map<string, EnvironmentProject>(); |
| 340 | for (const project of props.projects) { |
| 341 | map.set(scopedProjectKey(project.environmentId, project.id), project); |
| 342 | } |
| 343 | return map; |
| 344 | })(); |
| 345 | |
| 346 | const v2ProjectScopeKey = props.selectedProjectKey; |
| 347 | // An archived project's filter reads as all projects, matching the filter menu. |
| 348 | const v2ScopedProjectGroup = findHomeProjectScope( |
| 349 | pickableProjectGroups(projectScopes), |
| 350 | v2ProjectScopeKey, |
| 351 | ); |
| 352 | const threadSearch = useThreadSearch( |
| 353 | searchEnvironmentIds, |
| 354 | props.searchQuery, |
| 355 | v2ScopedProjectGroup === null |
| 356 | ? {} |
| 357 | : { projectIds: v2ScopedProjectGroup.projectRefs.map((ref) => ref.projectId) }, |
| 358 | ); |
| 359 | const threadSearchMatchByKey = useThreadListSearchMatches(props.threads, threadSearch); |
| 360 | const v2ProjectTitleByProjectKey = new Map( |
| 361 | projectScopes.flatMap((scope) => |
| 362 | scope.projectRefs.map( |
| 363 | (projectRef) => |
| 364 | [scopedProjectKey(projectRef.environmentId, projectRef.projectId), scope.title] as const, |
| 365 | ), |
| 366 | ), |
| 367 | ); |
| 368 | const handleSettleThread = props.onSettleThread; |
| 369 | const handleSnoozeThread = props.onSnoozeThread; |
| 370 | const handleUnsnoozeThread = props.onUnsnoozeThread; |
| 371 | const handlePinThread = props.onPinThread; |
| 372 | const handleMoveThread = props.onMoveThread; |
| 373 | const handleUnpinThread = props.onUnpinThread; |
| 374 | const handleSetThreadAutoSettle = props.onSetThreadAutoSettle; |
| 375 | const handleRegenerateThreadTitle = props.onRegenerateThreadTitle; |
| 376 | const handleRenameThread = props.onRenameThread; |
| 377 | const handleDeleteThread = props.onDeleteThread; |
| 378 | const handleUnsettleThread = props.onUnsettleThread; |
| 379 | const [settledVisibleCount, setSettledVisibleCount] = useState( |
| 380 | THREAD_LIST_V2_SETTLED_INITIAL_COUNT, |
| 381 | ); |
| 382 | const settledResetKey = `${props.selectedEnvironmentId ?? "all"}:${v2ProjectScopeKey ?? "all"}:${props.searchQuery.trim()}`; |
| 383 | const [lastSettledResetKey, setLastSettledResetKey] = useState(settledResetKey); |
| 384 | if (lastSettledResetKey !== settledResetKey) { |
| 385 | setLastSettledResetKey(settledResetKey); |
| 386 | setSettledVisibleCount(THREAD_LIST_V2_SETTLED_INITIAL_COUNT); |
| 387 | } |
| 388 | const showMoreSettled = () => |
| 389 | setSettledVisibleCount((count) => count + THREAD_LIST_V2_SETTLED_PAGE_COUNT); |
| 390 | const { |
| 391 | loaded: shelfPreferencesLoaded, |
| 392 | settledShelfExpanded, |
| 393 | snoozedShelfExpanded, |
| 394 | workingShelfEnabled, |
| 395 | workingShelfExpanded, |
| 396 | toggleSettledShelf, |
| 397 | toggleSnoozedShelf, |
| 398 | toggleWorkingShelf, |
| 399 | } = useThreadListV2ShelfPreferences(); |
| 400 | const now = useThreadListNow(props.threads); |
| 401 | const listEnvironments = useAtomValue(threadListEnvironmentsAtom); |
| 402 | const { |
| 403 | providersByEnvironmentId, |
| 404 | machineByEnvironmentId, |
| 405 | settlementEnvironmentIds, |
| 406 | snoozeEnvironmentIds, |
| 407 | snoozePresetsByEnvironmentId, |
| 408 | pinningEnvironmentIds, |
| 409 | autoSettleOptOutEnvironmentIds, |
| 410 | pinReorderEnvironmentIds, |
| 411 | activeReorderEnvironmentIds, |
| 412 | titleRegenerationEnvironmentIds, |
| 413 | } = listEnvironments; |
| 414 | const resolveProviderInstance = useThreadRowProviderInstanceResolver(providersByEnvironmentId); |
| 415 | const pendingOrder = usePendingThreadOrder(); |
| 416 | const { |
| 417 | layout: threadListV2Layout, |
| 418 | items: threadListV2Items, |
| 419 | contentOnlyThreadKeys, |
| 420 | olderSettledShelf, |
| 421 | } = useThreadListV2({ |
| 422 | threads: props.threads, |
| 423 | pendingTasks: props.pendingTasks, |
| 424 | environmentId: props.selectedEnvironmentId, |
| 425 | projectRefs: v2ScopedProjectGroup === null ? null : v2ScopedProjectGroup.projectRefs, |
| 426 | searchQuery: props.searchQuery, |
| 427 | searchMatches: threadSearchMatchByKey, |
| 428 | settlementEnvironmentIds, |
| 429 | snoozeEnvironmentIds, |
| 430 | pinReorderEnvironmentIds, |
| 431 | activeReorderEnvironmentIds, |
| 432 | queuedThreadKeys, |
| 433 | settledLimit: settledVisibleCount, |
| 434 | now, |
| 435 | pendingOrder, |
| 436 | shelfPreferencesLoaded, |
| 437 | workingShelfEnabled, |
| 438 | workingShelfExpanded, |
| 439 | snoozedShelfExpanded, |
| 440 | settledShelfExpanded, |
| 441 | selectedThreadKey, |
| 442 | }); |
| 443 | // Called as `props.onSelectThread(...)`, the compiler would cache this handler on the whole |
| 444 | // props object, which is new on every render of Home's parent. |
| 445 | const { onSelectThread } = props; |
| 446 | // A row listed for a message match opens the thread at that message, by |
| 447 | // tap or by shortcut. |
| 448 | const selectThread = (thread: EnvironmentThreadShell) => { |
| 449 | const key = threadSearchMatchKey({ environmentId: thread.environmentId, threadId: thread.id }); |
| 450 | const messageId = contentOnlyThreadKeys.has(key) |
| 451 | ? threadSearchMatchByKey.get(key)?.messageId |
| 452 | : undefined; |
| 453 | if (messageId) { |
| 454 | requestThreadMessageJump(scopedThreadKey(thread.environmentId, thread.id), messageId); |
| 455 | } |
| 456 | onSelectThread(thread); |
| 457 | }; |
| 458 | useThreadJumpShortcuts(threadListV2Items, selectThread); |
| 459 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- reads the list's visible range after commit to enable swipe gestures on those rows |
| 460 | useEffect(() => { |
| 461 | if (swipeEnabled) activateVisibleRows(threadListV2Items); |
| 462 | }, [activateVisibleRows, swipeEnabled, threadListV2Items]); |
| 463 | |
| 464 | const renderV2Item = ({ item }: { readonly item: ThreadListV2ListItem }) => { |
| 465 | if (item.type === "v2-pending") { |
| 466 | const pendingScopeKey = scopedProjectKey( |
| 467 | item.pendingTask.environmentId, |
| 468 | item.pendingTask.projectId, |
| 469 | ); |
| 470 | return ( |
| 471 | <ThreadListV2PendingRow |
| 472 | pane={sidebarSurface ? "sidebar" : "screen"} |
| 473 | pendingTask={item.pendingTask} |
| 474 | selected={ |
| 475 | activeNewTask?.pendingKey === item.pendingTask.key || |
| 476 | (item.pendingTask.kind === "pending" && |
| 477 | scopedThreadKey(item.pendingTask.environmentId, item.pendingTask.message.threadId) === |
| 478 | selectedThreadKey) |
| 479 | } |
| 480 | project={projectByKey.get(pendingScopeKey) ?? null} |
| 481 | environmentLabel={ |
| 482 | Object.keys(props.savedConnectionsById).length > 1 |
| 483 | ? (props.savedConnectionsById[item.pendingTask.environmentId]?.environmentLabel ?? |
| 484 | null) |
| 485 | : null |
| 486 | } |
| 487 | environmentMachine={machineByEnvironmentId.get(item.pendingTask.environmentId)} |
| 488 | showPendingDivider={item.showPendingDivider} |
| 489 | showTrailingDivider={item.showTrailingDivider} |
| 490 | onSelectPendingTask={props.onSelectPendingTask} |
| 491 | onDeletePendingTask={props.onDeletePendingTask} |
| 492 | fullSwipeWidth={fullSwipeWidth} |
| 493 | onSwipeableClose={handleSwipeableClose} |
| 494 | onSwipeableWillOpen={handleSwipeableWillOpen} |
| 495 | simultaneousSwipeGesture={homeScrollGesture} |
| 496 | activationKey={item.key} |
| 497 | /> |
| 498 | ); |
| 499 | } |
| 500 | if (item.type === "v2-working-shelf") { |
| 501 | return ( |
| 502 | <ThreadListV2WorkingShelfHeader |
| 503 | count={item.count} |
| 504 | disabled={item.disabled} |
| 505 | expanded={item.expanded} |
| 506 | onToggle={toggleWorkingShelf} |
| 507 | /> |
| 508 | ); |
| 509 | } |
| 510 | if (item.type === "v2-snoozed-shelf") { |
| 511 | return ( |
| 512 | <ThreadListV2SnoozedShelfHeader |
| 513 | count={item.count} |
| 514 | disabled={item.disabled} |
| 515 | expanded={item.expanded} |
| 516 | onToggle={toggleSnoozedShelf} |
| 517 | /> |
| 518 | ); |
| 519 | } |
| 520 | if (item.type === "v2-settled-shelf") { |
| 521 | return ( |
| 522 | <ThreadListV2SettledShelfHeader |
| 523 | count={item.count} |
| 524 | disabled={item.disabled} |
| 525 | expanded={item.expanded} |
| 526 | onToggle={toggleSettledShelf} |
| 527 | /> |
| 528 | ); |
| 529 | } |
| 530 | const thread = item.item.thread; |
| 531 | return ( |
| 532 | <ThreadListV2Row |
| 533 | pane={sidebarSurface ? "sidebar" : "screen"} |
| 534 | selected={scopedThreadKey(thread.environmentId, thread.id) === selectedThreadKey} |
| 535 | fullSwipeWidth={fullSwipeWidth} |
| 536 | onNewThreadOnBranch={props.onNewThreadOnBranch} |
| 537 | thread={thread} |
| 538 | variant={item.item.variant} |
| 539 | hasQueuedMessages={item.hasQueuedMessages} |
| 540 | snoozed={item.item.snoozed} |
| 541 | pinned={item.item.pinned} |
| 542 | wokeAt={item.item.wokeAt} |
| 543 | snoozePresetMinute={item.snoozePresetMinute ?? ""} |
| 544 | snoozeWakeLabelText={item.snoozeWakeLabelText} |
| 545 | timeLabel={item.timeLabel} |
| 546 | showTrailingDivider={item.showTrailingDivider} |
| 547 | project={projectByKey.get(scopedProjectKey(thread.environmentId, thread.projectId)) ?? null} |
| 548 | providerInstance={resolveProviderInstance(thread)} |
| 549 | providers={providersByEnvironmentId.get(thread.environmentId)} |
| 550 | environmentLabel={ |
| 551 | Object.keys(props.savedConnectionsById).length > 1 |
| 552 | ? (props.savedConnectionsById[thread.environmentId]?.environmentLabel ?? null) |
| 553 | : null |
| 554 | } |
| 555 | environmentMachine={machineByEnvironmentId.get(thread.environmentId)} |
| 556 | searchMatch={ |
| 557 | // Only rows listed for their messages show one and open at it. |
| 558 | contentOnlyThreadKeys.has( |
| 559 | threadSearchMatchKey({ environmentId: thread.environmentId, threadId: thread.id }), |
| 560 | ) |
| 561 | ? threadSearchMatchByKey.get( |
| 562 | threadSearchMatchKey({ environmentId: thread.environmentId, threadId: thread.id }), |
| 563 | ) |
| 564 | : undefined |
| 565 | } |
| 566 | searchQuery={threadSearch.query} |
| 567 | onSelectThread={selectThread} |
| 568 | onOpenThreadPullRequests={handleOpenThreadPullRequests} |
| 569 | onDeleteThread={handleDeleteThread} |
| 570 | onArchiveThread={props.onArchiveThread} |
| 571 | onRenameThread={handleRenameThread} |
| 572 | onRegenerateThreadTitle={handleRegenerateThreadTitle} |
| 573 | titleRegenerationSupported={titleRegenerationEnvironmentIds.has(thread.environmentId)} |
| 574 | settlementSupported={settlementEnvironmentIds.has(thread.environmentId)} |
| 575 | onSettleThread={handleSettleThread} |
| 576 | snoozeSupported={snoozeEnvironmentIds.has(thread.environmentId)} |
| 577 | snoozePresets={snoozePresetsByEnvironmentId.get(thread.environmentId)} |
| 578 | pinningSupported={pinningEnvironmentIds.has(thread.environmentId)} |
| 579 | autoSettleOptOutSupported={autoSettleOptOutEnvironmentIds.has(thread.environmentId)} |
| 580 | reorderSupported={ |
| 581 | item.item.pinned |
| 582 | ? pinReorderEnvironmentIds.has(thread.environmentId) |
| 583 | : !workingShelfEnabled && activeReorderEnvironmentIds.has(thread.environmentId) |
| 584 | } |
| 585 | canMoveUp={item.canMoveUp} |
| 586 | canMoveDown={item.canMoveDown} |
| 587 | onSnoozeThread={handleSnoozeThread} |
| 588 | onUnsnoozeThread={handleUnsnoozeThread} |
| 589 | onUnsettleThread={handleUnsettleThread} |
| 590 | onPinThread={handlePinThread} |
| 591 | onUnpinThread={handleUnpinThread} |
| 592 | onSetThreadAutoSettle={handleSetThreadAutoSettle} |
| 593 | onMoveThread={handleMoveThread} |
| 594 | onSwipeableClose={handleSwipeableClose} |
| 595 | onSwipeableWillOpen={handleSwipeableWillOpen} |
| 596 | simultaneousSwipeGesture={homeScrollGesture} |
| 597 | activationKey={item.key} |
| 598 | /> |
| 599 | ); |
| 600 | }; |
| 601 | const v2KeyExtractor = (item: ThreadListV2ListItem) => item.key; |
| 602 | |
| 603 | // extraData changes invalidate every visible row; put the minute clock on items to limit updates. |
| 604 | const v2ExtraData = { |
| 605 | activeNewTask, |
| 606 | projectByKey, |
| 607 | projectTitleByProjectKey: v2ProjectTitleByProjectKey, |
| 608 | listEnvironments, |
| 609 | savedConnectionsById: props.savedConnectionsById, |
| 610 | searchQuery: props.searchQuery, |
| 611 | selectedThreadKey, |
| 612 | threadSearchMatchByKey, |
| 613 | workingShelfEnabled, |
| 614 | }; |
| 615 | |
| 616 | const hasAnyThreads = |
| 617 | props.threads.some((thread) => thread.archivedAt === null) || |
| 618 | props.pendingTasks.length > 0 || |
| 619 | threadListV2Layout.settledCount > 0; |
| 620 | // A hidden list gets no layout, so it never reports its rows as laid out. |
| 621 | const hasRowsToLayOut = props.visible && hasAnyThreads && threadListV2Items.length > 0; |
| 622 | // oxlint-disable-next-line t3code/no-unreasoned-state-sync -- holds the native launch splash, which is outside React, until the rows are gone or this list unmounts; a layout effect runs before the splash hides in this commit |
| 623 | useLayoutEffect(() => { |
| 624 | return hasRowsToLayOut ? holdLaunchSplashForHomeRows() : undefined; |
| 625 | }, [hasRowsToLayOut]); |
| 626 | const selectedEnvironmentLabel = |
| 627 | props.selectedEnvironmentId === null |
| 628 | ? null |
| 629 | : (props.savedConnectionsById[props.selectedEnvironmentId]?.environmentLabel ?? |
| 630 | "this environment"); |
| 631 | const emptyState = deriveHomeEmptyState(props.catalogState, props.projects.length, m); |
| 632 | |
| 633 | if (!hasAnyThreads) { |
| 634 | return ( |
| 635 | <View className={containerClassName}> |
| 636 | <ScreenContent surface={sidebarSurface ? "drawer" : "screen"}> |
| 637 | <View |
| 638 | className={cn( |
| 639 | "flex-1 items-center justify-center px-8", |
| 640 | Platform.OS !== "android" && contentBackground, |
| 641 | )} |
| 642 | style={{ |
| 643 | paddingBottom: |
| 644 | Platform.OS === "ios" ? iosBottomClearance : Math.max(insets.bottom, 24), |
| 645 | paddingTop: emptyTopInset, |
| 646 | paddingLeft: 32 + (contentSideInsets?.left ?? 0), |
| 647 | paddingRight: 32 + (contentSideInsets?.right ?? 0), |
| 648 | }} |
| 649 | > |
| 650 | <View className="w-full max-w-[430px]"> |
| 651 | <EmptyState |
| 652 | title={emptyState.title} |
| 653 | detail={emptyState.detail} |
| 654 | actionLabel={emptyState.showsAddEnvironment ? gt("Add environment") : undefined} |
| 655 | onAction={emptyState.showsAddEnvironment ? props.onAddConnection : undefined} |
| 656 | action={ |
| 657 | Platform.OS === "android" && emptyState.showsAddEnvironment ? ( |
| 658 | <FloatingActionButton |
| 659 | label={gt("Add environment")} |
| 660 | icon="plus" |
| 661 | variant="extended" |
| 662 | tone="primary" |
| 663 | onPress={props.onAddConnection} |
| 664 | /> |
| 665 | ) : undefined |
| 666 | } |
| 667 | variant="plain" |
| 668 | /> |
| 669 | {emptyState.loading ? ( |
| 670 | <View className="mt-4 items-center"> |
| 671 | <AppActivityIndicator colorClassName="accent-icon-muted" /> |
| 672 | </View> |
| 673 | ) : null} |
| 674 | </View> |
| 675 | </View> |
| 676 | </ScreenContent> |
| 677 | </View> |
| 678 | ); |
| 679 | } |
| 680 | |
| 681 | const listHeader = Platform.OS === "ios" ? undefined : <HomeTopContentSpacer />; |
| 682 | |
| 683 | const v2ListHeader = ( |
| 684 | <> |
| 685 | {listHeader} |
| 686 | {Platform.OS === "ios" && workspace ? ( |
| 687 | <ThreadListFilters |
| 688 | gutter={sidebarSurface ? 8 : 0} |
| 689 | environments={props.environments} |
| 690 | projects={pickableProjectGroups( |
| 691 | sortHomeProjectScopes({ |
| 692 | scopes: projectScopes, |
| 693 | threads: props.threads, |
| 694 | pendingTasks: [], |
| 695 | projectSortOrder: "updated_at", |
| 696 | }), |
| 697 | ).map((scope) => ({ |
| 698 | key: scope.key, |
| 699 | label: scope.title, |
| 700 | }))} |
| 701 | selectedEnvironmentId={props.selectedEnvironmentId} |
| 702 | selectedProjectKey={props.selectedProjectKey} |
| 703 | onEnvironmentChange={props.onEnvironmentChange} |
| 704 | onProjectChange={props.onProjectChange} |
| 705 | /> |
| 706 | ) : null} |
| 707 | </> |
| 708 | ); |
| 709 | |
| 710 | const v2ListEmpty = |
| 711 | hasSearchQuery && threadSearch.isPending ? ( |
| 712 | <EmptyState |
| 713 | title={gt("Searching thread messages…")} |
| 714 | detail={gt('Looking for "{query}" in messages.', { query: props.searchQuery })} |
| 715 | variant={Platform.OS === "android" ? "plain" : undefined} |
| 716 | /> |
| 717 | ) : hasSearchQuery && threadSearch.failed ? ( |
| 718 | <EmptyState |
| 719 | title={gt("Search failed")} |
| 720 | detail={gt('Couldn\'t search messages for "{query}". Change the search to try again.', { |
| 721 | query: props.searchQuery, |
| 722 | })} |
| 723 | variant={Platform.OS === "android" ? "plain" : undefined} |
| 724 | /> |
| 725 | ) : hasSearchQuery ? ( |
| 726 | <EmptyState |
| 727 | title={gt("No results")} |
| 728 | detail={gt('No threads matching "{query}".', { query: props.searchQuery })} |
| 729 | variant={Platform.OS === "android" ? "plain" : undefined} |
| 730 | /> |
| 731 | ) : v2ScopedProjectGroup !== null ? ( |
| 732 | <EmptyState |
| 733 | title={gt("No threads in {name}", { name: v2ScopedProjectGroup.title })} |
| 734 | detail={gt("Choose another project or create a new task.")} |
| 735 | variant={Platform.OS === "android" ? "plain" : undefined} |
| 736 | /> |
| 737 | ) : selectedEnvironmentLabel ? ( |
| 738 | <EmptyState |
| 739 | title={gt("No threads in {name}", { name: selectedEnvironmentLabel })} |
| 740 | detail={gt("Choose another environment or create a new task.")} |
| 741 | variant={Platform.OS === "android" ? "plain" : undefined} |
| 742 | /> |
| 743 | ) : ( |
| 744 | <EmptyState |
| 745 | title={gt("No threads yet")} |
| 746 | detail={gt("Create a task to start a new coding session.")} |
| 747 | variant={Platform.OS === "android" ? "plain" : undefined} |
| 748 | /> |
| 749 | ); |
| 750 | |
| 751 | if (Platform.OS === "android" && threadListV2Items.length === 0) { |
| 752 | return ( |
| 753 | <ScreenContent surface="screen"> |
| 754 | <View |
| 755 | className="flex-1 items-center justify-center px-4" |
| 756 | style={{ paddingBottom: insets.bottom }} |
| 757 | > |
| 758 | {v2ListEmpty} |
| 759 | </View> |
| 760 | </ScreenContent> |
| 761 | ); |
| 762 | } |
| 763 | |
| 764 | return ( |
| 765 | <View className={containerClassName}> |
| 766 | <ScreenContent surface="screen"> |
| 767 | <View className={cn("flex-1", Platform.OS !== "android" && contentBackground)}> |
| 768 | <SwipeableScrollGateProvider enabled={swipeEnabled} activation={swipeRowActivation}> |
| 769 | <GestureDetector gesture={homeScrollGesture}> |
| 770 | <AnimatedLegendList |
| 771 | itemLayoutAnimation={THREAD_LIST_ROW_LAYOUT} |
| 772 | ref={listRef} |
| 773 | onLoad={() => { |
| 774 | settleLaunchSplashHomeRows(); |
| 775 | activateVisibleRows(threadListV2Items); |
| 776 | }} |
| 777 | onTouchStart={(event) => trackListTouches(event, true)} |
| 778 | onTouchEnd={(event) => trackListTouches(event, false)} |
| 779 | onTouchCancel={(event) => trackListTouches(event, false)} |
| 780 | renderScrollComponent={Platform.OS === "ios" ? renderHomeScrollView : undefined} |
| 781 | data={threadListV2Items} |
| 782 | renderItem={renderV2Item} |
| 783 | keyExtractor={v2KeyExtractor} |
| 784 | getItemType={(item) => item.type} |
| 785 | itemsAreEqual={threadListV2ListItemsAreEqual} |
| 786 | estimatedItemSize={ESTIMATED_THREAD_LIST_V2_ROW_HEIGHT} |
| 787 | drawDistance={THREAD_LIST_V2_DRAW_DISTANCE} |
| 788 | recycleItems |
| 789 | extraData={v2ExtraData} |
| 790 | ListHeaderComponent={v2ListHeader} |
| 791 | // Lifted above the rows so an iOS filter menu's dismissal morph is not covered. |
| 792 | ListHeaderComponentStyle={{ zIndex: 1 }} |
| 793 | ListFooterComponent={ |
| 794 | settledShelfExpanded && threadListV2Layout.hiddenSettledCount > 0 ? ( |
| 795 | <ThreadListV2ShowMoreRow |
| 796 | hiddenCount={threadListV2Layout.hiddenSettledCount} |
| 797 | {...(olderSettledShelf.loading |
| 798 | ? { state: "loading" as const } |
| 799 | : olderSettledShelf.failed |
| 800 | ? { state: "failed" as const } |
| 801 | : {})} |
| 802 | onPress={() => { |
| 803 | olderSettledShelf.retry(); |
| 804 | showMoreSettled(); |
| 805 | }} |
| 806 | /> |
| 807 | ) : null |
| 808 | } |
| 809 | ListEmptyComponent={v2ListEmpty} |
| 810 | style={{ flex: 1 }} |
| 811 | automaticallyAdjustsScrollIndicatorInsets={Platform.OS === "ios"} |
| 812 | contentInsetAdjustmentBehavior="never" |
| 813 | contentInset={Platform.OS === "ios" ? { top: contentTopInset } : undefined} |
| 814 | initialScrollOffset={ |
| 815 | initialScrollOffset > 0 ? initialScrollOffset - contentTopInset : undefined |
| 816 | } |
| 817 | showsVerticalScrollIndicator={false} |
| 818 | keyboardDismissMode="on-drag" |
| 819 | keyboardShouldPersistTaps="handled" |
| 820 | {...scrollGateHandlers} |
| 821 | scrollEventThrottle={16} |
| 822 | contentContainerStyle={{ |
| 823 | paddingLeft: |
| 824 | (contentSideInsets?.left ?? 0) + (sidebarSurface ? 8 : 0) + androidListGutter, |
| 825 | paddingRight: |
| 826 | (contentSideInsets?.right ?? 0) + (sidebarSurface ? 8 : 0) + androidListGutter, |
| 827 | paddingBottom: |
| 828 | Platform.OS === "ios" |
| 829 | ? iosBottomClearance |
| 830 | : (workspace?.bottomClearance ?? |
| 831 | Math.max(insets.bottom, 16) + |
| 832 | (Platform.OS === "android" ? fabClearance : 88)), |
| 833 | }} |
| 834 | /> |
| 835 | </GestureDetector> |
| 836 | </SwipeableScrollGateProvider> |
| 837 | </View> |
| 838 | </ScreenContent> |
| 839 | </View> |
| 840 | ); |
| 841 | } |