Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Convert RoomForeword, TextCopy and RoomAvatarEditor to TS #25424

Merged
merged 3 commits into from
May 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,33 +1,44 @@
import { IRoom, isVoipRoom, isDirectMessageRoom } from '@rocket.chat/core-typings';
import { Avatar, Margins, Flex, Box, Tag } from '@rocket.chat/fuselage';
import React, { useCallback } from 'react';
import React, { ReactElement } from 'react';

import { Rooms, Users } from '../../app/models/client';
import { Users } from '../../app/models/client';
import { getUserAvatarURL } from '../../app/utils/client';
import { useTranslation } from '../contexts/TranslationContext';
import { useUser } from '../contexts/UserContext';
import { useReactiveValue } from '../hooks/useReactiveValue';
import { useUser, useUserRoom } from '../contexts/UserContext';
import { VoipRoomForeword } from './voip/room/VoipRoomForeword';

const RoomForeword = ({ _id, rid = _id }) => {
type RoomForewordProps = { _id: IRoom['_id']; rid?: IRoom['_id'] } | { rid: IRoom['_id']; _id?: IRoom['_id'] };

const RoomForeword = ({ _id, rid }: RoomForewordProps): ReactElement | null => {
const roomId = _id || rid;
if (!roomId) {
throw new Error('Room id required - RoomForeword');
}

const t = useTranslation();

const user = useUser();
const room = useReactiveValue(useCallback(() => Rooms.findOne({ _id: rid }), [rid]));
const room = useUserRoom(roomId);

if (!room) {
return null;
}

if (room?.t === 'v') {
if (isVoipRoom(room)) {
return <VoipRoomForeword room={room} />;
}

if (room?.t !== 'd') {
if (!isDirectMessageRoom(room)) {
return (
<Box fontScale='c1' color='default' display='flex' justifyContent='center'>
{t('Start_of_conversation')}
</Box>
);
}

const usernames = room.usernames.filter((username) => username !== user.username);
if (usernames.length < 1) {
const usernames = room.usernames?.filter((username) => username !== user?.username);
if (!usernames || usernames.length < 1) {
return null;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,21 @@
import { Box, Icon, Button, Scrollable } from '@rocket.chat/fuselage';
import React, { useCallback } from 'react';
import React, { useCallback, ComponentProps, ReactElement } from 'react';

import { useToastMessageDispatch } from '../contexts/ToastMessagesContext';
import { useTranslation } from '../contexts/TranslationContext';

const defaultWrapperRenderer = (text) => (
type TextCopyProps = {
text: string;
wrapper?: (text: string) => ReactElement;
} & ComponentProps<typeof Box>;

const defaultWrapperRenderer = (text: string): ReactElement => (
<Box fontFamily='mono' alignSelf='center' fontScale='p2' style={{ wordBreak: 'break-all' }} mie='x4' flexGrow={1} maxHeight='x108'>
{text}
</Box>
);

const TextCopy = ({ text, wrapper = defaultWrapperRenderer, ...props }) => {
const TextCopy = ({ text, wrapper = defaultWrapperRenderer, ...props }: TextCopyProps): ReactElement => {
const t = useTranslation();
const dispatchToastMessage = useToastMessageDispatch();

Expand Down
2 changes: 1 addition & 1 deletion apps/meteor/client/components/avatar/RoomAvatar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import BaseAvatar from './BaseAvatar';

type RoomAvatarProps = {
/* @deprecated */
size?: 'x16' | 'x20' | 'x28' | 'x36' | 'x40' | 'x124';
size?: 'x16' | 'x20' | 'x28' | 'x36' | 'x40' | 'x124' | 'x332';
/* @deprecated */
url?: string;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
import { IRoom } from '@rocket.chat/core-typings';
import { css } from '@rocket.chat/css-in-js';
import { Box, Button, ButtonGroup, Icon } from '@rocket.chat/fuselage';
import { useMutableCallback } from '@rocket.chat/fuselage-hooks';
import React, { useEffect } from 'react';
import React, { useEffect, ReactElement } from 'react';

import { getAvatarURL } from '../../../app/utils/lib/getAvatarURL';
import { useTranslation } from '../../contexts/TranslationContext';
import { useFileInput } from '../../hooks/useFileInput';
import RoomAvatar from './RoomAvatar';

const RoomAvatarEditor = ({ room, roomAvatar, onChangeAvatar = () => {}, ...props }) => {
type RoomAvatarEditorProps = {
room: IRoom;
roomAvatar?: string;
onChangeAvatar: (url: string | null) => void;
};

const RoomAvatarEditor = ({ room, roomAvatar, onChangeAvatar }: RoomAvatarEditorProps): ReactElement => {
const t = useTranslation();

const handleChangeAvatar = useMutableCallback((file) => {
const reader = new FileReader();
reader.readAsDataURL(file);
reader.onloadend = () => {
onChangeAvatar(reader.result);
reader.onloadend = (): void => {
if (typeof reader.result === 'string') {
onChangeAvatar(reader.result);
}
};
});

Expand All @@ -32,7 +41,7 @@ const RoomAvatarEditor = ({ room, roomAvatar, onChangeAvatar = () => {}, ...prop
const defaultUrl = room.prid ? getAvatarURL({ roomId: room.prid }) : getAvatarURL({ username: `@${room.name}` }); // Discussions inherit avatars from the parent room

return (
<Box borderRadius='x2' maxWidth='x332' w='full' position='relative' {...props}>
<Box borderRadius='x2' maxWidth='x332' w='full' position='relative'>
<RoomAvatar {...(roomAvatar !== undefined && { url: roomAvatar === null ? defaultUrl : roomAvatar })} room={room} size='x332' />
<Box
className={[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const BackupCodesModal: FC<BackupCodesModalProps> = ({ codes, onClose, ...props
<Box mb='x8' withRichContent>
{t('Make_sure_you_have_a_copy_of_your_codes_1')}
</Box>
<TextCopy text={codesText} wordBreak='break-word' mb='x8' />
<TextCopy text={codesText} mb='x8' />
<Box mb='x8' withRichContent>
{t('Make_sure_you_have_a_copy_of_your_codes_2')}
</Box>
Expand Down