Skip to content

Commit 6af20bc

Browse files
committed
perf(mobile): retain unchanged Markdown components
Signed-off-by: Tom Brow <[email protected]>
1 parent 4cd82f5 commit 6af20bc

3 files changed

Lines changed: 488 additions & 36 deletions

File tree

‎mobile/lib/features/channels/message_content.dart‎

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import 'dart:io';
33
import 'dart:math' as math;
44

55
import 'package:flutter/material.dart';
6+
import 'package:flutter/foundation.dart';
67
import 'package:flutter/services.dart';
78
import 'package:flutter_hooks/flutter_hooks.dart';
89
import 'package:hooks_riverpod/hooks_riverpod.dart';
@@ -33,6 +34,7 @@ import 'message_media.dart';
3334
import 'voice_note_attachment.dart';
3435

3536
part 'message_content/media_carousel.dart';
37+
part 'message_content/inline_components.dart';
3638
part 'message_content/token_pill.dart';
3739
part 'message_content/video_preview.dart';
3840

@@ -176,13 +178,33 @@ class MessageContent extends HookConsumerWidget {
176178
in ref.watch(channelsProvider).asData?.value ?? const [])
177179
channel.name.toLowerCase(): channel.id,
178180
};
179-
final resolvedChannelTap =
180-
onChannelTap ??
181-
(String channelId) {
181+
final channelHandler = useRef(onChannelTap)..value = onChannelTap;
182+
final resolvedChannelTap = useMemoized(
183+
() => (String channelId) {
184+
final handler = channelHandler.value;
185+
if (handler != null) {
186+
handler(channelId);
187+
} else {
182188
ref
183189
.read(pendingDeepLinkProvider.notifier)
184190
.open(Uri(scheme: 'buzz', host: 'channel', path: channelId));
185-
};
191+
}
192+
},
193+
const [],
194+
);
195+
final replyHandler = useRef(onMediaReply)..value = onMediaReply;
196+
final moreHandler = useRef(onMediaMore)..value = onMediaMore;
197+
final mediaReply = useMemoized(
198+
() =>
199+
() => replyHandler.value?.call(),
200+
const [],
201+
);
202+
final mediaMore = useMemoized(
203+
() =>
204+
(BuildContext context, String url) =>
205+
moreHandler.value?.call(context, url),
206+
const [],
207+
);
186208
final channelPresentationKey = [
187209
for (final entry
188210
in (resolvedChannelNames.entries.toList()
@@ -265,7 +287,23 @@ class MessageContent extends HookConsumerWidget {
265287
result = '\u200B$result';
266288
}
267289
return result;
268-
}, [linkNormalizedContent, resolvedMentionNames]);
290+
}, [linkNormalizedContent, mentionPresentationKey]);
291+
292+
final inlineComponents = _useMessageInlineComponents(
293+
content: content,
294+
finalContent: finalContent,
295+
mentionNames: resolvedMentionNames,
296+
bindings: mentionBindings,
297+
agentPubkeys: resolvedAgentMentionPubkeys,
298+
channelNames: resolvedChannelNames,
299+
customEmoji: customEmoji,
300+
emojiSize: inlineCustomEmojiSize,
301+
tags: tags,
302+
hasMediaReply: onMediaReply != null,
303+
hasMediaMore: onMediaMore != null,
304+
onMentionTap: onMentionTap,
305+
onChannelTap: resolvedChannelTap,
306+
);
269307

270308
final markdown = KeyedSubtree(
271309
key: ValueKey(
@@ -291,35 +329,16 @@ class MessageContent extends HookConsumerWidget {
291329
resolvedChannelTap,
292330
resolvedChannelNames,
293331
),
294-
imageBuilder: (context, imageUrl, _, _) =>
295-
_buildMedia(context, imageUrl, imetaByUrl[imageUrl]),
332+
imageBuilder: (context, imageUrl, _, _) => _buildMedia(
333+
context,
334+
imageUrl,
335+
imetaByUrl[imageUrl],
336+
onReply: onMediaReply == null ? null : mediaReply,
337+
onMore: onMediaMore == null ? null : mediaMore,
338+
),
296339
textAlign: textAlign,
297340
maxLines: maxLines,
298-
inlineComponents: [
299-
_MentionMd(
300-
mentionNames: resolvedMentionNames,
301-
bindings: mentionBindings,
302-
displayLabels: {
303-
for (final range in mentionOccurrences(
304-
content,
305-
mentionBindings.keys,
306-
))
307-
range.label: content.substring(range.start + 1, range.end),
308-
},
309-
agentMentionPubkeys: resolvedAgentMentionPubkeys,
310-
onMentionTap: onMentionTap,
311-
),
312-
CustomEmojiMd(
313-
customEmoji,
314-
content: finalContent,
315-
size: inlineCustomEmojiSize,
316-
),
317-
_ChannelLinkMd(
318-
channelNames: resolvedChannelNames,
319-
onChannelTap: resolvedChannelTap,
320-
),
321-
...MarkdownComponent.inlineComponents,
322-
],
341+
inlineComponents: inlineComponents,
323342
),
324343
);
325344
if (trailingGallery == null) return markdown;
@@ -343,7 +362,13 @@ class MessageContent extends HookConsumerWidget {
343362
);
344363
}
345364

346-
Widget _buildMedia(BuildContext context, String imageUrl, ImetaEntry? imeta) {
365+
Widget _buildMedia(
366+
BuildContext context,
367+
String imageUrl,
368+
ImetaEntry? imeta, {
369+
VoidCallback? onReply,
370+
MediaViewerMoreAction? onMore,
371+
}) {
347372
final mediaKind = classifyMediaUrl(imageUrl, imeta: imeta);
348373
if (mediaKind == MessageMediaKind.audio) {
349374
return Padding(
@@ -360,15 +385,15 @@ class MessageContent extends HookConsumerWidget {
360385
return _MessageVideoPreview(
361386
url: imageUrl,
362387
imeta: imeta,
363-
onReply: onMediaReply,
388+
onReply: onReply,
364389
);
365390
}
366391
return _MessageImagePreview(
367392
url: imageUrl,
368393
imeta: imeta,
369394
semanticLabel: imeta?.alt ?? 'Message image',
370-
onReply: onMediaReply,
371-
onMore: onMediaMore,
395+
onReply: onReply,
396+
onMore: onMore,
372397
);
373398
}
374399

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
part of '../message_content.dart';
2+
3+
List<MarkdownComponent> _useMessageInlineComponents({
4+
required String content,
5+
required String finalContent,
6+
required Map<String, String> mentionNames,
7+
required Map<String, Set<String>> bindings,
8+
required Set<String> agentPubkeys,
9+
required Map<String, String> channelNames,
10+
required List<CustomEmoji> customEmoji,
11+
required double emojiSize,
12+
required List<List<String>> tags,
13+
required bool hasMediaReply,
14+
required bool hasMediaMore,
15+
required void Function(String)? onMentionTap,
16+
required void Function(String) onChannelTap,
17+
}) {
18+
// Parsed spans retain these callbacks. Forward to the current handlers without
19+
// invalidating the parser for a new closure from an otherwise unchanged row.
20+
final mentionHandler = useRef(onMentionTap)..value = onMentionTap;
21+
final channelHandler = useRef(onChannelTap)..value = onChannelTap;
22+
final mentionTap = useMemoized(
23+
() =>
24+
(String pubkey) => mentionHandler.value?.call(pubkey),
25+
const [],
26+
);
27+
final channelTap = useMemoized(
28+
() =>
29+
(String channelId) => channelHandler.value(channelId),
30+
const [],
31+
);
32+
final inputs = _InlineComponentInputs(
33+
content: content,
34+
finalContent: finalContent,
35+
mentionNames: mentionNames,
36+
bindings: bindings,
37+
agentPubkeys: agentPubkeys,
38+
channelNames: channelNames,
39+
customEmoji: customEmoji,
40+
emojiSize: emojiSize,
41+
tags: tags,
42+
hasMediaReply: hasMediaReply,
43+
hasMediaMore: hasMediaMore,
44+
hasMentionHandler: onMentionTap != null,
45+
);
46+
47+
// gpt_markdown compares components by identity. Recreating them on every
48+
// parent rebuild reparses unchanged message bodies on the UI isolate.
49+
return useMemoized(
50+
() => [
51+
_MentionMd(
52+
mentionNames: inputs.mentionNames,
53+
bindings: inputs.bindings,
54+
displayLabels: {
55+
for (final range in mentionOccurrences(content, bindings.keys))
56+
range.label: content.substring(range.start + 1, range.end),
57+
},
58+
agentMentionPubkeys: inputs.agentPubkeys,
59+
onMentionTap: inputs.hasMentionHandler ? mentionTap : null,
60+
),
61+
CustomEmojiMd(inputs.customEmoji, content: finalContent, size: emojiSize),
62+
_ChannelLinkMd(
63+
channelNames: inputs.channelNames,
64+
onChannelTap: channelTap,
65+
),
66+
...MarkdownComponent.inlineComponents,
67+
],
68+
[inputs],
69+
);
70+
}
71+
72+
class _InlineComponentInputs {
73+
final String content;
74+
final String finalContent;
75+
final Map<String, String> mentionNames;
76+
final Map<String, Set<String>> bindings;
77+
final Set<String> agentPubkeys;
78+
final Map<String, String> channelNames;
79+
final List<CustomEmoji> customEmoji;
80+
final double emojiSize;
81+
final bool hasMentionHandler;
82+
final List<List<String>> tags;
83+
final bool hasMediaReply;
84+
final bool hasMediaMore;
85+
86+
_InlineComponentInputs({
87+
required this.content,
88+
required this.finalContent,
89+
required Map<String, String> mentionNames,
90+
required Map<String, Set<String>> bindings,
91+
required Set<String> agentPubkeys,
92+
required Map<String, String> channelNames,
93+
required List<CustomEmoji> customEmoji,
94+
required this.emojiSize,
95+
required this.hasMentionHandler,
96+
required List<List<String>> tags,
97+
required this.hasMediaReply,
98+
required this.hasMediaMore,
99+
}) : tags = List.unmodifiable(
100+
tags.map((tag) => List<String>.unmodifiable(tag)),
101+
),
102+
mentionNames = Map.unmodifiable(mentionNames),
103+
bindings = Map.unmodifiable({
104+
for (final entry in bindings.entries)
105+
entry.key: Set<String>.unmodifiable(entry.value),
106+
}),
107+
agentPubkeys = Set.unmodifiable(agentPubkeys),
108+
channelNames = Map.unmodifiable(channelNames),
109+
customEmoji = List.unmodifiable(customEmoji);
110+
111+
@override
112+
bool operator ==(Object other) =>
113+
other is _InlineComponentInputs &&
114+
content == other.content &&
115+
finalContent == other.finalContent &&
116+
emojiSize == other.emojiSize &&
117+
hasMentionHandler == other.hasMentionHandler &&
118+
hasMediaReply == other.hasMediaReply &&
119+
hasMediaMore == other.hasMediaMore &&
120+
tags.length == other.tags.length &&
121+
tags.indexed.every(
122+
(entry) => listEquals(entry.$2, other.tags[entry.$1]),
123+
) &&
124+
mapEquals(mentionNames, other.mentionNames) &&
125+
mapEquals(channelNames, other.channelNames) &&
126+
setEquals(agentPubkeys, other.agentPubkeys) &&
127+
listEquals(customEmoji, other.customEmoji) &&
128+
bindings.length == other.bindings.length &&
129+
bindings.entries.every(
130+
(entry) => setEquals(entry.value, other.bindings[entry.key]),
131+
);
132+
133+
@override
134+
int get hashCode =>
135+
Object.hash(content, finalContent, emojiSize, hasMentionHandler);
136+
}

0 commit comments

Comments
 (0)