-
Notifications
You must be signed in to change notification settings - Fork 74
/
DefaultHeader.tsx
179 lines (162 loc) · 4.85 KB
/
DefaultHeader.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import { Box, color, Flex, Image, media, space } from "@artsy/palette"
import { DefaultHeader_headerArtworks } from "__generated__/DefaultHeader_headerArtworks.graphql"
import { AnalyticsSchema } from "Artsy/Analytics"
import { useTracking } from "Artsy/Analytics/useTracking"
import React, { FC } from "react"
import { createFragmentContainer, graphql } from "react-relay"
import styled from "styled-components"
import { useWindowSize } from "Utils/Hooks/useWindowSize"
interface CollectionDefaultHeaderProps {
headerArtworks: DefaultHeader_headerArtworks
defaultHeaderImageHeight: number
collection_id: string
collection_slug: string
}
const IMAGE_MARGIN_X = 10
const LARGE_VIEWPORT_WIDTH = 880
export const CollectionDefaultHeader: FC<CollectionDefaultHeaderProps> = ({
headerArtworks,
defaultHeaderImageHeight,
collection_id,
collection_slug,
}) => {
const { hits: artworks } = headerArtworks
if (!artworks) {
return null
}
const viewportWidth = useWindowSize().width
const smallViewport = viewportWidth < LARGE_VIEWPORT_WIDTH
/**
* Relay is returning 12 artworks since this query populates both the artworks
* used for merchandisable artists and those used for this component.
* Slice the artworks array to get just the first 10 in the result set.
*/
const duplicatedArtworks = artworks.slice(0, 10)
const artworksToRender = getHeaderArtworks(
duplicatedArtworks,
viewportWidth,
smallViewport
)
const { trackEvent } = useTracking()
return (
<header>
<DefaultHeaderContainer
position={["relative", "absolute"]}
left={["auto", 0]}
width={["auto", 1]}
height={[160, 160, 250]}
>
<HeaderArtworks>
{artworksToRender.map((artwork, i) => {
return (
<a
href={artwork.href}
key={artwork.href}
onClick={() => {
trackEvent({
action_type: AnalyticsSchema.ActionType.Click,
context_module: AnalyticsSchema.ContextModule.ArtworkBanner,
context_page_owner_type:
AnalyticsSchema.OwnerType.Collection,
context_page: AnalyticsSchema.PageName.CollectionPage,
context_page_owner_id: collection_id,
context_page_owner_slug: collection_slug,
destination_path: artwork.href,
})
}}
>
<HeaderImage
height={defaultHeaderImageHeight}
src={
smallViewport
? (artwork.image.small.url as string)
: (artwork.image.large.url as string)
}
preventRightClick
/>
</a>
)
})}
</HeaderArtworks>
</DefaultHeaderContainer>
</header>
)
}
export const getHeaderArtworks = (
artworksArray: any[],
headerWidth: number,
isSmallViewport: boolean
) => {
let artworkWidths = 0
let shouldAppendDuplicateArtworksToHeader = true
const headerArtworks: any[] = []
if (artworksArray.length < 1) {
return [] as any[]
}
/**
* Loop through the initial artworks array, appending an artwork to the output array,
* until the summed widths of the artworks in the output array are greater than the
* width of the viewport.
*/
while (shouldAppendDuplicateArtworksToHeader) {
for (const artwork of artworksArray) {
if (artworkWidths > headerWidth) {
headerArtworks.push(artwork)
shouldAppendDuplicateArtworksToHeader = false
return headerArtworks
}
headerArtworks.push(artwork)
isSmallViewport
? (artworkWidths += artwork.image.small.width + IMAGE_MARGIN_X)
: (artworkWidths += artwork.image.large.width + IMAGE_MARGIN_X)
}
}
}
export const CollectionDefaultHeaderFragmentContainer = createFragmentContainer(
CollectionDefaultHeader,
{
headerArtworks: graphql`
fragment DefaultHeader_headerArtworks on FilterArtworks {
hits {
href
id
image {
small: resized(height: 160) {
url
width
height
}
large: resized(height: 220) {
url
width
height
}
}
}
}
`,
}
)
const DefaultHeaderContainer = styled(Box)`
background-color: ${color("black5")};
overflow: hidden;
${media.xs`
margin-left: -20px;
margin-right: -20px;
`};
`
const HeaderArtworks = styled(Flex)`
flex-direction: row;
position: absolute;
bottom: 0;
& a:first-child > img {
margin-left: 0px;
}
& a:last-child > img {
margin-left: 0px;
}
`
const HeaderImage = styled(Image)`
margin-right: ${space(0.5)}px;
margin-left: ${space(0.5)}px;
`