mirror of
https://github.com/block/buzz.git
synced 2026-11-24 10:12:19 +00:00
A picked chip is now bound to its recipient by its place in the draft, not by its label. The editor carries each binding across every edit with a selection-aware diff, so deleting the first of two identical chips removes that recipient, text typed before a chip moves it, and a chip stays bound until an edit reaches into it or a letter or digit is typed directly against it. Punctuation, straight and curly apostrophes, a hyphen or a slash after a chip keep it and its recipient (`@Bob's idea`, `@Bob-bot`, `@Bob/Alice`), the same boundary rule buzz-app uses (recommendation 3). Namesakes no longer put a public key in the composer. A picked chip whose name another candidate shares reads the chooser's short label, `@Sam · a1b2`, while the draft text stays `@Sam`; both Sams can be picked and each chip keeps its own key. The sent content qualifies namesakes of different keys as `@Sam (<key>)` so bubbles still tell them apart, except inside code, and each picked Sam gets its own p tag (recommendation 4). The ambiguity refusal for a typed, unpicked `@sam` is unchanged. Drafts persist their bindings as positions (`mentions`), with the saved label-keyed `mention_keys` still read and an unreadable record kept as a tombstone that refuses to send while an `@` remains. The agent prefill and failed-send recovery restore text and bindings in one update. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Signed-off-by: Matt Toohey <contact@matttoohey.com>
274 lines
9.7 KiB
Dart
274 lines
9.7 KiB
Dart
part of '../compose_bar.dart';
|
|
|
|
Future<void> _sendTextOnlyDraft({
|
|
required BuildContext context,
|
|
required _MarkdownEditingController controller,
|
|
required ObjectRef<int> draftRevision,
|
|
required int submittedDraftRevision,
|
|
required FocusNode focusNode,
|
|
required VoidCallback clearComposer,
|
|
required Future<void> Function() addMentionedNonMembers,
|
|
required _ComposeDraftPayload payload,
|
|
required _OutgoingMentions outgoing,
|
|
required ComposeBarOnSend onSend,
|
|
required ScaffoldMessengerState? messenger,
|
|
}) async {
|
|
TextEditingValue? clearedDraftText;
|
|
List<_ComposerMention>? clearedDraftMentions;
|
|
var clearedDraftMentionsInvalid = false;
|
|
int? clearedDraftRevision;
|
|
|
|
void restoreClearedDraft() {
|
|
if (!context.mounted ||
|
|
clearedDraftText == null ||
|
|
clearedDraftMentions == null ||
|
|
clearedDraftRevision == null ||
|
|
draftRevision.value != clearedDraftRevision) {
|
|
return;
|
|
}
|
|
controller.replaceDraft(
|
|
clearedDraftText,
|
|
mentions: clearedDraftMentions,
|
|
mentionsInvalid: clearedDraftMentionsInvalid,
|
|
);
|
|
focusNode.requestFocus();
|
|
}
|
|
|
|
try {
|
|
await addMentionedNonMembers();
|
|
// A draft that changed while adding ran is not the draft the user sent.
|
|
// Keep it and send nothing.
|
|
if (context.mounted && draftRevision.value != submittedDraftRevision) {
|
|
messenger?.showFeedbackSnackBar(
|
|
const SnackBar(content: Text('Not sent. Message changed.')),
|
|
kind: ToastKind.error,
|
|
);
|
|
return;
|
|
}
|
|
// Clear before optimistic insertion so the outgoing row and draft never
|
|
// appear simultaneously during the send transition.
|
|
if (context.mounted) {
|
|
clearedDraftText = controller.value;
|
|
clearedDraftMentions = controller.mentions;
|
|
clearedDraftMentionsInvalid = controller.mentionsInvalid;
|
|
clearComposer();
|
|
clearedDraftRevision = draftRevision.value;
|
|
}
|
|
await onSend(
|
|
payload.content,
|
|
outgoing.pubkeys,
|
|
mediaTags: [...payload.mediaTags, ...outgoing.referenceTags],
|
|
);
|
|
} on StateError {
|
|
restoreClearedDraft();
|
|
_reportSendCancelledByCommunitySwitch(messenger);
|
|
} catch (error) {
|
|
// The caller runs unawaited, so surface publish failures and restore the
|
|
// sent draft unless the user has already started a new one.
|
|
restoreClearedDraft();
|
|
messenger?.showFeedbackSnackBar(
|
|
SnackBar(content: Text(_composeSendErrorMessage(error))),
|
|
kind: ToastKind.error,
|
|
);
|
|
}
|
|
}
|
|
|
|
void _useComposeDraftLifecycle({
|
|
required BuildContext context,
|
|
required ObjectRef<_ComposerVoiceNote> voiceNote,
|
|
required WidgetRef ref,
|
|
required _MarkdownEditingController controller,
|
|
required String draftKey,
|
|
required String channelId,
|
|
required String? threadHeadId,
|
|
required String draftIdentity,
|
|
required ObjectRef<int> draftRevision,
|
|
required ObjectRef<String?> prefillText,
|
|
required ValueNotifier<List<_PendingAttachment>> attachments,
|
|
required ObjectRef<int> uploadGeneration,
|
|
required ObjectRef<UploadCancellationToken?> activeUploadCancellation,
|
|
required ValueNotifier<int> uploadingCount,
|
|
required ValueNotifier<bool> isSending,
|
|
required ValueNotifier<_AttachmentSurface> attachmentSurface,
|
|
required ValueNotifier<String?> uploadError,
|
|
required _IOSAttachmentPopoverController iosAttachmentPopover,
|
|
required VoidCallback onDraftIdentityChanged,
|
|
}) {
|
|
// Retire the old listener before restoring another scope's text: replacement
|
|
// effects can run before the previous effect's cleanup.
|
|
final owner = useMemoized(Object.new, [draftKey, draftIdentity]);
|
|
final currentOwner = useRef(owner)..value = owner;
|
|
final lastDraftIdentity = useRef<String?>(null);
|
|
useEffect(() {
|
|
final identity = '$draftIdentity\u0000$draftKey';
|
|
final shouldHydrate = lastDraftIdentity.value != identity;
|
|
final identityChanged =
|
|
lastDraftIdentity.value != null && lastDraftIdentity.value != identity;
|
|
lastDraftIdentity.value = identity;
|
|
final saved = ref.read(composeDraftsProvider.notifier).draftFor(draftKey);
|
|
// Text and bindings restore from one persisted snapshot, together.
|
|
final restored = [
|
|
if (saved != null)
|
|
for (final mention in saved.mentions)
|
|
_ComposerMention.restored(mention, saved.text),
|
|
];
|
|
void hydrate(String text) => controller.replaceDraft(
|
|
TextEditingValue(text: text),
|
|
mentions: restored,
|
|
mentionsInvalid: saved?.mentionsInvalid ?? false,
|
|
);
|
|
if (identityChanged) {
|
|
prefillText.value = null;
|
|
draftRevision.value += 1;
|
|
onDraftIdentityChanged();
|
|
uploadGeneration.value += 1;
|
|
activeUploadCancellation.value?.cancel();
|
|
activeUploadCancellation.value = null;
|
|
uploadingCount.value = 0;
|
|
isSending.value = false;
|
|
attachmentSurface.value = _AttachmentSurface.closed;
|
|
uploadError.value = null;
|
|
unawaited(iosAttachmentPopover.dispose());
|
|
final staleAttachments = attachments.value;
|
|
attachments.value = const [];
|
|
unawaited(_deleteOwnedAttachments(staleAttachments));
|
|
hydrate(saved?.text ?? '');
|
|
} else if (saved != null && controller.text.isEmpty) {
|
|
hydrate(saved.text);
|
|
} else if (shouldHydrate && saved == null) {
|
|
controller.replaceDraft(controller.value, mentions: const []);
|
|
}
|
|
|
|
var lastPersistedText = controller.text;
|
|
var lastBindings = [for (final m in controller.mentions) m.toDraft()];
|
|
var lastInvalid = controller.mentionsInvalid;
|
|
void persistDraft() {
|
|
if (!identical(currentOwner.value, owner)) return;
|
|
final text = controller.text;
|
|
// An untouched agent prefill is not a draft.
|
|
if (text == prefillText.value) return;
|
|
// The editor already carried the bindings across the edit, so text and
|
|
// bindings are one snapshot here.
|
|
final bindings = [for (final m in controller.mentions) m.toDraft()];
|
|
final invalid = controller.mentionsInvalid;
|
|
if (text == lastPersistedText &&
|
|
invalid == lastInvalid &&
|
|
listEquals(bindings, lastBindings)) {
|
|
return;
|
|
}
|
|
lastBindings = bindings;
|
|
lastInvalid = invalid;
|
|
lastPersistedText = text;
|
|
draftRevision.value += 1;
|
|
ref
|
|
.read(composeDraftsProvider.notifier)
|
|
.save(
|
|
key: draftKey,
|
|
channelId: channelId,
|
|
threadHeadId: threadHeadId,
|
|
text: text,
|
|
mentions: bindings,
|
|
mentionsInvalid: invalid,
|
|
);
|
|
}
|
|
|
|
controller.addListener(persistDraft);
|
|
// Another writer, such as a failed quick send from Siri, can save this
|
|
// draft while the composer is open; show its text rather than overwrite
|
|
// it with the next keystroke. Own saves match lastPersistedText.
|
|
final external = ref.listenManual(
|
|
composeDraftsProvider.select(
|
|
(drafts) => drafts.where((d) => d.key == draftKey).firstOrNull,
|
|
),
|
|
(_, saved) {
|
|
if (!identical(currentOwner.value, owner)) return;
|
|
if (saved == null ||
|
|
((saved.text == lastPersistedText ||
|
|
saved.text == controller.text) &&
|
|
saved.mentionsInvalid == lastInvalid &&
|
|
listEquals(saved.mentions, lastBindings))) {
|
|
return;
|
|
}
|
|
// A newer draft: an ordinary send that fails must not restore over it.
|
|
// Text and the keys its mentions are bound to change together.
|
|
draftRevision.value += 1;
|
|
lastPersistedText = saved.text;
|
|
lastBindings = List.of(saved.mentions);
|
|
lastInvalid = saved.mentionsInvalid;
|
|
controller.replaceDraft(
|
|
TextEditingValue(text: saved.text),
|
|
mentions: [
|
|
for (final mention in saved.mentions)
|
|
_ComposerMention.restored(mention, saved.text),
|
|
],
|
|
mentionsInvalid: saved.mentionsInvalid,
|
|
);
|
|
},
|
|
);
|
|
return () {
|
|
controller.removeListener(persistDraft);
|
|
external.close();
|
|
};
|
|
}, [controller, draftKey, draftIdentity, onDraftIdentityChanged]);
|
|
_useComposerShareDelivery(
|
|
context: context,
|
|
ref: ref,
|
|
draftKey: draftKey,
|
|
controller: controller,
|
|
attachments: attachments,
|
|
draftRevision: draftRevision,
|
|
voiceNote: voiceNote,
|
|
uploadError: uploadError,
|
|
);
|
|
}
|
|
|
|
// Report aggregate work independently of rebuilds, including sub-frame failures.
|
|
void _useComposerBusy({
|
|
required ValueNotifier<bool> isSending,
|
|
required ValueNotifier<int> uploadingCount,
|
|
required ValueNotifier<int> preparingAttachments,
|
|
required bool isDraft,
|
|
required ValueChanged<bool>? onChanged,
|
|
}) {
|
|
final busyCallback = useRef(onChanged);
|
|
busyCallback.value = onChanged;
|
|
useEffect(() {
|
|
var active = true;
|
|
void reportCurrent() {
|
|
if (!active) return;
|
|
busyCallback.value?.call(
|
|
isSending.value ||
|
|
uploadingCount.value > 0 ||
|
|
(isDraft && preparingAttachments.value > 0),
|
|
);
|
|
}
|
|
|
|
void reportBusy() {
|
|
// Scope changes can reset the composer from a build-time effect.
|
|
// Ordinary operation completions still release the parent immediately.
|
|
if (SchedulerBinding.instance.schedulerPhase ==
|
|
SchedulerPhase.persistentCallbacks) {
|
|
WidgetsBinding.instance.addPostFrameCallback((_) => reportCurrent());
|
|
} else {
|
|
reportCurrent();
|
|
}
|
|
}
|
|
|
|
final sources = [isSending, uploadingCount, preparingAttachments];
|
|
for (final source in sources) {
|
|
source.addListener(reportBusy);
|
|
}
|
|
return () {
|
|
active = false;
|
|
for (final source in sources) {
|
|
source.removeListener(reportBusy);
|
|
}
|
|
// Disposal can happen during a parent build. Release after that frame.
|
|
final callback = busyCallback.value;
|
|
WidgetsBinding.instance.addPostFrameCallback(
|
|
(_) => callback?.call(false),
|
|
);
|
|
};
|
|
}, [isSending, uploadingCount, preparingAttachments, isDraft]);
|
|
}
|