-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Lightbox.tsx
60 lines (55 loc) · 1.75 KB
/
Lightbox.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
import React from 'react'
import * as MediaLibrary from 'expo-media-library'
import {msg} from '@lingui/macro'
import {useLingui} from '@lingui/react'
import {saveImageToMediaLibrary, shareImageModal} from '#/lib/media/manip'
import {useLightbox, useLightboxControls} from '#/state/lightbox'
import * as Toast from '../util/Toast'
import ImageView from './ImageViewing'
export function Lightbox() {
const {activeLightbox} = useLightbox()
const {closeLightbox} = useLightboxControls()
const onClose = React.useCallback(() => {
closeLightbox()
}, [closeLightbox])
const {_} = useLingui()
const [permissionResponse, requestPermission] = MediaLibrary.usePermissions({
granularPermissions: ['photo'],
})
const saveImageToAlbumWithToasts = React.useCallback(
async (uri: string) => {
if (!permissionResponse || permissionResponse.granted === false) {
Toast.show(
_(msg`Permission to access camera roll is required.`),
'info',
)
if (permissionResponse?.canAskAgain) {
requestPermission()
} else {
Toast.show(
_(
msg`Permission to access camera roll was denied. Please enable it in your system settings.`,
),
'xmark',
)
}
return
}
try {
await saveImageToMediaLibrary({uri})
Toast.show(_(msg`Saved to your camera roll`))
} catch (e: any) {
Toast.show(_(msg`Failed to save image: ${String(e)}`), 'xmark')
}
},
[permissionResponse, requestPermission, _],
)
return (
<ImageView
lightbox={activeLightbox}
onRequestClose={onClose}
onPressSave={saveImageToAlbumWithToasts}
onPressShare={uri => shareImageModal({uri})}
/>
)
}