Display correct client link by default

This commit is contained in:
Jorik Schellekens 2020-09-01 10:43:01 +02:00
parent 74d223e475
commit e6b9325d05
3 changed files with 86 additions and 22 deletions

View File

@ -14,24 +14,36 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import React from 'react'; import React, { useState } from 'react';
import './InviteTile.scss'; import './InviteTile.scss';
import Tile from './Tile'; import Tile from './Tile';
import LinkButton from './LinkButton'; import LinkButton from './LinkButton';
import TextButton from './TextButton'; import ClientSelection from './ClientSelection';
import { Client, ClientKind } from '../clients/types'; import { Client, ClientKind } from '../clients/types';
import { SafeLink } from '../parser/types'; import { SafeLink } from '../parser/types';
import TextButton from './TextButton';
interface IProps { interface IProps {
children?: React.ReactNode; children?: React.ReactNode;
client: Client; client: Client | null;
link: SafeLink; link: SafeLink;
} }
const InviteTile: React.FC<IProps> = ({ children, client, link }: IProps) => { const InviteTile: React.FC<IProps> = ({ children, client, link }: IProps) => {
const [showAdvanced, setShowAdvanced] = useState(false);
let invite: React.ReactNode; let invite: React.ReactNode;
let advanced: React.ReactNode;
// This i s a hacky way to get a the overlapping list of client
// options working.
let advancedPlaceholder: React.ReactNode;
if (client === null) {
invite = null;
} else {
let inviteUseString: string;
switch (client.kind) { switch (client.kind) {
case ClientKind.LINKED_CLIENT: case ClientKind.LINKED_CLIENT:
invite = ( invite = (
@ -39,18 +51,52 @@ const InviteTile: React.FC<IProps> = ({ children, client, link }: IProps) => {
Accept invite Accept invite
</LinkButton> </LinkButton>
); );
inviteUseString = `Accepting will open ${link.identifier} in ${client.name}.`;
break; break;
case ClientKind.TEXT_CLIENT: case ClientKind.TEXT_CLIENT:
// TODO: copy to clipboard
invite = <p>{client.toInviteString(link)}</p>; invite = <p>{client.toInviteString(link)}</p>;
inviteUseString = `These are instructions for ${client.name}.`;
break; break;
} }
const advancedToggle = showAdvanced ? (
<TextButton onClick={(): void => setShowAdvanced(!showAdvanced)}>
Hide advanced options
</TextButton>
) : (
<p>
{inviteUseString}
<TextButton
onClick={(): void => setShowAdvanced(!showAdvanced)}
>
Change Client.
</TextButton>
</p>
);
invite = (
<>
{invite}
{advancedToggle}
</>
);
}
if (client === null || showAdvanced) {
advanced = <ClientSelection link={link} />;
advancedPlaceholder = <div className="advancedPlaceholder" />;
}
return ( return (
<>
<Tile className="inviteTile"> <Tile className="inviteTile">
{children} {children}
{invite} {invite}
<TextButton>Advanced options</TextButton> {advancedPlaceholder}
</Tile> </Tile>
{advanced}
</>
); );
}; };

View File

@ -14,7 +14,7 @@ See the License for the specific language governing permissions and
limitations under the License. limitations under the License.
*/ */
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect, useContext } from 'react';
import { getEvent, client } from 'matrix-cypher'; import { getEvent, client } from 'matrix-cypher';
import { RoomPreviewWithTopic } from './RoomPreview'; import { RoomPreviewWithTopic } from './RoomPreview';
@ -22,13 +22,14 @@ import InviteTile from './InviteTile';
import { SafeLink, LinkKind } from '../parser/types'; import { SafeLink, LinkKind } from '../parser/types';
import UserPreview from './UserPreview'; import UserPreview from './UserPreview';
import EventPreview from './EventPreview'; import EventPreview from './EventPreview';
import Clients from '../clients'; import { clientMap } from '../clients';
import { import {
getRoomFromId, getRoomFromId,
getRoomFromAlias, getRoomFromAlias,
getRoomFromPermalink, getRoomFromPermalink,
getUser, getUser,
} from '../utils/cypher-wrapper'; } from '../utils/cypher-wrapper';
import { ClientContext } from '../contexts/ClientContext';
interface IProps { interface IProps {
link: SafeLink; link: SafeLink;
@ -91,8 +92,20 @@ const LinkPreview: React.FC<IProps> = ({ link }: IProps) => {
(async (): Promise<void> => setContent(await invite({ link })))(); (async (): Promise<void> => setContent(await invite({ link })))();
}, [link]); }, [link]);
const [{ rememberSelection, clientId }] = useContext(ClientContext);
// Select which client to link to
const displayClientId =
rememberSelection && clientId
? clientId
: link.arguments.client
? link.arguments.client
: null;
const client = displayClientId ? clientMap[displayClientId] : null;
return ( return (
<InviteTile client={Clients[0]} link={link}> <InviteTile client={client} link={link}>
{content} {content}
</InviteTile> </InviteTile>
); );

View File

@ -10,4 +10,9 @@
row-gap: 60px; row-gap: 60px;
align-items: center; align-items: center;
.advanced {
position: relative;
top: -335px;
}
} }