Skip to content

Commit 87384fd

Browse files
committed
Refactor metadata out
1 parent 6b8716a commit 87384fd

File tree

5 files changed

+52
-33
lines changed

5 files changed

+52
-33
lines changed

src/components/tree/Reference.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const Reference: FC<Props> = ({
3434
simple = true,
3535
}: Props) => (
3636
<div className="reference" id={label}>
37-
{!!title && simple && <div className="title">{title}</div>}
3837
{!!authors && (
3938
<Fragment>
4039
<span className="authors">
@@ -50,7 +49,7 @@ const Reference: FC<Props> = ({
5049
{". "}
5150
</Fragment>
5251
)}
53-
{!!title && !simple && (
52+
{!!title && (
5453
<Fragment>
5554
<span className="title">{title}</span>
5655
{". "}

src/hooks/useUpload.ts

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,24 @@
1-
import md5 from "md5";
21
import { useCallback, useContext } from "react";
32

43
import FirebaseContext from "../context/FirebaseContext";
54
import FileContext from "../context/FileContext";
6-
import * as isi from "../utils/isiUtils";
7-
import * as scopus from "../utils/scopusUtils";
5+
import metadata from "../utils/metadata";
86

97
const useUpload = () => {
108
const { add, track } = useContext(FileContext);
119
const firebase = useContext(FirebaseContext);
1210

1311
const upload = useCallback(
1412
(name: string, blob: Blob) => {
15-
blob.text().then((text) => {
13+
metadata(name, blob).then((meta) => {
1614
if (firebase === null) return;
17-
const hash = md5(text);
18-
const keywords = isi.looksLikeIsi(text)
19-
? isi.mostCommon(isi.keywords(text), 3)
20-
: isi.mostCommon(scopus.getKeywords(text), 3);
21-
const articles = isi.looksLikeIsi(text)
22-
? isi.countArticles(text)
23-
: scopus.countArticles(text);
24-
const citations = isi.looksLikeIsi(text)
25-
? isi.countReferences(text)
26-
: scopus.countReferences(text);
27-
const metadata = {
28-
name,
29-
blob,
30-
hash,
31-
keywords,
32-
articles,
33-
citations,
34-
valid: true,
35-
};
36-
add(metadata);
37-
const ref = firebase.storage().ref(`isi-files/${hash}`);
15+
add(meta);
16+
const ref = firebase.storage().ref(`isi-files/${meta.hash}`);
3817

3918
ref
4019
.getDownloadURL()
4120
.then(() => {
42-
track(hash, 100);
21+
track(meta.hash, 100);
4322
})
4423
.catch(() => {
4524
const task = ref.put(blob);
@@ -48,11 +27,11 @@ const useUpload = () => {
4827
(snapshot) => {
4928
const percent =
5029
(snapshot.bytesTransferred / snapshot.totalBytes) * 100;
51-
track(hash, percent);
30+
track(meta.hash, percent);
5231
},
5332
(error) => console.log,
5433
() => {
55-
track(hash, 100);
34+
track(meta.hash, 100);
5635
}
5736
);
5837
});

src/utils/customTypes.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,4 @@ export interface Article {
3434
issue?: string | null;
3535
page?: string | null;
3636
doi?: string | null;
37-
simple?: boolean;
3837
}

src/utils/metadata.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { FileMetadata } from "./customTypes";
2+
import * as isi from "./isiUtils";
3+
import * as scopus from "./scopusUtils";
4+
import md5 from "md5";
5+
6+
const metadata = async (name: string, blob: Blob): Promise<FileMetadata> => {
7+
const content = await blob.text();
8+
const hash = md5(content);
9+
10+
if (isi.looksLikeIsi(content)) {
11+
return {
12+
name,
13+
blob,
14+
hash,
15+
keywords: isi.mostCommon(isi.keywords(content), 3),
16+
articles: isi.countArticles(content),
17+
citations: isi.countReferences(content),
18+
valid: true,
19+
};
20+
}
21+
22+
if (scopus.looksLikeScopus(content)) {
23+
return {
24+
name,
25+
blob,
26+
hash,
27+
keywords: isi.mostCommon(scopus.keywords(content), 3),
28+
articles: scopus.countArticles(content),
29+
citations: scopus.countReferences(content),
30+
valid: true,
31+
};
32+
}
33+
34+
return {
35+
name,
36+
hash,
37+
blob,
38+
valid: false,
39+
};
40+
};
41+
42+
export default metadata;

src/utils/scopusUtils.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const looksLikeScopus = (content: string): boolean => {
1717
return true;
1818
};
1919

20-
const getKeywords = (content: string): string[] =>
20+
const keywords = (content: string): string[] =>
2121
content
2222
.split("\n")
2323
.filter((line) => !!line)
@@ -72,4 +72,4 @@ const countReferences = (content: string): number =>
7272
{ counting: false, count: 0 }
7373
).count;
7474

75-
export { looksLikeScopus, getKeywords, countArticles, countReferences };
75+
export { looksLikeScopus, keywords, countArticles, countReferences };

0 commit comments

Comments
 (0)