-
Notifications
You must be signed in to change notification settings - Fork 2k
Help Center: add image upload for chats #96105
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
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
dac5d1a
HelpCenter: add upload (wip)
heavyweight 12e7829
Minor fixes and refactoring
heavyweight 970a58c
Merge branch 'trunk' into add/smooch-file-upload
heavyweight bef429a
Changed icon
escapemanuele 7eaf0da
Use correct constants
heavyweight 1d7bf87
Improve isAttachingFile state
heavyweight 5dad25c
Store global clientId as fallback
heavyweight 88f109a
Merge branch 'trunk' into add/smooch-file-upload
agrullon95 0ae6c68
Merge branch 'trunk' into add/smooch-file-upload
renancarvalho f110f76
Fix attach button style
renancarvalho a06a074
Move up the flag check
escapemanuele 36ce670
Improved function
escapemanuele 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
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
81 changes: 81 additions & 0 deletions
81
packages/odie-client/src/components/send-message-input/attachment-button.tsx
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,81 @@ | ||
import { HelpCenterSelect } from '@automattic/data-stores'; | ||
import { HELP_CENTER_STORE } from '@automattic/help-center/src/stores'; | ||
import { | ||
useAttachFileToConversation, | ||
useAuthenticateZendeskMessaging, | ||
} from '@automattic/zendesk-client'; | ||
import { FormFileUpload, Spinner } from '@wordpress/components'; | ||
import { useSelect } from '@wordpress/data'; | ||
import { useCallback } from '@wordpress/element'; | ||
import { Icon, image } from '@wordpress/icons'; | ||
import React from 'react'; | ||
import { useOdieAssistantContext } from '../../context'; | ||
import { zendeskMessageConverter } from '../../utils'; | ||
|
||
const getFileType = ( file: File ) => { | ||
if ( file.type.includes( 'image' ) ) { | ||
return 'image'; | ||
} | ||
|
||
return 'text'; | ||
}; | ||
|
||
const getPlaceholderAttachmentMessage = ( file: File ) => { | ||
return zendeskMessageConverter( { | ||
role: 'user', | ||
type: getFileType( file ), | ||
displayName: '', | ||
text: '', | ||
id: String( new Date().getTime() ), | ||
received: new Date().getTime(), | ||
source: { type: 'web', id: '', integrationId: '' }, | ||
mediaUrl: URL.createObjectURL( file ), | ||
} ); | ||
}; | ||
|
||
export const AttachmentButton: React.FC = () => { | ||
const { chat, addMessage } = useOdieAssistantContext(); | ||
const { data: authData } = useAuthenticateZendeskMessaging( true, 'messenger' ); | ||
const { isPending: isAttachingFile, mutateAsync: attachFileToConversation } = | ||
useAttachFileToConversation(); | ||
const { zendeskClientId } = useSelect( ( select ) => { | ||
const helpCenterSelect: HelpCenterSelect = select( HELP_CENTER_STORE ); | ||
return { | ||
zendeskClientId: helpCenterSelect.getZendeskClientId(), | ||
}; | ||
}, [] ); | ||
|
||
const inferredClientId = chat.clientId ? chat.clientId : zendeskClientId; | ||
const onFileUpload = useCallback( | ||
async ( event: React.ChangeEvent< HTMLInputElement > ) => { | ||
if ( | ||
authData && | ||
chat.conversationId && | ||
inferredClientId && | ||
event.currentTarget.files?.length | ||
) { | ||
const file = event.currentTarget.files[ 0 ]; | ||
attachFileToConversation( { | ||
authData, | ||
file, | ||
conversationId: chat.conversationId, | ||
clientId: inferredClientId, | ||
} ).then( () => { | ||
addMessage( getPlaceholderAttachmentMessage( file ) ); | ||
} ); | ||
} | ||
}, | ||
[ chat.conversationId, authData ] | ||
); | ||
|
||
if ( ! chat.conversationId || ! inferredClientId ) { | ||
return null; | ||
} | ||
|
||
return ( | ||
<FormFileUpload accept="image/*" onChange={ onFileUpload } disabled={ isAttachingFile }> | ||
{ isAttachingFile && <Spinner style={ { margin: 0 } } /> } | ||
{ ! isAttachingFile && <Icon icon={ image } /> } | ||
</FormFileUpload> | ||
); | ||
}; |
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
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,64 @@ | ||
import config from '@automattic/calypso-config'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
import apiFetch, { APIFetchOptions } from '@wordpress/api-fetch'; | ||
import { | ||
SMOOCH_APP_ID, | ||
SMOOCH_APP_ID_STAGING, | ||
SMOOCH_INTEGRATION_ID, | ||
SMOOCH_INTEGRATION_ID_STAGING, | ||
WIDGET_URL, | ||
WIDGET_URL_STAGING, | ||
} from './constants'; | ||
|
||
export const useAttachFileToConversation = () => { | ||
return useMutation( { | ||
mutationFn: ( { | ||
authData, | ||
clientId, | ||
conversationId, | ||
file, | ||
}: { | ||
authData: { isLoggedIn: boolean; jwt: string; externalId: string | undefined }; | ||
clientId: string; | ||
conversationId: string; | ||
file: File; | ||
} ) => { | ||
const currentEnvironment = config( 'env_id' ); | ||
const isTestMode = currentEnvironment !== 'production'; | ||
|
||
const integrationId = isTestMode ? SMOOCH_INTEGRATION_ID_STAGING : SMOOCH_INTEGRATION_ID; | ||
const url = isTestMode ? WIDGET_URL_STAGING : WIDGET_URL; | ||
const appId = isTestMode ? SMOOCH_APP_ID_STAGING : SMOOCH_APP_ID; | ||
|
||
const formData = new FormData(); | ||
|
||
formData.append( | ||
'author', | ||
JSON.stringify( { | ||
role: 'appUser', | ||
appUserId: authData.externalId, | ||
client: { | ||
platform: 'web', | ||
id: clientId, | ||
integrationId: integrationId, | ||
}, | ||
} ) | ||
); | ||
formData.append( 'message', JSON.stringify( {} ) ); | ||
formData.append( 'source', file ); | ||
|
||
return apiFetch( { | ||
path: `${ url }/sc/sdk/v2/apps/${ appId }/conversations/${ conversationId }/files`, | ||
method: 'POST', | ||
body: formData, | ||
credentials: 'include', | ||
headers: { | ||
Authorization: `Bearer ${ authData.jwt }`, | ||
'x-smooch-appid': appId, | ||
'x-smooch-clientid': clientId, | ||
'x-smooch-sdk': 'web/zendesk/0.1', | ||
}, | ||
} as APIFetchOptions ); | ||
}, | ||
} ); | ||
}; |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@heavyweight I have added this to add the border and correct size