Wait for a per-file acknowledgement
Read and hash real local file bytes, then hold the handler promise until the local adapter acknowledges or rejects each receipt. Cancel and retry keep a stable request ID.
Keyboard
- Browse files and Start queued are available by keyboard.
- Use the file Cancel or Retry action; focus follows the surviving row action.
- Acknowledge or Reject processed files resolves the local adapter outside the queue.
States covered
- Measured local bytes
- Processing while awaiting a receipt
- Cancellation through AbortSignal
- Rejected acknowledgement
- Retry with stable request ID
- No network or durable storage
Install this complete example and its component dependencies.
npx shadcn@latest add https://ui.bld8.dev/r/example-workflow-upload-acknowledgement.jsonComplete source
/*!
MIT License
Copyright (c) 2026 Jaison
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
'use client';
import '@bld8/ui/styles.css';
import { useCallback, useEffect, useRef, useState } from 'react';
import { UploadQueue } from '@bld8/ui/upload-queue';
import type { UploadFileHandler, UploadQueueItem, UploadReceipt } from '@bld8/ui/upload-state';
import { Button } from '@bld8/ui/core';
type PendingReceipt = { accept: () => void; reject: () => void };
export function AcknowledgedFileProcessingExample() {
const [items, setItems] = useState<readonly UploadQueueItem[]>([]);
const [waiting, setWaiting] = useState(0);
const pending = useRef(new Map<string, PendingReceipt>()), alive = useRef(true);
useEffect(() => { alive.current = true; return () => { alive.current = false; }; }, []);
const processFile = useCallback<UploadFileHandler>(async (file, { signal, requestId, onProgress }) => {
const reader = file.stream().getReader();
const chunks: Uint8Array<ArrayBuffer>[] = [];
let loaded = 0;
const cancelReader = () => { void reader.cancel().catch(() => {}); };
signal.addEventListener('abort', cancelReader, { once: true });
try {
while (true) {
if (signal.aborted) throw new DOMException('Cancelled', 'AbortError');
const next = await reader.read(); if (next.done) break;
chunks.push(new Uint8Array(next.value)); loaded += next.value.byteLength; onProgress({ loaded, total: file.size });
}
} finally { signal.removeEventListener('abort', cancelReader); reader.releaseLock(); }
if (signal.aborted) throw new DOMException('Cancelled', 'AbortError');
const bytes = new Uint8Array(loaded); let offset = 0;
for (const chunk of chunks) { bytes.set(chunk, offset); offset += chunk.length; }
const digest = await crypto.subtle.digest('SHA-256', bytes);
if (signal.aborted) throw new DOMException('Cancelled', 'AbortError');
onProgress({ loaded: file.size, total: file.size });
const receipt: UploadReceipt = { id: 'local-' + requestId, requestId, bytes: file.size, sha256: Array.from(new Uint8Array(digest), value => value.toString(16).padStart(2, '0')).join('') };
return new Promise<UploadReceipt>((resolve, reject) => {
const cleanup = () => { pending.current.delete(requestId); signal.removeEventListener('abort', abort); if (alive.current) setWaiting(pending.current.size); };
const abort = () => { cleanup(); reject(new DOMException('Cancelled', 'AbortError')); };
if (signal.aborted) { abort(); return; }
signal.addEventListener('abort', abort, { once: true });
pending.current.set(requestId, { accept: () => { cleanup(); resolve(receipt); }, reject: () => { cleanup(); reject(new Error('Local receipt rejected')); } });
if (alive.current) setWaiting(pending.current.size);
});
}, []);
return <div style={{ width: '100%', display: 'grid', gap: 20 }}>
<p className="bui-hint">No files leave this browser. The local adapter reads and hashes bytes, then waits for your acknowledgement. It does not upload or store files.</p>
<UploadQueue label="Acknowledged local processing" onUploadFile={processFile} onItemsChange={setItems} maxFiles={3} maxSize={256 * 1024} concurrency={1} />
<div style={{ display: 'flex', flexWrap: 'wrap', gap: 12 }}><Button disabled={!waiting} onClick={() => { for (const receipt of [...pending.current.values()]) receipt.accept(); }}>Acknowledge processed files</Button><Button variant="secondary" disabled={!waiting} onClick={() => { for (const receipt of [...pending.current.values()]) receipt.reject(); }}>Reject processed files</Button></div>
<p role="status">{waiting} file acknowledgement{waiting === 1 ? '' : 's'} pending.</p>
<output aria-label="Upload attempt records" className="bui-hint" style={{ overflowWrap: 'anywhere' }}>{JSON.stringify(items.map(item => ({ requestId: item.requestId, attempt: item.attempt, status: item.status })))}</output>
</div>;
}