This repository has been archived on 2023-11-14. You can view files and clone it, but cannot push or open issues/pull-requests.
viscord/packages/renderer/src/pages/NameTextbox.tsx

44 lines
1.0 KiB
TypeScript
Raw Normal View History

2022-07-23 17:37:24 -04:00
import {
useCallback,
useContext,
useEffect,
useRef,
useState,
} from 'react';
2022-07-29 13:52:59 -04:00
import useClientId from '../hooks/useClientId';
2022-07-23 17:37:24 -04:00
import { useApi } from '../lib/useApi';
export default function NameTextbox() {
2022-07-29 13:52:59 -04:00
const { clientId } = useClientId()
2022-07-23 17:37:24 -04:00
const [name, setName] = useState<string | null>(null);
const [inputElement, setInputElement] = useState<HTMLInputElement | null>(null);
const { send } = useApi({
2022-07-30 21:27:56 -04:00
'client:get'(data: any) {
setName(data.name);
2022-07-23 17:37:24 -04:00
},
}, [name, clientId]);
const update = useCallback(() => {
if(inputElement === null) return;
if(clientId === null) return;
send('client:rename', {
clientId: clientId,
name: inputElement.value,
});
setName(inputElement.value);
}, [clientId, name]);
useEffect(() => {
if(clientId === null) return;
if(inputElement === null) return;
2022-07-30 21:27:56 -04:00
send('client:get', { clientId });
2022-07-23 17:37:24 -04:00
}, [inputElement, clientId]);
return <input
ref={setInputElement}
value={name ?? ''}
onChange={update}
/>;
}