mirror of
https://github.com/vector-im/element-web.git
synced 2024-11-16 13:14:58 +08:00
Extract logic to a function
For better readability of the call to useAsyncMemo() Signed-off-by: Paulo Pinto <paulo.pinto@automattic.com>
This commit is contained in:
parent
b461569008
commit
dabc13c98f
@ -16,7 +16,7 @@ limitations under the License.
|
|||||||
|
|
||||||
import React, { useContext, useEffect } from "react";
|
import React, { useContext, useEffect } from "react";
|
||||||
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
import { MatrixEvent } from "matrix-js-sdk/src/models/event";
|
||||||
import { IPreviewUrlResponse } from "matrix-js-sdk/src/client";
|
import { IPreviewUrlResponse, MatrixClient } from "matrix-js-sdk/src/client";
|
||||||
|
|
||||||
import { useStateToggle } from "../../../hooks/useStateToggle";
|
import { useStateToggle } from "../../../hooks/useStateToggle";
|
||||||
import LinkPreviewWidget from "./LinkPreviewWidget";
|
import LinkPreviewWidget from "./LinkPreviewWidget";
|
||||||
@ -40,13 +40,7 @@ const LinkPreviewGroup: React.FC<IProps> = ({ links, mxEvent, onCancelClick, onH
|
|||||||
|
|
||||||
const ts = mxEvent.getTs();
|
const ts = mxEvent.getTs();
|
||||||
const previews = useAsyncMemo<[string, IPreviewUrlResponse][]>(async () => {
|
const previews = useAsyncMemo<[string, IPreviewUrlResponse][]>(async () => {
|
||||||
return Promise.all<[string, IPreviewUrlResponse] | void>(links.map(async link => {
|
return fetchPreviews(cli, links, ts);
|
||||||
try {
|
|
||||||
return [link, await cli.getUrlPreview(link, ts)];
|
|
||||||
} catch (error) {
|
|
||||||
console.error("Failed to get URL preview: " + error);
|
|
||||||
}
|
|
||||||
})).then(a => a.filter(Boolean)) as Promise<[string, IPreviewUrlResponse][]>;
|
|
||||||
}, [links, ts], []);
|
}, [links, ts], []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -89,4 +83,15 @@ const LinkPreviewGroup: React.FC<IProps> = ({ links, mxEvent, onCancelClick, onH
|
|||||||
</div>;
|
</div>;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const fetchPreviews = (cli: MatrixClient, links: string[], ts: number):
|
||||||
|
Promise<[string, IPreviewUrlResponse][]> => {
|
||||||
|
return Promise.all<[string, IPreviewUrlResponse] | void>(links.map(async link => {
|
||||||
|
try {
|
||||||
|
return [link, await cli.getUrlPreview(link, ts)];
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Failed to get URL preview: " + error);
|
||||||
|
}
|
||||||
|
})).then(a => a.filter(Boolean)) as Promise<[string, IPreviewUrlResponse][]>;
|
||||||
|
};
|
||||||
|
|
||||||
export default LinkPreviewGroup;
|
export default LinkPreviewGroup;
|
||||||
|
Loading…
Reference in New Issue
Block a user