#
vincentlu
7 天以前 bb71a2e6ae4ccaf7a712ce5ed29f0f5ffade52bd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import * as XLSX from 'xlsx';
import { useCallback, useMemo, useRef, useState } from 'react';
 
export function useExcelParse({ batchSize = 10, processBatch, params }) {
    const importIdRef = useRef(0);
    const [importer, setImporter] = useState({ state: 'idle' });
 
    const reset = useCallback(() => {
        setImporter({ state: 'idle' });
        importIdRef.current += 1;
    }, []);
 
    const parseExcel = useCallback(async (file) => {
        if (!file) return;
        setImporter({ state: 'parsing' });
 
        const importId = importIdRef.current;
 
        try {
            const buf = await file.arrayBuffer();
            const wb = XLSX.read(buf, { type: 'array' });
            const sheet = wb.SheetNames[0];
            const rows = XLSX.utils.sheet_to_json(wb.Sheets[sheet], { defval: '' });
 
            setImporter({
                state: 'running',
                rowCount: rows.length,
                importCount: 0,
                errorCount: 0,
                remainingTime: null,
            });
 
            let totalTime = 0;
            for (let i = 0; i < rows.length; i += batchSize) {
                if (importIdRef.current !== importId) return;
 
                const batch = rows.slice(i, i + batchSize);
                try {
                    const start = Date.now();
                    await processBatch(batch, params);
                    totalTime += Date.now() - start;
 
                    const mean = totalTime / (i + batch.length);
                    setImporter((prev) =>
                        prev.state === 'running'
                            ? {
                                ...prev,
                                importCount: prev.importCount + batch.length,
                                remainingTime: mean * (rows.length - (prev.importCount + batch.length)),
                            }
                            : prev
                    );
                } catch (err) {
                    setImporter((prev) =>
                        prev.state === 'running'
                            ? {
                                ...prev,
                                errorCount: prev.errorCount + batch.length,
                                errorMsg: prev.errorMsg
                                    ? `${prev.errorMsg}\n${err?.message || String(err)}`
                                    : (err?.message || String(err)),
                            }
                            : prev
                    );
                }
            }
 
            setImporter((prev) =>
                prev.state === 'running' ? { ...prev, state: 'complete', remainingTime: null } : prev
            );
        } catch (error) {
            setImporter({ state: 'error', error });
        }
    }, [batchSize, processBatch, params]);
 
    return useMemo(() => ({ importer, parseExcel, reset }), [importer, parseExcel, reset]);
}