2020-08-17 18:48:13 +02:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Matrix.org Foundation C.I.C.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2020-08-18 12:16:31 +02:00
|
|
|
import React, { useState, useEffect } from 'react';
|
|
|
|
import { getEvent, client } from 'matrix-cypher';
|
|
|
|
|
|
|
|
import { RoomPreviewWithTopic } from './RoomPreview';
|
|
|
|
import InviteTile from './InviteTile';
|
|
|
|
import { SafeLink, LinkKind } from '../parser/types';
|
|
|
|
import UserPreview from './UserPreview';
|
|
|
|
import EventPreview from './EventPreview';
|
|
|
|
import Clients from '../clients';
|
2020-08-18 12:07:26 +02:00
|
|
|
import {
|
|
|
|
getRoomFromId,
|
|
|
|
getRoomFromAlias,
|
|
|
|
getRoomFromPermalink,
|
|
|
|
getUser,
|
2020-08-18 12:16:31 +02:00
|
|
|
} from '../utils/cypher-wrapper';
|
2020-08-17 18:48:13 +02:00
|
|
|
|
|
|
|
interface IProps {
|
|
|
|
link: SafeLink;
|
|
|
|
}
|
|
|
|
|
|
|
|
const LOADING: JSX.Element = <>Generating invite</>;
|
|
|
|
|
|
|
|
const invite = async ({ link }: { link: SafeLink }): Promise<JSX.Element> => {
|
2020-08-18 12:07:26 +02:00
|
|
|
// TODO: replace with client fetch
|
2020-08-18 12:16:31 +02:00
|
|
|
const defaultClient = await client('https://matrix.org');
|
2020-08-17 18:48:13 +02:00
|
|
|
switch (link.kind) {
|
|
|
|
case LinkKind.Alias:
|
|
|
|
return (
|
|
|
|
<RoomPreviewWithTopic
|
|
|
|
room={
|
2020-08-18 12:07:26 +02:00
|
|
|
await getRoomFromAlias(defaultClient, link.identifier)
|
2020-08-17 18:48:13 +02:00
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
case LinkKind.RoomId:
|
|
|
|
return (
|
|
|
|
<RoomPreviewWithTopic
|
2020-08-18 12:07:26 +02:00
|
|
|
room={await getRoomFromId(defaultClient, link.identifier)}
|
2020-08-17 18:48:13 +02:00
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
case LinkKind.UserId:
|
|
|
|
return (
|
|
|
|
<UserPreview
|
2020-08-18 12:07:26 +02:00
|
|
|
user={await getUser(defaultClient, link.identifier)}
|
2020-08-17 18:48:13 +02:00
|
|
|
userId={link.identifier}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
case LinkKind.Permalink:
|
|
|
|
return (
|
|
|
|
<EventPreview
|
2020-08-18 12:07:26 +02:00
|
|
|
room={await getRoomFromPermalink(defaultClient, link)}
|
2020-08-17 18:48:13 +02:00
|
|
|
event={
|
|
|
|
await getEvent(
|
2020-08-18 12:07:26 +02:00
|
|
|
defaultClient,
|
2020-08-17 18:48:13 +02:00
|
|
|
link.roomLink,
|
|
|
|
link.eventId
|
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
|
|
|
|
default:
|
|
|
|
// Todo Implement events
|
|
|
|
return <></>;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const LinkPreview: React.FC<IProps> = ({ link }: IProps) => {
|
|
|
|
const [content, setContent] = useState(LOADING);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
(async (): Promise<void> => setContent(await invite({ link })))();
|
|
|
|
}, [link]);
|
|
|
|
|
|
|
|
return (
|
|
|
|
<InviteTile client={Clients[0]} link={link}>
|
|
|
|
{content}
|
|
|
|
</InviteTile>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default LinkPreview;
|