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 UserStatusMenu to TS #25265

Merged
merged 5 commits into from
May 10, 2022
Merged
Changes from 1 commit
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,17 +1,33 @@
import { UserStatus as UserStatusType } from '@rocket.chat/core-typings';
import { Button, PositionAnimated, Options, useCursor, Box } from '@rocket.chat/fuselage';
import type { Placements } from '@rocket.chat/fuselage-hooks';
import React, { useRef, useCallback, useState, useMemo, useEffect } from 'react';

import { useSetting } from '../contexts/SettingsContext';
import { useTranslation } from '../contexts/TranslationContext';
import { UserStatus } from './UserStatus';

const UserStatusMenu = ({ onChange, optionWidth = undefined, initialStatus = 'offline', placement = 'bottom-end', ...props }) => {
type UserStatusMenuProps = {
margin: string;
debdutdeb marked this conversation as resolved.
Show resolved Hide resolved
onChange: (type: UserStatusType) => void;
initialStatus?: UserStatusType;
optionWidth?: any;
debdutdeb marked this conversation as resolved.
Show resolved Hide resolved
placement?: Placements;
};

const UserStatusMenu = ({
margin,
onChange,
initialStatus = UserStatusType.OFFLINE,
optionWidth = undefined,
placement = 'bottom-end',
}: UserStatusMenuProps): React.ReactElement => {
debdutdeb marked this conversation as resolved.
Show resolved Hide resolved
const t = useTranslation();
const [status, setStatus] = useState(initialStatus);
const allowInvisibleStatus = useSetting('Accounts_AllowInvisibleStatusOption');
const [status, setStatus] = useState<UserStatusType>(initialStatus);
const allowInvisibleStatus = useSetting('Accounts_AllowInvisibleStatusOption') as boolean;
tassoevan marked this conversation as resolved.
Show resolved Hide resolved

const options = useMemo(() => {
const renderOption = (status, label) => (
const renderOption = (status: UserStatusType, label: string): React.ReactElement => (
debdutdeb marked this conversation as resolved.
Show resolved Hide resolved
<Box display='flex' flexDirection='row' alignItems='center'>
<Box marginInlineEnd='x8'>
<UserStatus status={status} />
Expand All @@ -20,14 +36,14 @@ const UserStatusMenu = ({ onChange, optionWidth = undefined, initialStatus = 'of
</Box>
);

const statuses = [
['online', renderOption('online', t('Online'))],
['away', renderOption('away', t('Away'))],
['busy', renderOption('busy', t('Busy'))],
const statuses: Array<[value: UserStatusType, label: React.ReactElement]> = [
debdutdeb marked this conversation as resolved.
Show resolved Hide resolved
[UserStatusType.ONLINE, renderOption(UserStatusType.ONLINE, t('Online'))],
[UserStatusType.AWAY, renderOption(UserStatusType.AWAY, t('Away'))],
[UserStatusType.BUSY, renderOption(UserStatusType.BUSY, t('Busy'))],
];

if (allowInvisibleStatus) {
statuses.push(['offline', renderOption('offline', t('Invisible'))]);
statuses.push([UserStatusType.OFFLINE, renderOption(UserStatusType.OFFLINE, t('Invisible'))]);
}

return statuses;
Expand All @@ -39,9 +55,13 @@ const UserStatusMenu = ({ onChange, optionWidth = undefined, initialStatus = 'of
hide();
});

const ref = useRef();
const ref = useRef<HTMLElement>(null);
debdutdeb marked this conversation as resolved.
Show resolved Hide resolved
const onClick = useCallback(() => {
ref.current.focus() & show();
if (!ref?.current) {
return;
}
(ref.current as HTMLButtonElement).focus();
show();
ref.current.classList.add('focus-visible');
}, [show]);

Expand All @@ -58,7 +78,7 @@ const UserStatusMenu = ({ onChange, optionWidth = undefined, initialStatus = 'of

return (
<>
<Button ref={ref} small square ghost onClick={onClick} onBlur={hide} onKeyUp={handleKeyUp} onKeyDown={handleKeyDown} {...props}>
<Button ref={ref} small square ghost onClick={onClick} onBlur={hide} onKeyUp={handleKeyUp} onKeyDown={handleKeyDown} margin={margin}>
debdutdeb marked this conversation as resolved.
Show resolved Hide resolved
<UserStatus status={status} />
</Button>
<PositionAnimated width='auto' visible={visible} anchor={ref} placement={placement}>
Expand Down