QueryEditorModeSwitcher.tsx 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import React, { useEffect, useState } from 'react';
  2. import { Button, ConfirmModal } from '@grafana/ui';
  3. type Props = {
  4. isRaw: boolean;
  5. onChange: (newIsRaw: boolean) => void;
  6. };
  7. export const QueryEditorModeSwitcher = ({ isRaw, onChange }: Props): JSX.Element => {
  8. const [isModalOpen, setModalOpen] = useState(false);
  9. useEffect(() => {
  10. // if the isRaw changes, we hide the modal
  11. setModalOpen(false);
  12. }, [isRaw]);
  13. if (isRaw) {
  14. return (
  15. <>
  16. <Button
  17. aria-label="Switch to visual editor"
  18. icon="pen"
  19. variant="secondary"
  20. type="button"
  21. onClick={() => {
  22. // we show the are-you-sure modal
  23. setModalOpen(true);
  24. }}
  25. ></Button>
  26. <ConfirmModal
  27. isOpen={isModalOpen}
  28. title="Switch to visual editor mode"
  29. body="Are you sure to switch to visual editor mode? You will lose the changes done in raw query mode."
  30. confirmText="Yes, switch to editor mode"
  31. dismissText="No, stay in raw query mode"
  32. onConfirm={() => {
  33. onChange(false);
  34. }}
  35. onDismiss={() => {
  36. setModalOpen(false);
  37. }}
  38. />
  39. </>
  40. );
  41. } else {
  42. return (
  43. <Button
  44. aria-label="Switch to text editor"
  45. icon="pen"
  46. variant="secondary"
  47. type="button"
  48. onClick={() => {
  49. onChange(true);
  50. }}
  51. ></Button>
  52. );
  53. }
  54. };