-
Notifications
You must be signed in to change notification settings - Fork 1
feat: [FC-13] custom document message #40
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
huydosgtech
wants to merge
3
commits into
master
Choose a base branch
from
feat/FC-13-custom-document-message
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,119 @@ | ||
| import React, { forwardRef, useCallback, useImperativeHandle } from 'react'; | ||
| import { Platform, PermissionsAndroid } from 'react-native'; | ||
| import DocumentPicker, { | ||
| type DocumentPickerResponse, | ||
| } from 'react-native-document-picker'; | ||
| import uuid from 'react-native-uuid'; | ||
| import { formatSize, getAbsoluteFilePathWithName } from '../../utilities'; | ||
| import { type MediaType, type MessageProps } from '../../interfaces'; | ||
| import { useChatContext } from '../../hooks'; | ||
|
|
||
| interface FileAttachmentProps { | ||
| onSend: (message: MessageProps) => void; | ||
| } | ||
|
|
||
| export interface FileAttachmentRef { | ||
| pickDocument: () => Promise<void>; | ||
| } | ||
|
|
||
| const FileAttachment = forwardRef<FileAttachmentRef, FileAttachmentProps>( | ||
| ({ onSend }, ref) => { | ||
| const { userInfo } = useChatContext(); | ||
|
|
||
| const checkPermissionAndroid = async () => { | ||
| if ( | ||
| Platform.OS === 'android' && | ||
| PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE | ||
| ) { | ||
| const granted = await PermissionsAndroid.request( | ||
| PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, | ||
| { | ||
| title: 'Write External Storage Permission', | ||
| message: 'This app needs access to your storage to download files.', | ||
| buttonNeutral: 'Ask Me Later', | ||
| buttonNegative: 'Cancel', | ||
| buttonPositive: 'OK', | ||
| } | ||
| ); | ||
| if (granted !== PermissionsAndroid.RESULTS.GRANTED) { | ||
| console.log('WRITE_EXTERNAL_STORAGE permission denied'); | ||
| return false; | ||
| } | ||
| return true; | ||
| } else { | ||
| return true; | ||
| } | ||
| }; | ||
|
|
||
| const onSendMessage = useCallback( | ||
| async (media: DocumentPickerResponse) => { | ||
| try { | ||
| const extension = media?.name?.split('.').pop() ?? 'pdf'; | ||
| const type = 'document' as MediaType; | ||
| const id = uuid.v4(); | ||
| const user = { | ||
| _id: userInfo?.id || '', | ||
| ...userInfo, | ||
| }; | ||
| const message = { | ||
| id: id, | ||
| _id: id, | ||
| text: '', | ||
| type: type, | ||
| path: await getAbsoluteFilePathWithName( | ||
| media.uri, | ||
| media.name ?? '' | ||
| ), | ||
| extension, | ||
| size: formatSize(media.size), | ||
| fileName: media.name || '', | ||
| user: user, | ||
| } as MessageProps; | ||
| onSend(message); | ||
| } catch (error) { | ||
| console.log('error: ', error); | ||
| } | ||
| }, | ||
| [onSend, userInfo] | ||
| ); | ||
|
|
||
| const handleDocumentPick = useCallback(async () => { | ||
| if (!(await checkPermissionAndroid())) return; | ||
|
|
||
| try { | ||
| const result = await DocumentPicker.pick({ | ||
| type: [ | ||
| DocumentPicker.types.pdf, | ||
| DocumentPicker.types.zip, | ||
| DocumentPicker.types.doc, | ||
| DocumentPicker.types.docx, | ||
| DocumentPicker.types.pptx, | ||
| DocumentPicker.types.ppt, | ||
| DocumentPicker.types.xls, | ||
| DocumentPicker.types.xlsx, | ||
| ], | ||
| }); | ||
| const media = result?.[0]; | ||
| if (media) { | ||
| onSendMessage(media); | ||
| } else { | ||
| console.log('Something went wrong. Please try again.'); | ||
| } | ||
| } catch (err) { | ||
| if (DocumentPicker.isCancel(err)) { | ||
| console.log('Document Picker was cancelled'); | ||
| } else { | ||
| throw err; | ||
| } | ||
| } | ||
| }, [onSendMessage]); | ||
|
|
||
| useImperativeHandle(ref, () => ({ | ||
| pickDocument: handleDocumentPick, | ||
| })); | ||
|
|
||
| return null; | ||
| } | ||
| ); | ||
|
|
||
| export default FileAttachment; |
This file contains hidden or 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,3 @@ | ||
| import FileAttachment, { FileAttachmentRef } from './FileAttachment'; | ||
|
|
||
| export { FileAttachment, FileAttachmentRef }; |
This file contains hidden or 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,2 @@ | ||
| export * from './camera'; | ||
| export * from './fileAttachment'; |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,126 @@ | ||
| import React, { useCallback } from 'react'; | ||
| import { | ||
| StyleSheet, | ||
| View, | ||
| Pressable, | ||
| Image, | ||
| ViewStyle, | ||
| StyleProp, | ||
| ImageStyle, | ||
| Text, | ||
| TextProps, | ||
| Share, | ||
| } from 'react-native'; | ||
| import type { MessageProps } from '../../../interfaces'; | ||
| import Images from '../../../asset'; | ||
|
|
||
| export interface CustomDocumentBubbleProps { | ||
| message: MessageProps; | ||
| position: 'left' | 'right'; | ||
| icon?: string; | ||
| bubbleContainerStyle?: StyleProp<ViewStyle>; | ||
| buttonStyle?: StyleProp<ViewStyle>; | ||
| iconStyle?: StyleProp<ImageStyle>; | ||
| textStyle?: StyleProp<TextProps>; | ||
| textSizeStyle?: StyleProp<TextProps>; | ||
| doucmentStyle?: StyleProp<ViewStyle>; | ||
|
|
||
| renderCustomDocument?: ( | ||
| message: MessageProps, | ||
| position: 'left' | 'right' | ||
| ) => React.ReactNode; | ||
| } | ||
|
|
||
| export const CustomDocumentBubble: React.FC<CustomDocumentBubbleProps> = ({ | ||
| position, | ||
| message, | ||
| icon = Images.document, | ||
| iconStyle, | ||
| bubbleContainerStyle, | ||
| buttonStyle, | ||
| doucmentStyle, | ||
| textStyle, | ||
| textSizeStyle, | ||
| renderCustomDocument, | ||
| }) => { | ||
| const handleDocumentsPress = useCallback(async () => { | ||
| if (!message || !message.path) return; | ||
| try { | ||
| await Share.share({ | ||
| url: message.path, | ||
| title: message.fileName, | ||
| message: message.fileName, | ||
| }); | ||
| } catch (err) { | ||
| console.log('Error, Failed to download document'); | ||
| } | ||
| }, [message]); | ||
|
|
||
| const renderDocument = () => { | ||
| return ( | ||
| <View style={[styles.wrapper, doucmentStyle]}> | ||
| <Image source={icon} style={[styles.icon, iconStyle]} /> | ||
| <View style={[styles.groupText]}> | ||
| <Text style={[styles.text, textStyle]}>{message.fileName}</Text> | ||
| <Text style={[styles.size, textSizeStyle]}>{message.size}</Text> | ||
| </View> | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| if (renderCustomDocument) { | ||
| renderCustomDocument(message, position); | ||
| } | ||
|
|
||
| return ( | ||
| <View | ||
| style={[ | ||
| styles.bubbleContainer, | ||
| bubbleContainerStyle, | ||
| position === 'left' ? styles.flexStart : styles.flexEnd, | ||
| ]} | ||
| > | ||
| <Pressable onPress={handleDocumentsPress} style={buttonStyle}> | ||
| {renderDocument()} | ||
| </Pressable> | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| bubbleContainer: { | ||
| paddingHorizontal: 5, | ||
| marginTop: 5, | ||
| flex: 1, | ||
| }, | ||
| flexEnd: { | ||
| justifyContent: 'flex-end', | ||
| }, | ||
| flexStart: { | ||
| justifyContent: 'flex-start', | ||
| }, | ||
| icon: { | ||
| width: 35, | ||
| height: 35, | ||
| }, | ||
| wrapper: { | ||
| flexDirection: 'row', | ||
| alignItems: 'center', | ||
| padding: 10, | ||
| backgroundColor: '#e0f7fa', | ||
| borderRadius: 10, | ||
| }, | ||
| groupText: { | ||
| marginLeft: 10, | ||
| }, | ||
| text: { | ||
| fontSize: 15, | ||
| color: 'black', | ||
| fontWeight: 'bold', | ||
| }, | ||
| size: { | ||
| fontSize: 13, | ||
| color: 'gray', | ||
| marginTop: 5, | ||
| }, | ||
| }); |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.