2022-08-26 09:54:03 +00:00
|
|
|
/* eslint-disable lines-around-comment */
|
2022-10-19 07:19:40 +00:00
|
|
|
import React, { useCallback, useEffect, useRef, useState } from 'react';
|
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
import { useDispatch, useSelector } from 'react-redux';
|
|
|
|
import { makeStyles } from 'tss-react/mui';
|
2017-06-29 03:35:43 +00:00
|
|
|
|
2022-10-20 09:11:27 +00:00
|
|
|
import { IReduxState } from '../../../app/types';
|
2019-01-13 19:33:28 +00:00
|
|
|
import {
|
2022-09-27 07:10:28 +00:00
|
|
|
getParticipantById,
|
|
|
|
getParticipantDisplayName
|
2022-08-26 09:54:03 +00:00
|
|
|
} from '../../../base/participants/functions';
|
2022-10-11 10:47:54 +00:00
|
|
|
import { updateSettings } from '../../../base/settings/actions';
|
2022-09-13 07:36:00 +00:00
|
|
|
import { withPixelLineHeight } from '../../../base/styles/functions.web';
|
2022-08-26 09:54:03 +00:00
|
|
|
// @ts-ignore
|
2021-12-15 13:18:41 +00:00
|
|
|
import { Tooltip } from '../../../base/tooltip';
|
2022-08-26 09:54:03 +00:00
|
|
|
// @ts-ignore
|
2021-12-15 13:18:41 +00:00
|
|
|
import { getIndicatorsTooltipPosition } from '../../../filmstrip/functions.web';
|
2019-04-11 08:28:51 +00:00
|
|
|
import { appendSuffix } from '../../functions';
|
2017-06-29 03:35:43 +00:00
|
|
|
|
|
|
|
/**
|
2018-10-30 05:02:23 +00:00
|
|
|
* The type of the React {@code Component} props of {@link DisplayName}.
|
2017-06-29 03:35:43 +00:00
|
|
|
*/
|
2022-10-20 09:11:27 +00:00
|
|
|
interface IProps {
|
2019-01-13 19:33:28 +00:00
|
|
|
|
2017-06-29 03:35:43 +00:00
|
|
|
/**
|
2018-10-30 05:02:23 +00:00
|
|
|
* Whether or not the display name should be editable on click.
|
2017-06-29 03:35:43 +00:00
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
allowEditing: boolean;
|
2017-06-29 03:35:43 +00:00
|
|
|
|
2018-10-30 05:02:23 +00:00
|
|
|
/**
|
|
|
|
* A string to append to the displayName, if provided.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
displayNameSuffix: string;
|
2017-06-29 03:35:43 +00:00
|
|
|
|
2018-10-30 05:02:23 +00:00
|
|
|
/**
|
|
|
|
* The ID attribute to add to the component. Useful for global querying for
|
|
|
|
* the component by legacy components and torture tests.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
elementID: string;
|
2017-06-29 03:35:43 +00:00
|
|
|
|
2018-10-30 05:02:23 +00:00
|
|
|
/**
|
|
|
|
* The ID of the participant whose name is being displayed.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
participantID: string;
|
2017-06-29 03:35:43 +00:00
|
|
|
|
2022-04-12 13:19:10 +00:00
|
|
|
/**
|
|
|
|
* The type of thumbnail.
|
|
|
|
*/
|
2022-09-08 09:52:36 +00:00
|
|
|
thumbnailType: string;
|
2022-08-26 09:54:03 +00:00
|
|
|
}
|
2018-10-30 05:02:23 +00:00
|
|
|
|
2022-11-15 07:50:22 +00:00
|
|
|
const useStyles = makeStyles()(theme => {
|
2021-12-15 13:18:41 +00:00
|
|
|
return {
|
|
|
|
displayName: {
|
2022-09-13 07:36:00 +00:00
|
|
|
...withPixelLineHeight(theme.typography.labelBold),
|
2021-12-15 13:18:41 +00:00
|
|
|
color: theme.palette.text01,
|
|
|
|
overflow: 'hidden',
|
|
|
|
textOverflow: 'ellipsis',
|
|
|
|
whiteSpace: 'nowrap'
|
|
|
|
},
|
|
|
|
|
|
|
|
editDisplayName: {
|
|
|
|
outline: 'none',
|
|
|
|
border: 'none',
|
|
|
|
background: 'none',
|
|
|
|
boxShadow: 'none',
|
|
|
|
padding: 0,
|
2022-09-13 07:36:00 +00:00
|
|
|
...withPixelLineHeight(theme.typography.labelBold),
|
2021-12-15 13:18:41 +00:00
|
|
|
color: theme.palette.text01
|
|
|
|
}
|
|
|
|
};
|
2022-10-19 07:19:40 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
const DisplayName = ({
|
|
|
|
allowEditing,
|
|
|
|
displayNameSuffix,
|
|
|
|
elementID,
|
|
|
|
participantID,
|
|
|
|
thumbnailType
|
2022-10-20 09:11:27 +00:00
|
|
|
}: IProps) => {
|
2022-10-19 07:19:40 +00:00
|
|
|
const { classes } = useStyles();
|
2022-10-20 09:11:27 +00:00
|
|
|
const configuredDisplayName = useSelector((state: IReduxState) =>
|
|
|
|
getParticipantById(state, participantID))?.name ?? '';
|
|
|
|
const nameToDisplay = useSelector((state: IReduxState) => getParticipantDisplayName(state, participantID));
|
2022-10-19 07:19:40 +00:00
|
|
|
const [ editDisplayNameValue, setEditDisplayNameValue ] = useState('');
|
|
|
|
const [ isEditing, setIsEditing ] = useState(false);
|
|
|
|
const dispatch = useDispatch();
|
|
|
|
const { t } = useTranslation();
|
|
|
|
const nameInputRef = useRef<HTMLInputElement | null>(null);
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
if (isEditing && nameInputRef.current) {
|
|
|
|
nameInputRef.current.select();
|
2017-06-29 03:35:43 +00:00
|
|
|
}
|
2022-10-19 07:19:40 +00:00
|
|
|
}, [ isEditing ]);
|
2017-06-29 03:35:43 +00:00
|
|
|
|
2022-10-19 07:19:40 +00:00
|
|
|
const onClick = useCallback((e: React.MouseEvent) => {
|
|
|
|
e.stopPropagation();
|
|
|
|
}, []);
|
2017-06-29 03:35:43 +00:00
|
|
|
|
2022-10-19 07:19:40 +00:00
|
|
|
const onChange = useCallback((event: React.ChangeEvent<HTMLInputElement>) => {
|
|
|
|
setEditDisplayNameValue(event.target.value);
|
|
|
|
}, []);
|
2017-06-29 03:35:43 +00:00
|
|
|
|
2022-10-19 07:19:40 +00:00
|
|
|
const onSubmit = useCallback(() => {
|
|
|
|
dispatch(updateSettings({
|
|
|
|
displayName: editDisplayNameValue
|
|
|
|
}));
|
2021-12-15 13:18:41 +00:00
|
|
|
|
2022-10-19 07:19:40 +00:00
|
|
|
setEditDisplayNameValue('');
|
|
|
|
setIsEditing(false);
|
|
|
|
nameInputRef.current = null;
|
|
|
|
}, [ editDisplayNameValue, nameInputRef ]);
|
2017-06-29 03:35:43 +00:00
|
|
|
|
2022-10-19 07:19:40 +00:00
|
|
|
const onKeyDown = useCallback((event: React.KeyboardEvent) => {
|
2017-06-29 03:35:43 +00:00
|
|
|
if (event.key === 'Enter') {
|
2022-10-19 07:19:40 +00:00
|
|
|
onSubmit();
|
2017-06-29 03:35:43 +00:00
|
|
|
}
|
2022-10-19 07:19:40 +00:00
|
|
|
}, [ onSubmit ]);
|
2017-06-29 03:35:43 +00:00
|
|
|
|
2022-10-19 07:19:40 +00:00
|
|
|
const onStartEditing = useCallback((e: React.MouseEvent) => {
|
|
|
|
if (allowEditing) {
|
2021-12-15 13:18:41 +00:00
|
|
|
e.stopPropagation();
|
2022-10-19 07:19:40 +00:00
|
|
|
setIsEditing(true);
|
|
|
|
setEditDisplayNameValue(configuredDisplayName);
|
2017-06-29 03:35:43 +00:00
|
|
|
}
|
2022-10-19 07:19:40 +00:00
|
|
|
}, [ allowEditing ]);
|
2017-06-29 03:35:43 +00:00
|
|
|
|
2022-10-19 07:19:40 +00:00
|
|
|
if (allowEditing && isEditing) {
|
|
|
|
return (
|
|
|
|
<input
|
|
|
|
autoFocus = { true }
|
|
|
|
className = { classes.editDisplayName }
|
|
|
|
id = 'editDisplayName'
|
|
|
|
onBlur = { onSubmit }
|
|
|
|
onChange = { onChange }
|
|
|
|
onClick = { onClick }
|
|
|
|
onKeyDown = { onKeyDown }
|
|
|
|
placeholder = { t('defaultNickname') }
|
|
|
|
ref = { nameInputRef }
|
|
|
|
spellCheck = { 'false' }
|
|
|
|
type = 'text'
|
|
|
|
value = { editDisplayNameValue } />
|
|
|
|
);
|
2017-06-29 03:35:43 +00:00
|
|
|
}
|
|
|
|
|
2022-10-19 07:19:40 +00:00
|
|
|
return (
|
|
|
|
<Tooltip
|
|
|
|
content = { appendSuffix(nameToDisplay, displayNameSuffix) }
|
|
|
|
position = { getIndicatorsTooltipPosition(thumbnailType) }>
|
|
|
|
<span
|
|
|
|
className = { `displayname ${classes.displayName}` }
|
|
|
|
id = { elementID }
|
|
|
|
onClick = { onStartEditing }>
|
|
|
|
{appendSuffix(nameToDisplay, displayNameSuffix)}
|
|
|
|
</span>
|
|
|
|
</Tooltip>
|
|
|
|
);
|
|
|
|
};
|
2019-01-13 19:33:28 +00:00
|
|
|
|
|
|
|
|
2022-10-19 07:19:40 +00:00
|
|
|
export default DisplayName;
|