diff options
author | dan <dan.abramov@gmail.com> | 2023-10-05 23:28:56 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-10-05 15:28:56 -0700 |
commit | 260b03a05c22232373cbf8cb0d7dda41a3302343 (patch) | |
tree | f7ce8b72c80fbdc723245dc34d1db56288b7b176 /src/view/com/lightbox/Lightbox.tsx | |
parent | eb7306b16512e317f477c7a28e1e3b0ce5c65ff8 (diff) | |
download | voidsky-260b03a05c22232373cbf8cb0d7dda41a3302343.tar.zst |
Remove unused lightbox options (#1616)
* Inline lightbox helpers * Delete unused useImagePrefetch * Delete unused long press gesture * Always enable double tap * Always enable swipe to close * Remove unused onImageIndexChange * Inline custom Hooks into ImageViewing * Declare LightboxFooter outside Lightbox * Add more TODO comments * Inline useDoubleTapToZoom * Remove dead utils, move utils used only once
Diffstat (limited to 'src/view/com/lightbox/Lightbox.tsx')
-rw-r--r-- | src/view/com/lightbox/Lightbox.tsx | 170 |
1 files changed, 86 insertions, 84 deletions
diff --git a/src/view/com/lightbox/Lightbox.tsx b/src/view/com/lightbox/Lightbox.tsx index 072bfebfa..ad66dce32 100644 --- a/src/view/com/lightbox/Lightbox.tsx +++ b/src/view/com/lightbox/Lightbox.tsx @@ -15,94 +15,10 @@ import * as MediaLibrary from 'expo-media-library' export const Lightbox = observer(function Lightbox() { const store = useStores() - const [isAltExpanded, setAltExpanded] = React.useState(false) - const [permissionResponse, requestPermission] = MediaLibrary.usePermissions() - const onClose = React.useCallback(() => { store.shell.closeLightbox() }, [store]) - const saveImageToAlbumWithToasts = React.useCallback( - async (uri: string) => { - if (!permissionResponse || permissionResponse.granted === false) { - Toast.show('Permission to access camera roll is required.') - if (permissionResponse?.canAskAgain) { - requestPermission() - } else { - Toast.show( - 'Permission to access camera roll was denied. Please enable it in your system settings.', - ) - } - return - } - - try { - await saveImageToMediaLibrary({uri}) - Toast.show('Saved to your camera roll.') - } catch (e: any) { - Toast.show(`Failed to save image: ${String(e)}`) - } - }, - [permissionResponse, requestPermission], - ) - - const LightboxFooter = React.useCallback( - ({imageIndex}: {imageIndex: number}) => { - const lightbox = store.shell.activeLightbox - if (!lightbox) { - return null - } - - let altText = '' - let uri = '' - if (lightbox.name === 'images') { - const opts = lightbox as models.ImagesLightbox - uri = opts.images[imageIndex].uri - altText = opts.images[imageIndex].alt || '' - } else if (lightbox.name === 'profile-image') { - const opts = lightbox as models.ProfileImageLightbox - uri = opts.profileView.avatar || '' - } - - return ( - <View style={[styles.footer]}> - {altText ? ( - <Pressable - onPress={() => setAltExpanded(!isAltExpanded)} - accessibilityRole="button"> - <Text - style={[s.gray3, styles.footerText]} - numberOfLines={isAltExpanded ? undefined : 3}> - {altText} - </Text> - </Pressable> - ) : null} - <View style={styles.footerBtns}> - <Button - type="primary-outline" - style={styles.footerBtn} - onPress={() => saveImageToAlbumWithToasts(uri)}> - <FontAwesomeIcon icon={['far', 'floppy-disk']} style={s.white} /> - <Text type="xl" style={s.white}> - Save - </Text> - </Button> - <Button - type="primary-outline" - style={styles.footerBtn} - onPress={() => shareImageModal({uri})}> - <FontAwesomeIcon icon="arrow-up-from-bracket" style={s.white} /> - <Text type="xl" style={s.white}> - Share - </Text> - </Button> - </View> - </View> - ) - }, - [store.shell.activeLightbox, isAltExpanded, saveImageToAlbumWithToasts], - ) - if (!store.shell.activeLightbox) { return null } else if (store.shell.activeLightbox.name === 'profile-image') { @@ -132,6 +48,92 @@ export const Lightbox = observer(function Lightbox() { } }) +const LightboxFooter = observer(function LightboxFooter({ + imageIndex, +}: { + imageIndex: number +}) { + const store = useStores() + const [isAltExpanded, setAltExpanded] = React.useState(false) + const [permissionResponse, requestPermission] = MediaLibrary.usePermissions() + + const saveImageToAlbumWithToasts = React.useCallback( + async (uri: string) => { + if (!permissionResponse || permissionResponse.granted === false) { + Toast.show('Permission to access camera roll is required.') + if (permissionResponse?.canAskAgain) { + requestPermission() + } else { + Toast.show( + 'Permission to access camera roll was denied. Please enable it in your system settings.', + ) + } + return + } + + try { + await saveImageToMediaLibrary({uri}) + Toast.show('Saved to your camera roll.') + } catch (e: any) { + Toast.show(`Failed to save image: ${String(e)}`) + } + }, + [permissionResponse, requestPermission], + ) + + const lightbox = store.shell.activeLightbox + if (!lightbox) { + return null + } + + let altText = '' + let uri = '' + if (lightbox.name === 'images') { + const opts = lightbox as models.ImagesLightbox + uri = opts.images[imageIndex].uri + altText = opts.images[imageIndex].alt || '' + } else if (lightbox.name === 'profile-image') { + const opts = lightbox as models.ProfileImageLightbox + uri = opts.profileView.avatar || '' + } + + return ( + <View style={[styles.footer]}> + {altText ? ( + <Pressable + onPress={() => setAltExpanded(!isAltExpanded)} + accessibilityRole="button"> + <Text + style={[s.gray3, styles.footerText]} + numberOfLines={isAltExpanded ? undefined : 3}> + {altText} + </Text> + </Pressable> + ) : null} + <View style={styles.footerBtns}> + <Button + type="primary-outline" + style={styles.footerBtn} + onPress={() => saveImageToAlbumWithToasts(uri)}> + <FontAwesomeIcon icon={['far', 'floppy-disk']} style={s.white} /> + <Text type="xl" style={s.white}> + Save + </Text> + </Button> + <Button + type="primary-outline" + style={styles.footerBtn} + onPress={() => shareImageModal({uri})}> + <FontAwesomeIcon icon="arrow-up-from-bracket" style={s.white} /> + <Text type="xl" style={s.white}> + Share + </Text> + </Button> + </View> + </View> + ) +}) + const styles = StyleSheet.create({ footer: { paddingTop: 16, |