/
index.tsx
97 lines (88 loc) · 2.6 KB
/
index.tsx
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import { ReactElement, useState, useEffect } from "react";
import styles from "./index.module.scss";
import Papa from "papaparse";
import List from "./List";
import FileDrop from "./FileDrop";
import { split, trim, toArray } from "lodash";
import { ListItem, FormatedRemark } from "./types";
const formatRemark = (string: string): FormatedRemark => {
const splitRes = split(trim(string), '"');
let pointer = 0;
const pointerMap = {
1: "",
2: "",
3: "",
};
const arr = toArray(splitRes[0]);
arr.forEach((letter, index) => {
if (/[\u0000-\u001f]/.test(letter)) {
if (!/[\u0000-\u001f]/.test(arr[index + 1])) {
pointer++;
}
} else {
if (pointer === 0) {
pointer++;
}
pointerMap[pointer] += letter;
}
});
return pointerMap;
};
const formatAvatar = (string: string): string[] => {
return string
.replace(/[\u0000-\u001f�]/g, " ")
.split('"')[0]
.match(/http(s)?:\/\/\S+/g);
};
interface IProps {}
export default function Index({}: IProps): ReactElement {
const [contactList, setContactList] = useState<ListItem[]>([]);
const [viewportHeight, setViewportHeight] = useState<number>(1000);
useEffect(() => {
setViewportHeight(window.innerHeight);
}, []);
const onFileChange = (e) => {
const file = e.target.files[0];
if (file) {
Papa.parse(file, {
complete: (res) => {
const [keys, ...data] = res.data;
const list = data.map((dataItem) => {
const obj = {} as ListItem;
dataItem.forEach((attributeValue, index) => {
obj[keys[index]] = attributeValue;
if (keys[index] === "dbContactRemark") {
obj.formatedRemark = formatRemark(attributeValue);
}
if (keys[index] === "dbContactHeadImage") {
obj.formatedAvatar = formatAvatar(attributeValue);
}
});
return obj;
});
const formatedList = list.filter(
(item) => !!item.userName && item.type !== "4"
);
console.log(formatedList);
setContactList(formatedList);
},
});
}
};
return (
<div className={styles.bg}>
<div className={styles.content}>
{!contactList.length && <FileDrop onFileChange={onFileChange} />}
{!!contactList.length && (
<>
<List data={contactList} viewportHeight={viewportHeight} />
<div
className={styles.backIcon}
onClick={() => setContactList([])}
></div>
</>
)}
</div>
</div>
);
}