-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: crated thumbnail using expo-video-thumbnail
- Loading branch information
Otavio Stasiak
committed
Oct 2, 2024
1 parent
9dfa85b
commit e85cca2
Showing
2 changed files
with
89 additions
and
33 deletions.
There are no files selected for viewing
86 changes: 86 additions & 0 deletions
86
app/containers/message/Components/Attachments/Thumbnail.tsx
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,86 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import { StyleSheet, View } from 'react-native'; | ||
|
||
import { getThumbnailAsync } from 'expo-video-thumbnails'; | ||
import FastImage from 'react-native-fast-image'; | ||
|
||
import { CustomIcon } from '../../../CustomIcon'; | ||
import OverlayComponent from '../OverlayComponent'; | ||
|
||
const styles = StyleSheet.create({ | ||
container: { | ||
flex: 1, | ||
alignItems: 'center', | ||
justifyContent: 'center' | ||
}, | ||
overlay: { | ||
flex: 1 | ||
}, | ||
image: { | ||
width: '100%', | ||
height: '100%' | ||
}, | ||
playerIcon: { | ||
position: 'absolute', | ||
shadowColor: '#000', | ||
shadowOpacity: 0.3, | ||
shadowOffset: { | ||
width: 1, | ||
height: 1 | ||
} | ||
} | ||
}); | ||
|
||
type Image = { | ||
loading: boolean; | ||
uri: string | null; | ||
}; | ||
|
||
type ThumbnailProps = { | ||
url: string; | ||
encrypted?: boolean; | ||
}; | ||
|
||
const Thumbnail = ({ url, encrypted = false }: ThumbnailProps) => { | ||
const icon = encrypted ? 'encrypted' : 'play-filled'; | ||
|
||
const [image, setImage] = useState<Image>({ | ||
loading: true, | ||
uri: null | ||
}); | ||
|
||
const generateThumbnail = async () => { | ||
try { | ||
if (!url) return; | ||
|
||
const { uri } = await getThumbnailAsync(url, { | ||
time: 1 | ||
}); | ||
setImage({ | ||
loading: false, | ||
uri | ||
}); | ||
} catch (e) { | ||
console.warn(e); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
generateThumbnail(); | ||
}, [url]); | ||
|
||
return ( | ||
<View style={styles.container}> | ||
{image.loading || !image.uri ? ( | ||
<OverlayComponent style={styles.overlay} loading={image.loading} iconName='arrow-down-circle' /> | ||
) : ( | ||
<> | ||
<FastImage style={styles.image} source={{ uri: image.uri }} /> | ||
<CustomIcon name={icon} size={54} color='#fff' style={styles.playerIcon} /> | ||
</> | ||
)} | ||
</View> | ||
); | ||
}; | ||
|
||
export default Thumbnail; |
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