1
|
import { useEffect, useState } from "react"
|
2
|
import { ScrollView, Text, VStack } from "native-base"
|
3
|
import { useSelector } from "react-redux"
|
4
|
import { RootState } from "../../stores/store"
|
5
|
import ItemPreview from "./ItemPreview"
|
6
|
import { ItemPreviewType } from "../../types/listViewTypes"
|
7
|
import { log } from "../../logging/logger"
|
8
|
|
9
|
|
10
|
const ListView = () => {
|
11
|
const items = useSelector((state: RootState) => state.listView.data)
|
12
|
|
13
|
/**
|
14
|
* Searches through names by given role, return getty_data.display_name if exists otherwise name.value
|
15
|
* @param item
|
16
|
* @param role
|
17
|
*/
|
18
|
const parseNameByRole = (item: ItemPreviewType, role: string) => {
|
19
|
// @ts-ignore
|
20
|
const name = item.object[0].name?.find((name) => name.role === role)
|
21
|
return name && name ?
|
22
|
name.getty_data ?
|
23
|
name.getty_data.display_name : name.value
|
24
|
: ""
|
25
|
}
|
26
|
|
27
|
/**
|
28
|
* Returns artist if it is original, otherwise returns copist and original artist in copy of
|
29
|
* @param item
|
30
|
*/
|
31
|
const parseArtists = (item: ItemPreviewType) => {
|
32
|
// if is copy - display copist and copy of original artist if they are present in data
|
33
|
if (item.src?.value && item.src?.value == "copy") {
|
34
|
const copist = parseNameByRole(item, "copist")
|
35
|
const artist = parseNameByRole(item, "artist")
|
36
|
return `${ copist } ${ artist !== "" ? `(copy of ${ artist })` : "" }`
|
37
|
} else {
|
38
|
// if is original - display artist
|
39
|
return parseNameByRole(item, "artist")
|
40
|
}
|
41
|
}
|
42
|
|
43
|
/**
|
44
|
* Parses image name
|
45
|
* cycles through all objects and returns first image name with changed suffix to .png of undefined if not found
|
46
|
* @param item
|
47
|
*/
|
48
|
const parseImage = (item: ItemPreviewType) => {
|
49
|
// @ts-ignore
|
50
|
const image = item.object.find((object) => object.images)
|
51
|
// @ts-ignore
|
52
|
return image && image.images ? image.images[0].file.split('.')[0] + '.png' : undefined
|
53
|
}
|
54
|
|
55
|
useEffect(() => {
|
56
|
log.debug("ListView", "Items:", items)
|
57
|
}, [items])
|
58
|
|
59
|
return (
|
60
|
<>
|
61
|
{ items.map((item) => (
|
62
|
<ItemPreview
|
63
|
// @ts-ignore
|
64
|
caption={ item.object[0].caption }
|
65
|
title={ item.text }
|
66
|
name={ parseArtists(item) }
|
67
|
image={ parseImage(item) }
|
68
|
/>
|
69
|
)) }
|
70
|
</>
|
71
|
)
|
72
|
}
|
73
|
|
74
|
export default ListView
|