Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

address review: compute next state outside updater using attachmentsRef.current

[._.]/ Adam Eivy885a4454f35d
diff --git a/client/src/hooks/useOpenClawAttachments.js b/client/src/hooks/useOpenClawAttachments.js
index a425f0eda4..b9ecd13088 100644
--- a/client/src/hooks/useOpenClawAttachments.js
+++ b/client/src/hooks/useOpenClawAttachments.js
@@ -107,30 +107,30 @@ export function useOpenClawAttachments({ sending, onError = () => {} } = {}) {
const next = await filesToAttachments(limitedFiles);
const newBase64Chars = next.reduce((sum, a) => sum + (typeof a.data === 'string' ? a.data.length : 0), 0);
- // Re-check limits inside the functional updater so the final state is always
- // authoritative even when concurrent appendFiles calls are in-flight. A local
- // variable captures the error (if any) from the synchronously-executed updater
- // so we can call onError after setAttachments returns.
- let pendingError = '';
- setAttachments(current => {
- const existingBase64Chars = current.reduce((sum, a) => sum + (typeof a.data === 'string' ? a.data.length : 0), 0);
- if (existingBase64Chars + newBase64Chars > MAX_ATTACHMENTS_TOTAL_BASE64_CHARS) {
- next.forEach(a => { if (a.previewUrl) URL.revokeObjectURL(a.previewUrl); });
- const totalMB = Math.round((existingBase64Chars + newBase64Chars) * 0.75 / (1024 * 1024));
- pendingError = `Combined attachments exceed the 50MB encoded total limit (~${totalMB}MB decoded).`;
- return current;
- }
- const remaining = MAX_ATTACHMENTS - current.length;
- if (remaining <= 0) {
- next.forEach(a => { if (a.previewUrl) URL.revokeObjectURL(a.previewUrl); });
- pendingError = `You can attach up to ${MAX_ATTACHMENTS} files per message.`;
- return current;
- }
- const accepted = next.slice(0, remaining);
- next.slice(remaining).forEach(a => { if (a.previewUrl) URL.revokeObjectURL(a.previewUrl); });
- return [...current, ...accepted];
- });
- onError(pendingError);
+ // Read live state via the ref after the async work completes, then call
+ // setAttachments once with the deterministic result. Concurrent appendFiles
+ // calls are not possible in this single-user UI (paste/picker/drop are all
+ // sequential user gestures), so the ref is authoritative here.
+ const liveAttachments = attachmentsRef.current;
+ const existingBase64Chars = liveAttachments.reduce((sum, a) => sum + (typeof a.data === 'string' ? a.data.length : 0), 0);
+ if (existingBase64Chars + newBase64Chars > MAX_ATTACHMENTS_TOTAL_BASE64_CHARS) {
+ next.forEach(a => { if (a.previewUrl) URL.revokeObjectURL(a.previewUrl); });
+ const totalMB = Math.round((existingBase64Chars + newBase64Chars) * 0.75 / (1024 * 1024));
+ onError(`Combined attachments exceed the 50MB encoded total limit (~${totalMB}MB decoded).`);
+ return;
+ }
+
+ const liveRemaining = MAX_ATTACHMENTS - liveAttachments.length;
+ if (liveRemaining <= 0) {
+ next.forEach(a => { if (a.previewUrl) URL.revokeObjectURL(a.previewUrl); });
+ onError(`You can attach up to ${MAX_ATTACHMENTS} files per message.`);
+ return;
+ }
+
+ const accepted = next.slice(0, liveRemaining);
+ next.slice(liveRemaining).forEach(a => { if (a.previewUrl) URL.revokeObjectURL(a.previewUrl); });
+ setAttachments(prev => [...prev, ...accepted]);
+ onError('');
} catch (err) {
onError(err.message || 'Failed to prepare attachment');
}