Skip to content

Commit 05b84f7

Browse files
refactor(mobile): pull request list, filters and viewed files without synchronizing effects
The mobile pull request screens kept state in step with effects: the list copied its query head into the pages atom and its stats answers into the stats atom, tracked the active scope in a layout effect to abort loads, the Android filter menu was published from the list to the header through a layout effect, viewed-file presses were reconciled with server reads in four effects over refs, and the reviewer list scrolled to the top whenever its candidates changed. - usePullRequestList: the head is merged in the sources atom's read; stats answers land through the listStats query's onAnswer (new hook in client-runtime); page loads and refreshes are per-scope Atom.fn families, so leaving a scope interrupts them. 3 effects -> 0. - PullRequestsScreen builds the filters and hands the header menu to a renderFrame prop, so the Android toolbar reads it in the same render. PullRequestFilterMenuContext is deleted. 1 effect -> 0. - usePullRequestFilesViewed keeps presses (version, read-at-write) in state and derives the overlay in render (pullRequestFilesViewed.ts, tested). The head-change refresh moves to the detail query's onAnswer. 4 -> 0. - PullRequestReviewersScreen scrolls to the top from the search handler. 1 -> 0. - PullRequestToolbar.android drops its navigation-state subscription; the back handler already checks focus when pressed. Its effect stays: it registers a native listener for as long as search is open. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 278588a commit 05b84f7

13 files changed

Lines changed: 480 additions & 392 deletions

‎apps/mobile/src/features/pull-requests/PullRequestFilterMenuContext.tsx‎

Lines changed: 0 additions & 50 deletions
This file was deleted.

‎apps/mobile/src/features/pull-requests/PullRequestListFilters.tsx‎

Lines changed: 55 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { useMemo } from "react";
33
import { Platform, ScrollView } from "react-native";
44
import type { MenuProps } from "../../components/ui/Menu.types";
55
import { ListFilterMenu } from "../workspace/ListFilterMenu";
6-
import { usePublishPullRequestFilterMenu } from "./PullRequestFilterMenuContext";
76
import { useProjects, useThreadShells } from "../../state/entities";
87
import { useMobileProjectGroupingSettings } from "../../state/project-grouping";
98
import { buildHomeProjectScopes, sortHomeProjectScopes } from "../home/homeThreadList";
@@ -62,9 +61,18 @@ function useProjectsInSidebarOrder<Project extends { id: string; environmentId:
6261
return useMemo(() => sortProjectsByGroupOrder(projects, scopes), [projects, scopes]);
6362
}
6463

65-
export function PullRequestListFilters(props: {
66-
/** Android sidebar rows sit further in than full-width rows; the pills follow them. */
67-
readonly sidebar?: boolean;
64+
export interface PullRequestFilterMenu {
65+
readonly actions: MenuProps["actions"];
66+
readonly onSelect: (id: string) => void;
67+
/** Whether any filter, the environment, or a non-default sort is active. */
68+
readonly customized: boolean;
69+
}
70+
71+
/**
72+
* Builds the filter pills and, on Android, the header's full filter menu. The list screen owns
73+
* the call so both the pills and the header it frames read the same menu.
74+
*/
75+
export function usePullRequestListFilters(props: {
6876
readonly environments: readonly { id: string; label: string }[];
6977
readonly environmentId: string | null;
7078
readonly onEnvironmentChange: (id: string | null) => void;
@@ -280,7 +288,7 @@ export function PullRequestListFilters(props: {
280288
];
281289
const customized = count > 0 || value.sort !== "updated" || props.environmentId !== null;
282290
// The Android header's filter button opens every filter.
283-
const headerMenu =
291+
const headerMenu: PullRequestFilterMenu | null =
284292
Platform.OS === "android"
285293
? {
286294
actions: [
@@ -296,7 +304,35 @@ export function PullRequestListFilters(props: {
296304
customized,
297305
}
298306
: null;
299-
usePublishPullRequestFilterMenu(headerMenu);
307+
return {
308+
headerMenu,
309+
select,
310+
count,
311+
more,
312+
reset,
313+
environmentLabel:
314+
props.environments.length > 1
315+
? (props.environments.find((environment) => environment.id === props.environmentId)
316+
?.label ?? "All environments")
317+
: null,
318+
environmentActions,
319+
state: value.state,
320+
stateActions,
321+
involvement: value.involvement,
322+
involvementActions,
323+
selectedLabelCount: selectedLabels.length,
324+
labelActions,
325+
author,
326+
authorActions,
327+
};
328+
}
329+
330+
export function PullRequestListFilters(props: {
331+
/** Android sidebar rows sit further in than full-width rows; the pills follow them. */
332+
readonly sidebar?: boolean;
333+
readonly filters: ReturnType<typeof usePullRequestListFilters>;
334+
}) {
335+
const { count, more, reset, select, author, selectedLabelCount } = props.filters;
300336
return (
301337
<ScrollView
302338
horizontal
@@ -319,43 +355,40 @@ export function PullRequestListFilters(props: {
319355
onSelect={select}
320356
/>
321357
)}
322-
{props.environments.length > 1 ? (
358+
{props.filters.environmentLabel !== null ? (
323359
<ListFilterMenu
324-
label={
325-
props.environments.find((environment) => environment.id === props.environmentId)
326-
?.label ?? "All environments"
327-
}
360+
label={props.filters.environmentLabel}
328361
accessibilityLabel="Choose pull request environment"
329-
actions={environmentActions}
362+
actions={props.filters.environmentActions}
330363
onSelect={select}
331364
/>
332365
) : null}
333366
<ListFilterMenu
334-
label={STATES[value.state]}
335-
active={value.state !== "open"}
367+
label={STATES[props.filters.state]}
368+
active={props.filters.state !== "open"}
336369
accessibilityLabel="Filter by state"
337-
actions={stateActions}
370+
actions={props.filters.stateActions}
338371
onSelect={select}
339372
/>
340373
<ListFilterMenu
341-
label={INVOLVEMENT[value.involvement]}
342-
active={value.involvement !== "all"}
374+
label={INVOLVEMENT[props.filters.involvement]}
375+
active={props.filters.involvement !== "all"}
343376
accessibilityLabel="Filter by involvement"
344-
actions={involvementActions}
377+
actions={props.filters.involvementActions}
345378
onSelect={select}
346379
/>
347380
<ListFilterMenu
348-
label={selectedLabels.length ? `Labels ${selectedLabels.length}` : "Labels"}
349-
active={selectedLabels.length > 0}
381+
label={selectedLabelCount ? `Labels ${selectedLabelCount}` : "Labels"}
382+
active={selectedLabelCount > 0}
350383
accessibilityLabel="Filter by label"
351-
actions={labelActions}
384+
actions={props.filters.labelActions}
352385
onSelect={select}
353386
/>
354387
<ListFilterMenu
355388
label={author === "me" ? "By you" : (author ?? "Author")}
356389
active={Boolean(author)}
357390
accessibilityLabel="Filter by author"
358-
actions={authorActions}
391+
actions={props.filters.authorActions}
359392
onSelect={select}
360393
/>
361394
</ScrollView>

‎apps/mobile/src/features/pull-requests/PullRequestReviewersScreen.tsx‎

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { useNavigation, type StaticScreenProps } from "@react-navigation/native"
77
import { squashAtomCommandFailure } from "@t3tools/client-runtime/state/runtime";
88
import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";
99
import type { PullRequestReviewerCandidate } from "@t3tools/contracts";
10-
import { useEffect, useRef, useState } from "react";
10+
import { useRef, useState } from "react";
1111
import {
1212
FlatList,
1313
Platform,
@@ -86,13 +86,14 @@ export function PullRequestReviewersScreen({ route }: StaticScreenProps<PullRequ
8686
(a, b) => Number(b.isRequested) - Number(a.isRequested) || a.login.localeCompare(b.login),
8787
);
8888
})();
89-
useEffect(() => {
90-
if (candidates.length === 0) return;
89+
// A new search shows its best matches from the top.
90+
const search = (next: string) => {
91+
setQuery(next);
9192
listRef.current?.scrollToOffset({
9293
offset: Platform.OS === "ios" ? -insets.top : 0,
9394
animated: false,
9495
});
95-
}, [candidates, insets.top]);
96+
};
9697
const update = async (
9798
candidate: Pick<PullRequestReviewerCandidate, "id" | "kind" | "isRequested">,
9899
) => {
@@ -118,16 +119,16 @@ export function PullRequestReviewersScreen({ route }: StaticScreenProps<PullRequ
118119
return;
119120
}
120121
if (!pr?.capabilities.reviewers.listCandidates) {
121-
setQuery("");
122+
search("");
122123
detail.refresh();
123124
}
124125
};
125126
const listCandidates = pr?.capabilities.reviewers.listCandidates === true;
126127
const searchOptions = {
127128
placeholder: "Search reviewers",
128129
autoCapitalize: "none" as const,
129-
onChangeText: (event: { nativeEvent: { text: string } }) => setQuery(event.nativeEvent.text),
130-
onCancelButtonPress: () => setQuery(""),
130+
onChangeText: (event: { nativeEvent: { text: string } }) => search(event.nativeEvent.text),
131+
onCancelButtonPress: () => search(""),
131132
hideNavigationBar: false,
132133
hideWhenScrolling: false,
133134
obscureBackground: false,
@@ -150,7 +151,7 @@ export function PullRequestReviewersScreen({ route }: StaticScreenProps<PullRequ
150151
? [
151152
createNativeMailSearchToolbarItem({
152153
placeholder: "Search reviewers",
153-
onSearchTextChange: setQuery,
154+
onSearchTextChange: search,
154155
showsSearchDismissButton: true,
155156
searchTextChangeId: "pr-reviewer-search",
156157
}),
@@ -182,7 +183,7 @@ export function PullRequestReviewersScreen({ route }: StaticScreenProps<PullRequ
182183
placeholder="Search reviewers"
183184
placeholderTextColorClassName="accent-foreground-muted"
184185
value={query}
185-
onChangeText={setQuery}
186+
onChangeText={search}
186187
autoCapitalize="none"
187188
className="min-h-11 rounded-xl bg-grouped-card px-3 text-sm"
188189
/>
@@ -287,7 +288,7 @@ export function PullRequestReviewersScreen({ route }: StaticScreenProps<PullRequ
287288
placeholder="Reviewer login or ID"
288289
placeholderTextColorClassName="accent-foreground-muted"
289290
value={query}
290-
onChangeText={setQuery}
291+
onChangeText={search}
291292
autoCapitalize="none"
292293
autoCorrect={false}
293294
className="min-h-12 rounded-xl bg-grouped-card px-3 text-base"

‎apps/mobile/src/features/pull-requests/PullRequestToolbar.android.tsx‎

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useNavigation, useNavigationState } from "@react-navigation/native";
1+
import { useNavigation } from "@react-navigation/native";
22
import { useCallback, useEffect, useRef } from "react";
33
import { BackHandler, Keyboard, View, type TextInputInstance } from "react-native";
44
import { AndroidHeaderIconButton } from "../../components/ui/AndroidScreenHeader";
@@ -8,17 +8,18 @@ import { useToolbarLayout } from "../../components/ui/useToolbarLayout";
88
import { useHardwareKeyboardCommand } from "../keyboard/hardwareKeyboardCommands";
99
import { WorkspaceListToolbarRow } from "../workspace/WorkspaceListToolbarRow";
1010
import { useWorkspaceTabs } from "../workspace/WorkspaceTabsContext";
11-
import { usePullRequestFilterMenu } from "./PullRequestFilterMenuContext";
11+
import type { PullRequestFilterMenu } from "./PullRequestListFilters";
1212
import { openSettings } from "../settings/settings-navigation";
1313

1414
/** PR search shares the thread list's Android header row. */
15-
export function PullRequestToolbar(props: { readonly onRequestVisibility?: () => void }) {
15+
export function PullRequestToolbar(props: {
16+
readonly filters: PullRequestFilterMenu | null;
17+
readonly onRequestVisibility?: () => void;
18+
}) {
1619
const navigation = useNavigation();
17-
const filters = usePullRequestFilterMenu();
20+
const { filters } = props;
1821
const workspace = useWorkspaceTabs()!;
1922
const { dispatch, getScrollOffset, recordScrollOffset, isTabActive } = workspace;
20-
const homeFocused = useNavigationState((state) => state.routes[state.index]?.name === "Home");
21-
const listFocused = homeFocused && workspace.state.selectedTab === "pullRequests";
2223
const { onRequestVisibility } = props;
2324
const tab = workspace.state.tabs.pullRequests;
2425
const input = useRef<TextInputInstance>(null);
@@ -41,9 +42,10 @@ export function PullRequestToolbar(props: { readonly onRequestVisibility?: () =>
4142
dispatch({ type: "cancel", tab: "pullRequests" });
4243
}, [dispatch, recordScrollOffset, tab.searchEntryOffset]);
4344
useEffect(() => {
44-
if (!searching || !listFocused) return;
45+
if (!searching) return;
4546
const subscription = BackHandler.addEventListener("hardwareBackPress", () => {
46-
// Frozen Home screens can keep effects alive after a detail route opens.
47+
// Frozen Home screens stay mounted under a detail route or another tab,
48+
// so back only applies while the PR list is in front.
4749
const state = navigation.getState();
4850
if (!state || state.routes[state.index]?.name !== "Home" || !isTabActive("pullRequests"))
4951
return false;
@@ -52,7 +54,7 @@ export function PullRequestToolbar(props: { readonly onRequestVisibility?: () =>
5254
return true;
5355
});
5456
return () => subscription.remove();
55-
}, [searching, closeSearch, listFocused, navigation, isTabActive]);
57+
}, [searching, closeSearch, navigation, isTabActive]);
5658
const { paddingTop, paddingBottom } = useToolbarLayout();
5759
return (
5860
<View className="bg-header px-2" style={{ paddingTop, paddingBottom }}>

0 commit comments

Comments
 (0)