Projekt

Obecné

Profil

Stáhnout (2.44 KB) Statistiky
| Větev: | Tag: | Revize:
1 9c55d3bb Schwobik
import { useEffect, useState } from "react"
2 ca53e9f1 Schwobik
import { ScrollView, Text, VStack } from "native-base"
3 9c55d3bb Schwobik
import { useSelector } from "react-redux"
4
import { RootState } from "../../stores/store"
5 ca53e9f1 Schwobik
import ItemPreview from "./ItemPreview"
6 af8eb9a6 Schwobik
import { ItemPreviewType } from "../../types/listViewTypes"
7
import { log } from "../../logging/logger"
8
9 9c55d3bb Schwobik
10
const ListView = () => {
11
    const items = useSelector((state: RootState) => state.listView.data)
12
13 af8eb9a6 Schwobik
    /**
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 9c55d3bb Schwobik
    return (
60 af8eb9a6 Schwobik
        <>
61 ca53e9f1 Schwobik
            { items.map((item) => (
62
                <ItemPreview
63 af8eb9a6 Schwobik
                    // @ts-ignore
64 ca53e9f1 Schwobik
                    caption={ item.object[0].caption }
65
                    title={ item.text }
66 af8eb9a6 Schwobik
                    name={ parseArtists(item) }
67
                    image={ parseImage(item) }
68 ca53e9f1 Schwobik
                />
69
            )) }
70 af8eb9a6 Schwobik
        </>
71 9c55d3bb Schwobik
    )
72
}
73
74
export default ListView