env.esq/src/components/LinkPreview.tsx

188 lines
5.2 KiB
TypeScript
Raw Normal View History

/*
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-09-01 10:43:01 +02:00
import React, { useState, useEffect, useContext } from 'react';
2020-09-16 15:57:16 +02:00
import { getEvent, client } from '../matrix-cypher';
2020-08-18 12:16:31 +02:00
import { RoomPreviewWithTopic } from './RoomPreview';
import InviteTile from './InviteTile';
import { SafeLink, LinkKind } from '../parser/types';
import UserPreview, { WrappedInviterPreview } from './UserPreview';
2020-08-18 12:16:31 +02:00
import EventPreview from './EventPreview';
2020-09-17 16:41:32 +02:00
import GroupPreview from './GroupPreview';
2020-09-13 15:58:37 +02:00
import HomeserverOptions from './HomeserverOptions';
import DefaultPreview from './DefaultPreview';
import Toggle from './Toggle';
2020-09-01 10:43:01 +02:00
import { clientMap } from '../clients';
2020-08-18 12:07:26 +02:00
import {
getRoomFromId,
getRoomFromAlias,
getRoomFromPermalink,
getUser,
2020-09-17 16:41:32 +02:00
getGroup,
2020-08-18 12:16:31 +02:00
} from '../utils/cypher-wrapper';
2020-09-01 10:43:01 +02:00
import { ClientContext } from '../contexts/ClientContext';
import useHSs from '../utils/getHS';
interface IProps {
link: SafeLink;
}
2020-09-13 15:58:37 +02:00
const invite = async ({
clientAddress,
link,
}: {
clientAddress: string;
link: SafeLink;
}): Promise<JSX.Element> => {
2020-08-18 12:07:26 +02:00
// TODO: replace with client fetch
switch (link.kind) {
case LinkKind.Alias:
return (
<RoomPreviewWithTopic
room={
2020-09-16 01:19:52 +02:00
await getRoomFromAlias(clientAddress, link.identifier)
}
/>
);
case LinkKind.RoomId:
return (
<RoomPreviewWithTopic
2020-09-16 01:19:52 +02:00
room={await getRoomFromId(clientAddress, link.identifier)}
/>
);
case LinkKind.UserId:
return (
<UserPreview
2020-09-16 01:19:52 +02:00
user={await getUser(clientAddress, link.identifier)}
userId={link.identifier}
/>
);
case LinkKind.Permalink:
return (
<EventPreview
2020-09-16 01:19:52 +02:00
room={await getRoomFromPermalink(clientAddress, link)}
event={
await getEvent(
2020-09-16 01:19:52 +02:00
await client(clientAddress),
link.roomLink,
link.eventId
)
}
/>
);
2020-09-17 16:41:32 +02:00
case LinkKind.GroupId:
return (
<GroupPreview
group={await getGroup(clientAddress, link.identifier)}
/>
);
default:
// Todo Implement events
return <></>;
}
};
2020-09-13 15:58:37 +02:00
interface PreviewProps extends IProps {
client: string;
}
2020-09-13 15:58:37 +02:00
const Preview: React.FC<PreviewProps> = ({ link, client }: PreviewProps) => {
const [content, setContent] = useState(<DefaultPreview link={link} />);
// TODO: support multiple clients with vias
useEffect(() => {
2020-09-13 15:58:37 +02:00
(async (): Promise<void> =>
setContent(
await invite({
clientAddress: client,
link,
})
))();
}, [link, client]);
return content;
};
const LinkPreview: React.FC<IProps> = ({ link }: IProps) => {
let content: JSX.Element;
const [showHSOptions, setShowHSOPtions] = useState(false);
const hses = useHSs(link);
2020-09-13 19:02:18 +02:00
if (!hses.length) {
2020-09-13 15:58:37 +02:00
content = (
<>
<DefaultPreview link={link} />
<Toggle
checked={showHSOptions}
onChange={(): void => setShowHSOPtions(!showHSOptions)}
>
2020-09-16 01:19:52 +02:00
About {link.identifier}
2020-09-13 15:58:37 +02:00
</Toggle>
</>
);
if (showHSOptions) {
content = (
<>
{content}
2020-09-16 01:19:52 +02:00
<HomeserverOptions link={link} />
2020-09-13 15:58:37 +02:00
</>
);
}
} else {
content = <Preview link={link} client={hses[0]} />;
2020-09-13 15:58:37 +02:00
}
const [{ clientId }] = useContext(ClientContext);
2020-09-01 10:43:01 +02:00
// Select which client to link to
const displayClientId = clientId
? clientId
: link.arguments.client
? link.arguments.client
: null;
2020-09-01 10:43:01 +02:00
const client = displayClientId ? clientMap[displayClientId] : null;
const sharer = link.arguments.sharer ? (
<WrappedInviterPreview
link={{
kind: LinkKind.UserId,
identifier: link.arguments.sharer,
arguments: { vias: [] },
originalLink: '',
}}
/>
2020-09-16 01:19:52 +02:00
) : (
<p style={{ margin: '0 0 10px 0' }}>You're invited to join</p>
);
return (
2020-09-01 10:43:01 +02:00
<InviteTile client={client} link={link}>
{sharer}
{content}
</InviteTile>
);
};
export default LinkPreview;