ActionButtons.tsx 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. // Copyright 2017-2020 @polkadot/app-js authors & contributors
  2. // This software may be modified and distributed under the terms
  3. // of the Apache-2.0 license. See the LICENSE file for details.
  4. import React, { useCallback, useState } from 'react';
  5. import { Button, Input, Popup } from '@polkadot/react-components';
  6. import { useTranslation } from './translate';
  7. interface Props {
  8. className?: string;
  9. isCustomExample: boolean;
  10. isRunning: boolean;
  11. removeSnippet: () => void;
  12. runJs: () => void;
  13. saveSnippet: (snippetName: string) => void;
  14. snippetName?: string;
  15. stopJs: () => void;
  16. }
  17. function ActionButtons ({ className = '', isCustomExample, isRunning, removeSnippet, runJs, saveSnippet, stopJs }: Props): React.ReactElement<Props> {
  18. const { t } = useTranslation();
  19. const [isOpen, setIsOpen] = useState(false);
  20. const [snippetName, setSnippetName] = useState('');
  21. const _onChangeName = useCallback(
  22. (snippetName: string) => setSnippetName(snippetName),
  23. []
  24. );
  25. const _onPopupOpen = useCallback(
  26. () => setIsOpen(true),
  27. []
  28. );
  29. const _onPopupClose = useCallback(
  30. (): void => {
  31. setSnippetName('');
  32. setIsOpen(false);
  33. },
  34. []
  35. );
  36. const _saveSnippet = useCallback(
  37. (): void => {
  38. saveSnippet(snippetName);
  39. _onPopupClose();
  40. },
  41. [_onPopupClose, saveSnippet, snippetName]
  42. );
  43. return (
  44. <Button.Group className={`${className} action-button`}>
  45. {isCustomExample
  46. ? (
  47. <Button
  48. icon='trash'
  49. onClick={removeSnippet}
  50. />
  51. )
  52. : (
  53. <Popup
  54. className='popup-local'
  55. isOpen={isOpen}
  56. on='click'
  57. onClose={_onPopupClose}
  58. trigger={
  59. <Button
  60. icon='save'
  61. onClick={_onPopupOpen}
  62. />
  63. }
  64. >
  65. <Input
  66. autoFocus
  67. maxLength={50}
  68. min={1}
  69. onChange={_onChangeName}
  70. onEnter={_saveSnippet}
  71. placeholder={t<string>('Name your example')}
  72. value={snippetName}
  73. withLabel={false}
  74. />
  75. <Button
  76. icon='save'
  77. isDisabled={!snippetName.length}
  78. label={t<string>('Save snippet to local storage')}
  79. onClick={_saveSnippet}
  80. />
  81. </Popup>
  82. )
  83. }
  84. {isRunning
  85. ? (
  86. <Button
  87. icon='times'
  88. onClick={stopJs}
  89. />
  90. )
  91. : (
  92. <Button
  93. className='play-button'
  94. icon='play'
  95. onClick={runJs}
  96. />
  97. )
  98. }
  99. </Button.Group>
  100. );
  101. }
  102. export default React.memo(ActionButtons);