Keep vault state and file transfers within their originating session

This commit is contained in:
2026-09-06 09:39:05 +03:00
parent 6dc6fadda8
commit 277c9407a2
5 changed files with 73 additions and 11 deletions
+33 -11
View File
@@ -1,6 +1,8 @@
import { getToken } from '../../services/auth';
import { sessionGuard } from '../../services/sessionGuard';
import { readableDocument, retryDocumentExtraction } from '../../services/storedDocument';
import { documentProcessingLabel } from '../../services/documentProcessing';
import React, { useState, useEffect, useRef } from 'react';
import React, { useState, useEffect, useRef, useCallback } from 'react';
import { useApp } from '../../context/AppContext';
import { listDocuments, uploadDocument, deleteDocument, downloadDocument, formatBayt, updateDocumentMeta } from '../../services/account';
import { FolderKanban, Upload, Trash2, Download, Loader2, AlertTriangle, FileText, HardDrive, Layers, Package, CheckCircle2, XCircle, CalendarClock } from 'lucide-react';
@@ -14,28 +16,42 @@ const KATEGORILER = ['Yasal Belgeler', 'Kalite Belgeleri', 'Mali Belgeler', 'Res
*/
export const DocumentVault = () => {
const { authUser } = useApp();
const [durum, setDurum] = useState(null);
const [busy, setBusy] = useState(false);
const [hata, setHata] = useState(null);
const [islemeBilgi, setIslemeBilgi] = useState(null);
const [kategori, setKategori] = useState(KATEGORILER[0]);
return <DocumentVaultSession key={authUser?.email || 'signed-out'} signedIn={Boolean(authUser)} />;
};
const DocumentVaultSession = ({signedIn}) => {
const live = useRef(true);
const session = useRef(sessionGuard(getToken));
const current = () => live.current && session.current();
useEffect(() => { live.current = true; return () => { live.current = false; }; }, []);
const useOwnedState = (initial) => {
const [value, setValue] = useState(initial);
const setOwned = useCallback((next) => { if (current()) setValue(next); }, []);
return [value, setOwned];
};
const [durum, setDurum] = useOwnedState(null);
const [busy, setBusy] = useOwnedState(false);
const [hata, setHata] = useOwnedState(null);
const [islemeBilgi, setIslemeBilgi] = useOwnedState(null);
const [kategori, setKategori] = useOwnedState(KATEGORILER[0]);
const dosyaRef = useRef(null);
const yukle = () => {
if (!authUser) return;
if (!signedIn || !current()) return;
setHata(null);
listDocuments().then(setDurum).catch((e) => setHata(e.message));
};
useEffect(yukle, [authUser]);
useEffect(yukle, [signedIn]);
const yenidenIsle = async (document) => {
if (!current()) return;
setBusy(true); setHata(null); setIslemeBilgi(null);
try {
const result = await retryDocumentExtraction(document);
setIslemeBilgi(`${document.ad}: ${result.sayi || 0} kalem çıkarıldı.`);
} catch (error) { setHata(error.message); }
finally {
try { setDurum(await listDocuments()); }
try { if (current()) setDurum(await listDocuments()); }
catch { setHata(previous => [previous, 'Belge listesi yenilenemedi.'].filter(Boolean).join(' ')); }
setBusy(false);
}
@@ -59,7 +75,7 @@ export const DocumentVault = () => {
// sorulmaz; kalemler otomatik havuza yazılır. docx/xlsx/zip sunucuda,
// PDF tarayıcıda (pdfjs) okunur. Onlarca şartnameyi tek seferde atmak için.
const topluRef = useRef(null);
const [toplu, setToplu] = useState([]); // [{ad, durum, ikn, kalem, hata}]
const [toplu, setToplu] = useOwnedState([]); // [{ad, durum, ikn, kalem, hata}]
const topluSecildi = async (e) => {
const dosyalar = [...(e.target.files || [])];
if (!dosyalar.length) return;
@@ -67,6 +83,7 @@ export const DocumentVault = () => {
setToplu(dosyalar.map((f) => ({ ad: f.name, durum: 'bekliyor' })));
const guncelle = (i, veri) => setToplu((o) => o.map((x, j) => (j === i ? { ...x, ...veri } : x)));
for (let i = 0; i < dosyalar.length; i++) {
if (!current()) break;
const f = dosyalar[i];
guncelle(i, { durum: 'yukleniyor' });
try {
@@ -80,19 +97,22 @@ export const DocumentVault = () => {
metin = (await extractText(f)).text;
ikn = iknCikar(metin);
}
if (!current()) break;
const cevap = await uploadDocument(f, 'İhale Dokümanı', ikn, { otoIkn: true, documentType:'ihale' });
if (!current()) break;
ikn = cevap?.ikn || ikn;
let kalem = cevap?.kalem?.sayi || null;
let kalemHata = cevap.processing?.status === 'error' ? cevap.processing.error : null;
if (ikn && !kalem && metin) {
try {
const { kalemCikar } = await import('../../services/kalem');
if (!current()) break;
kalem = (await kalemCikar({ ikn, metin, dosya: f.name, documentId:cevap.id })).sayi || null;
} catch (error) { kalemHata = error.message; }
}
guncelle(i, { durum: 'tamam', ikn, kalem, kalemHata });
setDurum(cevap);
try { setDurum(await listDocuments()); }
try { if (current()) setDurum(await listDocuments()); }
catch { setHata('Dosya kayıtlı; güncel belge listesi alınamadı. Listeyi yeniden yükleyin.'); }
} catch (err) {
guncelle(i, { durum: 'hata', hata: err.message });
@@ -105,6 +125,7 @@ export const DocumentVault = () => {
// Şirket belgesi süre takibi: imza sirküleri, oda kayıt, borcu yoktur —
// hepsinin süresi dolar, kimse teklif hazırlanana kadar fark etmez.
const sureYaz = async (id, tarih) => {
if (!current()) return;
setHata(null);
try { setDurum(await updateDocumentMeta(id, { sonKullanma: tarih || null })); }
catch (e) { setHata(e.message); }
@@ -114,6 +135,7 @@ export const DocumentVault = () => {
const yakinda = (durum?.belgeler || []).filter((b) => kalanGun(b) != null && kalanGun(b) >= 0 && kalanGun(b) <= 30);
const sil = async (id) => {
if (!current()) return;
setBusy(true); setHata(null);
try { setDurum(await deleteDocument(id)); }
catch (e) { setHata(e.message); }