-
Notifications
You must be signed in to change notification settings - Fork 10.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Chore: Rewrite SaveToWebdav Modal to React Component (#24365)
Co-authored-by: Pierre Lehnen <[email protected]>
- Loading branch information
1 parent
7dbf2ed
commit 31360e1
Showing
18 changed files
with
192 additions
and
108 deletions.
There are no files selected for viewing
This file contains 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 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 was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
2 changes: 1 addition & 1 deletion
2
...av/server/methods/getWebdavCredentials.ts → ...webdav/server/lib/getWebdavCredentials.ts
This file contains 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 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,21 @@ | ||
import { WebdavAccounts } from '@rocket.chat/models'; | ||
import type { IWebdavAccount } from '@rocket.chat/core-typings'; | ||
|
||
import { getWebdavCredentials } from './getWebdavCredentials'; | ||
import { WebdavClientAdapter } from './webdavClientAdapter'; | ||
|
||
export const uploadFileToWebdav = async (accountId: IWebdavAccount['_id'], fileData: string | Buffer, name: string): Promise<void> => { | ||
const account = await WebdavAccounts.findOneById(accountId); | ||
if (!account) { | ||
throw new Error('error-invalid-account'); | ||
} | ||
|
||
const uploadFolder = 'Rocket.Chat Uploads/'; | ||
const buffer = Buffer.from(fileData); | ||
|
||
const cred = getWebdavCredentials(account); | ||
const client = new WebdavClientAdapter(account.serverURL, cred); | ||
// eslint-disable-next-line @typescript-eslint/no-empty-function | ||
await client.createDirectory(uploadFolder).catch(() => {}); | ||
await client.putFileContents(`${uploadFolder}/${name}`, buffer, { overwrite: false }); | ||
}; |
This file contains 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 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 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
31 changes: 11 additions & 20 deletions
31
apps/meteor/app/webdav/server/methods/uploadFileToWebdav.ts
This file contains 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 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,4 @@ | ||
import { IWebdavAccountIntegration } from '@rocket.chat/core-typings'; | ||
|
||
export const getWebdavServerName = ({ name, serverURL, username }: Omit<IWebdavAccountIntegration, '_id'>): string => | ||
name || `${username}@${serverURL?.replace(/^https?\:\/\//i, '')}`; |
This file contains 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
120 changes: 120 additions & 0 deletions
120
apps/meteor/client/views/room/webdav/SaveToWebdavModal.tsx
This file contains 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,120 @@ | ||
import { MessageAttachment, IWebdavAccount } from '@rocket.chat/core-typings'; | ||
import { Modal, Box, ButtonGroup, Button, FieldGroup, Field, Select, SelectOption, Throbber } from '@rocket.chat/fuselage'; | ||
import { useUniqueId } from '@rocket.chat/fuselage-hooks'; | ||
import { useMethod, useToastMessageDispatch, useTranslation } from '@rocket.chat/ui-contexts'; | ||
import React, { ReactElement, useState, useMemo, useEffect, useRef } from 'react'; | ||
import { useForm, Controller } from 'react-hook-form'; | ||
|
||
import { useEndpointData } from '../../../hooks/useEndpointData'; | ||
import { getWebdavServerName } from '../../../lib/getWebdavServerName'; | ||
|
||
type SaveToWebdavModalProps = { | ||
onClose: () => void; | ||
data: { | ||
attachment: MessageAttachment; | ||
url: string; | ||
}; | ||
}; | ||
|
||
const SaveToWebdavModal = ({ onClose, data }: SaveToWebdavModalProps): ReactElement => { | ||
const t = useTranslation(); | ||
const [isLoading, setIsLoading] = useState(false); | ||
const dispatchToastMessage = useToastMessageDispatch(); | ||
const uploadFileToWebdav = useMethod('uploadFileToWebdav'); | ||
const fileRequest = useRef<XMLHttpRequest | null>(null); | ||
const accountIdField = useUniqueId(); | ||
|
||
const { | ||
control, | ||
handleSubmit, | ||
formState: { errors }, | ||
} = useForm<{ accountId: string }>(); | ||
|
||
const { value } = useEndpointData('/v1/webdav.getMyAccounts'); | ||
|
||
const accountsOptions: SelectOption[] = useMemo(() => { | ||
if (value?.accounts) { | ||
return value.accounts.map(({ _id, ...current }) => [_id, getWebdavServerName(current)]); | ||
} | ||
|
||
return []; | ||
}, [value?.accounts]); | ||
|
||
useEffect(() => fileRequest.current?.abort, []); | ||
|
||
const handleSaveFile = ({ accountId }: { accountId: IWebdavAccount['_id'] }): void => { | ||
setIsLoading(true); | ||
|
||
const { | ||
url, | ||
attachment: { title }, | ||
} = data; | ||
|
||
fileRequest.current = new XMLHttpRequest(); | ||
fileRequest.current.open('GET', url, true); | ||
fileRequest.current.responseType = 'arraybuffer'; | ||
fileRequest.current.onload = async (): Promise<void> => { | ||
const arrayBuffer = fileRequest.current?.response; | ||
if (arrayBuffer) { | ||
const fileData = new Uint8Array(arrayBuffer); | ||
|
||
try { | ||
const response = await uploadFileToWebdav(accountId, fileData, title); | ||
if (!response.success) { | ||
return dispatchToastMessage({ type: 'error', message: t(response.message) }); | ||
} | ||
return dispatchToastMessage({ type: 'success', message: t('File_uploaded') }); | ||
} catch (error) { | ||
return dispatchToastMessage({ type: 'error', message: error as Error }); | ||
} finally { | ||
setIsLoading(false); | ||
onClose(); | ||
} | ||
} | ||
}; | ||
fileRequest.current.send(null); | ||
}; | ||
|
||
return ( | ||
<Modal is='form' onSubmit={handleSubmit(handleSaveFile)}> | ||
<Modal.Header> | ||
<Modal.Title>{t('Save_To_Webdav')}</Modal.Title> | ||
<Modal.Close title={t('Close')} onClick={onClose} /> | ||
</Modal.Header> | ||
<Modal.Content> | ||
{isLoading && ( | ||
<Box alignItems='center' display='flex' justifyContent='center' minHeight='x32'> | ||
<Throbber /> | ||
</Box> | ||
)} | ||
{!isLoading && ( | ||
<FieldGroup> | ||
<Field> | ||
<Field.Label>{t('Select_a_webdav_server')}</Field.Label> | ||
<Field.Row> | ||
<Controller | ||
name='accountId' | ||
control={control} | ||
rules={{ required: true }} | ||
render={({ field }): ReactElement => ( | ||
<Select {...field} options={accountsOptions} id={accountIdField} placeholder={t('Select_an_option')} /> | ||
)} | ||
/> | ||
</Field.Row> | ||
{errors.accountId && <Field.Error>{t('Field_required')}</Field.Error>} | ||
</Field> | ||
</FieldGroup> | ||
)} | ||
</Modal.Content> | ||
<Modal.Footer> | ||
<ButtonGroup align='end'> | ||
<Button onClick={onClose}>{t('Cancel')}</Button> | ||
<Button primary type='submit' disabled={isLoading}> | ||
{isLoading ? t('Please_wait') : t('Save_To_Webdav')} | ||
</Button> | ||
</ButtonGroup> | ||
</Modal.Footer> | ||
</Modal> | ||
); | ||
}; | ||
export default SaveToWebdavModal; |
This file contains 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 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
Oops, something went wrong.