The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 04:45 UTC
| 1 | import { usePaneRole } from "../../components/ui/Pane"; |
| 2 | import { Button } from "../../components/ui/Button"; |
| 3 | import { |
| 4 | applyPullRequestDraftFilterChange, |
| 5 | parsePullRequestQuery, |
| 6 | } from "@t3tools/client-runtime/pull-request-query"; |
| 7 | import { AppActivityIndicator } from "../../components/ui/AppActivityIndicator"; |
| 8 | import { LegendList, type LegendListRef } from "@legendapp/list/react-native"; |
| 9 | import { useNavigation } from "@react-navigation/native"; |
| 10 | import { useGT } from "gt-react-native"; |
| 11 | import { use, useMemo, useRef, useState, type ReactNode } from "react"; |
| 12 | import { useAtomValue } from "@effect/atom-react"; |
| 13 | import { Keyboard, Platform, Pressable, View } from "react-native"; |
| 14 | import { RefreshControl } from "../../components/ui/RefreshControl"; |
| 15 | import { NativeScrollView } from "../../native/NativeScrollViewMarker"; |
| 16 | import { useSafeAreaInsets } from "react-native-safe-area-context"; |
| 17 | |
| 18 | import { AppText as Text } from "../../components/ui/AppText"; |
| 19 | import { useContentTopInset } from "../../components/ui/useBarInset"; |
| 20 | import { SymbolView } from "../../components/ui/AppSymbol"; |
| 21 | import { EmptyState } from "../../components/ui/EmptyState"; |
| 22 | import { NativePrimaryColumnContext } from "../../native/v5-workspace-context"; |
| 23 | import { useNativeColumnLayoutMetrics } from "../../native/native-layout-metrics"; |
| 24 | import { useWorkspaceState } from "../../state/workspace"; |
| 25 | import { useProjects } from "../../state/entities"; |
| 26 | import { environmentServerConfigsAtom } from "../../state/server"; |
| 27 | import { PullRequestListRow } from "./PullRequestListRow"; |
| 28 | import { |
| 29 | PullRequestListFilters, |
| 30 | usePullRequestListFilters, |
| 31 | type PullRequestFilterMenu, |
| 32 | } from "./PullRequestListFilters"; |
| 33 | import { SavedViewsMenu } from "../layout/workspace/SavedViewsMenu"; |
| 34 | import { pullRequestViewMatches } from "./pullRequestSavedViews"; |
| 35 | import { usePullRequestList } from "./usePullRequestList"; |
| 36 | import { usePullRequestListControls } from "./usePullRequestListControls"; |
| 37 | import { usePullRequestListActions } from "./usePullRequestListActions"; |
| 38 | import { |
| 39 | filterPullRequestList, |
| 40 | pullRequestListFacets, |
| 41 | pullRequestListInput, |
| 42 | pullRequestListRowsAreEqual, |
| 43 | scopedPullRequestListKey as pullRequestKey, |
| 44 | type EnvironmentPullRequestListEntry, |
| 45 | withoutScratchProjectScope, |
| 46 | pullRequestProjectChoices, |
| 47 | } from "./pullRequestList"; |
| 48 | import { useDebouncedValue } from "../../state/queries"; |
| 49 | import { useWorkspaceTabs } from "../layout/workspace/WorkspaceTabsContext"; |
| 50 | import { useSearchScrollRestoration } from "../layout/workspace/useSearchScrollRestoration"; |
| 51 | import { useSelectWorkspaceDetail } from "../layout/workspace/useSelectWorkspaceDetail"; |
| 52 | |
| 53 | function PullRequestListError(props: { readonly message: string }) { |
| 54 | const [expanded, setExpanded] = useState(false); |
| 55 | return ( |
| 56 | <Pressable |
| 57 | accessibilityRole="button" |
| 58 | accessibilityLabel={props.message} |
| 59 | accessibilityState={{ expanded }} |
| 60 | onPress={() => setExpanded((value) => !value)} |
| 61 | // A long press selects the text; collapsing under that selection crashes Android's |
| 62 | // smart-select animation, so it must not also count as a press. |
| 63 | onLongPress={() => undefined} |
| 64 | className="min-h-11 flex-row items-center gap-3 px-4 py-2.5 active:opacity-70" |
| 65 | > |
| 66 | <SymbolView |
| 67 | name="exclamationmark.triangle" |
| 68 | size={18} |
| 69 | tintColorClassName="accent-icon-muted" |
| 70 | /> |
| 71 | <Text |
| 72 | selectable={expanded} |
| 73 | numberOfLines={expanded ? undefined : 1} |
| 74 | ellipsizeMode="tail" |
| 75 | className="min-w-0 flex-1 text-sm text-foreground-muted" |
| 76 | > |
| 77 | {props.message} |
| 78 | </Text> |
| 79 | <SymbolView |
| 80 | name={expanded ? "chevron.up" : "chevron.down"} |
| 81 | size={11} |
| 82 | tintColorClassName="accent-icon-muted" |
| 83 | /> |
| 84 | </Pressable> |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | export function PullRequestsScreen( |
| 89 | props: { |
| 90 | readonly sidebar?: boolean; |
| 91 | readonly selectedPullRequestKey?: string | null; |
| 92 | /** |
| 93 | * Places the list under a header that opens the full filter menu (the Android toolbar). The |
| 94 | * filters live here, so the header gets them in the same render as the list. |
| 95 | */ |
| 96 | readonly renderFrame?: (filters: PullRequestFilterMenu | null, list: ReactNode) => ReactNode; |
| 97 | } = {}, |
| 98 | ) { |
| 99 | const navigation = useNavigation(); |
| 100 | const gt = useGT(); |
| 101 | const primaryColumn = use(NativePrimaryColumnContext); |
| 102 | const workspace = useWorkspaceTabs()!; |
| 103 | const { environments } = useWorkspaceState(); |
| 104 | const configs = useAtomValue(environmentServerConfigsAtom); |
| 105 | const insets = useSafeAreaInsets(); |
| 106 | const contentTopInset = useContentTopInset(); |
| 107 | // UIKit insets the list by its column's safe area, which this context can report as 0. |
| 108 | const columnSafeBottom = useNativeColumnLayoutMetrics()?.safeArea.bottom; |
| 109 | const bottomSafeArea = |
| 110 | Platform.OS === "ios" ? (columnSafeBottom ?? insets.bottom) : insets.bottom; |
| 111 | const listRef = useRef<LegendListRef>(null); |
| 112 | const tab = workspace.state.tabs.pullRequests; |
| 113 | const [initialScrollOffset] = useState(() => workspace.getScrollOffset("pullRequests")); |
| 114 | useSearchScrollRestoration(tab.searchMode, () => { |
| 115 | void listRef.current?.scrollToOffset({ |
| 116 | offset: tab.scrollOffset - contentTopInset, |
| 117 | animated: false, |
| 118 | }); |
| 119 | }); |
| 120 | const maintainVisiblePosition = { |
| 121 | data: tab.searchMode === "resting", |
| 122 | shouldRestorePosition: () => workspace.getScrollOffset("pullRequests") > 0, |
| 123 | }; |
| 124 | const debouncedQuery = useDebouncedValue(tab.query.trim(), 180); |
| 125 | const query = tab.searchMode === "resting" ? "" : debouncedQuery; |
| 126 | const typedQuery = tab.searchMode === "resting" ? "" : tab.query; |
| 127 | const typedDraft = parsePullRequestQuery(typedQuery).filters.draft; |
| 128 | const listControls = usePullRequestListControls(); |
| 129 | const selectedEnvironmentId = listControls.environmentId; |
| 130 | const environmentId = environments.some((item) => item.environmentId === selectedEnvironmentId) |
| 131 | ? selectedEnvironmentId |
| 132 | : null; |
| 133 | const selectDetail = useSelectWorkspaceDetail(); |
| 134 | const selectPullRequest = (item: EnvironmentPullRequestListEntry) => { |
| 135 | Keyboard.dismiss(); |
| 136 | const params = { |
| 137 | environmentId: String(item.environmentId), |
| 138 | projectId: String(item.projectId), |
| 139 | repository: item.repository, |
| 140 | number: item.number, |
| 141 | host: item.host, |
| 142 | initialSummary: item, |
| 143 | }; |
| 144 | selectDetail({ name: "PullRequest", params }); |
| 145 | }; |
| 146 | const allProjects = useProjects(); |
| 147 | const savedControls = listControls.controlsFor(environmentId); |
| 148 | const controls = useMemo( |
| 149 | () => withoutScratchProjectScope(savedControls, allProjects), |
| 150 | [allProjects, savedControls], |
| 151 | ); |
| 152 | const targets = useMemo( |
| 153 | () => |
| 154 | environments |
| 155 | .filter( |
| 156 | (environment) => |
| 157 | configs.get(environment.environmentId) !== undefined && |
| 158 | (environmentId === null || environment.environmentId === environmentId) && |
| 159 | (!controls.projectEnvironmentId || |
| 160 | environment.environmentId === controls.projectEnvironmentId), |
| 161 | ) |
| 162 | .sort((left, right) => left.environmentId.localeCompare(right.environmentId)) |
| 163 | .map((environment) => ({ |
| 164 | environmentId: environment.environmentId, |
| 165 | input: pullRequestListInput(controls, query), |
| 166 | })), |
| 167 | [environments, environmentId, configs, controls, query], |
| 168 | ); |
| 169 | const result = usePullRequestList(targets); |
| 170 | const connectedTargets = targets.filter((target) => |
| 171 | environments.some( |
| 172 | (environment) => |
| 173 | environment.environmentId === target.environmentId && |
| 174 | environment.connectionState === "connected", |
| 175 | ), |
| 176 | ); |
| 177 | const { actions, select, busyKey } = usePullRequestListActions( |
| 178 | connectedTargets.map((target) => target.environmentId), |
| 179 | result.updateEntry, |
| 180 | ); |
| 181 | const paneRole = usePaneRole(); |
| 182 | const sidebar = props.sidebar === true || (primaryColumn !== null && paneRole === "sidebar"); |
| 183 | const selectedKey = props.selectedPullRequestKey ?? primaryColumn?.selectedPullRequestKey; |
| 184 | const connectedKey = connectedTargets.map((target) => target.environmentId).join("\n"); |
| 185 | const entries = useMemo( |
| 186 | () => |
| 187 | filterPullRequestList( |
| 188 | result.entries, |
| 189 | controls, |
| 190 | result.data?.viewers ?? {}, |
| 191 | query, |
| 192 | result.data.localSearchHosts, |
| 193 | new Set(connectedKey.split("\n")), |
| 194 | ), |
| 195 | [result.entries, controls, query, result.data, connectedKey], |
| 196 | ); |
| 197 | const facets = pullRequestListFacets(result.entries); |
| 198 | const [knownFacets, setKnownFacets] = useState({ environmentId, source: facets, ...facets }); |
| 199 | if (knownFacets.environmentId !== environmentId || knownFacets.source !== facets) { |
| 200 | setKnownFacets((previous) => ({ |
| 201 | source: facets, |
| 202 | environmentId, |
| 203 | authors: [ |
| 204 | ...new Set([ |
| 205 | ...(previous.environmentId === environmentId ? previous.authors : []), |
| 206 | ...facets.authors, |
| 207 | ]), |
| 208 | ].sort(), |
| 209 | labels: [ |
| 210 | ...new Set([ |
| 211 | ...(previous.environmentId === environmentId ? previous.labels : []), |
| 212 | ...facets.labels, |
| 213 | ]), |
| 214 | ].sort(), |
| 215 | labelColors: { |
| 216 | ...facets.labelColors, |
| 217 | ...(previous.environmentId === environmentId ? previous.labelColors : {}), |
| 218 | }, |
| 219 | })); |
| 220 | } |
| 221 | const labelColors = |
| 222 | knownFacets.environmentId === environmentId ? knownFacets.labelColors : facets.labelColors; |
| 223 | const projects = pullRequestProjectChoices(allProjects, { |
| 224 | environmentId, |
| 225 | controls, |
| 226 | environmentLabels: new Map( |
| 227 | environments.map((item) => [item.environmentId, item.environmentLabel]), |
| 228 | ), |
| 229 | servesPullRequests: (id) => configs.get(id)?.environment.capabilities.pullRequests === true, |
| 230 | }); |
| 231 | const filters = usePullRequestListFilters({ |
| 232 | environments: environments.map((item) => ({ |
| 233 | id: String(item.environmentId), |
| 234 | label: item.environmentLabel, |
| 235 | })), |
| 236 | environmentId, |
| 237 | onEnvironmentChange: listControls.setEnvironmentId, |
| 238 | value: typedDraft |
| 239 | ? { ...controls, filters: { ...controls.filters, draft: typedDraft } } |
| 240 | : controls, |
| 241 | onChange: (next) => { |
| 242 | const update = applyPullRequestDraftFilterChange(typedQuery, controls.filters, next.filters); |
| 243 | listControls.setControls(environmentId, { ...next, filters: update.filters }); |
| 244 | if (update.query !== typedQuery) |
| 245 | workspace.dispatch({ type: "query", tab: "pullRequests", query: update.query }); |
| 246 | void listRef.current?.scrollToOffset({ |
| 247 | offset: -contentTopInset, |
| 248 | animated: false, |
| 249 | }); |
| 250 | }, |
| 251 | authors: knownFacets.environmentId === environmentId ? knownFacets.authors : facets.authors, |
| 252 | labels: knownFacets.environmentId === environmentId ? knownFacets.labels : facets.labels, |
| 253 | labelColors, |
| 254 | projects, |
| 255 | onSearch: (kind) => |
| 256 | navigation.navigate("PullRequestFilterSearch", { |
| 257 | kind, |
| 258 | environmentId, |
| 259 | environmentIds: targets.map((target) => String(target.environmentId)), |
| 260 | suggestions: knownFacets.environmentId === environmentId ? knownFacets[kind] : facets[kind], |
| 261 | labelColors, |
| 262 | }), |
| 263 | }); |
| 264 | const renderPullRequest = ({ item }: { readonly item: EnvironmentPullRequestListEntry }) => ( |
| 265 | <PullRequestListRow |
| 266 | item={item} |
| 267 | onSelect={selectPullRequest} |
| 268 | getActions={actions} |
| 269 | onAction={select} |
| 270 | busy={busyKey === pullRequestKey(item)} |
| 271 | selected={selectedKey === pullRequestKey(item)} |
| 272 | sidebar={sidebar} |
| 273 | /> |
| 274 | ); |
| 275 | const list = ( |
| 276 | <View className={sidebar ? "flex-1 bg-drawer" : "flex-1 bg-screen"}> |
| 277 | <LegendList |
| 278 | ref={listRef} |
| 279 | data={entries} |
| 280 | extraData={`${selectedKey}:${busyKey}`} |
| 281 | keyExtractor={pullRequestKey} |
| 282 | itemsAreEqual={pullRequestListRowsAreEqual} |
| 283 | estimatedItemSize={88} |
| 284 | drawDistance={1_000} |
| 285 | maintainVisibleContentPosition={result.continued ? maintainVisiblePosition : false} |
| 286 | onEndReached={() => void result.loadMore()} |
| 287 | onEndReachedThreshold={0.5} |
| 288 | onViewableItemsChanged={result.onViewableItemsChanged} |
| 289 | refreshControl={ |
| 290 | <RefreshControl |
| 291 | refreshing={result.refreshing} |
| 292 | onRefresh={() => void result.onRefresh()} |
| 293 | // The list already accounts for the navigation header in its content inset. |
| 294 | progressViewOffset={Platform.OS === "ios" ? 60 : undefined} |
| 295 | /> |
| 296 | } |
| 297 | alwaysBounceVertical |
| 298 | // Android flashes a list's scrollbar whenever it becomes visible, which was every return |
| 299 | // to this tab. The thread list shows none either. |
| 300 | showsVerticalScrollIndicator={Platform.OS !== "android"} |
| 301 | recycleItems |
| 302 | renderScrollComponent={Platform.OS === "ios" ? NativeScrollView : undefined} |
| 303 | contentInsetAdjustmentBehavior="never" |
| 304 | contentInset={Platform.OS === "ios" ? { top: contentTopInset } : undefined} |
| 305 | // A plain offset: LegendList adds the list header to an index-based restore, which |
| 306 | // landed every return one header lower. |
| 307 | initialScrollOffset={ |
| 308 | initialScrollOffset > 0 ? initialScrollOffset - contentTopInset : undefined |
| 309 | } |
| 310 | contentContainerStyle={{ |
| 311 | paddingBottom: Math.max(bottomSafeArea, workspace.bottomClearance), |
| 312 | }} |
| 313 | onScroll={(event) => { |
| 314 | workspace.recordScrollOffset( |
| 315 | "pullRequests", |
| 316 | Math.max(0, event.nativeEvent.contentOffset.y + contentTopInset), |
| 317 | ); |
| 318 | }} |
| 319 | keyboardDismissMode="on-drag" |
| 320 | keyboardShouldPersistTaps="handled" |
| 321 | renderItem={renderPullRequest} |
| 322 | // Lifted above the rows so an iOS filter menu's dismissal morph is not covered. |
| 323 | ListHeaderComponentStyle={{ zIndex: 1 }} |
| 324 | ListHeaderComponent={ |
| 325 | <> |
| 326 | <PullRequestListFilters |
| 327 | sidebar={sidebar} |
| 328 | savedViewsMenu={ |
| 329 | <SavedViewsMenu |
| 330 | read={(preferences) => preferences.pullRequestSavedViews} |
| 331 | write={(pullRequestSavedViews) => ({ pullRequestSavedViews })} |
| 332 | list="pullRequest" |
| 333 | suggestedName={ |
| 334 | controls.involvement === "reviewing" ? gt("PRs Requesting My Review") : "" |
| 335 | } |
| 336 | matches={pullRequestViewMatches} |
| 337 | scope={{ |
| 338 | environmentId, |
| 339 | controls, |
| 340 | query: tab.searchMode === "resting" ? "" : tab.query, |
| 341 | }} |
| 342 | onApply={async (view) => { |
| 343 | await listControls.applyView(view); |
| 344 | workspace.dispatch( |
| 345 | view.query.trim() |
| 346 | ? { type: "search", tab: "pullRequests" } |
| 347 | : { type: "cancel", tab: "pullRequests", scrollToTop: true }, |
| 348 | ); |
| 349 | workspace.dispatch({ |
| 350 | type: "query", |
| 351 | tab: "pullRequests", |
| 352 | query: view.query.trim(), |
| 353 | }); |
| 354 | void listRef.current?.scrollToOffset({ |
| 355 | offset: Platform.OS === "ios" ? -insets.top : 0, |
| 356 | animated: false, |
| 357 | }); |
| 358 | }} |
| 359 | /> |
| 360 | } |
| 361 | filters={filters} |
| 362 | /> |
| 363 | {result.data?.errors.length ? ( |
| 364 | <View key={JSON.stringify(targets)}> |
| 365 | {result.data.errors.map((error) => ( |
| 366 | <PullRequestListError |
| 367 | key={`${error.environmentId} ${error.projectTitle} ${error.message}`} |
| 368 | message={`${error.projectTitle || environments.find((environment) => environment.environmentId === error.environmentId)?.environmentLabel || gt("Environment", { $context: "fallback name for a server" })}: ${error.message}`} |
| 369 | /> |
| 370 | ))} |
| 371 | </View> |
| 372 | ) : null} |
| 373 | </> |
| 374 | } |
| 375 | ListFooterComponent={ |
| 376 | result.loadingMore ? ( |
| 377 | <View className="items-center py-5"> |
| 378 | <AppActivityIndicator |
| 379 | colorClassName="accent-icon" |
| 380 | accessibilityLabel={gt("Loading more pull requests")} |
| 381 | /> |
| 382 | </View> |
| 383 | ) : result.loadMoreError ? ( |
| 384 | <View className="items-center gap-2 px-8 py-4"> |
| 385 | <Text className="text-xs text-foreground-muted">{result.loadMoreError}</Text> |
| 386 | <Button |
| 387 | label={gt("Try again")} |
| 388 | accessibilityLabel={gt("Retry loading more pull requests")} |
| 389 | onPress={() => void result.loadMore(true)} |
| 390 | /> |
| 391 | </View> |
| 392 | ) : result.hasMore ? ( |
| 393 | // A filtered page can be too short to scroll, which leaves nothing to reach the end of. |
| 394 | <View className="items-center py-4"> |
| 395 | <Button |
| 396 | label={gt("Load more")} |
| 397 | accessibilityLabel={gt("Load more pull requests")} |
| 398 | onPress={() => void result.loadMore()} |
| 399 | /> |
| 400 | </View> |
| 401 | ) : undefined |
| 402 | } |
| 403 | ListEmptyComponent={ |
| 404 | result.isPending && connectedTargets.length > 0 ? ( |
| 405 | <View className="items-center py-12"> |
| 406 | <AppActivityIndicator colorClassName="accent-icon" /> |
| 407 | </View> |
| 408 | ) : ( |
| 409 | <View className="px-8 py-12"> |
| 410 | <EmptyState |
| 411 | title={ |
| 412 | result.error |
| 413 | ? gt("Couldn't load pull requests") |
| 414 | : targets.length && connectedTargets.length === 0 |
| 415 | ? gt("Environment offline") |
| 416 | : targets.length |
| 417 | ? query |
| 418 | ? gt("No results") |
| 419 | : gt("No matching pull requests") |
| 420 | : gt("Connect an environment") |
| 421 | } |
| 422 | detail={ |
| 423 | result.error ?? |
| 424 | (targets.length && connectedTargets.length === 0 |
| 425 | ? gt("Reconnect to load pull requests that aren't saved on this device.") |
| 426 | : targets.length |
| 427 | ? gt("Pull requests from the selected environments' projects appear here.") |
| 428 | : gt("Add an environment in Settings to browse its pull requests.")) |
| 429 | } |
| 430 | variant="plain" |
| 431 | /> |
| 432 | </View> |
| 433 | ) |
| 434 | } |
| 435 | /> |
| 436 | </View> |
| 437 | ); |
| 438 | return props.renderFrame ? props.renderFrame(filters.headerMenu, list) : list; |
| 439 | } |