refactor: read the message draft through an effect event
the render-synced ref was doing what useEffectEvent is for. same behaviour — the cleanup still sees the last committed message — with one fewer effect, and it matches how the rest of the codebase reaches non-reactive values from an effect.
11234520 unmodified lines2627282930303132333435363738394041383940414243444849454647484950512 unmodified linesimport { useEffect, useRef } from 'react';import { useEffect, useEffectEvent } from 'react';
import { useSession } from '#/state/session';
import { useCurrentConvoId } from './current-convo-id';20 unmodified lines };}
/** * saves the active conversation's draft on unmount. * saves the draft when the conversation changes or the composer unmounts. * * @param message current composer text */export function useSaveMessageDraft(message: string) { const { currentConvoId } = useCurrentConvoId(); const { currentAccount } = useSession(); const key = draftKey(currentAccount?.did, currentConvoId); const messageRef = useRef(message); useEffect(() => { messageRef.current = message; }); const getMessage = useEffectEvent(() => message);
useEffect(() => { return () => { if (!key) { return; } if (messageRef.current) { drafts.set(key, messageRef.current); const draft = getMessage(); if (draft) { drafts.set(key, draft); } else { drafts.delete(key); } };2 unmodified lines