fix(portal): en idempotensnyckel per vald fil #460

Merged
admin merged 1 commits from fix/ui-upload-key into main 2026-11-03 00:04:55 +00:00
+11 -2
View File
@@ -73,6 +73,10 @@ export default function DocumentsPage() {
const [uploading, setUploading] = useState(false);
const [unknownOutcome, setUnknownOutcome] = useState<unknown | null>(null);
const inputRef = useRef<HTMLInputElement>(null);
// One idempotency key per selected file: a retry (double click, network
// error, reload-and-pick-the-same-file) returns the SAME document instead of
// creating a duplicate. Picking a different file resets it.
const uploadKey = useRef<string | null>(null);
const [instruction, setInstruction] = useState('');
const [fields, setFields] = useState<FieldRow[]>([{ name: 'organization', type: 'string' }]);
@@ -119,7 +123,9 @@ export default function DocumentsPage() {
const form = new FormData();
form.set('file', file);
form.set('project_id', projectId.trim());
form.set('idempotency_key', crypto.randomUUID());
const key = uploadKey.current ?? crypto.randomUUID();
uploadKey.current = key;
form.set('idempotency_key', key);
form.set('classification', 'restricted');
try {
const response = await fetch('/api/0pt/documents/upload', {
@@ -293,7 +299,10 @@ export default function DocumentsPage() {
ref={inputRef}
type="file"
className="hidden"
onChange={(event) => setFile(event.target.files?.[0] ?? null)}
onChange={(event) => {
uploadKey.current = null;
setFile(event.target.files?.[0] ?? null);
}}
/>
</div>