-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
05d8537
commit 7bf041b
Showing
15 changed files
with
1,435 additions
and
701 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
import React, { useEffect, useRef } from "react" | ||
import { | ||
Animated, | ||
Image, | ||
ImageSourcePropType, | ||
ImageStyle, | ||
StyleSheet, | ||
View, | ||
ViewStyle, | ||
} from "react-native" | ||
import { ImageInput } from "src/API" | ||
import DefaultImage from "../../assets/20x20.png" | ||
import useJCImage from "./useJCImage" | ||
|
||
export enum JCImageQuality { | ||
small = "small", | ||
medium = "medium", | ||
large = "large", | ||
} | ||
export enum JCImageStyle { | ||
IconSmall = "IconSmall", | ||
} | ||
type JCImageType = "icon" | "resource" | ||
export type JCImageProps = { | ||
containerStyle?: ViewStyle | ||
imageStyle?: ImageStyle | ||
style: JCImageStyle | ||
quality: JCImageQuality | ||
type: JCImageType | ||
image: ImageInput | null | undefined | ||
linkToProfile?: boolean | ||
showNameInToolTip?: boolean | ||
} | ||
|
||
export const ProfileImageStyles = StyleSheet.create({ | ||
Bordered: { | ||
borderColor: "#E4E1E1", | ||
borderWidth: 0, | ||
// for some reason, borderWidth:1 breaks the image spacing on Chrome browser randomly | ||
// not sure what's going on here | ||
}, | ||
ImageContainer: { | ||
backgroundColor: "#fff", | ||
}, | ||
IconSmall: { | ||
height: 20, | ||
width: 20, | ||
}, | ||
}) | ||
export default function JCImage(props: JCImageProps) { | ||
const { url, isLoading = true } = useJCImage(props) | ||
const fadeAnim = useRef(new Animated.Value(0)).current | ||
const fadeAnim2 = useRef(new Animated.Value(1)).current | ||
const fade = () => { | ||
Animated.timing(fadeAnim, { | ||
toValue: 1, | ||
duration: 1000, | ||
useNativeDriver: true, | ||
}).start() | ||
Animated.timing(fadeAnim2, { | ||
toValue: 0, | ||
duration: 1000, | ||
useNativeDriver: true, | ||
}).start() | ||
} | ||
useEffect(() => { | ||
if (url && !isLoading) fade() | ||
}, [url, isLoading]) | ||
return ( | ||
<View> | ||
<Animated.View style={{ opacity: fadeAnim2, position: "absolute" }}> | ||
<Image | ||
resizeMode={"cover"} | ||
style={[ProfileImageStyles[props.style], props.imageStyle, ProfileImageStyles.Bordered]} | ||
source={DefaultImage} | ||
/> | ||
</Animated.View> | ||
{!isLoading && url ? ( | ||
<Animated.View style={{ opacity: fadeAnim }}> | ||
<Image | ||
resizeMode={props.type === "icon" ? "contain" : "cover"} | ||
style={[ProfileImageStyles[props.style], props.imageStyle, ProfileImageStyles.Bordered]} | ||
source={url as ImageSourcePropType} | ||
/> | ||
</Animated.View> | ||
) : null} | ||
</View> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { Storage } from "aws-amplify" | ||
import { useEffect, useState } from "react" | ||
import { JCImageProps } from "./JCImage" | ||
|
||
export default function useJCImage(props: JCImageProps) { | ||
const [url, setUrl] = useState<string>("") | ||
const [isLoading, setIsLoading] = useState(true) | ||
useEffect(() => { | ||
const loadImageFromStorage = async () => { | ||
let imgUrl: string | null | undefined | ||
switch (props.quality) { | ||
case "large": | ||
imgUrl = props.image?.filenameLarge | ||
break | ||
case "medium": | ||
imgUrl = props.image?.filenameMedium | ||
break | ||
case "small": | ||
imgUrl = props.image?.filenameSmall | ||
break | ||
default: | ||
imgUrl = props.image?.filenameMedium | ||
break | ||
} | ||
try { | ||
setIsLoading(true) | ||
if (imgUrl) { | ||
// Storage.get() not throwing error when file does not exist | ||
// Current implementation shows default image while loading | ||
// Errored requests that don't throw an error will maintain loading state, | ||
const pfImage = await Storage.get(imgUrl, { | ||
level: "protected", | ||
contentType: "image/png", | ||
identityId: props.image?.userId ?? "", | ||
}) | ||
setIsLoading(false) | ||
setUrl(pfImage) | ||
} else { | ||
console.log("No Image URL Found.") | ||
} | ||
} catch (err) { | ||
console.log({ err }) | ||
setUrl("") | ||
} | ||
} | ||
if (props.image) loadImageFromStorage() | ||
}, [props.quality, props.image]) | ||
return { url: url, isLoading } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.