2023-08-14 14:10:33 +05:00
/ *
This file is part of the Notesnook project ( https : //notesnook.com/)
Copyright ( C ) 2023 Streetwriters ( Private ) Limited
This program is free software : you can redistribute it and / or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation , either version 3 of the License , or
( at your option ) any later version .
This program is distributed in the hope that it will be useful ,
but WITHOUT ANY WARRANTY ; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
GNU General Public License for more details .
You should have received a copy of the GNU General Public License
along with this program . If not , see < http : //www.gnu.org/licenses/>.
* /
import React , { useState } from "react" ;
2023-08-14 15:02:51 +05:00
import { Image , ScrollView , TouchableOpacity , View } from "react-native" ;
2024-05-04 23:02:07 +05:00
import { Image as ImageType } from "react-native-image-crop-picker" ;
2023-08-14 14:10:33 +05:00
import { useThemeColors } from "../../../../../../packages/theme/dist" ;
import { presentSheet } from "../../../services/event-manager" ;
import { SIZE } from "../../../utils/size" ;
import { Button } from "../../ui/button" ;
import { IconButton } from "../../ui/icon-button" ;
import { Notice } from "../../ui/notice" ;
import Paragraph from "../../ui/typography/paragraph" ;
export default function AttachImage ( {
response ,
onAttach ,
close
} : {
2024-05-04 23:02:07 +05:00
response : ImageType [ ] ;
2023-08-14 14:10:33 +05:00
onAttach : ( { compress } : { compress : boolean } ) = > void ;
close : ( ( ctx? : string | undefined ) = > void ) | undefined ;
} ) {
const { colors } = useThemeColors ( ) ;
const [ compress , setCompress ] = useState ( true ) ;
return (
< View
style = { {
alignItems : "center" ,
paddingHorizontal : 12
} }
>
2023-08-14 15:02:51 +05:00
< View
style = { {
backgroundColor : colors.secondary.background ,
marginBottom : 12 ,
height : 140 ,
width : "100%" ,
borderRadius : 10 ,
padding : 5 ,
paddingHorizontal : 12
} }
>
< Paragraph style = { { color : colors.primary.paragraph , marginBottom : 6 } } >
2024-05-04 23:02:07 +05:00
Attaching { response ? . length } image ( s ) :
2023-08-14 15:02:51 +05:00
< / Paragraph >
< ScrollView horizontal >
2024-05-04 23:02:07 +05:00
{ response ? . map ( ( item ) = > (
< TouchableOpacity key = { item . filename } activeOpacity = { 0.9 } >
2023-08-14 15:02:51 +05:00
< Image
source = { {
2024-05-04 23:02:07 +05:00
uri : item.sourceURL || item . path
2023-08-14 15:02:51 +05:00
} }
style = { {
width : 100 ,
height : 100 ,
borderRadius : 5 ,
backgroundColor : "black" ,
marginRight : 6
} }
resizeMode = "cover"
/ >
< / TouchableOpacity >
) ) }
< / ScrollView >
< / View >
2023-08-14 14:10:33 +05:00
< TouchableOpacity
activeOpacity = { 1 }
style = { {
flexDirection : "row" ,
alignSelf : "center" ,
marginBottom : 12 ,
alignItems : "center" ,
width : "100%"
} }
onPress = { ( ) = > {
setCompress ( ! compress ) ;
} }
>
< IconButton
size = { SIZE . lg }
name = { compress ? "checkbox-marked" : "checkbox-blank-outline" }
color = { compress ? colors.primary.accent : colors.primary.icon }
2024-03-06 09:16:19 +05:00
style = { {
2023-08-14 14:10:33 +05:00
width : 25 ,
height : 25
} }
onPress = { ( ) = > {
setCompress ( ! compress ) ;
} }
/ >
< Paragraph
style = { {
flexShrink : 1 ,
marginLeft : 3
} }
size = { SIZE . sm }
>
2024-03-01 20:28:21 +05:00
Compress ( recommended )
2023-08-14 14:10:33 +05:00
< / Paragraph >
< / TouchableOpacity >
{ ! compress ? (
< Notice
type = "alert"
text = "Images uploaded without compression are slow to load and take more bandwidth. We recommend compressing images unless you need image in original quality."
size = "small"
style = { {
width : "100%" ,
marginBottom : 12
} }
/ >
) : (
< Notice
type = "information"
2024-03-01 20:28:21 +05:00
text = "Compressed images are uploaded in Full HD resolution and usually are good enough for most use cases."
2023-08-14 14:10:33 +05:00
size = "small"
style = { {
width : "100%" ,
marginBottom : 12
} }
/ >
) }
< Button
2024-03-01 20:28:21 +05:00
title = { ` ${
2024-05-04 23:02:07 +05:00
( response ? . length || 0 ) > 1 ? "Attach Images" : "Attach Image"
2024-03-01 20:28:21 +05:00
} ` }
2023-08-14 14:10:33 +05:00
type = "accent"
width = "100%"
onPress = { ( ) = > {
onAttach ( {
compress
} ) ;
} }
/ >
< / View >
) ;
}
2024-05-04 23:02:07 +05:00
AttachImage . present = ( response : ImageType [ ] , context? : string ) = > {
return new Promise <
| {
compress : boolean ;
}
| undefined
> ( ( resolve ) = > {
2023-08-14 14:10:33 +05:00
let resolved = false ;
presentSheet ( {
2024-05-04 23:02:07 +05:00
context : context ,
2023-08-14 14:10:33 +05:00
component : ( ref , close , update ) = > (
< AttachImage
response = { response }
close = { close }
onAttach = { ( result ) = > {
resolved = true ;
console . log ( "closing" ) ;
resolve ( result ) ;
close ? . ( ) ;
} }
/ >
) ,
onClose : ( ) = > {
if ( resolved ) return ;
resolve ( undefined ) ;
}
} ) ;
} ) ;
} ;