refactor(desktop): ref the whole mention-wake options object

useMentionWakePreflight destructured its ten options into locals and then
reassembled them into an identical object twice — once as the useRef
initializer, once as the per-render assignment. Both were hand-maintained
mirrors of the parameter list: adding an option without updating them
silently froze that field at its mount-time value, with no type error.

Keep the options object as the parameter and ref it directly
(contextRef -> optionsRef). There is no field list left to maintain — a
new option is automatically fresh in optionsRef.current, so drift is
impossible by construction. The hook also stops allocating a second
context object per render on top of the caller's literal.

The invalidation effect still destructures the subset it depends on, but
that list is not a mirror: it is the deliberately narrower set of
"which prop changes re-arm the preflight", excluding
getManagedAgentsByPubkey and startManagedAgent as the existing
biome-ignore documents.

useEffectEvent would delete the ref entirely, but prepareMentionWake
escapes the hook into MessageComposer's keystroke path, which violates
its "call only from inside Effects" contract. The latest-ref pattern
stays; it just no longer needs a hand-copied field list.

The public parameter type is unchanged, so the call site and every
existing test compile and pass untouched.

Signed-off-by: Matt Toohey <contact@matttoohey.com>
This commit is contained in:
Matt Toohey
2026-08-26 21:50:24 +10:00
parent 21c1b338b5
commit 600c8b1c17

View File

@@ -69,18 +69,7 @@ export function buildMentionWakePlan({
return { key: `${channelId}:${pubkeys.join(",")}`, pubkeys };
}
export function useMentionWakePreflight({
channelId,
contentRef,
enabled,
expectedRelayUrl,
expectedSignerPubkey,
getDraftMentionRefs,
getManagedAgentsByPubkey,
isManagedAgentPubkey,
memberPubkeys,
startManagedAgent,
}: {
export function useMentionWakePreflight(options: {
channelId: string | null;
contentRef: React.MutableRefObject<string>;
enabled: boolean;
@@ -94,30 +83,10 @@ export function useMentionWakePreflight({
}) {
const timerRef = React.useRef<ReturnType<typeof setTimeout> | null>(null);
const activePlanKeyRef = React.useRef<string | null>(null);
const contextRef = React.useRef({
channelId,
contentRef,
enabled,
expectedRelayUrl,
expectedSignerPubkey,
getDraftMentionRefs,
getManagedAgentsByPubkey,
isManagedAgentPubkey,
memberPubkeys,
startManagedAgent,
});
contextRef.current = {
channelId,
contentRef,
enabled,
expectedRelayUrl,
expectedSignerPubkey,
getDraftMentionRefs,
getManagedAgentsByPubkey,
isManagedAgentPubkey,
memberPubkeys,
startManagedAgent,
};
// Latest-ref over the whole options object: the stable callbacks below read
// fresh values without a hand-maintained field list that could drift.
const optionsRef = React.useRef(options);
optionsRef.current = options;
const cancelMentionWake = React.useCallback(() => {
if (timerRef.current !== null) clearTimeout(timerRef.current);
@@ -126,7 +95,7 @@ export function useMentionWakePreflight({
}, []);
const currentPlan = React.useCallback((content: string) => {
const context = contextRef.current;
const context = optionsRef.current;
if (
!context.enabled ||
!context.expectedRelayUrl ||
@@ -157,6 +126,20 @@ export function useMentionWakePreflight({
: null;
}, []);
// Not a mirror of the options object: this is the subset whose change should
// re-arm the preflight, deliberately excluding getManagedAgentsByPubkey and
// startManagedAgent (see the biome-ignore below).
const {
channelId,
contentRef,
enabled,
expectedRelayUrl,
expectedSignerPubkey,
getDraftMentionRefs,
isManagedAgentPubkey,
memberPubkeys,
} = options;
const prepareMentionWake = React.useCallback(
(content: string) => {
const plan = currentPlan(content);
@@ -172,12 +155,12 @@ export function useMentionWakePreflight({
const beforeLookup = currentPlan(contentRef.current);
if (beforeLookup?.key !== plan.key) return;
const managedAgents =
await contextRef.current.getManagedAgentsByPubkey();
await optionsRef.current.getManagedAgentsByPubkey();
if (activePlanKeyRef.current !== plan.key) return;
const beforeWake = currentPlan(contentRef.current);
if (beforeWake?.key !== plan.key) return;
const context = contextRef.current;
const context = optionsRef.current;
await Promise.allSettled(
beforeWake.pubkeys.flatMap((pubkey) => {
const agent = managedAgents.get(pubkey);
@@ -202,7 +185,7 @@ export function useMentionWakePreflight({
[cancelMentionWake, contentRef, currentPlan],
);
// biome-ignore lint/correctness/useExhaustiveDependencies: contextRef keeps callbacks fresh; these inputs are plan invalidation signals.
// biome-ignore lint/correctness/useExhaustiveDependencies: optionsRef keeps callbacks fresh; these inputs are plan invalidation signals.
React.useEffect(() => {
prepareMentionWake(contentRef.current);
}, [