identify.tsx 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import React, { useCallback, useEffect, useRef, useState } from 'react';
  2. import { CircleLoader } from 'react-spinners';
  3. import { useCTA } from '../hooks/cta';
  4. import * as Styled from './identify.styles';
  5. export type Props = {
  6. connecting: boolean;
  7. onIdentify: (name: string) => void;
  8. setInteracted: (interacted: boolean) => void;
  9. };
  10. export const LoadingWrapper: React.FC = () => (
  11. <Styled.Container>
  12. <CircleLoader size={100} color="white" />
  13. </Styled.Container>
  14. );
  15. export const Identify: React.FC<Props> = ({ connecting, onIdentify, setInteracted }) => {
  16. const [name, setName] = useState<string>('');
  17. const onChange = useCallback(
  18. (event: React.ChangeEvent<HTMLInputElement>) => setName(event.target.value),
  19. [],
  20. );
  21. const onConnect = useCallback(() => {
  22. onIdentify(name);
  23. setInteracted(true);
  24. }, [name, onIdentify, setInteracted]);
  25. const input = useRef<HTMLInputElement>(null);
  26. useEffect(() => {
  27. setImmediate(() => {
  28. input.current?.focus();
  29. });
  30. }, []);
  31. const inputOnKeydown = useCallback(
  32. (event: React.KeyboardEvent<HTMLInputElement>) => {
  33. if (event.key === 'Enter') {
  34. onConnect();
  35. }
  36. },
  37. [onConnect],
  38. );
  39. const buttonHandlers = useCTA(onConnect);
  40. if (connecting) {
  41. return <LoadingWrapper />;
  42. }
  43. return (
  44. <Styled.Container>
  45. <Styled.Title>go-music-player</Styled.Title>
  46. <Styled.Instruction>Set client name:</Styled.Instruction>
  47. <Styled.InputGroup>
  48. <input ref={input} type="text" onChange={onChange} onKeyDown={inputOnKeydown} />
  49. <button disabled={connecting} {...buttonHandlers}>
  50. Connect
  51. </button>
  52. </Styled.InputGroup>
  53. </Styled.Container>
  54. );
  55. };