CrawlerStartButton.tsx 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. import { css } from '@emotion/css';
  2. import React, { useState } from 'react';
  3. import { GrafanaTheme2 } from '@grafana/data';
  4. import { getBackendSrv, config } from '@grafana/runtime';
  5. import { Button, CodeEditor, Modal, useTheme2 } from '@grafana/ui';
  6. export const CrawlerStartButton = () => {
  7. const styles = getStyles(useTheme2());
  8. const [open, setOpen] = useState(false);
  9. const [body, setBody] = useState({
  10. mode: 'thumbs',
  11. theme: config.theme2.isLight ? 'light' : 'dark',
  12. });
  13. const onDismiss = () => setOpen(false);
  14. const doStart = () => {
  15. getBackendSrv()
  16. .post('/api/admin/crawler/start', body)
  17. .then((v) => {
  18. console.log('GOT', v);
  19. onDismiss();
  20. });
  21. };
  22. return (
  23. <>
  24. <Modal title={'Start crawler'} isOpen={open} onDismiss={onDismiss}>
  25. <div className={styles.wrap}>
  26. <CodeEditor
  27. height={200}
  28. value={JSON.stringify(body, null, 2) ?? ''}
  29. showLineNumbers={false}
  30. readOnly={false}
  31. language="json"
  32. showMiniMap={false}
  33. onBlur={(text: string) => {
  34. setBody(JSON.parse(text)); // force JSON?
  35. }}
  36. />
  37. </div>
  38. <Modal.ButtonRow>
  39. <Button type="submit" onClick={doStart}>
  40. Start
  41. </Button>
  42. <Button variant="secondary" onClick={onDismiss}>
  43. Cancel
  44. </Button>
  45. </Modal.ButtonRow>
  46. </Modal>
  47. <Button onClick={() => setOpen(true)} variant="primary">
  48. Start
  49. </Button>
  50. </>
  51. );
  52. };
  53. const getStyles = (theme: GrafanaTheme2) => {
  54. return {
  55. wrap: css`
  56. border: 2px solid #111;
  57. `,
  58. };
  59. };