-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement the download_file widget action
Signed-off-by: Michael Weimann <[email protected]>
- Loading branch information
1 parent
8518d2f
commit 354910f
Showing
8 changed files
with
98 additions
and
7 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'@matrix-widget-toolkit/api': minor | ||
--- | ||
|
||
Add support for the download_file widget action |
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,66 @@ | ||
/* | ||
* Copyright 2024 Nordeck IT + Consulting GmbH | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import { useWidgetApi } from '@matrix-widget-toolkit/react'; | ||
import React, { useCallback, useEffect, useState } from 'react'; | ||
|
||
type ImageProps = { | ||
alt?: string; | ||
/** | ||
* MXC URI of the image that should be shown | ||
*/ | ||
contentUrl: string; | ||
}; | ||
|
||
/** | ||
* Component that loads the image from the content repository and displays it. | ||
*/ | ||
export const Image: React.FC<ImageProps> = function ({ | ||
contentUrl, | ||
...imageProps | ||
}) { | ||
const [dataUrl, setDataUrl] = useState<string>(); | ||
const widgetApi = useWidgetApi(); | ||
|
||
const handleLoad = useCallback(() => { | ||
if (dataUrl) { | ||
URL.revokeObjectURL(dataUrl); | ||
} | ||
}, [dataUrl]); | ||
|
||
useEffect(() => { | ||
(async () => { | ||
try { | ||
const result = await widgetApi.downloadFile(contentUrl); | ||
|
||
if (!(result.file instanceof Blob)) { | ||
throw new Error('Got non Blob file response'); | ||
} | ||
|
||
const downloadedFileDataUrl = URL.createObjectURL(result.file); | ||
setDataUrl(downloadedFileDataUrl); | ||
} catch (error) { | ||
console.log('Error downloading file', error); | ||
} | ||
})(); | ||
}, [contentUrl]); | ||
|
||
if (dataUrl === undefined) { | ||
return null; | ||
} | ||
|
||
return <img {...imageProps} src={dataUrl} onLoad={handleLoad} />; | ||
}; |
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
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