Skip to content

Commit df6843c

Browse files
loganjLarry
andauthored
feat(plugins): let a message renderer claim the attachments it draws (#1408)
Signed-off-by: Logan Johnson <[email protected]> Co-authored-by: Larry <627498bd4bd1f281a16431e3c6cce3b5c25b6692798c78672298aefbf2f8f8b5@buzz.block.builderlab.xyz>
1 parent fd87275 commit df6843c

7 files changed

Lines changed: 481 additions & 19 deletions

File tree

‎docs/plugin-architecture.md‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1100,12 +1100,16 @@ mounted page; see [sidebar ownership](channels.md#ownership).
11001100

11011101
## Conversation contributions
11021102

1103-
`registerMessage({ id, title, matches, component })` contributes an optional whole
1103+
`registerMessage({ id, title, matches, ownsAttachment?, component })` contributes an optional whole
11041104
message body. Components receive `{ message: ChannelMessage, canOpen, open }`; the first active
11051105
match wins, throwing matchers are skipped, and render failure/removal restores the
11061106
host body. Registration uses the same owned contribution lifetime as inline/link
11071107
renderers. The host retains author/time chrome, actions, attachments and session
1108-
ownership. `MessageRenderer` is a host-matched author-preview type, not event
1108+
ownership. `ownsAttachment(message, attachment)` lets the component claim the
1109+
attachments it draws itself: the host leaves those out of its list, keeps the
1110+
others, and lists every one again if the component fails. Claims count only
1111+
while the host draws that component; attachment-only and session-card-only
1112+
messages skip it, so their attachments stay listed. A throwing claim is false. `MessageRenderer` is a host-matched author-preview type, not event
11091113
admission or cross-version capability negotiation.
11101114

11111115
`registerMessageAction({ id, title, icon?, matches, component, marker? })` adds an

‎src/features/conversation/ContributionBoundary.tsx‎

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,30 @@ export function contributionKey(entry: object) {
1212
return key;
1313
}
1414
export class ContributionBoundary extends Component<
15-
{ children: ReactNode; fallback: ReactNode },
15+
{
16+
children: ReactNode;
17+
fallback: ReactNode;
18+
/** True when the fallback replaces the children; false when a failed boundary unmounts. */
19+
onFailedChange?: ((failed: boolean) => void) | undefined;
20+
},
1621
{ failed: boolean }
1722
> {
1823
state = { failed: false };
1924
static getDerivedStateFromError() {
2025
return { failed: true };
2126
}
27+
componentDidCatch() {
28+
this.props.onFailedChange?.(true);
29+
}
30+
// StrictMode replays unmount and mount on the same failed instance without
31+
// replaying componentDidCatch, so a mount reports a retained failure again.
32+
componentDidMount() {
33+
if (this.state.failed) this.props.onFailedChange?.(true);
34+
}
35+
// A remount starts healthy, so anything keyed to this failure must reset too.
36+
componentWillUnmount() {
37+
if (this.state.failed) this.props.onFailedChange?.(false);
38+
}
2239
render() {
2340
return this.state.failed ? this.props.fallback : this.props.children;
2441
}

‎src/features/conversation/MessageBody.tsx‎

Lines changed: 61 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,33 +4,83 @@ import type { ContributionReader, MessageRenderer } from "./contracts";
44
import { ContributionBoundary, contributionKey } from "./ContributionBoundary";
55
import { WithTargetOpener } from "./opener";
66

7+
const EMPTY: readonly never[] = [];
8+
const NONE = { snapshot: () => EMPTY, subscribe: () => () => {} };
9+
10+
/** The renderer for `message`: the first active match. Throwing matchers are skipped. */
11+
export function useMessageRenderer(
12+
registry: ContributionReader<MessageRenderer> | undefined,
13+
message: ChannelMessage,
14+
) {
15+
const reader = registry ?? NONE;
16+
const renderers = useSyncExternalStore(
17+
reader.subscribe,
18+
reader.snapshot,
19+
reader.snapshot,
20+
);
21+
return renderers.find((entry) => {
22+
try {
23+
return entry.matches(message);
24+
} catch {
25+
return false;
26+
}
27+
});
28+
}
29+
730
/** First active match wins; a broken optional renderer leaves the host fallback. */
831
export function MessageBody({
932
registry,
1033
message,
1134
children,
35+
onFailedChange,
1236
}: {
1337
registry: ContributionReader<MessageRenderer>;
1438
message: ChannelMessage;
1539
children: ReactNode;
40+
/** Reports a renderer's failure while its fallback is mounted. */
41+
onFailedChange?: (renderer: MessageRenderer, failed: boolean) => void;
1642
}) {
17-
const renderers = useSyncExternalStore(
18-
registry.subscribe,
19-
registry.snapshot,
20-
registry.snapshot,
43+
const renderer = useMessageRenderer(registry, message);
44+
return (
45+
<MessageCard
46+
registry={registry}
47+
renderer={renderer}
48+
message={message}
49+
onFailedChange={onFailedChange}
50+
>
51+
{children}
52+
</MessageCard>
2153
);
22-
const renderer = renderers.find((entry) => {
23-
try {
24-
return entry.matches(message);
25-
} catch {
26-
return false;
27-
}
28-
});
54+
}
55+
56+
/** Draws an already selected renderer, so its caller can rely on the same choice. */
57+
export function MessageCard({
58+
registry,
59+
renderer,
60+
message,
61+
children,
62+
onFailedChange,
63+
}: {
64+
registry: ContributionReader<MessageRenderer>;
65+
renderer: ReturnType<typeof useMessageRenderer>;
66+
message: ChannelMessage;
67+
children: ReactNode;
68+
/**
69+
* Called with true when the fallback replaces `renderer`, and with false when
70+
* that failed card unmounts: its next mount starts healthy.
71+
*/
72+
onFailedChange?:
73+
| ((renderer: MessageRenderer, failed: boolean) => void)
74+
| undefined;
75+
}) {
2976
if (!renderer) return children;
3077
return (
3178
<ContributionBoundary
3279
key={`${contributionKey(renderer)}:${message.channelId}:${message.id}`}
3380
fallback={children}
81+
onFailedChange={
82+
onFailedChange && ((failed) => onFailedChange(renderer, failed))
83+
}
3484
>
3585
<WithTargetOpener
3686
component={renderer.component}

‎src/features/conversation/contracts.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,14 @@ export type MessageRenderer = Readonly<{
174174
id: string;
175175
title: string;
176176
matches(message: ChannelMessage): boolean;
177+
/** True for each attachment the component draws itself. The host leaves
178+
* those out of its attachment list only while it draws the component, and
179+
* lists them again if the component fails. A throwing claim is treated as
180+
* false. */
181+
ownsAttachment?(
182+
message: ChannelMessage,
183+
attachment: ChannelMessage["attachments"][number],
184+
): boolean;
177185
component: ComponentType<TargetOpener & { message: ChannelMessage }>;
178186
}>;
179187
/** One ⋯ menu entry for a message. The host owns the menu, focus and row lifetime. */

‎src/features/conversation/service.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ export class ConversationService extends Service implements Conversation {
244244
validate(value);
245245
if (typeof value.matches !== "function")
246246
throw new Error("A message renderer needs a matcher");
247+
if (
248+
value.ownsAttachment !== undefined &&
249+
typeof value.ownsAttachment !== "function"
250+
)
251+
throw new Error("A message renderer's ownsAttachment must be a function");
247252
this.messageEntries.register(this.ctx, value);
248253
}
249254
registerAccessory(value: ComposerAccessory) {

0 commit comments

Comments
 (0)