2022-09-07 10:00:28 +03:00
|
|
|
import React, { CSSProperties, FC, useState } from 'react';
|
2022-05-14 01:07:49 +03:00
|
|
|
import { useRecoilValue } from 'recoil';
|
2022-11-23 21:52:39 +03:00
|
|
|
import { Input, Button, Select, Form } from 'antd';
|
2022-09-03 21:38:52 +03:00
|
|
|
import { MessageType } from '../../../interfaces/socket-events';
|
|
|
|
import WebsocketService from '../../../services/websocket-service';
|
2022-10-11 02:26:09 +03:00
|
|
|
import { websocketServiceAtom, currentUserAtom } from '../../stores/ClientConfigStore';
|
2023-04-24 20:58:57 +03:00
|
|
|
import styles from './NameChangeModal.module.scss';
|
2022-08-10 05:56:45 +03:00
|
|
|
|
|
|
|
const { Option } = Select;
|
2022-05-14 01:07:49 +03:00
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
export type UserColorProps = {
|
|
|
|
color: number;
|
|
|
|
};
|
2022-05-14 01:07:49 +03:00
|
|
|
|
2022-09-07 10:00:28 +03:00
|
|
|
const UserColor: FC<UserColorProps> = ({ color }) => {
|
2022-08-10 05:56:45 +03:00
|
|
|
const style: CSSProperties = {
|
|
|
|
textAlign: 'center',
|
2022-08-30 09:17:12 +03:00
|
|
|
backgroundColor: `var(--theme-color-users-${color})`,
|
2022-08-10 05:56:45 +03:00
|
|
|
width: '100%',
|
|
|
|
height: '100%',
|
|
|
|
};
|
|
|
|
return <div style={style} />;
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|
2022-08-10 05:56:45 +03:00
|
|
|
|
2023-06-13 08:54:12 +03:00
|
|
|
type NameChangeModalProps = {
|
|
|
|
closeModal: () => void;
|
|
|
|
};
|
|
|
|
|
|
|
|
export const NameChangeModal: FC<NameChangeModalProps> = ({ closeModal }) => {
|
2022-10-11 02:26:09 +03:00
|
|
|
const currentUser = useRecoilValue(currentUserAtom);
|
2022-11-07 02:33:21 +03:00
|
|
|
const websocketService = useRecoilValue<WebsocketService>(websocketServiceAtom);
|
|
|
|
const [newName, setNewName] = useState<string>(currentUser?.displayName);
|
|
|
|
|
2022-10-11 02:26:09 +03:00
|
|
|
if (!currentUser) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
const { displayName, displayColor } = currentUser;
|
2022-05-14 01:07:49 +03:00
|
|
|
|
2022-11-23 21:52:39 +03:00
|
|
|
const saveEnabled = () =>
|
|
|
|
newName !== displayName && newName !== '' && websocketService?.isConnected();
|
|
|
|
|
2022-05-26 23:52:04 +03:00
|
|
|
const handleNameChange = () => {
|
2022-11-23 21:52:39 +03:00
|
|
|
if (!saveEnabled()) return;
|
|
|
|
|
2022-05-26 23:52:04 +03:00
|
|
|
const nameChange = {
|
|
|
|
type: MessageType.NAME_CHANGE,
|
|
|
|
newName,
|
|
|
|
};
|
|
|
|
websocketService.send(nameChange);
|
2023-06-13 08:54:12 +03:00
|
|
|
closeModal();
|
2022-05-26 23:52:04 +03:00
|
|
|
};
|
2022-05-14 01:07:49 +03:00
|
|
|
|
2022-08-10 05:56:45 +03:00
|
|
|
const handleColorChange = (color: string) => {
|
|
|
|
const colorChange = {
|
|
|
|
type: MessageType.COLOR_CHANGE,
|
|
|
|
newColor: Number(color),
|
|
|
|
};
|
|
|
|
websocketService.send(colorChange);
|
|
|
|
};
|
|
|
|
|
|
|
|
const maxColor = 8; // 0...n
|
2023-05-21 07:15:25 +03:00
|
|
|
const colorOptions = [...Array(maxColor)].map((_, i) => i);
|
2022-08-10 05:56:45 +03:00
|
|
|
|
2022-11-23 21:52:39 +03:00
|
|
|
const saveButton = (
|
2022-11-23 22:41:58 +03:00
|
|
|
<Button
|
|
|
|
type="primary"
|
|
|
|
id="name-change-submit"
|
|
|
|
onClick={handleNameChange}
|
|
|
|
disabled={!saveEnabled()}
|
|
|
|
>
|
2022-11-23 21:52:39 +03:00
|
|
|
Change name
|
|
|
|
</Button>
|
|
|
|
);
|
|
|
|
|
2022-05-26 23:52:04 +03:00
|
|
|
return (
|
|
|
|
<div>
|
2022-11-23 21:52:39 +03:00
|
|
|
Your chat display name is what people see when you send chat messages.
|
2023-04-24 20:58:57 +03:00
|
|
|
<Form onSubmitCapture={handleNameChange} className={styles.form}>
|
2022-11-23 21:52:39 +03:00
|
|
|
<Input.Search
|
|
|
|
enterButton={saveButton}
|
|
|
|
id="name-change-field"
|
|
|
|
value={newName}
|
|
|
|
onChange={e => setNewName(e.target.value)}
|
|
|
|
placeholder="Your chat display name"
|
2023-01-24 07:25:47 +03:00
|
|
|
aria-label="Your chat display name"
|
2022-11-23 21:52:39 +03:00
|
|
|
maxLength={30}
|
|
|
|
showCount
|
|
|
|
defaultValue={displayName}
|
2023-04-24 20:58:57 +03:00
|
|
|
className={styles.inputGroup}
|
2022-11-23 21:52:39 +03:00
|
|
|
/>
|
|
|
|
</Form>
|
2023-04-24 20:58:57 +03:00
|
|
|
<Form.Item label="Your Color" className={styles.colorChange}>
|
2022-08-10 05:56:45 +03:00
|
|
|
<Select
|
|
|
|
style={{ width: 120 }}
|
|
|
|
onChange={handleColorChange}
|
2022-10-11 02:26:09 +03:00
|
|
|
defaultValue={displayColor.toString()}
|
2023-04-24 20:58:57 +03:00
|
|
|
className={styles.colorDropdown}
|
2022-08-10 05:56:45 +03:00
|
|
|
>
|
|
|
|
{colorOptions.map(e => (
|
|
|
|
<Option key={e.toString()} title={e}>
|
2023-01-24 07:25:47 +03:00
|
|
|
<UserColor color={e} aria-label={e.toString()} />
|
2022-08-10 05:56:45 +03:00
|
|
|
</Option>
|
|
|
|
))}
|
|
|
|
</Select>
|
2022-11-23 21:52:39 +03:00
|
|
|
</Form.Item>
|
|
|
|
You can also authenticate an IndieAuth or Fediverse account via the "Authenticate"
|
|
|
|
menu.
|
2022-05-26 23:52:04 +03:00
|
|
|
</div>
|
|
|
|
);
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|