Skip to content

Commit d00906d

Browse files
committed
feat(mobile): share thread sort preferences
1 parent 59b0201 commit d00906d

22 files changed

Lines changed: 471 additions & 91 deletions

‎apps/mobile/src/features/home/HomeHeader.android.tsx‎

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { useActiveThreadSort } from "../threads/use-active-thread-sort";
2+
import { ACTIVE_THREAD_SORT_OPTIONS } from "@t3tools/client-runtime/state/shared-settings";
13
import type { MenuAction } from "@react-native-menu/menu";
24
import { useCallback, useMemo } from "react";
35
import { NativeStackScreenOptions } from "../../native/StackHeader";
@@ -17,15 +19,28 @@ function checkedMenuState(checked: boolean) {
1719
}
1820

1921
export function HomeHeader(props: HomeHeaderProps) {
20-
// Thread List v2 lays the list out in fixed creation order, so the
21-
// sort/group filter controls would be silently ignored — hide them and
22-
// key the "customized" icon state off the environment filter alone.
2322
const threadListV2Enabled = useThreadListV2Enabled();
23+
const { order, setOrder, available } = useActiveThreadSort();
2424
const hasCustomListOptions = threadListV2Enabled
25-
? props.selectedEnvironmentId !== null || props.selectedProjectKey !== null
25+
? props.selectedEnvironmentId !== null ||
26+
props.selectedProjectKey !== null ||
27+
order !== "manual"
2628
: hasCustomHomeListOptions(props);
2729
const menuActions = useMemo<MenuAction[]>(
2830
() => [
31+
...(threadListV2Enabled && available
32+
? [
33+
{
34+
id: "active-sort",
35+
title: "Sort active threads",
36+
subactions: ACTIVE_THREAD_SORT_OPTIONS.map((option) => ({
37+
id: `active-sort:${option.value}`,
38+
title: option.label,
39+
state: checkedMenuState(order === option.value),
40+
})),
41+
},
42+
]
43+
: []),
2944
{
3045
id: "environment",
3146
title: "Environment",
@@ -86,6 +101,8 @@ export function HomeHeader(props: HomeHeaderProps) {
86101
] satisfies MenuAction[])),
87102
],
88103
[
104+
order,
105+
available,
89106
props.environments,
90107
props.projectSortOrder,
91108
props.projects,
@@ -98,6 +115,13 @@ export function HomeHeader(props: HomeHeaderProps) {
98115
const handleMenuAction = useCallback(
99116
(event: { nativeEvent: { event: string } }) => {
100117
const id = event.nativeEvent.event;
118+
const activeSort = ACTIVE_THREAD_SORT_OPTIONS.find(
119+
(option) => id === `active-sort:${option.value}`,
120+
);
121+
if (threadListV2Enabled && available && activeSort) {
122+
setOrder(activeSort.value);
123+
return;
124+
}
101125
if (id === "environment:all") {
102126
props.onEnvironmentChange(null);
103127
return;
@@ -141,7 +165,7 @@ export function HomeHeader(props: HomeHeaderProps) {
141165
return;
142166
}
143167
},
144-
[props],
168+
[props, threadListV2Enabled, available, setOrder],
145169
);
146170

147171
return (

‎apps/mobile/src/features/home/HomeHeader.tsx‎

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { useActiveThreadSort } from "../threads/use-active-thread-sort";
2+
import { ACTIVE_THREAD_SORT_OPTIONS } from "@t3tools/client-runtime/state/shared-settings";
13
import { NativeHeaderToolbar, NativeStackScreenOptions } from "../../native/StackHeader";
24
import { useCallback, useRef } from "react";
35
import type { SearchBarCommands } from "react-native-screens";
@@ -22,12 +24,12 @@ export type { HomeHeaderEnvironment } from "./HomeHeader.types";
2224
export function HomeHeader(props: HomeHeaderProps) {
2325
const searchBarRef = useRef<SearchBarCommands>(null);
2426
const iconColor = useUniwindTheme()["--color-icon"];
25-
// Thread List v2 lays the list out in fixed creation order, so the
26-
// sort/group filter controls would be silently ignored — hide them and
27-
// key the "customized" icon state off the environment filter alone.
2827
const threadListV2Enabled = useThreadListV2Enabled();
28+
const { order, setOrder, available } = useActiveThreadSort();
2929
const hasCustomListOptions = threadListV2Enabled
30-
? props.selectedEnvironmentId !== null || props.selectedProjectKey !== null
30+
? props.selectedEnvironmentId !== null ||
31+
props.selectedProjectKey !== null ||
32+
order !== "manual"
3133
: hasCustomHomeListOptions(props);
3234
const focusSearch = useCallback(() => {
3335
searchBarRef.current?.focus();
@@ -37,6 +39,7 @@ export function HomeHeader(props: HomeHeaderProps) {
3739
const filterMenu = buildHomeListFilterMenu({
3840
...props,
3941
listOrganization: !threadListV2Enabled,
42+
...(available ? { activeThreadSort: { order, onChange: setOrder } } : {}),
4043
});
4144

4245
return (
@@ -166,6 +169,20 @@ export function HomeHeader(props: HomeHeaderProps) {
166169
</NativeHeaderToolbar.Menu>
167170
)}
168171

172+
{threadListV2Enabled && available ? (
173+
<NativeHeaderToolbar.Menu title="Sort active threads">
174+
<NativeHeaderToolbar.Label>Sort active threads</NativeHeaderToolbar.Label>
175+
{ACTIVE_THREAD_SORT_OPTIONS.map((option) => (
176+
<NativeHeaderToolbar.MenuAction
177+
key={option.value}
178+
isOn={order === option.value}
179+
onPress={() => setOrder(option.value)}
180+
>
181+
<NativeHeaderToolbar.Label>{option.label}</NativeHeaderToolbar.Label>
182+
</NativeHeaderToolbar.MenuAction>
183+
))}
184+
</NativeHeaderToolbar.Menu>
185+
) : null}
169186
{threadListV2Enabled ? null : (
170187
<NativeHeaderToolbar.Menu title="Sort threads">
171188
<NativeHeaderToolbar.Label>Sort threads</NativeHeaderToolbar.Label>

‎apps/mobile/src/features/home/HomeScreen.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useActiveThreadSort } from "../threads/use-active-thread-sort";
12
import type { ThreadMoveDestination } from "../threads/threadOrder";
23
import { createThreadMovePlanner } from "../threads/threadOrder";
34
import {
@@ -655,6 +656,7 @@ export function HomeScreen(props: HomeScreenProps) {
655656
),
656657
[serverConfigs],
657658
);
659+
const { order: activeThreadSortOrder } = useActiveThreadSort();
658660
const pendingOrder = usePendingThreadOrder(nowMinute, snoozeWakeTick);
659661
const threadMovePlanners = useMemo(() => {
660662
const sectionPlanner = (section: "pinned" | "active") =>
@@ -665,12 +667,14 @@ export function HomeScreen(props: HomeScreenProps) {
665667
[...serverConfigs].flatMap(([id, config]) =>
666668
(section === "pinned"
667669
? config.environment.capabilities.threadPinReorder
668-
: config.environment.capabilities.threadActiveReorder) === true
670+
: activeThreadSortOrder === "manual" &&
671+
config.environment.capabilities.threadActiveReorder) === true
669672
? [id]
670673
: [],
671674
),
672675
),
673676
ordered: getThreadListV2OrderedSection({
677+
activeThreadSortOrder,
674678
threads: props.threads,
675679
section,
676680
pendingOrder,
@@ -682,6 +686,7 @@ export function HomeScreen(props: HomeScreenProps) {
682686
});
683687
return { pinned: sectionPlanner("pinned"), active: sectionPlanner("active") };
684688
}, [
689+
activeThreadSortOrder,
685690
serverConfigs,
686691
props.threads,
687692
pendingOrder,
@@ -705,6 +710,7 @@ export function HomeScreen(props: HomeScreenProps) {
705710
// Settled threads are live shells; archived threads keep their original
706711
// "hidden from lists" meaning.
707712
return buildThreadListV2Items({
713+
activeThreadSortOrder,
708714
pendingOrder,
709715
threads: props.threads.filter((thread) => thread.archivedAt === null),
710716
environmentId: props.selectedEnvironmentId,
@@ -721,6 +727,7 @@ export function HomeScreen(props: HomeScreenProps) {
721727
selectedThreadKey: null,
722728
});
723729
}, [
730+
activeThreadSortOrder,
724731
pendingOrder,
725732
queuedThreadKeys,
726733
nowMinute,

‎apps/mobile/src/features/home/home-list-filter-menu.ts‎

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { ACTIVE_THREAD_SORT_OPTIONS } from "@t3tools/client-runtime/state/shared-settings";
2+
import type { ActiveThreadSortOrder } from "@t3tools/contracts/settings";
13
import type { EnvironmentId, SidebarThreadSortOrder } from "@t3tools/contracts";
24

35
import type { HomeProjectSortOrder } from "./homeThreadList";
@@ -43,9 +45,11 @@ export function buildHomeListFilterMenu(props: {
4345
readonly onProjectChange: (projectKey: string | null) => void;
4446
readonly onProjectSortOrderChange: (sortOrder: HomeProjectSortOrder) => void;
4547
readonly onThreadSortOrderChange: (sortOrder: SidebarThreadSortOrder) => void;
46-
/** False hides the sort/group submenus. Thread List v2 uses a fixed
47-
creation-order layout, so offering those controls while it silently
48-
ignores them would be a lie; the environment filter still applies. */
48+
readonly activeThreadSort?: {
49+
order: ActiveThreadSortOrder;
50+
onChange: (order: ActiveThreadSortOrder) => void;
51+
};
52+
/** Legacy organization controls; v2 has its own active-thread sort. */
4953
readonly listOrganization?: boolean;
5054
}): HomeListFilterMenu {
5155
const items: Array<HomeListFilterMenuAction | HomeListFilterMenuSubmenu> = [];
@@ -120,6 +124,20 @@ export function buildHomeListFilterMenu(props: {
120124
);
121125
}
122126

127+
if (props.listOrganization === false && props.activeThreadSort) {
128+
const sort = props.activeThreadSort;
129+
items.push({
130+
type: "submenu",
131+
title: "Sort active threads",
132+
items: ACTIVE_THREAD_SORT_OPTIONS.map((option) => ({
133+
type: "action",
134+
title: option.label,
135+
state: sort.order === option.value ? "on" : "off",
136+
onPress: () => sort.onChange(option.value),
137+
})),
138+
});
139+
}
140+
123141
return {
124142
title: "Thread list options",
125143
items,

‎apps/mobile/src/features/home/useThreadListActions.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useActiveThreadSort } from "../threads/use-active-thread-sort";
12
import type { ThreadMoveDestination } from "../threads/threadOrder";
23
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
34
import { canSnooze, effectiveSnoozed } from "@t3tools/client-runtime/state/thread-settled";
@@ -245,6 +246,7 @@ export function useThreadListActions(): {
245246
readonly regenerateThreadTitle: (thread: EnvironmentThreadShell) => Promise<boolean>;
246247
} {
247248
const executeAction = useThreadActionExecutor();
249+
const { order: activeThreadSortOrder } = useActiveThreadSort();
248250
const snoozeMutation = useAtomCommand(threadEnvironment.snooze, { reportFailure: false });
249251
const unsnoozeMutation = useAtomCommand(threadEnvironment.unsnooze, { reportFailure: false });
250252
const pinMutation = useAtomCommand(threadEnvironment.pin, { reportFailure: false });
@@ -553,6 +555,7 @@ export function useThreadListActions(): {
553555
}
554556
}
555557
const configs = appAtomRegistry.get(environmentServerConfigsAtom);
558+
if (section === "active" && activeThreadSortOrder !== "manual") return false;
556559
const supportsReorder = (environmentId: EnvironmentThreadShell["environmentId"]) => {
557560
const capabilities = configs.get(environmentId)?.environment.capabilities;
558561
return section === "pinned"
@@ -677,6 +680,7 @@ export function useThreadListActions(): {
677680
}
678681
},
679682
[
683+
activeThreadSortOrder,
680684
settleThread,
681685
reorderActiveMutation,
682686
reorderPinnedMutation,

‎apps/mobile/src/features/threads/ThreadArrangementSheet.tsx‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { useActiveThreadSort } from "./use-active-thread-sort";
12
import { appAtomRegistry } from "../../state/atom-registry";
23
import { useAtomValue } from "@effect/atom-react";
34
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
@@ -150,6 +151,7 @@ export function ThreadArrangementSheet(props: { onClose: () => void }) {
150151
const insets = useSafeAreaInsets();
151152
const threads = useAtomValue(environmentThreadShells.threadShellsAtom);
152153
const configs = useAtomValue(environmentServerConfigsAtom);
154+
const { order: activeThreadSortOrder } = useActiveThreadSort();
153155
const queuedThreadKeys = useAtomValue(queuedThreadKeysAtom);
154156
const pendingOrder = useAtomValue(pendingThreadOrderAtom);
155157
const dropBusy = useAtomValue(threadDropBusyAtom);
@@ -172,6 +174,7 @@ export function ThreadArrangementSheet(props: { onClose: () => void }) {
172174
}, [threads, now]);
173175
const sections = useMemo(() => {
174176
const shared = {
177+
activeThreadSortOrder,
175178
threads,
176179
now,
177180
queuedThreadKeys,
@@ -199,7 +202,7 @@ export function ThreadArrangementSheet(props: { onClose: () => void }) {
199202
snoozed: parked.filter((thread) => effectiveSnoozed(thread, { now })),
200203
settled: parked.filter((thread) => !effectiveSnoozed(thread, { now })),
201204
};
202-
}, [threads, configs, now, queuedThreadKeys, pendingOrder]);
205+
}, [activeThreadSortOrder, threads, configs, now, queuedThreadKeys, pendingOrder]);
203206
const planners = useMemo(() => {
204207
const planner = (section: "pinned" | "active") =>
205208
createThreadMovePlanner({
@@ -211,15 +214,16 @@ export function ThreadArrangementSheet(props: { onClose: () => void }) {
211214
(
212215
section === "pinned"
213216
? config.environment.capabilities.threadPinReorder
214-
: config.environment.capabilities.threadActiveReorder
217+
: activeThreadSortOrder === "manual" &&
218+
config.environment.capabilities.threadActiveReorder
215219
)
216220
? [id]
217221
: [],
218222
),
219223
),
220224
});
221225
return { pinned: planner("pinned"), active: planner("active") };
222-
}, [sections, threads, configs]);
226+
}, [activeThreadSortOrder, sections, threads, configs]);
223227
const rows = useMemo(() => {
224228
const result: Row[] = [];
225229
let offset = 0;

0 commit comments

Comments
 (0)