Skip to content

Commit 123b267

Browse files
committed
fix(mobile): add missing thread rename action
1 parent 0e0ddae commit 123b267

9 files changed

Lines changed: 190 additions & 37 deletions

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

Lines changed: 63 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useCallback, useEffect, useState } from "react";
2-
import { Modal, Pressable, View } from "react-native";
2+
import { Modal, Pressable, TextInput, View } from "react-native";
33

44
import { cn } from "../lib/cn";
55
import { AppText } from "./AppText";
@@ -14,7 +14,20 @@ export type ConfirmDialogRequest = {
1414
readonly onCancel?: () => void;
1515
};
1616

17-
let presentRequest: ((request: ConfirmDialogRequest) => void) | null = null;
17+
export type TextInputDialogRequest = {
18+
readonly title: string;
19+
readonly initialValue: string;
20+
readonly cancelText?: string;
21+
readonly confirmText: string;
22+
readonly onConfirm: (value: string) => void;
23+
readonly onCancel?: () => void;
24+
};
25+
26+
type DialogRequest =
27+
| { readonly kind: "confirm"; readonly request: ConfirmDialogRequest }
28+
| { readonly kind: "text-input"; readonly request: TextInputDialogRequest };
29+
30+
let presentRequest: ((request: DialogRequest) => void) | null = null;
1831

1932
/**
2033
* Imperative confirm dialog, Alert.alert-shaped. Native iOS alerts already
@@ -23,7 +36,11 @@ let presentRequest: ((request: ConfirmDialogRequest) => void) | null = null;
2336
* once. Requires ConfirmDialogHost to be mounted at the app root.
2437
*/
2538
export function showConfirmDialog(request: ConfirmDialogRequest): void {
26-
presentRequest?.(request);
39+
presentRequest?.({ kind: "confirm", request });
40+
}
41+
42+
export function showTextInputDialog(request: TextInputDialogRequest): void {
43+
presentRequest?.({ kind: "text-input", request });
2744
}
2845

2946
/**
@@ -33,42 +50,64 @@ export function showConfirmDialog(request: ConfirmDialogRequest): void {
3350
* button color and a dimmer message than the title.
3451
*/
3552
export function ConfirmDialogHost() {
36-
const [request, setRequest] = useState<ConfirmDialogRequest | null>(null);
53+
const [presented, setPresented] = useState<DialogRequest | null>(null);
54+
const [inputValue, setInputValue] = useState("");
3755
useEffect(() => {
38-
presentRequest = setRequest;
56+
presentRequest = (request) => {
57+
setInputValue(request.kind === "text-input" ? request.request.initialValue : "");
58+
setPresented(request);
59+
};
3960
return () => {
4061
presentRequest = null;
4162
};
4263
}, []);
4364

4465
const handleCancel = useCallback(() => {
45-
request?.onCancel?.();
46-
setRequest(null);
47-
}, [request]);
66+
presented?.request.onCancel?.();
67+
setPresented(null);
68+
}, [presented]);
4869

4970
const handleConfirm = useCallback(() => {
50-
request?.onConfirm();
51-
setRequest(null);
52-
}, [request]);
71+
if (presented?.kind === "confirm") {
72+
presented.request.onConfirm();
73+
} else if (presented?.kind === "text-input") {
74+
presented.request.onConfirm(inputValue);
75+
}
76+
setPresented(null);
77+
}, [inputValue, presented]);
78+
79+
const confirmDisabled = presented?.kind === "text-input" && inputValue.trim().length === 0;
5380

5481
return (
5582
<Modal
56-
visible={request !== null}
83+
visible={presented !== null}
5784
transparent
5885
animationType="fade"
5986
statusBarTranslucent
6087
navigationBarTranslucent
6188
onRequestClose={handleCancel}
6289
>
63-
{request === null ? null : (
90+
{presented === null ? null : (
6491
<View className="flex-1 items-center justify-center bg-backdrop px-8">
6592
<View className="w-full rounded-[24px] bg-card px-6 pb-4 pt-5">
66-
<AppText className="text-lg font-t3-medium">{request.title}</AppText>
67-
{request.message === undefined ? null : (
93+
<AppText className="text-lg font-t3-medium">{presented.request.title}</AppText>
94+
{presented.kind === "confirm" && presented.request.message !== undefined ? (
6895
<AppText className="mt-2 text-sm text-foreground-secondary">
69-
{request.message}
96+
{presented.request.message}
7097
</AppText>
71-
)}
98+
) : null}
99+
{presented.kind === "text-input" ? (
100+
<TextInput
101+
accessibilityLabel={presented.request.title}
102+
autoFocus
103+
className="mt-4 rounded-xl border border-border bg-screen px-3 py-2.5 text-base text-foreground"
104+
onChangeText={setInputValue}
105+
onSubmitEditing={confirmDisabled ? undefined : handleConfirm}
106+
returnKeyType="done"
107+
selectTextOnFocus
108+
value={inputValue}
109+
/>
110+
) : null}
72111
<View className="mt-5 flex-row justify-end gap-1">
73112
<View className="overflow-hidden rounded-full">
74113
<Pressable
@@ -77,23 +116,27 @@ export function ConfirmDialogHost() {
77116
onPress={handleCancel}
78117
>
79118
<AppText className="text-base font-t3-medium">
80-
{request.cancelText ?? "Cancel"}
119+
{presented.request.cancelText ?? "Cancel"}
81120
</AppText>
82121
</Pressable>
83122
</View>
84123
<View className="overflow-hidden rounded-full">
85124
<Pressable
86125
accessibilityRole="button"
126+
disabled={confirmDisabled}
87127
className="min-h-10 items-center justify-center px-4 active:bg-subtle"
88128
onPress={handleConfirm}
89129
>
90130
<AppText
91131
className={cn(
92132
"text-base font-t3-medium",
93-
request.destructive && "text-danger-foreground",
133+
presented.kind === "confirm" &&
134+
presented.request.destructive &&
135+
"text-danger-foreground",
136+
confirmDisabled && "text-foreground-tertiary",
94137
)}
95138
>
96-
{request.confirmText}
139+
{presented.request.confirmText}
97140
</AppText>
98141
</Pressable>
99142
</View>

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export function HomeRouteScreen() {
5050
pinThread,
5151
unpinThread,
5252
moveThread,
53+
renameThread,
5354
regenerateThreadTitle,
5455
unsettleThread,
5556
} = useThreadListActions();
@@ -200,6 +201,7 @@ export function HomeRouteScreen() {
200201
onPinThread={pinThread}
201202
onUnpinThread={unpinThread}
202203
onMoveThread={moveThread}
204+
onRenameThread={renameThread}
203205
onRegenerateThreadTitle={regenerateThreadTitle}
204206
onEnvironmentChange={setSelectedEnvironmentId}
205207
onProjectChange={setSelectedProjectKey}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ interface HomeScreenProps {
125125
thread: EnvironmentThreadShell,
126126
direction: ThreadMoveDestination,
127127
) => Promise<boolean>;
128+
readonly onRenameThread: (thread: EnvironmentThreadShell) => void;
128129
readonly onRegenerateThreadTitle: (thread: EnvironmentThreadShell) => Promise<boolean>;
129130
readonly onSelectPendingTask: (pendingTask: PendingNewTask) => void;
130131
readonly onDeletePendingTask: (pendingTask: PendingNewTask) => void;
@@ -537,6 +538,10 @@ export function HomeScreen(props: HomeScreenProps) {
537538
},
538539
[props.onRegenerateThreadTitle],
539540
);
541+
const handleRenameThread = useCallback(
542+
(thread: EnvironmentThreadShell) => props.onRenameThread(thread),
543+
[props.onRenameThread],
544+
);
540545
const handleDeleteThread = props.onDeleteThread;
541546
const handleUnsettleThread = props.onUnsettleThread;
542547
// The settled tail renders in pages; expansion resets when the filter
@@ -862,6 +867,7 @@ export function HomeScreen(props: HomeScreenProps) {
862867
onSelectThread={props.onSelectThread}
863868
onDeleteThread={handleDeleteThread}
864869
onArchiveThread={props.onArchiveThread}
870+
onRenameThread={handleRenameThread}
865871
onRegenerateThreadTitle={handleRegenerateThreadTitle}
866872
titleRegenerationSupported={titleRegenerationEnvironmentIds.has(thread.environmentId)}
867873
settlementSupported={settlementEnvironmentIds.has(thread.environmentId)}
@@ -895,6 +901,7 @@ export function HomeScreen(props: HomeScreenProps) {
895901
handleMoveThread,
896902
handlePinThread,
897903
handleRegenerateThreadTitle,
904+
handleRenameThread,
898905
handleSettleThread,
899906
handleSnoozeThread,
900907
handleUnpinThread,
@@ -1019,6 +1026,7 @@ export function HomeScreen(props: HomeScreenProps) {
10191026
searchQuery={props.searchQuery}
10201027
onArchiveThread={props.onArchiveThread}
10211028
onDeleteThread={props.onDeleteThread}
1029+
onRenameThread={handleRenameThread}
10221030
onRegenerateThreadTitle={handleRegenerateThreadTitle}
10231031
titleRegenerationSupported={titleRegenerationEnvironmentIds.has(thread.environmentId)}
10241032
onSelectThread={props.onSelectThread}
@@ -1043,6 +1051,7 @@ export function HomeScreen(props: HomeScreenProps) {
10431051
handleSwipeableClose,
10441052
handleSwipeableWillOpen,
10451053
handleRegenerateThreadTitle,
1054+
handleRenameThread,
10461055
machineByEnvironmentId,
10471056
queuedThreadKeys,
10481057
props.onArchiveThread,

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

Lines changed: 49 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@ import { canSnooze, effectiveSnoozed } from "@t3tools/client-runtime/state/threa
44
import * as Cause from "effect/Cause";
55
import * as Haptics from "expo-haptics";
66
import { useCallback, useRef } from "react";
7-
import { Alert } from "react-native";
7+
import { Alert, Platform } from "react-native";
88

99
import { withThreadDismissal } from "./thread-dismissal";
10-
import { showConfirmDialog } from "../../components/ConfirmDialogHost";
10+
import { showConfirmDialog, showTextInputDialog } from "../../components/ConfirmDialogHost";
1111
import { scopedThreadKey } from "../../lib/scopedEntities";
1212
import { refreshArchivedThreadsForEnvironment } from "../archive/useArchivedThreadSnapshots";
1313
import { pinOrderKeyBetween } from "@t3tools/client-runtime/state/thread-sort";
@@ -27,6 +27,7 @@ import {
2727
threadDropLifecycle,
2828
} from "../threads/threadOrder";
2929
import { getThreadListV2OrderedSection } from "../threads/threadListV2";
30+
import { resolveThreadTitleRename } from "../threads/thread-title-rename";
3031

3132
/** Version skew: never send settle/unsettle to a server that predates them
3233
(capability defaults false on decode for older servers). */
@@ -240,6 +241,7 @@ export function useThreadListActions(): {
240241
thread: EnvironmentThreadShell,
241242
direction: ThreadMoveDestination,
242243
) => Promise<boolean>;
244+
readonly renameThread: (thread: EnvironmentThreadShell) => void;
243245
readonly regenerateThreadTitle: (thread: EnvironmentThreadShell) => Promise<boolean>;
244246
} {
245247
const executeAction = useThreadActionExecutor();
@@ -474,6 +476,50 @@ export function useThreadListActions(): {
474476
},
475477
[updateThreadMetadata],
476478
);
479+
const renameThread = useCallback(
480+
(thread: EnvironmentThreadShell) => {
481+
const commit = (title: string) => {
482+
const resolution = resolveThreadTitleRename({ title, originalTitle: thread.title });
483+
if (resolution.action === "reject-empty") {
484+
Alert.alert("Could not rename thread", "Thread title cannot be empty.");
485+
return;
486+
}
487+
if (resolution.action === "noop") return;
488+
selectionHaptic();
489+
void updateThreadMetadata({
490+
environmentId: thread.environmentId,
491+
input: { threadId: thread.id, title: resolution.title },
492+
}).then((result) => {
493+
if (result._tag === "Success") return;
494+
const error = Cause.squash(result.cause);
495+
Alert.alert(
496+
"Could not rename thread",
497+
error instanceof Error && error.message.trim().length > 0
498+
? error.message
499+
: "The thread could not be renamed.",
500+
);
501+
});
502+
};
503+
504+
if (Platform.OS === "ios") {
505+
Alert.prompt(
506+
"Rename thread",
507+
undefined,
508+
(title) => commit(title ?? ""),
509+
"plain-text",
510+
thread.title,
511+
);
512+
return;
513+
}
514+
showTextInputDialog({
515+
title: "Rename thread",
516+
initialValue: thread.title,
517+
confirmText: "Rename",
518+
onConfirm: commit,
519+
});
520+
},
521+
[updateThreadMetadata],
522+
);
477523

478524
// Plan against the complete section so filtering does not change a move.
479525
const reorderPinnedMutation = useAtomCommand(threadEnvironment.reorderPin, {
@@ -653,6 +699,7 @@ export function useThreadListActions(): {
653699
pinThread,
654700
unpinThread,
655701
moveThread,
702+
renameThread,
656703
regenerateThreadTitle,
657704
};
658705
}

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,7 @@ function ThreadNavigationSidebarPane(
170170
pinThread,
171171
unpinThread,
172172
moveThread,
173+
renameThread,
173174
regenerateThreadTitle,
174175
} = useThreadListActions();
175176
const threadListV2Enabled = useThreadListV2Enabled();
@@ -930,6 +931,7 @@ function ThreadNavigationSidebarPane(
930931
onSelectThread={handleSelectThread}
931932
onDeleteThread={confirmDeleteThread}
932933
onArchiveThread={archiveThread}
934+
onRenameThread={renameThread}
933935
onRegenerateThreadTitle={regenerateThreadTitle}
934936
titleRegenerationSupported={titleRegenerationEnvironmentIds.has(thread.environmentId)}
935937
settlementSupported={settlementEnvironmentIds.has(thread.environmentId)}
@@ -1047,6 +1049,7 @@ function ThreadNavigationSidebarPane(
10471049
fullSwipeWidth={props.width - 20}
10481050
onArchiveThread={archiveThread}
10491051
onDeleteThread={confirmDeleteThread}
1052+
onRenameThread={renameThread}
10501053
onRegenerateThreadTitle={regenerateThreadTitle}
10511054
titleRegenerationSupported={titleRegenerationEnvironmentIds.has(thread.environmentId)}
10521055
onSelectThread={handleSelectThread}
@@ -1089,6 +1092,7 @@ function ThreadNavigationSidebarPane(
10891092
projectByKey,
10901093
projectTitleByProjectKey,
10911094
regenerateThreadTitle,
1095+
renameThread,
10921096
props.onNewThreadInProject,
10931097
props.onNewThreadOnBranch,
10941098
props.searchQuery,

‎apps/mobile/src/features/threads/thread-list-items.tsx‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,6 +438,7 @@ export const PendingTaskListRow = memo(function PendingTaskListRow(props: {
438438

439439
const THREAD_ROW_MENU_ACTIONS: MenuAction[] = [
440440
{ id: "archive", title: "Archive", image: "archivebox" },
441+
{ id: "rename", title: "Rename", image: "square.and.pencil" },
441442
{ id: "delete", title: "Delete", image: "trash", attributes: { destructive: true } },
442443
];
443444

@@ -459,6 +460,7 @@ export const ThreadListRow = memo(function ThreadListRow(props: {
459460
readonly onArchiveThread: (thread: EnvironmentThreadShell) => void;
460461
readonly onDeleteThread: (thread: EnvironmentThreadShell) => void;
461462
readonly onNewThreadOnBranch: (thread: EnvironmentThreadShell) => void;
463+
readonly onRenameThread: (thread: EnvironmentThreadShell) => void;
462464
readonly onRegenerateThreadTitle: (thread: EnvironmentThreadShell) => void;
463465
readonly titleRegenerationSupported: boolean;
464466
readonly onSwipeableWillOpen: (methods: SwipeableMethods) => void;
@@ -490,6 +492,7 @@ export const ThreadListRow = memo(function ThreadListRow(props: {
490492
onSelectThread,
491493
onArchiveThread,
492494
onDeleteThread,
495+
onRenameThread,
493496
onRegenerateThreadTitle,
494497
onNewThreadOnBranch,
495498
} = props;
@@ -534,6 +537,7 @@ export const ThreadListRow = memo(function ThreadListRow(props: {
534537

535538
const handleDelete = useCallback(() => onDeleteThread(thread), [onDeleteThread, thread]);
536539
const handleArchive = useCallback(() => onArchiveThread(thread), [onArchiveThread, thread]);
540+
const handleRename = useCallback(() => onRenameThread(thread), [onRenameThread, thread]);
537541
const handleRegenerateTitle = useCallback(
538542
() => onRegenerateThreadTitle(thread),
539543
[onRegenerateThreadTitle, thread],
@@ -551,11 +555,12 @@ export const ThreadListRow = memo(function ThreadListRow(props: {
551555
]
552556
: []),
553557
THREAD_ROW_MENU_ACTIONS[0]!,
558+
THREAD_ROW_MENU_ACTIONS[1]!,
554559
...buildThreadTitleRegenerationMenuItems({
555560
supported: props.titleRegenerationSupported,
556561
isRegenerating: thread.titleRegeneration != null,
557562
}),
558-
THREAD_ROW_MENU_ACTIONS[1]!,
563+
THREAD_ROW_MENU_ACTIONS[2]!,
559564
],
560565
[props.titleRegenerationSupported, thread.branch, thread.titleRegeneration],
561566
);
@@ -572,10 +577,11 @@ export const ThreadListRow = memo(function ThreadListRow(props: {
572577
({ nativeEvent }: { readonly nativeEvent: { readonly event: string } }) => {
573578
if (nativeEvent.event === "new-thread-on-branch") onNewThreadOnBranch(thread);
574579
if (nativeEvent.event === "archive") handleArchive();
580+
if (nativeEvent.event === "rename") handleRename();
575581
if (nativeEvent.event === "regenerate-title") handleRegenerateTitle();
576582
if (nativeEvent.event === "delete") handleDelete();
577583
},
578-
[handleArchive, handleDelete, handleRegenerateTitle, onNewThreadOnBranch, thread],
584+
[handleArchive, handleDelete, handleRegenerateTitle, handleRename, onNewThreadOnBranch, thread],
579585
);
580586

581587
const statusPill = effectiveStatus ? (

0 commit comments

Comments
 (0)