|
| 1 | +import { |
| 2 | + Flex, |
| 3 | + Image, |
| 4 | + SkeletonBox, |
| 5 | + SkeletonText, |
| 6 | + Spacer, |
| 7 | + Text, |
| 8 | + useTheme, |
| 9 | + Screen, |
| 10 | + useScreenDimensions, |
| 11 | +} from "@artsy/palette-mobile" |
| 12 | +import { RouterLink } from "app/system/navigation/RouterLink" |
| 13 | +import { |
| 14 | + PlaceholderBox, |
| 15 | + ProvidePlaceholderContext, |
| 16 | + RandomWidthPlaceholderText, |
| 17 | +} from "app/utils/placeholders" |
| 18 | +import { times } from "lodash" |
| 19 | +import { FlatList, GestureResponderEvent, useWindowDimensions } from "react-native" |
| 20 | +import { isTablet } from "react-native-device-info" |
| 21 | + |
| 22 | +export const CARD_IMAGE_WIDTH = 295 |
| 23 | +export const CARD_IMAGE_HEIGHT = 230 |
| 24 | + |
| 25 | +interface CardWithMetaDataProps { |
| 26 | + isFluid?: boolean |
| 27 | + href: string |
| 28 | + imageURL: string | null | undefined |
| 29 | + title: string |
| 30 | + subtitle: string |
| 31 | + tag: string |
| 32 | + onPress?: (event: GestureResponderEvent) => void |
| 33 | + testId?: string |
| 34 | +} |
| 35 | + |
| 36 | +export const CardWithMetaData: React.FC<CardWithMetaDataProps> = (props) => { |
| 37 | + const { isFluid, href, imageURL, title, subtitle, tag, onPress } = props |
| 38 | + const numColumns = useNumColumns() |
| 39 | + |
| 40 | + const { space } = useTheme() |
| 41 | + const { width } = useWindowDimensions() |
| 42 | + |
| 43 | + return ( |
| 44 | + <> |
| 45 | + <Flex width={isFluid ? "100%" : CARD_IMAGE_WIDTH}> |
| 46 | + <RouterLink onPress={onPress} testID="article-card" to={href}> |
| 47 | + <Flex width={isFluid ? "100%" : CARD_IMAGE_WIDTH} overflow="hidden"> |
| 48 | + {!!imageURL && |
| 49 | + (isFluid ? ( |
| 50 | + <Image |
| 51 | + src={imageURL} |
| 52 | + // aspect ratio is fixed to 1.33 to match the old image aspect ratio |
| 53 | + aspectRatio={1.33} |
| 54 | + width={width / numColumns - 2 * space(2)} |
| 55 | + /> |
| 56 | + ) : ( |
| 57 | + <Image src={imageURL} width={CARD_IMAGE_WIDTH} height={CARD_IMAGE_HEIGHT} /> |
| 58 | + ))} |
| 59 | + <Spacer y={1} /> |
| 60 | + <Text numberOfLines={2} ellipsizeMode="tail" variant="sm-display" mb={0.5}> |
| 61 | + {title} |
| 62 | + </Text> |
| 63 | + {!!subtitle && ( |
| 64 | + <Text color="black60" variant="xs"> |
| 65 | + {subtitle} |
| 66 | + </Text> |
| 67 | + )} |
| 68 | + {!!tag && ( |
| 69 | + <Text color="black100" variant="xs"> |
| 70 | + {tag} |
| 71 | + </Text> |
| 72 | + )} |
| 73 | + </Flex> |
| 74 | + </RouterLink> |
| 75 | + </Flex> |
| 76 | + </> |
| 77 | + ) |
| 78 | +} |
| 79 | + |
| 80 | +export const CardWithMetaDataSkeleton: React.FC = () => { |
| 81 | + return ( |
| 82 | + <Flex width={CARD_IMAGE_WIDTH} overflow="hidden"> |
| 83 | + <SkeletonBox height={CARD_IMAGE_HEIGHT} width={CARD_IMAGE_WIDTH} /> |
| 84 | + <Spacer y={1} /> |
| 85 | + <SkeletonText variant="sm-display" mb={0.5}> |
| 86 | + Example title |
| 87 | + </SkeletonText> |
| 88 | + <SkeletonText variant="xs">Example subtitle</SkeletonText> |
| 89 | + <SkeletonText variant="xs">Berlin • Oct 8-Nov 9</SkeletonText> |
| 90 | + </Flex> |
| 91 | + ) |
| 92 | +} |
| 93 | + |
| 94 | +interface CardsWithMetaDataListPlaceholderProps { |
| 95 | + title?: string |
| 96 | + testID?: string |
| 97 | +} |
| 98 | + |
| 99 | +export const CardsWithMetaDataListPlaceholder: React.FC<CardsWithMetaDataListPlaceholderProps> = ({ |
| 100 | + title = "Artsy Editorial", |
| 101 | + testID, |
| 102 | +}) => { |
| 103 | + const numColumns = useNumColumns() |
| 104 | + |
| 105 | + return ( |
| 106 | + <Screen> |
| 107 | + <Screen.AnimatedHeader title={title} /> |
| 108 | + <Screen.Body fullwidth> |
| 109 | + <ProvidePlaceholderContext> |
| 110 | + <Flex testID={testID} flexDirection="column" justifyContent="space-between" height="100%"> |
| 111 | + <FlatList |
| 112 | + numColumns={numColumns} |
| 113 | + key={`${numColumns}`} |
| 114 | + ListHeaderComponent={() => <ListHeader title={title} />} |
| 115 | + data={times(6)} |
| 116 | + keyExtractor={(item) => `${item}-${numColumns}`} |
| 117 | + renderItem={({ item }) => { |
| 118 | + return ( |
| 119 | + <CardWithMetaDataListItem index={item} key={item}> |
| 120 | + <PlaceholderBox aspectRatio={1.33} width="100%" marginBottom={10} /> |
| 121 | + <RandomWidthPlaceholderText minWidth={50} maxWidth={100} marginTop={1} /> |
| 122 | + <RandomWidthPlaceholderText |
| 123 | + height={18} |
| 124 | + minWidth={200} |
| 125 | + maxWidth={200} |
| 126 | + marginTop={1} |
| 127 | + /> |
| 128 | + <RandomWidthPlaceholderText minWidth={100} maxWidth={100} marginTop={1} /> |
| 129 | + <Spacer y={2} /> |
| 130 | + </CardWithMetaDataListItem> |
| 131 | + ) |
| 132 | + }} |
| 133 | + ItemSeparatorComponent={() => <Spacer y={4} />} |
| 134 | + onEndReachedThreshold={1} |
| 135 | + /> |
| 136 | + </Flex> |
| 137 | + </ProvidePlaceholderContext> |
| 138 | + </Screen.Body> |
| 139 | + </Screen> |
| 140 | + ) |
| 141 | +} |
| 142 | + |
| 143 | +export const ListHeader = ({ title = "" }) => ( |
| 144 | + <Text mx={2} variant="lg-display" mb={2}> |
| 145 | + {title} |
| 146 | + </Text> |
| 147 | +) |
| 148 | + |
| 149 | +interface CardWithMetaDataListItemProps { |
| 150 | + index: number |
| 151 | +} |
| 152 | + |
| 153 | +export const CardWithMetaDataListItem: React.FC<CardWithMetaDataListItemProps> = ({ |
| 154 | + children, |
| 155 | + index, |
| 156 | +}) => { |
| 157 | + const numColumns = useNumColumns() |
| 158 | + |
| 159 | + if (numColumns === 1) { |
| 160 | + return <Flex mx={2}>{children}</Flex> |
| 161 | + } |
| 162 | + |
| 163 | + const ml = index % numColumns === 0 ? 2 : 1 |
| 164 | + const mr = index % numColumns < numColumns - 1 ? 1 : 2 |
| 165 | + |
| 166 | + return ( |
| 167 | + <Flex flex={1 / numColumns} flexDirection="row" ml={ml} mr={mr}> |
| 168 | + <Flex flex={1}>{children}</Flex> |
| 169 | + </Flex> |
| 170 | + ) |
| 171 | +} |
| 172 | + |
| 173 | +export const useNumColumns = () => { |
| 174 | + const { orientation } = useScreenDimensions() |
| 175 | + |
| 176 | + if (!isTablet()) { |
| 177 | + return 1 |
| 178 | + } |
| 179 | + |
| 180 | + return orientation === "portrait" ? 2 : 3 |
| 181 | +} |
0 commit comments