Skip to content

Commit 2a30353

Browse files
feat(mobile): arrange threads with drag handles (#10496)
1 parent 08463e2 commit 2a30353

12 files changed

Lines changed: 1037 additions & 38 deletions

‎apps/mobile/src/App.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { SafeAreaProvider } from "react-native-safe-area-context";
99
import { createStaticNavigation } from "@react-navigation/native";
1010

1111
import { RegistryContext } from "@effect/atom-react";
12+
import { ThreadArrangementHost } from "./features/threads/ThreadArrangementSheet";
1213
import { ConfirmDialogHost } from "./components/ConfirmDialogHost";
1314
import { CloudAuthProvider } from "./features/cloud/CloudAuthProvider";
1415
import { prepareNativeShowcaseCapture } from "./features/showcase/nativeShowcaseScene";
@@ -94,6 +95,7 @@ function AppContent() {
9495
<Navigation linking={appLinking} theme={navigationTheme} />
9596
</IncomingShareProvider>
9697
<ConfirmDialogHost />
98+
<ThreadArrangementHost />
9799
</BlurTargetView>
98100
{/* Anchored-menu overlays render here — in-window, so the
99101
keyboard stays up while a dropdown is open. */}

‎apps/mobile/src/components/AppSymbol.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ import IconLayoutColumns from "@tabler/icons-react-native/IconLayoutColumns";
5959
import IconLayoutSidebar from "@tabler/icons-react-native/IconLayoutSidebar";
6060
import IconLayoutSidebarRight from "@tabler/icons-react-native/IconLayoutSidebarRight";
6161
import IconLetterSpacing from "@tabler/icons-react-native/IconLetterSpacing";
62+
import IconMenu2 from "@tabler/icons-react-native/IconMenu2";
6263
import IconLink from "@tabler/icons-react-native/IconLink";
6364
import IconMessage from "@tabler/icons-react-native/IconMessage";
6465
import IconMinus from "@tabler/icons-react-native/IconMinus";
@@ -141,6 +142,7 @@ const ANDROID_ICON_BY_SF_SYMBOL: Partial<Record<SFSymbol, Icon>> = {
141142
"info.circle": IconInfoCircle,
142143
laptopcomputer: IconDeviceLaptop,
143144
link: IconLink,
145+
"line.3.horizontal": IconMenu2,
144146
"line.3.horizontal.decrease": IconFilter,
145147
"line.3.horizontal.decrease.circle": IconFilter,
146148
"line.3.horizontal.decrease.circle.fill": IconFilterFilled,

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { ThreadMoveDestination } from "../threads/threadOrder";
12
import { createThreadMovePlanner } from "../threads/threadOrder";
23
import {
34
LegendList,
@@ -122,7 +123,7 @@ interface HomeScreenProps {
122123
readonly onUnpinThread: (thread: EnvironmentThreadShell) => Promise<boolean>;
123124
readonly onMoveThread: (
124125
thread: EnvironmentThreadShell,
125-
direction: "up" | "down",
126+
direction: ThreadMoveDestination,
126127
) => Promise<boolean>;
127128
readonly onRegenerateThreadTitle: (thread: EnvironmentThreadShell) => Promise<boolean>;
128129
readonly onSelectPendingTask: (pendingTask: PendingNewTask) => void;
@@ -519,7 +520,7 @@ export function HomeScreen(props: HomeScreenProps) {
519520
[props.onPinThread],
520521
);
521522
const handleMoveThread = useCallback(
522-
(thread: EnvironmentThreadShell, direction: "up" | "down") => {
523+
(thread: EnvironmentThreadShell, direction: ThreadMoveDestination) => {
523524
void props.onMoveThread(thread, direction);
524525
},
525526
[props.onMoveThread],

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

Lines changed: 102 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import type { ThreadMoveDestination } from "../threads/threadOrder";
12
import type { EnvironmentThreadShell } from "@t3tools/client-runtime/state/shell";
2-
import { canSnooze } from "@t3tools/client-runtime/state/thread-settled";
3+
import { canSnooze, effectiveSnoozed } from "@t3tools/client-runtime/state/thread-settled";
34
import * as Cause from "effect/Cause";
45
import * as Haptics from "expo-haptics";
56
import { useCallback, useRef } from "react";
@@ -15,8 +16,16 @@ import { environmentServerConfigsAtom } from "../../state/server";
1516
import { environmentThreadShells, threadEnvironment } from "../../state/threads";
1617
import { queuedThreadKeysAtom } from "../../state/use-thread-outbox";
1718
import { useAtomCommand } from "../../state/use-atom-command";
18-
import { beginPendingThreadOrder, getPendingThreadOrder } from "../../state/thread-order";
19-
import { createPendingThreadOrder, createThreadMovePlanner } from "../threads/threadOrder";
19+
import {
20+
beginPendingThreadOrder,
21+
getPendingThreadOrder,
22+
threadDropBusyAtom,
23+
} from "../../state/thread-order";
24+
import {
25+
createPendingThreadOrder,
26+
createThreadMovePlanner,
27+
threadDropLifecycle,
28+
} from "../threads/threadOrder";
2029
import { getThreadListV2OrderedSection } from "../threads/threadListV2";
2130

2231
/** Version skew: never send settle/unsettle to a server that predates them
@@ -229,7 +238,7 @@ export function useThreadListActions(): {
229238
readonly unpinThread: (thread: EnvironmentThreadShell) => Promise<boolean>;
230239
readonly moveThread: (
231240
thread: EnvironmentThreadShell,
232-
direction: "up" | "down",
241+
direction: ThreadMoveDestination,
233242
) => Promise<boolean>;
234243
readonly regenerateThreadTitle: (thread: EnvironmentThreadShell) => Promise<boolean>;
235244
} {
@@ -474,9 +483,29 @@ export function useThreadListActions(): {
474483
reportFailure: false,
475484
});
476485
const moveThread = useCallback(
477-
async (thread: EnvironmentThreadShell, direction: "up" | "down") => {
478-
if (getPendingThreadOrder() !== null) return false;
479-
const section = thread.pinnedAt != null ? "pinned" : "active";
486+
async (thread: EnvironmentThreadShell, direction: ThreadMoveDestination) => {
487+
if (getPendingThreadOrder() !== null || appAtomRegistry.get(threadDropBusyAtom)) return false;
488+
const shells = appAtomRegistry.get(environmentThreadShells.threadShellsAtom);
489+
const current = shells.find(
490+
(row) => row.id === thread.id && row.environmentId === thread.environmentId,
491+
);
492+
if (!current || current.archivedAt !== null) return false;
493+
thread = current;
494+
const section =
495+
typeof direction === "object" && direction.section !== undefined
496+
? direction.section
497+
: thread.pinnedAt != null
498+
? "pinned"
499+
: "active";
500+
if (section === "settled") {
501+
if (!environmentSupportsSettlement(thread.environmentId)) return false;
502+
appAtomRegistry.set(threadDropBusyAtom, true);
503+
try {
504+
return await settleThread(thread);
505+
} finally {
506+
appAtomRegistry.set(threadDropBusyAtom, false);
507+
}
508+
}
480509
const configs = appAtomRegistry.get(environmentServerConfigsAtom);
481510
const supportsReorder = (environmentId: EnvironmentThreadShell["environmentId"]) => {
482511
const capabilities = configs.get(environmentId)?.environment.capabilities;
@@ -491,7 +520,6 @@ export function useThreadListActions(): {
491520
);
492521
return false;
493522
}
494-
const shells = appAtomRegistry.get(environmentThreadShells.threadShellsAtom);
495523
const ordered = getThreadListV2OrderedSection({
496524
threads: shells,
497525
section,
@@ -515,24 +543,67 @@ export function useThreadListActions(): {
515543
reorderableEnvironmentIds: new Set([...configs.keys()].filter(supportsReorder)),
516544
})(scopedThreadKey(thread.environmentId, thread.id), direction);
517545
if (assignments === null) return false;
546+
const lifecycle = threadDropLifecycle(thread, section, new Date().toISOString());
547+
const crossSection = !ordered.some(
548+
(row) => row.id === thread.id && row.environmentId === thread.environmentId,
549+
);
550+
if (
551+
crossSection &&
552+
(((section === "pinned" || thread.pinnedAt != null) &&
553+
!environmentSupportsPinning(thread.environmentId)) ||
554+
(thread.settledOverride === "settled" &&
555+
!environmentSupportsSettlement(thread.environmentId)) ||
556+
(effectiveSnoozed(thread, { now: new Date().toISOString() }) &&
557+
!environmentSupportsSnooze(thread.environmentId)))
558+
)
559+
return false;
518560
const shellByKey = new Map(
519-
ordered.map((shell) => [scopedThreadKey(shell.environmentId, shell.id), shell]),
561+
shells.map((shell) => [scopedThreadKey(shell.environmentId, shell.id), shell]),
520562
);
521563
selectionHaptic();
522-
const pending = beginPendingThreadOrder(
523-
createPendingThreadOrder({
524-
section,
525-
ordered,
526-
movedId: scopedThreadKey(thread.environmentId, thread.id),
527-
direction,
528-
assignments,
529-
}),
530-
);
564+
appAtomRegistry.set(threadDropBusyAtom, true);
565+
const pending = crossSection
566+
? null
567+
: beginPendingThreadOrder(
568+
createPendingThreadOrder({
569+
section,
570+
ordered,
571+
movedId: scopedThreadKey(thread.environmentId, thread.id),
572+
direction,
573+
assignments,
574+
}),
575+
);
531576
let succeeded = false;
532577
const reorder = section === "pinned" ? reorderPinnedMutation : reorderActiveMutation;
533578
try {
579+
if (crossSection) {
580+
if (section === "pinned") {
581+
const orderKey = assignments.find(
582+
({ id }) => id === scopedThreadKey(thread.environmentId, thread.id),
583+
)?.orderKey;
584+
const result = await pinMutation({
585+
environmentId: thread.environmentId,
586+
input: { threadId: thread.id, ...(orderKey === undefined ? {} : { orderKey }) },
587+
});
588+
if (result._tag === "Failure") {
589+
Alert.alert("Could not pin thread", String(Cause.squash(result.cause)));
590+
return false;
591+
}
592+
} else {
593+
if (lifecycle.unpin && !(await unpinThread(thread))) return false;
594+
if (lifecycle.unsettle && !(await unsettleThread(thread))) return false;
595+
if (lifecycle.unsnooze && !(await unsnoozeThread(thread))) return false;
596+
}
597+
}
534598
for (const assignment of assignments) {
535-
if (!pending.isPending()) return false;
599+
if (
600+
crossSection &&
601+
section === "pinned" &&
602+
thread.pinnedAt == null &&
603+
assignment.id === scopedThreadKey(thread.environmentId, thread.id)
604+
)
605+
continue;
606+
if (pending !== null && !pending.isPending()) return false;
536607
const target = shellByKey.get(assignment.id);
537608
if (target === undefined) continue;
538609
const result = await reorder({
@@ -552,13 +623,22 @@ export function useThreadListActions(): {
552623
}
553624
}
554625
succeeded = true;
555-
pending.complete();
626+
pending?.complete();
556627
return true;
557628
} finally {
558-
if (!succeeded) pending.cancel();
629+
if (!succeeded) pending?.cancel();
630+
appAtomRegistry.set(threadDropBusyAtom, false);
559631
}
560632
},
561-
[reorderActiveMutation, reorderPinnedMutation],
633+
[
634+
settleThread,
635+
reorderActiveMutation,
636+
reorderPinnedMutation,
637+
pinMutation,
638+
unpinThread,
639+
unsettleThread,
640+
unsnoozeThread,
641+
],
562642
);
563643

564644
const confirmDeleteThread = useConfirmDeleteThread(executeAction);

0 commit comments

Comments
 (0)