This repository has been archived on 2023-11-14. You can view files and clone it, but cannot push or open issues/pull-requests.
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({
|
|
|
|
|
'client:get'(_name: string) {
|
|
|
|
|
setName(_name);
|
|
|
|
|
},
|
|
|
|
|
}, [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;
|
|
|
|
|
send('client:get', clientId);
|
|
|
|
|
}, [inputElement, clientId]);
|
|
|
|
|
|
|
|
|
|
return <input
|
|
|
|
|
ref={setInputElement}
|
|
|
|
|
value={name ?? ''}
|
|
|
|
|
onChange={update}
|
|
|
|
|
/>;
|
|
|
|
|
}
|