You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
198 lines
5.1 KiB
TypeScript
198 lines
5.1 KiB
TypeScript
4 years ago
|
import React, { useCallback, useEffect, useState } from 'react';
|
||
4 years ago
|
import { useDispatch, useSelector } from 'react-redux';
|
||
3 years ago
|
|
||
|
import styled from 'styled-components';
|
||
4 years ago
|
import {
|
||
|
joinOpenGroupV2WithUIEvents,
|
||
|
parseOpenGroupV2,
|
||
3 years ago
|
} from '../../../session/apis/open_group_api/opengroupV2/JoinOpenGroupV2';
|
||
|
import { downloadPreviewOpenGroupV2 } from '../../../session/apis/open_group_api/opengroupV2/OpenGroupAPIV2';
|
||
|
import { updateDefaultBase64RoomData } from '../../../state/ducks/defaultRooms';
|
||
|
import { StateType } from '../../../state/reducer';
|
||
|
import { Avatar, AvatarSize } from '../../avatar/Avatar';
|
||
|
import { Flex } from '../../basic/Flex';
|
||
|
import { PillContainerHoverable, PillTooltipWrapper } from '../../basic/PillContainer';
|
||
|
import { SessionSpinner } from '../../basic/SessionSpinner';
|
||
|
import { H3 } from '../../basic/Text';
|
||
4 years ago
|
// tslint:disable: no-void-expression
|
||
|
|
||
|
export type JoinableRoomProps = {
|
||
|
completeUrl: string;
|
||
|
name: string;
|
||
4 years ago
|
roomId: string;
|
||
4 years ago
|
imageId?: string;
|
||
|
onClick: (completeUrl: string) => void;
|
||
4 years ago
|
base64Data?: string;
|
||
4 years ago
|
};
|
||
|
|
||
|
const SessionJoinableRoomAvatar = (props: JoinableRoomProps) => {
|
||
4 years ago
|
const dispatch = useDispatch();
|
||
4 years ago
|
useEffect(() => {
|
||
4 years ago
|
let isCancelled = false;
|
||
|
|
||
4 years ago
|
try {
|
||
|
const parsedInfos = parseOpenGroupV2(props.completeUrl);
|
||
|
if (parsedInfos) {
|
||
4 years ago
|
if (props.base64Data) {
|
||
|
return;
|
||
|
}
|
||
4 years ago
|
if (isCancelled) {
|
||
|
return;
|
||
|
}
|
||
4 years ago
|
void downloadPreviewOpenGroupV2(parsedInfos)
|
||
|
.then(base64 => {
|
||
4 years ago
|
if (isCancelled) {
|
||
|
return;
|
||
|
}
|
||
4 years ago
|
const payload = {
|
||
|
roomId: props.roomId,
|
||
|
base64Data: base64 || '',
|
||
|
};
|
||
4 years ago
|
dispatch(updateDefaultBase64RoomData(payload));
|
||
4 years ago
|
})
|
||
|
.catch(e => {
|
||
4 years ago
|
if (isCancelled) {
|
||
|
return;
|
||
|
}
|
||
4 years ago
|
window?.log?.warn('downloadPreviewOpenGroupV2 failed', e);
|
||
4 years ago
|
const payload = {
|
||
|
roomId: props.roomId,
|
||
|
base64Data: '',
|
||
|
};
|
||
4 years ago
|
dispatch(updateDefaultBase64RoomData(payload));
|
||
4 years ago
|
});
|
||
|
}
|
||
|
} catch (e) {
|
||
4 years ago
|
window?.log?.warn(e);
|
||
4 years ago
|
}
|
||
4 years ago
|
return () => {
|
||
|
isCancelled = true;
|
||
|
};
|
||
4 years ago
|
}, [props.imageId, props.completeUrl]);
|
||
4 years ago
|
|
||
4 years ago
|
return (
|
||
|
<Avatar
|
||
|
size={AvatarSize.XS}
|
||
4 years ago
|
base64Data={props.base64Data}
|
||
4 years ago
|
{...props}
|
||
3 years ago
|
pubkey=""
|
||
4 years ago
|
onAvatarClick={() => props.onClick(props.completeUrl)}
|
||
|
/>
|
||
|
);
|
||
|
};
|
||
|
|
||
4 years ago
|
const StyledRoomName = styled(Flex)`
|
||
|
overflow: hidden;
|
||
|
white-space: nowrap;
|
||
|
text-overflow: ellipsis;
|
||
|
padding: 0 10px;
|
||
|
`;
|
||
|
|
||
4 years ago
|
const StyledToolTip = styled.div`
|
||
4 years ago
|
padding: var(--margins-sm);
|
||
|
background: var(--color-clickable-hovered);
|
||
|
font-size: var(--font-size-xs);
|
||
|
border: 1px solid var(--color-pill-divider);
|
||
4 years ago
|
display: inline-block;
|
||
|
position: absolute;
|
||
|
white-space: normal;
|
||
|
|
||
|
top: 100%;
|
||
|
left: 10%;
|
||
|
|
||
|
border-radius: 300px;
|
||
4 years ago
|
z-index: 5;
|
||
4 years ago
|
opacity: 1;
|
||
|
animation: fadeIn 0.5s ease-out;
|
||
|
|
||
|
max-width: 80%;
|
||
|
|
||
|
@keyframes fadeIn {
|
||
|
from {
|
||
|
opacity: 0;
|
||
|
}
|
||
|
}
|
||
|
`;
|
||
|
|
||
4 years ago
|
const SessionJoinableRoomName = (props: JoinableRoomProps) => {
|
||
4 years ago
|
return <StyledRoomName>{props.name}</StyledRoomName>;
|
||
4 years ago
|
};
|
||
|
|
||
|
const SessionJoinableRoomRow = (props: JoinableRoomProps) => {
|
||
4 years ago
|
const [isHovering, setIsHovering] = useState(false);
|
||
|
|
||
|
const handleMouseEnter = () => {
|
||
|
setIsHovering(true);
|
||
4 years ago
|
};
|
||
4 years ago
|
const handleMouseLeave = () => {
|
||
|
setIsHovering(false);
|
||
4 years ago
|
};
|
||
4 years ago
|
|
||
4 years ago
|
const showTooltip = isHovering;
|
||
4 years ago
|
|
||
4 years ago
|
return (
|
||
4 years ago
|
<PillTooltipWrapper>
|
||
|
<PillContainerHoverable
|
||
|
onClick={() => {
|
||
|
props.onClick(props.completeUrl);
|
||
|
}}
|
||
|
margin="5px"
|
||
|
padding="5px"
|
||
|
onMouseEnter={handleMouseEnter}
|
||
|
onMouseLeave={handleMouseLeave}
|
||
|
>
|
||
|
<SessionJoinableRoomAvatar {...props} />
|
||
|
<SessionJoinableRoomName {...props} />
|
||
|
</PillContainerHoverable>
|
||
|
|
||
4 years ago
|
{showTooltip && false && <StyledToolTip>{props.name}</StyledToolTip>}
|
||
4 years ago
|
</PillTooltipWrapper>
|
||
4 years ago
|
);
|
||
|
};
|
||
|
|
||
4 years ago
|
export const SessionJoinableRooms = (props: { onRoomClicked: () => void }) => {
|
||
4 years ago
|
const joinableRooms = useSelector((state: StateType) => state.defaultRooms);
|
||
|
|
||
4 years ago
|
const onRoomClicked = useCallback(
|
||
|
(loading: boolean) => {
|
||
|
if (loading) {
|
||
|
props.onRoomClicked();
|
||
|
}
|
||
|
},
|
||
|
[props.onRoomClicked]
|
||
|
);
|
||
|
|
||
4 years ago
|
if (!joinableRooms.inProgress && !joinableRooms.rooms?.length) {
|
||
|
window?.log?.info('no default joinable rooms yet and not in progress');
|
||
4 years ago
|
return null;
|
||
4 years ago
|
}
|
||
|
|
||
4 years ago
|
const componentToRender = joinableRooms.inProgress ? (
|
||
|
<SessionSpinner loading={true} />
|
||
|
) : (
|
||
|
joinableRooms.rooms.map(r => {
|
||
|
return (
|
||
|
<SessionJoinableRoomRow
|
||
|
key={r.id}
|
||
|
completeUrl={r.completeUrl}
|
||
|
name={r.name}
|
||
|
roomId={r.id}
|
||
|
base64Data={r.base64Data}
|
||
|
onClick={completeUrl => {
|
||
4 years ago
|
void joinOpenGroupV2WithUIEvents(completeUrl, true, false, onRoomClicked);
|
||
4 years ago
|
}}
|
||
|
/>
|
||
|
);
|
||
|
})
|
||
|
);
|
||
|
|
||
4 years ago
|
return (
|
||
4 years ago
|
<Flex container={true} flexGrow={1} flexDirection="column" width="93%">
|
||
|
<H3 text={window.i18n('orJoinOneOfThese')} />
|
||
4 years ago
|
<Flex container={true} flexGrow={1} flexWrap="wrap" justifyContent="center">
|
||
|
{componentToRender}
|
||
4 years ago
|
</Flex>
|
||
4 years ago
|
</Flex>
|
||
|
);
|
||
|
};
|