Skip to content

Commit ddf250c

Browse files
committed
feat(mobile): the draft slides between the row and the card as the composer opens and closes
1 parent 693d128 commit ddf250c

1 file changed

Lines changed: 204 additions & 138 deletions

File tree

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

Lines changed: 204 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,14 @@ import {
3131
} from "@t3tools/shared/usageLimits";
3232
import { StackActions, useFocusEffect, useNavigation } from "@react-navigation/native";
3333
import type { ReactNode } from "react";
34-
import { useEffect, useEffectEvent, useRef, useState, type RefObject } from "react";
34+
import {
35+
useEffect,
36+
useEffectEvent,
37+
useLayoutEffect,
38+
useRef,
39+
useState,
40+
type RefObject,
41+
} from "react";
3542
import { I18nManager, Keyboard, Platform, View, type ViewStyle } from "react-native";
3643
import { composerImageGallery } from "../../lib/composerImageGallery";
3744
import { filePreviewGallery } from "../../lib/filePreviewGallery";
@@ -186,27 +193,84 @@ function useCompactComposerEditor(
186193
);
187194
const compactRowHeight = Math.max(44, minimum);
188195
const isRTL = I18nManager.isRTL;
189-
const editorStyle = useAnimatedStyle(() => ({
196+
const targetSurfaceHeight = isExpanded ? height + 52 : compactRowHeight;
197+
const animatedHeight = useSharedValue(height);
198+
const surfaceHeight = useSharedValue(targetSurfaceHeight);
199+
const expansion = useSharedValue(Number(isExpanded));
200+
const expansionTarget = useRef({ expanded: isExpanded, pending: false });
201+
// oxlint-disable-next-line t3code/no-unreasoned-state-sync -- drives the Reanimated card open and close once the editor has laid out at its new width
202+
useLayoutEffect(() => {
203+
const config = { duration: 180, reduceMotion: ReduceMotion.System };
204+
if (Platform.OS === "android") {
205+
animatedHeight.set(height);
206+
surfaceHeight.set(targetSurfaceHeight);
207+
expansion.set(Number(isExpanded));
208+
return;
209+
}
210+
const target = expansionTarget.current;
211+
if (target.expanded === isExpanded && !target.pending) {
212+
animatedHeight.set(withTiming(height, config));
213+
surfaceHeight.set(withTiming(targetSurfaceHeight, config));
214+
return;
215+
}
216+
target.expanded = isExpanded;
217+
target.pending = true;
218+
// Wrapping settles at the destination width a frame later; animating sooner overshoots.
219+
let nextFrame: number | undefined;
220+
const frame = requestAnimationFrame(() => {
221+
nextFrame = requestAnimationFrame(() => {
222+
target.pending = false;
223+
animatedHeight.set(withTiming(height, config));
224+
surfaceHeight.set(withTiming(targetSurfaceHeight, config));
225+
expansion.set(withTiming(Number(isExpanded), config));
226+
});
227+
});
228+
return () => {
229+
cancelAnimationFrame(frame);
230+
if (nextFrame !== undefined) cancelAnimationFrame(nextFrame);
231+
};
232+
}, [animatedHeight, expansion, height, isExpanded, surfaceHeight, targetSurfaceHeight]);
233+
const containerStyle = useAnimatedStyle(() => ({
234+
height: surfaceHeight.value,
235+
}));
236+
const editorStyle = useAnimatedStyle(() => {
237+
const compact = compactEditorInsets({
238+
slotX: editorX.value,
239+
slotWidth: editorWidth.value,
240+
rowWidth: rowWidth.value,
241+
isRTL,
242+
});
243+
const restBottom = (compactRowHeight - minimum) / 2;
244+
return {
245+
position: "absolute",
246+
overflow: "hidden",
247+
height: animatedHeight.value,
248+
left: compact.left + expansion.value * (14 - compact.left),
249+
right: compact.right + expansion.value * (14 - compact.right),
250+
bottom: restBottom + expansion.value * (52 - restBottom),
251+
};
252+
});
253+
const editorContentStyle = useAnimatedStyle(() => ({
190254
position: "absolute",
191-
overflow: "hidden",
192-
height,
193-
...(isExpanded
194-
? { left: 14, right: 14 }
195-
: compactEditorInsets({
196-
slotX: editorX.value,
197-
slotWidth: editorWidth.value,
198-
rowWidth: rowWidth.value,
199-
isRTL,
200-
})),
201-
// The full-width editor must move above the controls in the same frame it widens.
202-
bottom: isExpanded ? 52 : (compactRowHeight - minimum) / 2,
255+
top: 0,
256+
bottom: 0,
257+
start: 0,
258+
width: Math.max(0, isExpanded ? rowWidth.value - 28 : editorWidth.value - 8),
259+
transform: [
260+
{
261+
translateY: isExpanded
262+
? (1 - expansion.value) * ((minimum - text.lineHeight) / 2 - inset)
263+
: 0,
264+
},
265+
],
203266
}));
204267
return {
205268
isExpanded,
206269
modelIconOnly,
207270
rowWidth: slot.rowWidth,
208-
containerStyle: { height: isExpanded ? height + 52 : compactRowHeight },
271+
containerStyle,
209272
editorStyle,
273+
editorContentStyle,
210274
onRowLayout: (event: { nativeEvent: { layout: { width: number } } }) => {
211275
const width = event.nativeEvent.layout.width;
212276
rowWidth.set(width);
@@ -970,135 +1034,137 @@ export const ThreadComposer = function ThreadComposer(props: ThreadComposerProps
9701034
showsCompactDictation ? { display: "none" } : undefined,
9711035
]}
9721036
>
973-
<ComposerEditor
974-
hidden={props.hidden}
975-
draftKey={composerDraftKey}
976-
environmentId={props.environmentId}
977-
onOpenMention={(path) => {
978-
Keyboard.dismiss();
979-
if (props.isDraft) {
980-
navigation.navigate("NewTaskFile", {
981-
environmentId: String(props.environmentId),
982-
cwd: props.projectCwd ?? undefined,
983-
path: fileRoutePathSegments(path),
984-
});
985-
} else {
986-
navigation.navigate("ThreadFile", {
987-
environmentId: String(props.environmentId),
988-
threadId: String(props.selectedThread.id),
989-
path: fileRoutePathSegments(path),
990-
});
991-
}
992-
}}
993-
onOpenAttachment={openDraftDocument}
994-
chipsInert={!isExpanded}
995-
onInertChipPress={() => inputRef.current?.focus()}
996-
ref={inputRef}
997-
multiline
998-
value={draftMessage}
999-
// The save uses a draft snapshot; further typing would be discarded on success.
1000-
readOnly={isSubmissionLocked}
1001-
skills={composerMenu.skills}
1002-
selection={composerMenu.selection}
1003-
onChangeText={(text) => {
1004-
if (!isSubmissionLocked) props.onChangeDraftMessage(text);
1005-
}}
1006-
onSelectionChange={composerMenu.onSelectionChange}
1007-
onPasteImages={(uris) => {
1008-
if (isSubmissionLocked) return;
1009-
pendingNativeImagePasteCountRef.current += 1;
1010-
void props.onNativePasteImages(uris).finally(() => {
1011-
pendingNativeImagePasteCountRef.current -= 1;
1012-
});
1013-
}}
1014-
onPasteText={(paste) => {
1015-
if (isSubmissionLocked) return;
1016-
const insertPaste = () => {
1017-
const insertion = replaceTextSelection({
1018-
value: paste.value,
1019-
selection: paste.selection,
1020-
text: paste.text,
1037+
<Animated.View style={compactEditor.editorContentStyle}>
1038+
<ComposerEditor
1039+
hidden={props.hidden}
1040+
draftKey={composerDraftKey}
1041+
environmentId={props.environmentId}
1042+
onOpenMention={(path) => {
1043+
Keyboard.dismiss();
1044+
if (props.isDraft) {
1045+
navigation.navigate("NewTaskFile", {
1046+
environmentId: String(props.environmentId),
1047+
cwd: props.projectCwd ?? undefined,
1048+
path: fileRoutePathSegments(path),
1049+
});
1050+
} else {
1051+
navigation.navigate("ThreadFile", {
1052+
environmentId: String(props.environmentId),
1053+
threadId: String(props.selectedThread.id),
1054+
path: fileRoutePathSegments(path),
1055+
});
1056+
}
1057+
}}
1058+
onOpenAttachment={openDraftDocument}
1059+
chipsInert={!isExpanded}
1060+
onInertChipPress={() => inputRef.current?.focus()}
1061+
ref={inputRef}
1062+
multiline
1063+
value={draftMessage}
1064+
// The save uses a draft snapshot; further typing would be discarded on success.
1065+
readOnly={isSubmissionLocked}
1066+
skills={composerMenu.skills}
1067+
selection={composerMenu.selection}
1068+
onChangeText={(text) => {
1069+
if (!isSubmissionLocked) props.onChangeDraftMessage(text);
1070+
}}
1071+
onSelectionChange={composerMenu.onSelectionChange}
1072+
onPasteImages={(uris) => {
1073+
if (isSubmissionLocked) return;
1074+
pendingNativeImagePasteCountRef.current += 1;
1075+
void props.onNativePasteImages(uris).finally(() => {
1076+
pendingNativeImagePasteCountRef.current -= 1;
10211077
});
1022-
const selection = { start: insertion.cursor, end: insertion.cursor };
1023-
props.onChangeDraftMessage(insertion.value);
1024-
composerMenu.onSelectionChange(selection);
1025-
};
1026-
const capabilities = props.serverConfig?.environment.capabilities;
1027-
const advertisedMax = capabilities?.fileAttachments?.maxUploadBytes;
1028-
const maxBytes =
1029-
advertisedMax === undefined
1030-
? null
1031-
: clampFileAttachmentUploadBytes(advertisedMax);
1032-
const wouldExceedInputLimit =
1033-
paste.value.length -
1034-
Math.max(0, paste.selection.end - paste.selection.start) +
1035-
paste.text.length >
1036-
PROVIDER_SEND_TURN_MAX_INPUT_CHARS;
1037-
const canAttach =
1038-
maxBytes !== null &&
1039-
countComposerDraftAttachmentsAfterSelection(composerDraftKey, {
1040-
text: paste.value,
1041-
...paste.selection,
1042-
}) < PROVIDER_SEND_TURN_MAX_ATTACHMENTS &&
1043-
new TextEncoder().encode(paste.text).byteLength <= maxBytes;
1044-
if (
1045-
pastedTextDisposition({
1046-
text: paste.text,
1047-
wouldExceedInputLimit,
1048-
canAttach: true,
1049-
}) === "attachment"
1050-
) {
1051-
if (canAttach) {
1052-
pendingPastedTextAttachmentCountRef.current += 1;
1053-
setPendingPastedTextAttachmentCount(
1054-
pendingPastedTextAttachmentCountRef.current,
1055-
);
1056-
const finishAttachment = () => {
1057-
pendingPastedTextAttachmentCountRef.current = Math.max(
1058-
0,
1059-
pendingPastedTextAttachmentCountRef.current - 1,
1060-
);
1078+
}}
1079+
onPasteText={(paste) => {
1080+
if (isSubmissionLocked) return;
1081+
const insertPaste = () => {
1082+
const insertion = replaceTextSelection({
1083+
value: paste.value,
1084+
selection: paste.selection,
1085+
text: paste.text,
1086+
});
1087+
const selection = { start: insertion.cursor, end: insertion.cursor };
1088+
props.onChangeDraftMessage(insertion.value);
1089+
composerMenu.onSelectionChange(selection);
1090+
};
1091+
const capabilities = props.serverConfig?.environment.capabilities;
1092+
const advertisedMax = capabilities?.fileAttachments?.maxUploadBytes;
1093+
const maxBytes =
1094+
advertisedMax === undefined
1095+
? null
1096+
: clampFileAttachmentUploadBytes(advertisedMax);
1097+
const wouldExceedInputLimit =
1098+
paste.value.length -
1099+
Math.max(0, paste.selection.end - paste.selection.start) +
1100+
paste.text.length >
1101+
PROVIDER_SEND_TURN_MAX_INPUT_CHARS;
1102+
const canAttach =
1103+
maxBytes !== null &&
1104+
countComposerDraftAttachmentsAfterSelection(composerDraftKey, {
1105+
text: paste.value,
1106+
...paste.selection,
1107+
}) < PROVIDER_SEND_TURN_MAX_ATTACHMENTS &&
1108+
new TextEncoder().encode(paste.text).byteLength <= maxBytes;
1109+
if (
1110+
pastedTextDisposition({
1111+
text: paste.text,
1112+
wouldExceedInputLimit,
1113+
canAttach: true,
1114+
}) === "attachment"
1115+
) {
1116+
if (canAttach) {
1117+
pendingPastedTextAttachmentCountRef.current += 1;
10611118
setPendingPastedTextAttachmentCount(
10621119
pendingPastedTextAttachmentCountRef.current,
10631120
);
1064-
};
1065-
void props
1066-
.onNativePasteText(paste)
1067-
.then(finishAttachment, finishAttachment);
1068-
} else if (!wouldExceedInputLimit) {
1069-
insertPaste();
1070-
} else {
1071-
Dialog.alert(
1072-
"Pasted text is too large for this message",
1073-
"Remove some text or an attachment, then paste again.",
1074-
);
1121+
const finishAttachment = () => {
1122+
pendingPastedTextAttachmentCountRef.current = Math.max(
1123+
0,
1124+
pendingPastedTextAttachmentCountRef.current - 1,
1125+
);
1126+
setPendingPastedTextAttachmentCount(
1127+
pendingPastedTextAttachmentCountRef.current,
1128+
);
1129+
};
1130+
void props
1131+
.onNativePasteText(paste)
1132+
.then(finishAttachment, finishAttachment);
1133+
} else if (!wouldExceedInputLimit) {
1134+
insertPaste();
1135+
} else {
1136+
Dialog.alert(
1137+
"Pasted text is too large for this message",
1138+
"Remove some text or an attachment, then paste again.",
1139+
);
1140+
}
1141+
return;
10751142
}
1076-
return;
1143+
insertPaste();
1144+
}}
1145+
placeholder={props.placeholder}
1146+
onFocus={handleFocus}
1147+
onBlur={handleBlur}
1148+
onSubmit={(alternate) =>
1149+
void handleSend(
1150+
alternate && sendPresentation.alternate !== null
1151+
? sendPresentation.alternate
1152+
: undefined,
1153+
)
10771154
}
1078-
insertPaste();
1079-
}}
1080-
placeholder={props.placeholder}
1081-
onFocus={handleFocus}
1082-
onBlur={handleBlur}
1083-
onSubmit={(alternate) =>
1084-
void handleSend(
1085-
alternate && sendPresentation.alternate !== null
1086-
? sendPresentation.alternate
1087-
: undefined,
1088-
)
1089-
}
1090-
submitTitle={sendPresentation.label}
1091-
alternateSubmitTitle={
1092-
sendPresentation.alternate === null
1093-
? sendPresentation.label
1094-
: FOLLOW_UP_ACTION_LABEL[sendPresentation.alternate]
1095-
}
1096-
{...compactEditor.editorProps}
1097-
textStyle={{
1098-
...bodyText,
1099-
color: foregroundColor,
1100-
}}
1101-
/>
1155+
submitTitle={sendPresentation.label}
1156+
alternateSubmitTitle={
1157+
sendPresentation.alternate === null
1158+
? sendPresentation.label
1159+
: FOLLOW_UP_ACTION_LABEL[sendPresentation.alternate]
1160+
}
1161+
{...compactEditor.editorProps}
1162+
textStyle={{
1163+
...bodyText,
1164+
color: foregroundColor,
1165+
}}
1166+
/>
1167+
</Animated.View>
11021168
</Animated.View>
11031169
{!isExpanded && stripAttachments.length > 0 ? (
11041170
<View className="h-[44px] shrink-0 flex-row items-center gap-1 pl-1">

0 commit comments

Comments
 (0)